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?