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?
2 Answers
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.
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.
-
\$\begingroup\$ FPS can be a great measurment indeed, for example, when debugging shader performance rather than game logic performance. \$\endgroup\$ Commented Mar 30, 2019 at 15:59