Jump to content
  • Advertisement
Sign in to follow this  
Anddos

Calculate framerate with GetTickCount

This topic is 2413 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

This is my code, if i output framerate it says 0.00 fps


while (1)
{
// FPS stuff
int lastReport = GetTickCount();
int frameCount = 0;

// Check if there is a window message and remove it from the queue
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// If the message was to quit, break out of the while loop
if(msg.message == WM_QUIT) break;

// This Translates messages so windows understands them.
TranslateMessage (&msg);

// This sends the messages to the WndProc().
DispatchMessage (&msg);


}
else
{
// Print FPS to debug output window
frameCount++;
if (GetTickCount() - lastReport >= 1000)
{
framerate = frameCount / ((GetTickCount() - lastReport) / 1000);

lastReport = GetTickCount();
frameCount = 0;
}
}

Share this post


Link to post
Share on other sites
Advertisement
Cast your int variables to floats or doubles, otherwise you'll be using integer division which will always truncate (probably to zero, if you're above 1 frame per second). Like this:

framerate = (float)frameCount / ((float)(GetTickCount() - lastReport) / 1000);

I don't blame you though, this is one of the most illogical things ever. You'd think the compiler would do a deep cast to framerate's type implicitly, like in Pascal for instance.

Also you might want to use something more accurate than GetTickCount(), which has a limited precision and will have issues with high framerates (above ~80). I personally use QueryPerformanceCounter() and QueryPerformanceFrequency().

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!