1

How can i get frames per second using delta time.

Here's my code that return delta time

auto const old = last;
last = steady_clock::now();
const duration<float> frameTime = last - old;
return frameTime.count();

enter image description here

TestoHirvi
  • 11
  • 1
  • 4

2 Answers2

2

Suppose your delta time is in milliseconds, then fps (frames per second) is calculate as fps = 1000 / delta_time.

vaizq
  • 33
  • 6
0

You can get easily the fps by counting the fps you get under 1s. Hers the code:

float m_secondCounter;
//These are not the real fps, just temporary
float m_tempFps;
//This float is the fps we should use
float fps;

void update()
{
    /* How ever you get deltaTime in seconds */

    if (m_secondCounter <= 1) {
        m_secondCounter += deltaTime;
        m_tempFps++;
    }
    else 
    {
        //"fps" are the actual fps
        fps = m_tempFps;
        m_secondCounter = 0;
        m_tempFps = 0;
    }

    //Do something with the fps
    std::cout << "FPS: " << fps << std::endl;
}
jani
  • 1
  • 1