Frames Per Second (FPS)

Started by
13 comments, last by Gf11speed 23 years, 1 month ago
Now that I think about it, I have no idea how to display FPS in my game. It seems simple, but I just can''t figure it out. How exactly could I calculate FPS? I''m pretty sure I know how to find the the ''frames'' part, but not sure how to find the ''per second'' part. Does anyone know how to do this? Thanks.
Greg FieldsSyntasoft Gameswww.syntasoft.comPost-Time Horse Racing (Available Now)
Advertisement
what you do is this

var1 = GetTimeInMilliSeconds();


//code


var2 = GetTimeInMilliSeconds() - var1;

FPS = 1000 / var2

=)

to get the milliseconds you could use GetTickCount() but, thats not the best method...






woha
I use Borland C++ Builder 5, and this is how I''d do it. In the idle loop just have counter like:
framecount++;

Then put a timer onto the form that has an interval of 1000 milliseconds. In the OnTimer event output the framecount, then set it to zero.

Where is the GetTimeInMilliSeconds() function located? What .h file should I include? I can''t find it. I used GetTickCount() but that can''t possibly be returning correct results. I not doing anything and its fluctuating between 50 and 100 FPS.
Greg FieldsSyntasoft Gameswww.syntasoft.comPost-Time Horse Racing (Available Now)
Here''s my fps calculator:
	thisTick = GetTickCount();	incTick = thisTick - lastTick;	lastTick = thisTick;	if(incTick <= 0)		incTick = 1;      //don''t div by zero	fps = (fps + (1000 / incTick)) / 2; 


It''s about the same as Sprochet''s though, and does fluctuate, but it averages the 2 most recent rame rates, so it should be a little more accurate.


-Deku-chan

DK Art (my site, which has little programming-related stuff on it, but you should go anyway^_^)
Ok I think I got it right. I''m getting anywhere from 50-80 FPS in the game. That seems very smooth, I wonder if its right?
Greg FieldsSyntasoft Gameswww.syntasoft.comPost-Time Horse Racing (Available Now)
quote:Original post by Gf11speed

Ok I think I got it right. I''m getting anywhere from 50-80 FPS in the game. That seems very smooth, I wonder if its right?


What you could do to find out if it''s right is increment some variable every frame and output it onto the screen. Then just use a watch to see if the FPS you were getting is close. Not perfect, but it may at least tell you if the code you''re using to approximate the FPS was close. If it is close it''s probably right.


Need help? Well, go FAQ yourself.
What a plight we who try to make a story-based game have...writers of conventional media have words, we have but binary numbers
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
Is there anything wrong with the fps counting method that the MS DX samples in the DX SDK?

I think it''s a tad different than the stuff above, though i may just be remembering wrong. In that case, just tell me to shutup


Get Banner At: http://www.crosswinds.net/~druidgames/resist.jpg
BetaShare - Run Your Beta Right!
I think I'm gonna make a macro for this. Maybe I should do a sweet snippets for it...

Single Pole Digitally Filtered FrameRate.
No crappy twiddle, sub fps accuracy.
Downside is that is lags, but that's almost good in this instance.
Takes 17us to calculate on a K6-3 450MHz. (3D rendering times are around around 20,000us)
    float CGameEngine::CalcFrameRate(LARGE_INTEGER liNow_us, float* pfElapsed_sec)	{	//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;		if(pfElapsed_sec)		*pfElapsed_sec = fElapsed_ms / 1000.0f;	return(fFrameRate_fps);	}    


Magmai Kai Holmlor
- The disgruntled & disillusioned

Fixed a bug


Edited by - Magmai Kai Holmlor on March 18, 2001 10:21:57 PM
- 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 simply do

FPS=0;
LastTimeCheck=0;
FrameCount=0;

main ()
{
do everything

TextOut(0,0,FPS);

if(GetTickCount()-LastTimeCheck>1000)
{
FPS=FrameCount;
LastTimeCheck=GetTickCount();
FrameCount=0;
}
else
FrameCount++;

}


Simple and it works.

Ben
http://therabbithole.redback.inficad.com

This topic is closed to new replies.

Advertisement