SFML community forums

Help => General => Topic started by: lezebulon on June 07, 2014, 01:24:44 am

Title: question about the main loop in the sfml book sample
Post by: lezebulon on June 07, 2014, 01:24:44 am
Hi
the main loop in the sfml sample from the book is like so :
        sf::Clock clock;
        sf::Time timeSinceLastUpdate = sf::Time::Zero;

        while (mWindow.isOpen())
        {
                sf::Time dt = clock.restart();
                timeSinceLastUpdate += dt;
                while (timeSinceLastUpdate > TimePerFrame)
                {
                        timeSinceLastUpdate -= TimePerFrame;
                        processInput();
                        update(TimePerFrame);

                        // Check inside this loop, because stack might be empty before update() call
                        if (mStateStack.isEmpty())
                                mWindow.close();
                }

                updateStatistics(dt);
                render();
        }

what's the point of the inner while() loop ? If I understand correctly this will potentially call the input / process functions several times if the previous frame was rendered too slowly... but shouldn't this be handled by the input function who will poll all the queued events anyway ? Also the input / process block can be skipped if timeSinceLastUpdate <= TimePerFrame, but I don't understand what is the point of this.

Title: Re: question about the main loop in the sfml book sample
Post by: StormWingDelta on June 07, 2014, 01:52:09 am
It's a fixed timestep if I remember right. 
Title: Re: question about the main loop in the sfml book sample
Post by: dabbertorres on June 07, 2014, 09:25:55 am
It's a way to keep the game simulation running at the same speed across all competent machines.
If the simulation falls behind, it "skips" drawing, and runs one timestep of the simulation again, essentially playing catch-up.