February 7th, 2011

Windows Phone – running game loops in Silverlight

Silverlight 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.

  1. Create a storyboard either in the XAML or in your code behind file.
  2. Set the storyboards duration to the amount of time you want delayed between each action.
  3. Set the storyboard to repeat forever.
  4. Set up a Storyboard.Completed Event handler to fire a function every time your storyboard plays out: Storyboard.Completed += new EventHandler(Storyboard_Completed);
  5. Place your game logic inside the function called on completed.

CompositionTarget.Rendering

This is a good way to ensure your game loop is called every possible chance it can as it matches your games frame rate.

  1. Add CompositionTarget.Rendering to your event handlers: CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
  2. Place your game logic inside the function called

Dispatch Timer

This is good way to control your game loop repeating at a consistent rate.

  1. Add the new reference: using System.Windows.Threading;
  2. Declare Dispatch Timer as a member: DispatcherTimer timer = new DispatcherTimer();
  3. Set the interval value to control the time delay between each action: timer.Interval = TimeSpan.FromMilliseconds(25);
  4. Set up a Tick event handler on our DispatchTimer: timer.Tick += new EventHandler(timer_Tick);
  5. Place your game logic inside the function called on each Tick.
  6. Start the DispatchTimer: timer.Start();

The results

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.

Back Top

Responses to “Windows Phone – running game loops in Silverlight”

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Back Top