Game Development Reference
In-Depth Information
public void StartGameLoop()
{
// If initialization is already finished,
then simply return.
if (m_IsInitialized)
return;
m_IsInitialized = true;
// Start the message pump.
MessagePump.Run(m_Form, GameLoop);
}
First, this function checks if the game window has already been initialized. If so,
then we simply return out of this function. Otherwise, we set the
m_IsInitialized
member variable to
true
to indicate that it has been initialized. This function is
essentially the initialization function for our game loop. And lastly, it calls
Mes-
sagePump.Run
passing in our
RenderForm
object (the game window itself) and
our
GameLoop()
function. This will cause the
GameLoop()
function to be called
repeatedly until we close the game window. (
RenderForm
and
MessagePump
are
SlimDX classes.)
So why do we need
MessagePump
? In Windows, applications receive messages,
which are just notifications that something has occurred. For example, a key press
will generate a key pressed message. These messages are sent to whichever win-
dow is currently active. That program can then process and respond to the message.
In a game, we want a loop that runs continuously to simulate and draw each frame
immediately after the previous one. We still have to handle messages from Windows
though, or our game window will not work correctly anymore. For example, if we just
had the program stuck in a loop, never checking Windows messages, then nothing
will happen when you try to close the window since the program will never process
the close message. So, we use this
MessagePump
class to handle Windows mes-
sages for us while running our game loop.