,

Harnessing the XNA Game Loop Via the GameTimer Class

In XNA, responding to user input, updating your app’s state, and drawing content all happen periodically within the game loop. The game loop is the heart of every XNA game, and understanding it is fundamental to XNA development.

Unlike pure XNA games, which use the Game class for the game loop, hybrid apps rely on the Microsoft.Xna.Framework.GameTimer, which is unique to the phone.

In a pure XNA game, a Game class is used instead of an Application class. XNA games rely on overloading Game class methods that are specific to the game loop, such as Update and Draw, whereas hybrid apps (Windows Phone 7.1 apps that combine XNA and Silverlight) and apps that rely on XNA APIs, such as the sound effect API, use the GameTimer class instead. The GameTimer class offers various game loop events that correspond to methods of the same name in ordinary XNA games.

GameTimer has three events: Draw, FrameAction, and Update. When raised, the Draw and Update events deliver a GameTimerEventArgs object, which contains the same timing information as the GameTime object provided to the Game.Update and Game.Draw methods.

The GameTimer class extends the functionality around the game loop, going beyond the Game class by allowing you to create multiple GameTimer instances, each dedicated to drawing particular objects or sets of objects in your UI. The order in which the Draw event is raised for different GameTimer instances can be controlled using the GameTimer.DrawOrder property. DrawOrder indicates the order in which Draw events are called when more than one timer has the event raised at the same time. GameTimer objects with lower DrawOrder values have their event handlers called before those of GameTimer objects with higher values.

For example, the following excerpt shows three GameTimers. Each has a subscription to its Draw event, and each specifies a different DrawOrder value:

timer1.Draw += delegate { Debug.WriteLine("Timer 1"); };
timer1.DrawOrder = 10;

timer2.Draw += delegate { Debug.WriteLine("Timer 2"); };
timer2.DrawOrder = 100;

timer3.Draw += delegate { Debug.WriteLine("Timer 3"); };
timer3.DrawOrder = 1;

By specifying a lower DrawOrder value, the event handler of timer3 is called first, and timer2, having the highest DrawOrder, is called last. See the following output:

Timer 3
Timer 1
Timer 2
Timer 3
Timer 1
Timer 2
...

The other two GameTimer properties: GameTimer.UpdateOrder and FrameActionOrder, are analogous to the DrawOrder property yet pertain to the Update and FrameAction events, respectively.

Controlling the Interval Between Updates

The GameTimer.UpdateInterval property allows you to explicitly set the time between updates. By default this value is 333333 ticks or ~33 ms. A tick is the smallest unit of time, equal to 100 nanoseconds. This equates to 30 frames a second.


Note

Be careful when explicitly setting UpdateInterval. If set too short and the event handler consistently runs longer than the time interval set by UpdateInterval, game performance is adversely affected.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.63.13