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

Author Topic: Fixed time-step game loop and rendering  (Read 3193 times)

0 Members and 1 Guest are viewing this topic.

homar

  • Newbie
  • *
  • Posts: 7
    • View Profile
Fixed time-step game loop and rendering
« on: October 26, 2013, 10:44:28 pm »
Hello,

All of the examples of a fixed time-step game loop I have seen follow this logic:

sf::Clock clock;
const sf::Time timePerFrame = sf::seconds(1.0f / 60.0f);
sf::Time timeSinceLastUpdate = sf::Time::Zero;
while (window.isOpen())
{
        sf::Time dt = clock.restart();
        timeSinceLastUpdate += dt;

        while (timeSinceLastUpdate > timePerFrame)
        {
                timeSinceLastUpdate -= timePerFrame;
                ProcessEvents();
                Update(timePerFrame);
        }

        Render();
}
 

Is there not a problem with this code? The ProcessEvents() function generally checks for a sf::Event::Closed event and can close the window at this point. The problem is that the Render() function could then try to render to a closed window.  The result is that my program crashes with a segmentation fault if I have raw OpenGL calls in my Render() function.

If I use SFML's graphics module to draw 2D elements, then the program doesn't crash.  Does SFML check whether the window is still open (or the OpenGL context is still valid) when I call draw() on the window?

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Back to C++ gamedev with SFML in May 2023

homar

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Fixed time-step game loop and rendering
« Reply #2 on: October 26, 2013, 10:57:55 pm »
Thanks FRex!
I guess that I'll have to check for window.isOpen() before calling Render().

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: Fixed time-step game loop and rendering
« Reply #3 on: October 27, 2013, 01:01:15 am »
You could just return a bool, without directly calling the close method of the window, save it in a local variable and then use that information to get out of the loop.

 

anything