Advertisement
thecplusplusguy

SDL tutorial 6

Jun 22nd, 2012
1,040
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //Thanks for the typed in code to Tapit85
  3. //you may need an image call tree_key.bmp with the background 00ffff (which disappear)
  4. #include <SDL/SDL.h>
  5.  
  6. int main(int argc, char** argv)
  7. {
  8.     SDL_Init(SDL_INIT_EVERYTHING);
  9.     SDL_Surface *screen, *image;    // every surface except for screen needs to be freed
  10.     screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
  11. //  screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE|SDL_FULLSCREEN);
  12.     bool running = true;
  13.     const int FPS = 30;
  14.     Uint32 start;
  15.     bool b[4] = {0,0,0,0};
  16.     SDL_Rect rect;
  17.     rect.x = 10;
  18.     rect.y = 10;
  19.     rect.w = 20;
  20.     rect.h = 20;
  21.     Uint32 color = SDL_MapRGB(screen->format, 0xff,0xff,0xff);
  22.     Uint32 color2 = SDL_MapRGB(screen->format, 0,0,0);
  23.     image = SDL_DisplayFormat(SDL_LoadBMP("tree_key.bmp")); // better to check later if image is NULL
  24.     SDL_SetColorKey(image, SDL_SRCCOLORKEY, SDL_MapRGB(screen->format, 0x00, 0xff, 0xff));
  25.     while(running) {
  26.         start = SDL_GetTicks();
  27.         SDL_Event event;
  28.         while(SDL_PollEvent(&event)) {
  29.             switch(event.type) {
  30.                 case SDL_QUIT:
  31.                     running = false;
  32.                     break;
  33.                 case SDL_KEYDOWN:
  34.                     switch(event.key.keysym.sym) {
  35.                         case SDLK_UP:
  36.                             b[0] = 1;
  37.                             break;
  38.                         case SDLK_LEFT:
  39.                             b[1] = 1;
  40.                             break;
  41.                         case SDLK_DOWN:
  42.                             b[2] = 1;
  43.                             break;
  44.                         case SDLK_RIGHT:
  45.                             b[3] = 1;
  46.                             break;
  47.                    
  48.                     }
  49.                     break;
  50.                 case SDL_KEYUP:
  51.                     switch(event.key.keysym.sym) {
  52.                         case SDLK_UP:
  53.                             b[0] = 0;
  54.                             break;
  55.                         case SDLK_LEFT:
  56.                             b[1] = 0;
  57.                             break;
  58.                         case SDLK_DOWN:
  59.                             b[2] = 0;
  60.                             break;
  61.                         case SDLK_RIGHT:
  62.                             b[3] = 0;
  63.                             break;
  64.                    
  65.                     }
  66.                     break;
  67.             }
  68.         }
  69.  
  70.         //logic
  71.         if(b[0])
  72.             rect.y--;
  73.         if(b[1])
  74.             rect.x--;
  75.         if(b[2])
  76.             rect.y++;
  77.         if(b[3])
  78.             rect.x++;
  79.  
  80.         //render
  81.         SDL_FillRect(screen, &screen->clip_rect, color);    // fill the screen white (maybe better outside while loop)
  82. //      SDL_FillRect(screen, &rect, color2);
  83. //      SDL_Rect rect;
  84.         rect.x = 200;
  85.         rect.y = 100;
  86.         SDL_BlitSurface(image, NULL, screen, NULL); // copy image to screen to specified coordinates
  87.         SDL_BlitSurface(image, NULL, screen, &rect);
  88.         SDL_Flip(screen);
  89.  
  90.         if(1000/FPS > SDL_GetTicks()-start) {
  91.             SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
  92.         }
  93.     }
  94.     SDL_FreeSurface(image);
  95.     SDL_Quit();
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement