SFML community forums

Help => Graphics => Topic started by: Nanobyte7 on July 01, 2014, 02:37:17 am

Title: [SOLVED] This concept dosent make sense for me
Post by: Nanobyte7 on July 01, 2014, 02:37:17 am
In the SFML 2.0 development book code, I saw this:
Quote
const sf::Time Game::TimePerFrame = sf::seconds(1.f/60.f);

    [...]

    sf::Clock clock;
    sf::Time timeSinceLastUpdate = sf::Time::Zero;
    while (mWindow.isOpen())
    {
        sf::Time elapsedTime = clock.restart();
        timeSinceLastUpdate += elapsedTime;
        while (timeSinceLastUpdate > TimePerFrame)
        {
            timeSinceLastUpdate -= TimePerFrame;

            processEvents();
            update(TimePerFrame);

        }

        updateStatistics(elapsedTime);
        render();
    }

Why would there be a fixed time-step but unlimited fps? When execute my program using this same concept my computer starts making a noise from buffering or displaying everything too fast. Wouldn't it make more sense to just put render in the while loop after update?
Title: Re: This concept dosent make sense for me
Post by: Hapax on July 01, 2014, 04:51:24 am
Rendering once per cycle ensures that the display is only updated with the latest state. The fixed-step loop is to give the program chance to catch up if it's behind.
Title: AW: This concept dosent make sense for me
Post by: eXpl0it3r on July 01, 2014, 07:55:36 am
The goal of the fixed time step is not to limit the frame rate or has essentially nothing to do with rendering at all. The fixed time step is to get an consistent logic steps. The logic is thus independent from the rendering. If your PC only manages to render with 20 fps, the position calculation won't be effected by that, it will still move the same pixels per second.

Of course running the rendering as fast as possible isn't good, thus you can still use VSync or frame rate limit and since the logic doesn't depend on the FPS anymore, it doesn't matter if it hovers around 60 or so instead of being exactly 60.
Title: Re: This concept dosent make sense for me
Post by: Nanobyte7 on July 02, 2014, 12:28:12 am
Thank you for the explanation! Ill just use .setVerticalSyncEnabled and .setFramerateLimit to not kill my CPU :D
Title: Re: [SOLVED] This concept dosent make sense for me
Post by: dabbertorres on July 02, 2014, 01:51:08 am
No, not a combination of the two, just one (preferably setVerticalSyncEnabled).