FPS problem...

Started by
2 comments, last by mattd 14 years, 5 months ago
Hi there... i got a problem with my code that calculates the FPS of my application. Basically when i print the FPS on screen it starts a 0, then counts up till the actual frames per second, then jumps back to 0 and starts counting up again. If i print the FPS to the taskbar it doesnt jump back to 0? Heres the code. void CD3D::CalculateFrameRate(void) { // Local variables. static int lastTime = 0; static int frameTime = 0; static int currentTime = 0; // Get the current time in seconds. currentTime = timeGetTime() / 1000; // Store the interval between the current and last frame. g_FrameInterval = currentTime - frameTime; // Set the frameTime to the currentTime. frameTime = currentTime; //Add to the counter m_iFramesPerSecond++; if( currentTime - lastTime > 1 ) { // Here we set the lastTime to the currentTime lastTime = currentTime; // Reset the frames per second m_iFramesPerSecond = 0; } } I realize this is probably a pretty simple problem, but can anyone see where the problem is and offer me a solution? Thanks alot!
Advertisement
Well, you explicitly reset the FPS counter to 0 once every two seconds..

// Reset the frames per second
m_iFramesPerSecond = 0;

If you're then displaying m_iFramesPerSecond to the screen every frame you'll be seeing this same reset-count up-reset-count up effect.

First, the comparison currentTime - lastTime > 1 should probably be changed to currentTime - lastTime >= 1, so the reset counter routine gets called every second, not every.. second second.

Then, perhaps just before resetting this counter, you should copy it into another variable (ie. store the FPS for the last second) and then display this variable instead - this way you won't see the 'in progress' m_iFramesPerSecond FPS counter (and then go and rename the variables to reflect their new usages!)

As an aside, your timekeeping variables ((last|frame|current)Time) have type int, which means g_FrameInterval will only have a resolution of one whole second - your frame times are most likely on the order of ms, not s.

[Edited by - mattd on October 24, 2009 12:34:51 PM]
Thanks alot! It was so simple!
So are you saying that it would be better if they were of type double or type float so that they can be more accurate?
Yup. Especially when frame time can be a better metric of performance than FPS.

This topic is closed to new replies.

Advertisement