SDL learning 2

xiaoxiao2021-03-05  22

#include #include #include #include

/// Surface SDL_SURFACE * g_screensurface = null; // Main surface SDL_SURFACE * g_buffer = null; // Background buffer page SDL_SURFACE * g_backface = null; // Background page SDL_SURFACE * g_maskface = null; // Mask page

// Mask Rectangle Picture Points Points SDL_RECT RBUFFER;

Long m_lmain_pitch;

Const int xsize = 800; // 497; const int = 600; // 357;

/// uint32 timeeleft (void); // Time Control Void Init (Void); Void Shutdown (Void); // Loading Background Void OnkeyDown (int key); void render (); ///// INT Main (int Argc, char * argv []) {

INIT ();

BOOL BDONE = false; while (bdone == false) {sdl_event event; while (SDL_POLLEVENT)) {if (event.type == SDL_QUIT) BDONE = true; if (event.type == SDL_KEYDOWN) {IF (Event .key.keysym.sym == SDLK_ESCAPE) BDONE = true; onkeyDown (esym);}} render ();}} render ();} Shutdown (); Return 0;} /// Void onkeyDown (int key) { Switch (key) {copy SDLK_A: // Left RBuffer.x -; rbuffer.x = rbuffer.x-3; if (rBuffer.x <= 3) {rBuffer.x = 3;} Break; Case SDLK_S: / / Down RBuffer.y ; rBuffer.y = rBuffer.y 3; if (rBuffer.y> = (g_backface-> h)) {rBuffer.y = g_backface-> h-g_maskface-> h; } Break; Case SDLK_D: // Right RBuffer.x ; RBuffer.x = RBuffer.x 3; IF (RBuffer.x> (g_backface-> w) RBuffer.x = g_backface-> w- g_maskface-> w; // if (rbuffer.y> (387-120)) RBuffer.y = 387-120; Break; Case SDLK_W: / / RBuffer .y-; rBuffer.y = rBuffer.y-3; if (rBuffer.y <= 3) {rBuffer.y = 3;} Break;}} /// Void Init (Void) {IF (SDL_Init_Video <0) {Printf ("Unable to init SDL:% S / N", SDL_GETERROR ()); exit (1);} Atexit (SDL_QUIT); Create the main surface /// g_screensurface = SDL_SetVideoDe (xsize, ysize, 32 , SDL_HWSURFACE; if (g_screensurface == null) {Printf ("Unable to set video:% s / n", SDL_GETERROR ()); exit (1);} // m_lmain_pitch = g_screensurface-> pitch >> 2;

Create a back buffer page /// if ((g_Buffer = SDL_CreateRGBSurface (SDL_SWSURFACE, g_screenSurface-> w, g_screenSurface-> h, g_screenSurface-> format-> BitsPerPixel, g_screenSurface-> format-> Rmask, // 0x000000ff, g_screenSurface-> Format-> gmask, // 0x0000FF00, g_screensurface-> format-> bmask, // 0x00ff0000, g_screensurface-> format-> amask // 0xff000000)) == null) Return; rBuffer.x = 0; rBuffer.y = 0 RBuffer.w = 120; rBuffer.h = 120; // Load resource loadRES (); /// Start keyboard combo // SDL_ENABLEKEYREPEAT (SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);} /// void loading (void) {/ Load Background Figure g_backface = SDL_LOADBMP ("girl.bmp"); if (g_backface == null) {fprintf (stderr, "couldn't load / "back.bmp/", SDL_GETERROR ()); return;} g_maskface = SDL_LOADBMP ( "lg.bmp"); if (g_maskface == null) {fprintf (stderr, "couldn't load /"light.bmp/", SDL_Geterror ()); return;}}

/// Uint32 timeLeft (void) {static Uint32 timeToNextUpdate = 0; Uint32 currentTime; currentTime = SDL_GetTicks (); if (timeToNextUpdate <= currentTime) {timeToNextUpdate = currentTime UPDATE_INTERVAL; return 0;} return (timeToNextUpdate - currentTime);} /// void shutDown (void) {SDL_FreeSurface (g_screenSurface); SDL_FreeSurface (g_Buffer); SDL_FreeSurface (g_backface); SDL_FreeSurface (g_maskface);} /// void render () {SDL_Delay (timeLeft ()); SDL_BlitSurface (g_backface, NULL , g_buffer, null; // lock page, perform alpha_blend operation SDL_LOCKSURFACE (g_buffer); SDL_LOCKSURFACE (g_maskface);

Register uint32 * d = (uint32 *) g_buffer-> pixels; register uint32 * s = (uint32 *) g_maskface-> pixels; // Get the number of pixel points of each row is PITCH long mpitch = g_maskface-> pitch >> 2; / / There is a problem here. . . . There is no correct PITCH value long bpitch = g_buffer-> Pitch >> 2; // Key Steps To achieve alpha mixed int alpha; int ISPOS, IDPOS; Register uint32 SR, SG, SB, DR, DG, DB , SC, DC; For (int y = 0; y h; y ) {idpos = (y rBuffer.y) * bpitch rbuffer.x; // gbuffer pixel point location ISPOS = Y * mpitch; // g_maskface pixel point position

For (int x = 0; x w; x ) {sc = (uint32) s [ISPOS ]; alpha = (int) ((sc & 0xff000000) >> 24) >> 3; DC = (UINT32 ) D [IDPOS]; SR = (SC & 0xFF0000) >> 16; DR = (DC & 0xFF0000) >> 16; DR = ((SR * alpha) (DR * (32-alpha)) >> 5 ; SG = (SC & 0xFF00) >> 8; DG = (DC & 0xFF00) >> 8; DG = ((SG * ALPHA) (DG * (32-alpha))) >> 5; SB = SC & 0xFF; DB = DC & 0xFF; DB = ((SB * alpha) (DB * (32-alpha))) >> 5; D [IDPOS ] = (DR << 16) | (DG << 8) | DB;}} // SDL_UNLOCKSURFACE (g_maskface); SDL_UNLOCKSURFACE (g_buffer);

SDL_BLITSURFACE (g_buffer, null, g_screensurface, null); SDL_FLIP (g_screensurface);}

转载请注明原文地址:https://www.9cbs.com/read-37634.html

New Post(0)