February 7th, 2011
Windows Phone – running game loops in SilverlightSilverlight C# Tutorials, Silverllight Games, Windows Phone, Working with Silverlight, by Gavin Wignall.
While developing my WP7 game Bitbreaker, I tried and tested several methods to get a reliable game loop running.
What is a game loop
Many games require movement that is affected by constant changing factors. This often requires that movement to be adjusted on a regular basis. In arcade games, for example Bitbreaker, this action is required to happen many times each second. A game loop is the function that fires this repeatable action on a regular basis.
Game loop types
I tried the following types of game loops while developing my Windows Phone App.
- Looping a storyboard
- Composition Target Rendering
- Dispatch Timer
Looping a storyboard
This is a very simple way to create a game loop.
- Create a storyboard either in the XAML or in your code behind file.
- Set the storyboards duration to the amount of time you want delayed between each action.
- Set the storyboard to repeat forever.
- Set up a Storyboard.Completed Event handler to fire a function every time your storyboard plays out: Storyboard.Completed += new EventHandler(Storyboard_Completed);
- Place your game logic inside the function called on completed.
This is a good way to ensure your game loop is called every possible chance it can as it matches your games frame rate.
- Add CompositionTarget.Rendering to your event handlers: CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
- Place your game logic inside the function called
This is good way to control your game loop repeating at a consistent rate.
- Add the new reference: using System.Windows.Threading;
- Declare Dispatch Timer as a member: DispatcherTimer timer = new DispatcherTimer();
- Set the interval value to control the time delay between each action: timer.Interval = TimeSpan.FromMilliseconds(25);
- Set up a Tick event handler on our DispatchTimer: timer.Tick += new EventHandler(timer_Tick);
- Place your game logic inside the function called on each Tick.
- Start the DispatchTimer: timer.Start();
After trying all 3 methods on a Windows Phone, I found the DispatchTimer to be the best suited for my Windows Phone game. So while many people think Windows Phones are more suited for business users and the odd chore like comparing buildings insurance online, these results show that it’s more than suitable for gaming. I needed to get the fastest frame rate but also keep the movement in my game consistent. The dispatch timer achieved this very well.
I found using the looping storyboard approach gave me a low frame rate as it required communicating with the XAML each time, even if there were no visible parts moving.
I use the CompositionTarget.Rendering route often when creating games for the common desktop PC, this is due to it giving a very fast loop. However, when developing for the Windows Phone, we are using a much slower processor and can’t guarantee the frame rate will stay consistent. For Bitbreaker I required the ball to keep a consistent speed, using this approach would make the ball speed up and slow down when asking the game to perform different tasks of varying difficulty.