frame rate

Hi! Imagine that i want to know the FPS of my application. Then i suppose it should be nice to build a real-time FPS counter. The problem is…how? is there a function that can help us with that?
Thanks again (lines of code are always welcome :stuck_out_tongue: )

You didn’t say what language you’re coding in (presumably C++) but I’ve posted some pseudo-code

At every loop of your main rendering loop,
call a function to calculate the frame rate:

function CalculateFrameRate ()
  // add 1 to the # frames rendered
  get currentTime();      

  // make sure 1 second has elapsed
  // (you are calculating frames per sec)
  if( currentTime - lastTime > 1 second )
    lastTime = currentTime;

    // global FPS can be written to screen
    global_FPS = framesPerSecond;

    // reset counter
    framesPerSecond = 0;

This is what I use. Seems good enough anyway.
If there are any complaints, I’d be happy to hear
how to make this more accurate!

I believe the best way to count FPS is to get a start time for the frame, and an end time, and calculate the FPS using this information.

To do this you need a very accurate counter, and on Windows (you might of course not be using windows, but im sure theres an equivalent) with MMX chipsets there is a special performance counter that counts at a very high frequency. before rendering any frames you need to set up the counter

double lastFrameEnd;
double frameEnd;
double FPS;
double countFrequency;

lastFrameEnd = 1;
countFrequency = (double) large.QuadPart;

this sets a value for the lastFrameEnd (that means the calculation of the first frame only will be incorrect) and gets the frequency that the clock updates (this is generally almost as quick as the CPU speed)

THen for each frame (before rendering anything):

frameEnd = (double) large.QuadPart;
FPS = 1 / ((frameEnd - lastFrameEnd) / countFrequency);
sprintf(str, "FPS: %06.2f", FPS);
lastFrameEnd = frameEnd;

this gives you a string to display or printout etc.

Having tested this by rendering at a high frame rate with no vertical sync, then switching vertical sync on to get a constant frame rate thats equivalent to my monitors refresh rate, im pretty sure its accurate. Using the system time libraries doesnt really allow accurate timing because its update frequency is much less. There are other timing libraries to use also.

Sorry, i forgot to mention. I’m using VC++ in windows :stuck_out_tongue: thanks u both