0

i'm trying to calculate the FPS of my game, but the value is not stable, so i wanted to implement linear interpolation to make the displayed text smooth.. The problem is that i'm getting 1.#INF values, I tried googling but couldn't really find an explanation.

Here is the interpolation function:

float Lerp(float from, float to, float t)
{
    return (from*(1 - t) + to*t);
}

How i calculate the FPS:

void CalculateTime()
{
    deltaTime = (float)(SDL_GetTicks() - lastFrameTime) / 1000.0f; // this is around 0.02 each frame
    fps = 1.0f / deltaTime; // this is 50-60 every frame
    lastFps = Lerp(fps, lastFps, 0.5f); // lastFps is initialized at 60.0
    printf("%2.4f\n", lastFps);
}

Everything seems to be fine.. deltaTime, fps, lastFps are all floats (i tried double too but no difference) but when printf executes i get 1.#INF

Any ideas?

duplode
  • 33,731
  • 7
  • 79
  • 150
  • 1
    Your sample is not (quite) a compilable reproduction. If you could fix it up to be an independent example that compiles and runs, that would be nice. And then also use a debugger. – Puppy Sep 24 '15 at 17:35
  • 2
    deltaTime is at least once equal to 0, probably the very first time. Which produces infinity. You'll never get rid of it again. – Hans Passant Sep 24 '15 at 17:37

1 Answers1

1

Most likely, deltaTime was zero at one point (due to a frame taking less than a millisecond to draw). This gave a value of infinity for fps = 1.0f / deltaTime;. And due to the way you do your interpolation, once a value of infinity enters the system, lastFps will always be infinite.

interjay
  • 107,303
  • 21
  • 270
  • 254