[SDL] Speed In Games

Karsten-Olaf Laux klaux at student.uni-kl.de
Mon Aug 30 02:36:24 PDT 1999

on Mon, 30 Aug 1999 Ryan Wahle wrote:
>I have never used the SDL_GetTicks... Could you explain that alittle more?

SDL_GetTicks() returns the number of miliseconds which have passed since you
have started your application. Here is an excerpt from my mainloop (in fact it
is part of uapplication.cc from libu)
Limiting the gameloop to 40 revolutions per second does a great deal on linux
machines, since if your game is idle, it won't use any cpu cycles,
since SDL_Delay() gives away cpu time - any notebook users will love you for
this ;) 

  Uint32 lastticks;
  Uint32 tick;
  Uint32 delta_tick;

      //Timer stuff
      tick = SDL_GetTicks();
      delta_tick = tick - lastticks;
      lastticks = tick;
      //limit to 40 revolutions per second (40*25ms = 1 second)
      if(delta_tick < 25)
          SDL_Delay(25 - delta_tick);
          tick = SDL_GetTicks();
          delta_tick = delta_tick + tick - lastticks;
          lastticks = tick;               

     ///now delta_tick contains the number of ms since this line was last

     /// now do the game processing .................
   } //while      


Karsten-O. Laux           
klaux at student.uni-kl.de

More information about the SDL mailing list