Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Getting accurate fps reading out of a fixed timestep loop  (Read 2821 times)

0 Members and 1 Guest are viewing this topic.

barnack

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Getting accurate fps reading out of a fixed timestep loop
« on: August 17, 2019, 12:55:31 pm »
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();

        }
    }
« Last Edit: August 17, 2019, 01:09:23 pm by barnack »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Getting accurate fps reading out of a fixed timestep loop
« Reply #1 on: August 18, 2019, 08:26:45 pm »
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;
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

barnack

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: Getting accurate fps reading out of a fixed timestep loop
« Reply #2 on: August 31, 2019, 12:27:16 am »
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

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Getting accurate fps reading out of a fixed timestep loop
« Reply #3 on: September 04, 2019, 12:38:10 am »
Glad everything's okay for you now. :)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*