2
\$\begingroup\$

Just letting you guys know before anything I'm a beginner at programming. I am just wondering how I should go about calculating the fps using GLFW?

\$\endgroup\$
4
\$\begingroup\$

You can get the amount of milliseconds passed between 2 render calls by storing the time the last render happened and subtracting it from the current time. Then you can get the exact fps count by dividing 1000 with the time passed.

\$\endgroup\$
3
\$\begingroup\$

This is very straight-forward. The following code is a slightly modified version from the code I found from this link, which I came upon with a Google search of "GLFW get fps".

Anyways, here's the basic algorithm: you increment a frame counter each time you call the update() method in your game loop, and you display and clear that value once a second.

double previousTime = glfwGetTime();
int frameCount = 0;

while (isGameRunning) 
{
    // Measure speed
    double currentTime = glfwGetTime();
    frameCount++;
    // If a second has passed.
    if ( currentTime - previousTime >= 1.0 )
    {
        // Display the frame count here any way you want.
        displayFPS(frameCount);

        frameCount = 0;
        previousTime = currentTime;
    }

    // Rest of your game loop...
 }

Please note that FPS is not a very great measurement of performance, but the code above should let you measure the FPS.

\$\endgroup\$
  • \$\begingroup\$ FPS can be a great measurment indeed, for example, when debugging shader performance rather than game logic performance. \$\endgroup\$ – Vladislav Toncharov Mar 30 '19 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.