Game Development Reference
In-Depth Information
With PeekMessage defined, it can now be used to determine if there are any
messages waiting in the application's event queue.
private bool IsAppStillIdle()
{
Message msg;
return !PeekMessage(out msg,IntPtr.Zero, 0, 0, 0);
}
That's it. With IsAppStillIdle defined correctly, the program now has an
extremely fast game loop. This FastLoop class can be reused for any game
project you make in the future.
Now to test that the game loop really works, go to Project.cs and add the fol-
lowing line of code.
static void GameLoop()
{
// GameCode goes here
// GetInput
// Process
// Render
System.Console.WriteLine("loop");
}
Every time the game loop is called, it will output the word ''loop'' to the console.
To see the console, go to Debug > Windows > Output; this will bring up another
window. Run the application and in the output window, the word ''loop'' will
keep scrolling down the screen.
Adding High-Precision Timing
To animate things smoothly, it's important to know how much time has elapsed
between frames. This time can then be used to keep animation independent of
the frame-rate. Games should run the same speed on all computers; a game
character shouldn't suddenly be able to run faster if the computer is faster!
Timing in computer games is very important. The time between frames must
be accurate and of a high resolution or the game will appear to be jerky. To get
the best timing functions, some C code needs to be used. This is less daunting as
we've already used InterOp to peek at Windows messages. The time between
frames is often called elapsedTime or the delta time, sometimes dt for short,
and it's measured in fractions of a second.
 
Search WWH ::




Custom Search