Hi
I tried to write a fixed timestep loop as explained in this (https://gameprogrammingpatterns.com/game-loop.html)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();
}
}
There is a small amount of time that passes between getting the elapsed time and restarting the clock.
Try the shortcut that cuts it as fine as possible:
elapsed = clock.restart();
sf::Clock's restart also returns the time elapsed time before restarting.
Everything else looks about right, without going into the details of what each separate external function does.
Also, sf::Time has a constant for zeroing the time, which can help with clarity:
fps_t = st::Time::Zero;
There is a small amount of time that passes between getting the elapsed time and restarting the clock.
Try the shortcut that cuts it as fine as possible:
elapsed = clock.restart();
sf::Clock's restart also returns the time elapsed time before restarting.
Everything else looks about right, without going into the details of what each separate external function does.
Also, sf::Time has a constant for zeroing the time, which can help with clarity:
fps_t = st::Time::Zero;
Sorry for the late reply. This actually solved the "issue", now i'm having readings between 60 and 61 fps exactly as with any other application. It feels weird that this small thing was eating so relatively much time though