Jump to content
  • Advertisement
Sign in to follow this  
_damN_

FPS problem...

This topic is 3163 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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]

Share this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!