Halil KARAGÖZ
Halil KARAGÖZ

Reputation: 21

Calculating FPS With SDL 2.0

#define FPS_INTERVAL 1.0 //seconds.

Uint32 fps_lasttime = SDL_GetTicks(); //the last recorded time.
Uint32 fps_current; //the current FPS.
Uint32 fps_frames = 0; //frames passed since the last recorded fps.

// you would initalise these in a main method, really.

void loop()
{
   //YOUR PAINTING STUFF HERE...
   //********//

   fps_frames++;
   if (fps_lasttime < SDL_GetTicks() - FPS_INTERVAL*1000)
   {
      fps_lasttime = SDL_GetTicks();
      fps_current = fps_frames;
      fps_frames = 0;
   }
}

I took the code above from: http://sdl.beuc.net/sdl.wiki/SDL_Average_FPS_Measurement

I understand the code but the FPS_INTERVAL confuses me because I can't really understand why it's needed in the if statement. Can someone explain this?

And also do I have to use Uint32 or would any int do the job and why?

Upvotes: 1

Views: 6856

Answers (1)

Vlad Feinstein
Vlad Feinstein

Reputation: 11321

SDL_GetTicks() returns number of milliseconds; FPS_INTERVAL specifies number of seconds (here - 1) to count the frames; it is multiplied by 1,000 to get number of milliseconds.

Re: Uint32 - that is what SDL_GetTicks() returns. What other type would you use, and why?

Upvotes: 1

Related Questions