Game Development Reference
In-Depth Information
2. GetSeconds() returns monotonous time in seconds since the system start.
However, only frame deltas matter:
NewTime = GetSeconds();
float DeltaSeconds = static_cast<float>( NewTime -
OldTime );
OldTime = NewTime;
3.
We will update the game logic with a ixed timestep that corresponds to a game
running at 60 frames per second:
const float TIME_QUANTUM = 1.0f / 60.0f;
4.
Also, we need a failsafe mechanism to prevent excessive slowdowns of the game due
to slow rendering speed:
const float MAX_EXECUTION_TIME = 10.0f * TIME_QUANTUM;
5.
Now we accumulate the elapsed time:
ExecutionTime += DeltaSeconds;
if ( ExecutionTime > MAX_EXECUTION_TIME )
{ ExecutionTime = MAX_EXECUTION_TIME; }
6.
And invoke a sequence of the OnTimer() callbacks accordingly. All of OnTimer()
callbacks receive the same ixed timestep value:
while ( ExecutionTime > TIME_QUANTUM )
{
ExecutionTime -= TIME_QUANTUM;
OnTimer( TIME_QUANTUM );
}
7.
After the game has been updated, render the next frame:
OnDrawFrame();
}
How it works…
The OnDrawFrame() callback should be called after the update. If the device is fast enough,
OnDrawFrame() will be invoked after every single OnTimer() call. Otherwise, some frames
will be skipped to preserve the real-time speed of the game logic. And in the case when the
device is too slow to run even the game logic, our safeguard code will spring into action:
if ( ExecutionTime > MAX_EXECUTION_TIME )
{ ExecutionTime = MAX_EXECUTION_TIME; }
 
Search WWH ::




Custom Search