1
General / If statement vs While loop for Updates in a Fixed Timestep
« on: July 14, 2018, 01:18:31 am »
Let's say I'm in the process of implementing the game loop for my SFML game. In order to maintain a fixed timestep, I would follow something like outline in: https://gafferongames.com/post/fix_your_timestep/
In order to implement something like that, the code would probably be:
(This is directly copied and pasted from https://github.com/SFML/SFML-Game-Development-Book/blob/master/01_Intro/Source/Game.cpp, and seems to be the direct implementation of the concept linked to in the article above).
However, I recently bought the book SFML Game Development By Example (by Raimondas Pupius), and found the following paragraph in Chapter 2:
As evident, the difference between both snippets is that one uses a while loop to check if the elapsed time is greater than the frametime, and keeps updating/subtracting until it isn't. This seems logical to me. The other approach, as shown in the quote, suggests to skip updates (to increase the elapsed time) until the time comes. These to me seem completely different ways of implementing a similar concept. Which type of fixed timestep should I use?
(Side question: How do I embed a link with its own text i.e in markdown: [link text](URL)?)
In order to implement something like that, the code would probably be:
void Game::run()
{
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
while (mWindow.isOpen())
{
sf::Time elapsedTime = clock.restart();
timeSinceLastUpdate += elapsedTime;
while (timeSinceLastUpdate > TimePerFrame)
{
timeSinceLastUpdate -= TimePerFrame;
processEvents();
update(TimePerFrame);
}
updateStatistics(elapsedTime);
render();
}
}
{
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
while (mWindow.isOpen())
{
sf::Time elapsedTime = clock.restart();
timeSinceLastUpdate += elapsedTime;
while (timeSinceLastUpdate > TimePerFrame)
{
timeSinceLastUpdate -= TimePerFrame;
processEvents();
update(TimePerFrame);
}
updateStatistics(elapsedTime);
render();
}
}
(This is directly copied and pasted from https://github.com/SFML/SFML-Game-Development-Book/blob/master/01_Intro/Source/Game.cpp, and seems to be the direct implementation of the concept linked to in the article above).
However, I recently bought the book SFML Game Development By Example (by Raimondas Pupius), and found the following paragraph in Chapter 2:
Quote
... (Page 32)
Unlike the variable time-step, where the next update and draw happens as soon as the previous one is done, the fixed time-step approach will ensure that certain game logic is only happening at a provided rate. It's fairly simple to implement a fixed time-step. First, we must make sure that instead of overwriting the elapsed time value of the previous iteration, we add to it like so:void Game::RestartClock() {
m_elapsed += m_clock.restart();
}
The basic expression for calculating the amount of time for an individual update throughout a 1 second interval is illustrated here:
1.0f
FrameTime = --------------
Updates/s
(It's actually an image, but whatever)
Let's say we want our game to update 60 times a second. To find the frame time, we would divide 1 by 60 and check if the elapsed time has exceeded that value, as shown here:float frametime = 1.0f / 60.0f;
if (m_elapsed.asSeconds() >= frametime) {
// Do something 60 times a second.
m_elapsed -= sf::seconds(frametime);
}
As evident, the difference between both snippets is that one uses a while loop to check if the elapsed time is greater than the frametime, and keeps updating/subtracting until it isn't. This seems logical to me. The other approach, as shown in the quote, suggests to skip updates (to increase the elapsed time) until the time comes. These to me seem completely different ways of implementing a similar concept. Which type of fixed timestep should I use?
(Side question: How do I embed a link with its own text i.e in markdown: [link text](URL)?)