Advertisement
thecplusplusguy

SDL tutorial 8

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