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

Author Topic: question about the main loop in the sfml book sample  (Read 1349 times)

0 Members and 1 Guest are viewing this topic.

lezebulon

  • Full Member
  • ***
  • Posts: 235
    • View Profile
question about the main loop in the sfml book sample
« 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.


StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: question about the main loop in the sfml book sample
« Reply #1 on: June 07, 2014, 01:52:09 am »
It's a fixed timestep if I remember right. 
I have many ideas but need the help of others to find way to make use of them.

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: question about the main loop in the sfml book sample
« Reply #2 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.

 

anything