Calculating FPS...

Started by
4 comments, last by jwblair 23 years ago
How do you calculate FPS? I would to do this in Glut. I hear that it is simple to do, but I am not sure how. Can anyone help me with this?
Thanks!John William Blair "The path of the righteous man is beset on all sides by the inequities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of the darkness. For he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who attempt to poison and destroy my brothers. And you will know my name is the Lord when I lay my vengeance upon thee."
http://members.home.com/chucklez/wtc/index.html
Advertisement
This is a really common question. I don''t understand how someone could do something complex like making a graphical program and NOT know how calculate FPS. =)
Try searching the board a bit I''ve seen several examples before...
ello
sure it''s simple. I use the following lines of code , they''re probably "the worsest way" you should do it , but hey! , they seem to work quite fine for me

variables you use :
char fpsmsg[128]; - to generate text which goes onscreen
double time;-for time between start/end timing calls
double fps;-for fps according to start/end time (of course it''s fps of the piece of code you''re measuring the time for , in case it''s not your whole loop , it won''t be the real fps number but how many times per second you can run the piece of code you''re timing
__int64 t1,t2,freq;-values to keep time info

and here are the functions to start/end timing

void starttiming()
{
QueryPerformanceFrequency((LARGE_INTEGER *)&freq);
QueryPerformanceCounter((LARGE_INTEGER *)&t1);
}

void endtiming()
{
QueryPerformanceCounter((LARGE_INTEGER *)&t2);
time=(double)(t2-t1)/(double)(freq);
fps=(double)(freq)/(double)(t2-t1);
sprintf((char*)fpsmsg,"TIME: %f FPS: %3.3f",time,fps);
}

the starttimer simple gets current time and stores it in t1 for later. endtimer gets t2 time value and with a little bit of math calculates out needed data while sprintf makes it into a nice text message it gives accurate results for hungryds of frames per sec easily , so you don''t have to worry much , in case you want to test your whole code time (as to get the real fps value) simple use it like

mainloop
{
endtiming();
starttiming();
drawtext(fpsmsg);
}
and you''re done

the fps counter is a lil crazy and changes by a bit all the time , so you might rather want to sum fps rate from several frames and then show the value , and again after several frames change the shown value , else you will end up with a flickering fps counter which changes way too fast

hope it helps , have fun
#include
clock_t start, finish ;
double duration, frames, FPS ;

in your init function :
start = clock();

in your drawing function :
finish = clock() ;
duration += (double)(finish - start) / CLOCKS_PER_SEC ;
frames ++ ;
FPS = frames / duration ;
start = clock() ;

function to draw text on the screen (be sure to check NeHe tutorials to fully understand glRaster2*) :
void PrintFPS() {
char buffer[10] ;
int Nb = sprintf(buffer, " FPS : %3.1f", FPS) ;
glRasterPos2i(0, 0) ;
for (int i = 0; i < Nb; i++) glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, buffer) ;
}

here it is, but you''ll have to perfect this a code a bit because GLUT render a frame only when needed (resize, mouse event, ...). So you surely will need to add code in your idle function (glutIdleFunc)

that''s all



Thanks guys for all your help...

Thanks!

John William Blair
Thanks!John William Blair "The path of the righteous man is beset on all sides by the inequities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of the darkness. For he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who attempt to poison and destroy my brothers. And you will know my name is the Lord when I lay my vengeance upon thee."
http://members.home.com/chucklez/wtc/index.html
thanks to anonymous poster for the code.
i implemented this in something i''m working on and modified it to do the averaging. the code below averages it over 6 frames.
I''m kinda new to c and programming in general so if anyone sees any optimizations or anything please feel free to post an update ( i think it''s elegant enough )


void EndTiming()
{
static unsigned short int i = 0;
static double fAvgFps[6];

QueryPerformanceCounter((LARGE_INTEGER *)&ldTime2);
fTime = (double)(ldTime2-ldTime1)/(double)(ldFrequency);
fFps = (double)(ldFrequency)/(double)(ldTime2-ldTime1);

fAvgFps = fFps;

if( i == 5 )
{
for( int foo = 0; foo <= 5; foo++ )
fAvgFps[6] += fAvgFps[foo];

fAvgFps[6] = ( fAvgFps[6] / 6 );
i = 0;
sprintf( (char*)szFps," FPS: %3.1f", fAvgFps[6] );
}
else i++;
}

This topic is closed to new replies.

Advertisement