Good FPS code...

Started by
2 comments, last by elis-cool 21 years, 9 months ago
I want my FPS counter to be more accurate, and in most games I see they update the FPS (frames per second, not first person shooter silly ) counter several times per second... how do they do this? averaging ot or something? how would u do this? heres what I am currently using:
  
static DWORD oldTime = 0;
static int FPSnum = 0;
static int fps = 50;

if(timeGetTime() > oldTime)
{	
    fps = FPSnum;
    oldTime = timeGetTime() + 1000;
    FPSnum = 0;
}
else FPSnum++;  
CEO Platoon Studios
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
Advertisement
I THINK that might work. You might want to check out
the article "Frame Rate Independent Movement" under
the game programming section.

http://www.gamedev.net/reference/articles/article1604.asp
i know all about frame rate intependent movement and everything, but the code I have only updates it once per second, and so is not very accurate...

CEO Platoon Studios
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
When you measure the time between two frames (call this delta) you are taking the seconds per frame. Simple maths tells us that if we take the reciprocal (1/x) we''ll get the frames per second.

However if you are doing this when your game is running at 200fps your counter will be jumping all over the place so you will want to average this delta out. The simplest way to do this would be to gather 10 deltas and then find the average delta, 1/x it then display it to the user.

You may find that timeGetTime() doesn''t have a high enough accuracy (you will only get discrete fps values) though so you may want to look into QueryPerformanceCounter() and QueryPerformanceFrequency().

This topic is closed to new replies.

Advertisement