Hi, I’m learning SFML with “SFML Game Development” (
https://www.amazon.com/SFML-Game-Development-Jan-Haller/dp/1849696845) and there is something I don’t understand about fixed time steps technique to give always the same delta time to the update function:
/*
When we are over the required amount for one frame, we substract
desired length of this frame (TimePerFrame) and update the game.
*/
void Game::run()
{
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
while(mWindow.isOpen())
{
processEvents();
timeSinceLastUpdate += clock.restart();
// collects the user input and computes the game logic.
while (timeSinceLastUpdate > TimePerFrame)
{
timeSinceLastUpdate -= TimePerFrame;
processEvents();
update(TimePerFrame);
}
render();
}
}
So we accumulate the elapsed time in timeSinceLastUpdate and substract the desired length of this frame (TimePerFrame = 1.f /60.f) and do this until we are below the required amount again ant then render the scene (this is called logic frame rate). With this the frame rate is 60 fps.
With this, if rendering is slow, processEvents() and update() may be called multiple times before one render() call, so the game occasionally slutters becuase not every update is rendered, but the game doesn’t go slow down. On the other hand fast rendering can lead to call render() multiple times without a logic update in between.
Ok, the problem of this method is that CPU usage is high (about 50% or more).
Then I can use sf::RenderWindow::setFrameLimit(60) that calls sleep() internally but lacks precision, and then is sf::RenderWindow::setVerticalSyncEnabled() that adapts the graphical updates to the refresh rate of the monitor (GPU I think).
My question is about what method do you usually use: and accurate method like first, the setFrameLimit function or the setVerticalSyncEnabled()? And if the answer is the first one, how do you solve the problem of the CPU usage?
Thanks to everyone.