Game Development Reference
In-Depth Information
Here the game loop callback has been stored in a member variable, _callback ,
for later use. The Application.Idle event has been given a handler,
OnApplicationEnterIdle . This will be called when the application begins
to idle. The next part is a bit trickier. The code needs to know if the application is
still in the idle state, and if it is then it needs to continually call the loop callback.
Read this code to see how the callback will be called.
void OnApplicationEnterIdle(object sender, EventArgs e)
{
while (IsAppStillIdle())
{
_callback();
}
}
private bool IsAppStillIdle()
{
Message msg;
return !PeekMessage(out msg,IntPtr.Zero, 0, 0, 0);
}
The code calls the callback continuously unless IsAppStillIdle returns false.
IsAppStillIdle uses a new PeekMessage function to check if the appli-
cation has any important events that need to be dealt with. If there are important
messages for the application then these need to be handled before returning to
the game loop.
To check if the application is idle, we need to have a look at the Windows mes-
sage queue. Windows has a big queue of events. If a window is moved, an event is
added to the event queue; if it's minimized, an event is added to the queue; if a
user presses a key on the keyboard, an event is added—many different actions
generate events. All forms need to handle their own events. If our application has
some events in its queue, then it needs to stop idling and deal with them.
The easiest way to check the event queue is by using some Windows C functions.
In C#, this is called InterOp, short for InterOperation. Using some C functions in
a C# program is actually very easy to do. To see what's in the event queue, we're
going to use a C function called PeekMessage . We just want to have a peek at
the queue and see if anything's there. This is the C definition.
BOOL PeekMessage(
LPMSG lpMsg,
 
Search WWH ::




Custom Search