I can't figure out why this fps counter is inaccurate.

Posted by rmetzger on Stack Overflow See other posts from Stack Overflow or by rmetzger
Published on 2010-02-14T16:39:45Z Indexed on 2010/05/23 1:00 UTC
Read the original article Hit count: 284

Filed under:
|
|
|

I'm trying to track frames per second in my game. I don't want the fps to show as an average. I want to see how the frame rate is affected when I push keys and add models etc. So I am using a variable to store the current time and previous time, and when they differ by 1 second, then I update the fps.

My problem is that it is showing around 33fps but when I move the mouse around really fast, the fps jumps up to 49fps. Other times, if I change a simple line of code elsewhere not related to the frame counter, or close the project and open it later, the fps will be around 60. Vsync is on so I can't tell if the mouse is still effecting the fps.

Here is my code which is in an update function that happens every frame:

FrameCount++;
currentTime = timeGetTime ();
static unsigned long prevTime = currentTime;
TimeDelta = (currentTime - prevTime) / 1000;
if (TimeDelta > 1.0f)
{
 fps = FrameCount / TimeDelta;
 prevTime = currentTime;
 FrameCount = 0;
    TimeDelta = 0;
}

Here are the variable declarations:

int FrameCount;
double fps, currentTime, prevTime, TimeDelta, TimeElapsed;

Please let me know what is wrong here and how to fix it, or if you have a better way to count fps. Thanks!!!!!!

I am using DirectX 9 btw but I doubt that is relevant, and I am using PeekMessage. Should I be using an if else statement instead? Here is my message processing loop:

MSG msg;
ZeroMemory (&msg, sizeof (MSG));

while (msg.message != WM_QUIT)
{
    if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
        TranslateMessage (&msg);
        DispatchMessage (&msg);
    }

    Update ();
    RenderFrame ();
}

© Stack Overflow or respective owner

Related posts about game-development

Related posts about frame