References:

Overview

The application model is the part of XNA that models how a game at its very core is. In this page we summarize the main concepts. Most of the stuff here comes directly from the documentation.

The XNA Framework Game class provides a setup for processing game simulation, offering the basic methods (Initialize, Update and Draw). The first step in creating a new game is to make a class that derives from Game. The new class needs to override Update, Draw, and Initialize. This is what happens by default when you create a new XNA game project.

Initialize, Update and Draw

The Update method is responsible for handling game logic, and the Draw method is responsible for drawing each frame.Its up to you to put the correct code in the correct method. Especially in the case of a fixed time step, no rendering calls should be under the method update, since it might happen that the draw method is not called at all, and those computations would be a waste.

The Initialize method is responsible for game setup before the first frame of the game. It will stay empty for most of the time, but after a while you will start filling it. Typical calls are those referred to graphics setup such as screen resolution, plus the initialization of your game logic or other subparts of the game.

Loop Timing

A Game is either fixed step or variable step, defaulting to fixed step. The type of step determines how often Update will be called and affects how you need to represent time-based procedures such as movement and animation.

Fixed-Step Game Loops

A fixed-step Game tries to call its Update method on the fixed interval (specified in TargetElapsedTime). Setting IsFixedTimeStep to true causes a Game to use a fixed-step game loop. A new XNA project uses a fixed-step game loop with a default TargetElapsedTime of 1/60th of a second.

In a fixed-step game loop, Game calls Update once the TargetElapsedTime has elapsed. After Update is called, if it is not time to call Update again, Game calls Draw. After Draw is called, if it is not time to call Update again, Game idles until it is time to call Update.

If Update takes too long to process, Game sets IsRunningSlowly to true and calls Update again, without calling Draw in between. When an update runs longer than the TargetElapsedTime, Game responds by calling Update extra times and dropping the frames associated with those updates to catch up. This ensures that Update will have been called the expected number of times when the game loop catches up from a slow down. You can check the value of IsRunningSlowly in your Update if you want to detect dropped frames and shorten your Update processing to compensate. You can reset the elapsed times by calling ResetElapsedTime.

When your game pauses in the debugger, Game will not make extra calls to Update when the game resumes.

Variable-Step Game Loops

A variable-step game calls its Update and Draw methods in a continuous loop without regard to the TargetElapsedTime. Setting IsFixedTimeStep to false causes a Game to use a variable-step game loop.

Starting the Game

Calling the Game.Run method starts a game. This method starts a loop that will call Update and Draw multiple times a second until Exit is called.

Game Components, Services, ...

Developing your game you will grow your Game1 class, adding game logic, various calls to graphics, physics, networking, and so on. XNA offers a software engineering option which is to add game Components and Services. The main purpose is to allow transition of code from and to different projects.

They are not quite widely used as most people do not generalize their game that much. For further information refer to this link.

How to?

Here you can find the following list of how to:

Related Samples


Page URL: https://twiki.graphics.ethz.ch/bin/view/GameClass/ApplicationModel
2024-04-18
© 2024 Eidgenössische Technische Hochschule Zürich