Making FPS

Could anybody gimme and example, how to calculate FPS in VC ++ 6 ?


Use the function timeGetTime (returns time in milliseconds), and calculate the time taken to render the frame. FPS = 1/time.

Originally posted by Bob:
Use the function timeGetTime (returns time in milliseconds), and calculate the time taken to render the frame. FPS = 1/time.

Thanks, i`ll try.


On my site, I’ve a little example for displaying FPS.

Here is my function. You have to call it once every frame and then it will return the number of frames per second. Don’t call it more then one time a frame because them the fps won’t be correct.

#include “time.h”

int FPS()
static int frames = 0;
static int fcount = 0;
static clock_t next = clock() + 1000;


if ( clock(); >= next )
next = clock(); + 1000;
frames = fcount;
fcount = 0;

return frames;

Thanks ppl. I`ve just done it myselft trough WM_TIMER.

Take note that basing a timer on WM_TIMER produces a timer with somewhat variable accuracy. Why? Though the message is sent after the given delay, you don’t know how much time has passed since. If you want a more accurate timer, base it on the rtc or the cpu timestamp counter.

Actually, for high-resolution timing, you should use the high-performance counter. I can have triple digit framerates above 500, and still get accuracy beyond that of a float Lemme dig up my code… I forgot how to do it :eek: :stuck_out_tongue:

Ok, create these as global variables:
LARGE_INTEGER lastQueryValue;
LARGE_INTEGER timerFrequency;

Then, in your init code:
if ( !QueryPerformanceFrequency(&timerFrequency) )
return 0; //use a different timer or abort if you get to here… fortunately you shouldn’t get to here on very many machines, but it’s a good idea to have a substitute available

QueryPerformanceCounter( &lastQueryValue );

And in your main loop:

QueryPerformanceCounter( &tmpValue );
lastFrameTime = (float)( tmpValue.QuadPart - lastQueryValue.QuadPart ) / (float)timerFrequency.QuadPart;
lastQueryValue = tmpValue;

Presto, instant high-res counter. And fyi, quad ints have a maximum value of 2^64, so you won’t have any problem timing pretty much anything with this code No credit required nor desired, but you can credit me as pATCheS (please get it right :P) if you’d like.

[This message has been edited by pATChes11 (edited 12-29-2001).]

Just so that genetic doesn’t get confused, what patches11 (and Microsoft) call the high performance counter is in fact based on the cpu timestamp counter.

[This message has been edited by DFrey (edited 12-29-2001).]

Thx ppl. i`ll remake it as you wish :slight_smile: