Hi
I tried to write a fixed timestep loop as explained in
this article using SFML, and wanted to have an fps reading to check my engine's performance while i add stuff to it.
When i enable v-sync in various games i tend to get 60-61 FPS, while with the reading i'm doing here, enabling v-sync i get 58-59. I know it's very little but it looks weirdly inconsistent to me, so i thought maybe i'm doing something wrong in the loop.
size_t steps_per_second = 30;
sf::Time seconds_per_step = sf::seconds(1.f / steps_per_second);
Game cycle method:
void Game_engine::run()
{
sf::Clock clock;
sf::Time elapsed;
sf::Time lag;
#ifdef ENGINE_SHOW_FPS
size_t frames_count = 0;
sf::Time fps_t = sf::seconds(0.f);
#endif
clock.restart();
lag = sf::seconds(0.f);
while (window.isOpen())
{
elapsed = clock.getElapsedTime();
#ifdef ENGINE_SHOW_FPS
frames_count++;
fps_t += elapsed;
if (fps_t >= sf::seconds(1.f))
{
float fps = frames_count / fps_t.asSeconds();
text_debug_fps.setString("FPS: " + std::to_string(fps));
frames_count = 0;
fps_t = sf::seconds(0.f);
}
#endif
lag += elapsed;
clock.restart();
while (lag >= seconds_per_step)
{
events();
networking();
step();
lag -= seconds_per_step;
}
draw();
}
}