In my game loop,
m_currentFrameTimestamp = glfwGetTime(); // Get the current timestamp
if ((m_currentFrameTimestamp - m_lastFPSDisplayTimestamp) >= 0.25) { // If 1/4 of a second
// passed, update the FPS counter
double renderTime = m_currentFrameTimestamp - m_lastFrameTimestamp; // Frame time
m_fps = (unsigned int)round(1 / renderTime); // FPS
std::string title = "MyGame ["s;
title += std::to_string(m_fps);
title += " FPS]"s;
glfwSetWindowTitle(m_window, title.c_str()); // The title
m_lastFPSDisplayTime = m_currentFrameTime; // Reset the FPS update timer
}
m_lastFrameTime = m_currentFrameTime; // Reset the frame time timer
calculates the FPS from the frame time and displays it in the window title. However, with just this and glClear()
above, the FPS displayed in the title is very unstable (with each FPS update, it changes from 46 FPS to 87 FPS and back), even though back in the initialization step, I enabled VSync, and my display's frequency is 60 Hz. Furthermore, using Fraps, I found out that the program actually runs at a stable 60 FPS. So, what's the problem with my FPS calculation code (more specifically, why is glfwGetTime()
producing inaccurate results - my code, being way too simple, just can't be wrong, it's a very simple calculation that cannot give errors of any kind)?