Advertisement

FPS Counter

Started by March 10, 2001 08:46 PM
9 comments, last by Taphreek 23 years, 10 months ago
I didn''t know whether this is considered graphics, or general programming, so if this doesn''t belong here, apologize. Anyway, I was wondering if anyone has the code to a simple FPS counter, it sucks not knowing if anything you are doing is improving the framerate. Even if the framerate is output to a text file every update it would be fine. Thanks! -Taphreek Not a n00b, even if can''t write a basic FPS counter
-Taphreek
HEHE Well i will give you a VB Sample

Private FPS as single
Private Time1 as long
Private P as byte

Private Sub Form_Load()
Time1 = getTickCount
do
DoEvents

.....
do your stuff here
.....

if p = 9 then
p=0
FPS = 10000/(getTickCount-Time1)
Time1 = getTickCount
debug.print FPS
end if
p=p+1
loop
End Sub



-VBLimits
Sorry about the Spelling..
-VBLimitsSorry about the Spelling..
Advertisement
Uh, vb sucks, get out of here w/ that code.

Tap, search the forums for "fps counter" or something like that, the question has been asked alot, you should find it in no time

yeah, some details about what your using would be nice...
Windows/Linux/Ect...
What windowing system(glut?), ect...
  float CGameEngine::CalcFrameRate(LARGE_INTEGER liNow_us)	{	//Original framerate calculation had really crappy twiddle	//framerate = 1000.0f / (float)elapsed;	const float e = 2.718281828f;	//0.5Hz filter, time in ms, so /1000.0	static float filter = (float)pow(e, -2.0f * 0.5f / 1000.0f); 		static LARGE_INTEGER liTimeSnap_us;		//guess at initial framerate, so it doesn''t lag so bad on init	static float fFrameRate_fps = 33.3f;	float fElapsed_ms = (liNow_us.QuadPart - liTimeSnap_us.QuadPart) /1000.0f;  //convert us to ms	liTimeSnap_us = liNow_us;		float factor = (float)pow(filter, fElapsed_ms);	float ifactor = 1.0f - factor;		if(fElapsed_ms<=0.0f)		//if the elasped time is 0, it took less than 5us to render the frame!		fFrameRate_fps = 200000.0f;	else		//filter, *1000.0f to convert ms to sec (again)		fFrameRate_fps = fFrameRate_fps * factor + ifactor * 1000.0f / fElapsed_ms;			return(fFrameRate_fps);	}  

Single-pole, digitally filtered Frame Rate.

Magmai Kai Holmlor
- The disgruntled & disillusioned
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
I give you a simple (however not quite as accurate) example in Allegro.

  // Lions and tigers and global variables, oh my! :^)volatile unsigned long fps = 0, framecount = 0, total_frames = 0;void fps_proc(){        fps = framecount;        total_frames += framecount;        framecount = 0;}END_OF_FUNCTION(fps_proc);int GameLoop(){        // Some stuff here        ......        LOCK_VARIABLE(fps);        LOCK_VARIABLE(framecount);        LOCK_VARIABLE(total_frames);        LOCK_FUNCTION(fps_proc);        // Execute fps_proc once per second        install_int_ex(fps_proc, BPS_TO_TIMER(1));        do {                move_stuff();                draw_screen();                framecount++;        } while( !(want_exit) );        ......}  

Advertisement
Ok here''s a simple way to do it, but it''s not that accurate:
int Get_FPS(void){	static DWORD fps_time1=GetTickCount();	static int fps_count, fps;	DWORD fps_time2 = GetTickCount();	fps_count++;	if(fps_time2-fps_time1 >= 1000)	{		fps_time1=GetTickCount();		fps=fps_count;		fps_count=0;	}	return fps;} 


You call this function at the end of every frame.

There is also the more accurate way which is done in the VB Sample. In C it would be similar something like...

DWORD Time1 = GetTickCount();

...Render Frame...

FPS = 10000/(GetTickCount()-Time1);
Time1 = GetTickCount();




Digital Radiation
Well, if you''re using a Windows based system....

  //DEBUG FILE STUFFFILE *OutputFile;	//DEBUG************************************************************************	//Open Debug File	OutputFile = fopen("log.txt", "w+");	_LARGE_INTEGER PerfStart, PerfEnd, Freq;	unsigned long cyclespermilli;	char OutStr[100];	QueryPerformanceFrequency(&Freq);	cyclespermilli = Freq.QuadPart / 1000;	//DEBUG************************************************************************	//DEBUG**********************************************************************		QueryPerformanceCounter(&PerfStart);		//DEBUG**********************************************************************//Do rendering functions and stuff here...		//DEBUG**********************************************************************		QueryPerformanceCounter(&PerfEnd);		//DEBUG**********************************************************************		//DEBUG**********************************************************************		long Time = (PerfEnd.QuadPart-PerfStart.QuadPart) / cyclespermilli;		memset(&OutStr, ''\0'', sizeof(OutStr));		sprintf(OutStr, "Milliseconds: %d\n", Time);		fwrite(&OutStr, strlen(OutStr), 1, OutputFile);		//DEBUG**********************************************************************  



I hope that all looks good. Anyway. There are plenty of examples of using the Performance timer in the MSDN help at http://msdn.microsoft.com

Sorry it looks so bad. I cut out the rest of the engine and we plan on cutting anything with //DEBUG out of the code. =)

E.D.
Sorry that was me in that last post. It doesn''t look quite the way I wanted it to but oh well.

Its really accurate, pretty fast, and down to millisecond level. (in case you don''t know your FPS is equal to 1/ms where ms is the milliseconds. So for example, my engine takes 9 milliseconds to do its rendering, therefore, I''m running at about 111 FPS. When I include data processing in the loop, it takes about 15 milliseconds which is 66 FPS. Etc... you get the point.)

E.D.
Enoch DagorLead DeveloperDark Sky EntertainmentBeyond Protocol
Thanks for all the help. My only question left is I am using NeHe''s OpenGL text output and its kind of crappy. Anyone know anything better. Thanks again!
-Taphreek

This topic is closed to new replies.

Advertisement