Advertisement
thecplusplusguy

SDL tutorial 9

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