• Announcements

Archived

This topic is now archived and is closed to further replies.

Frames Per Second (FPS)

Recommended Posts

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.

Share on other sites
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...

Share on other sites
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.

Share on other sites
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.

Share on other sites
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^_^)

Share on other sites
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?

Share on other sites
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

Share on other sites
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

Share on other sites
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

Share on other sites
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

Share on other sites
Magmai: We use code similar to the following to calculate FPS. I added your filtered framerate calculation alongside it and get a 5 fps difference. That is, your function returns 25 whereas this function returns 30. The game loop is locked into a fixed time step of 30 fps. This code seems to be returning the correct value (I believe), whereas yours is 5 fps lower. Any ideas as to why the difference?

  static int oldtime, count, time;  count++;  if (count >= 10)  {    time = GetTickCount;    FPS = 10000.0f / max(time - oldtime, 1);    oldtime = time;    count = 0;  }

Steve ''Sly'' Williams  Code Monkey  Krome Studios

Share on other sites
I thinks it's due to the low accuracy of GetTickCount (+/- 10ms, QueryP... is good to +/-5us)... just a guess.

try this:
  static float FPS; static int oldtime, count, time; static LARGE_INTEGER liTime, liOldTime; count++; if (count >= 10) { QueryPerformanceCounter(&liTime); FPS = 10000000.0f / max((liTime.QuadPart - liOldTime.QuadPart), 1); liOldTime.QuadPart = liTime.QuadPart; count = 0; }

the numbers match then - I get a 5fps difference using tickcount as well.

Magmai Kai Holmlor
- The disgruntled & disillusioned

Call CalcFrameRate every frame; you can display the results every frame as well.

Edited by - Magmai Kai Holmlor on March 19, 2001 1:18:38 AM

Share on other sites
I tried your code and it was giving the same results (unfiltered) as your filtered routine does. So I guess the accuracy of the timers come into play here. On Win95 and possibly 98, GetTickCount is only accurate to 55ms as stated in MSDN.

Steve ''Sly'' Williams  Code Monkey  Krome Studios

Share on other sites
Use timeGetTime() instead, it may not be as accurate as QueryPerformanceCounter(), but msdn states that the resolution in W9x is 1ms, in NT or W2k it''s >= 5ms by default, but can be changed by timeBeginPeriod()/timeEndPeriod(). It is also available on all systems.
I use almost the exact same code as you Sly (except for GetTickCount()) and it works pretty ok.

Share on other sites
Are there any systems out there these days that do not support QueryPerformanceCounter? I have also been told of a few systems that return an incorrect value for QueryPerformanceFrequency. Can anyone confirm this? I found the following KB article about it, and it seems to be fairly rare. There is also this KB article about possible leaps forward in responses from QueryPerformanceCounter due to a design defect in certain chipsets.

Steve ''Sly'' Williams  Code Monkey  Krome Studios

• Partner Spotlight

• Forum Statistics

• Total Topics
627652
• Total Posts
2978421

• 10
• 12
• 22
• 13
• 33