Implementing the main loop
In the previous chapters our code examples used the OnTimer()
callback with a rough fixed timestep to update the state and the OnDrawFrame()
callback to render graphics. This is not suitable for a real-time game where we should update the state based on the real time elapsed since the last frame. However, it is still desirable to use a small fixed timestep in the call to OnTimer()
. We can solve this problem by interleaving calls to OnTimer()
and OnDrawFrame()
in a tricky fashion and put this logic into a game main loop.
Getting ready
There is a very interesting article called Fix Your Timestep! available at http://gafferongames.com/game-physics/fix-your-timestep, which explains in great detail different approaches to the implementation of a game main loop and why fixed timesteps are important.
How to do it…
The logic of the game main loop is platform-independent and can be put into a method:
void GenerateTicks() {
GetSeconds()
returns monotonous time in seconds since...