[SDL] FPS Counter?

Peter Mulholland darkmatter at freeuk.com
Thu Sep 14 10:51:16 PDT 2006


Hello DARKGuy,

Thursday, September 14, 2006, 6:24:53 PM, you wrote:

> Hi again! xD... my game's progressing awesomely thanks to all of you ^_^
> hehe... o.o but now I wonder xD I've been through google, lazy foo's
> tutorials, Aaron's SDL tutorials, and such, searching for an FPS counter
> code and I haven't found any, and in lazy foo's site the code is too old and
> it throws errors in SDL (it uses "timers"? which I supposed SDL had =/ ).
> Going through google all I can find are pseudo-codes and theories, but no
> example whatsoever :(. Does anybody here has a simple FPS counter that can
> share please? ^^...


It's quite easy:

while (gamerunning)
{
      Uint32 start_time, frame_time;
      float fps;

      start_time = SDL_GetTicks();

      // do stuff
      SDL_Flip();

      frame_time = SDL_GetTicks()-start_time;
      fps = (frame time > 0) ? 1000.0f / frame_time : 0.0f;
}

The extra bit in the fps calculation stops a divide by zero should a
frame happen to take less than a millisecond (it's not likely, but it
can happen)

-- 
Best regards,
 Peter                            mailto:darkmatter at freeuk.com





More information about the SDL mailing list