Game Development Reference
In-Depth Information
has three main stages: it first gets the state of any input (such as gamepad or the
keyboard), updates the state of the game world, and finally updates all the pixels
on the screen.
A Closer Look at the Game Loop
Game code is not just responsible for updating the graphics on the screen; it has
two other very important tasks to do. It must take input from the user such as ''If
the user is pressing button B, then make the character jump,'' and it must update
the game world, ''If the player has just killed the end boss, then go to the credits.''
All game loops follow a similar pattern.
while (true)
{
// Find out what state keyboard and joypads are in
UpdateInput();
// Handle input, update the game world, move characters etc
Process();
// Draw the current state of the game to the screen
Render();
}
These are the three main stages of the loop: update the player input, update the
game world, and then tell the graphics card what to render.
Implementing a Fast Game Loop in C#
Open Visual Studio. Visual Studio assumes by default you will be making a
software application that's event-driven rather than one that's executing code
continuously, such as a game. Event-driven programs execute code in response
to events coming from the operating system or from user input. It's easier to
write games with a main loop that can be used to continually update the state of
the game world. The default code needs to change a little to get a fast game loop
implemented. It's important that the loop runs as often as possible, so this C#
code uses some C functions to ensure it's the fastest possible game loop.
Create a new Windows Form Application and call the project GameLoop .A
Program.cs file will automatically be generated with the following code:
namespace GameLoop
{
static class Program
 
 
 
Search WWH ::




Custom Search