Advertisement
Semmu

Simple FPS capping in SDL

Oct 18th, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.64 KB | None | 0 0
  1. #include <SDL/SDL.h>
  2.  
  3. int main()
  4. {
  5.     SDL_Event e;
  6.  
  7.     SDL_Surface *output;
  8.     output = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
  9.  
  10.     int fps_cap = 1;
  11.  
  12.     int loop = 1;
  13.     int last_loop, time;
  14.  
  15.     while (loop)
  16.     {
  17.         last_loop = SDL_GetTicks();
  18.  
  19.         while (SDL_PollEvent(&e))
  20.         {
  21.             switch (e.type)
  22.             {
  23.                 case SDL_QUIT:
  24.                     loop = 0;
  25.                 break;
  26.  
  27.                 default: break;
  28.             }
  29.         }
  30.  
  31.         SDL_FillRect(output, NULL, SDL_MapRGB(output->format, rand() % 256,  rand() % 256,  rand() % 256));
  32.         SDL_Flip(output);
  33.  
  34.  
  35.  
  36.         time = SDL_GetTicks() - last_loop;
  37.         if (time < 1000 / fps_cap)
  38.             SDL_Delay(1000 / fps_cap - time);
  39.     }
  40.  
  41.     return 0;
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement