Monday, 21 February 2011

XNA Challenge starter Kit 2011 – Getting started

Ok the first thing to note is that the starter kit is only deployable for a Windows 7 Mobile solution developed in VS 2010 under XNA 4.0 on a Windows 7 machine. If you want to run this version in the Windows 7 mobile emulator or deploy it on an actual WM7 phone I recommend you.

  1. Have Windows 7 installed on your PC.
  2. Download the XNA Windows Phone Developer Tools RTW
  3. This has a a bundle of stuff including the emulator needed to run the starter kit as is.
  4. You will also need VS 2010 express or upwards that you can get from Dreamspark.
  5. If you have Vista you may have driver issues with wddm 1.1 driver which the emulator needs along with Direct X 10. At the time of writing I’m installing updates for my NVIDIA 8600M Graphics card. But I’m not sure how this will go so…

If you want to develop the starter kit to deploy a windows and/or XBOX solution you need to

  1. The first thing to do is create a copy of the project for Windows.
  2. Then unload the Win 7 project from the solution.
  3. The solution has been developed on a Windows 7 machine and as such if you compile it you will get an error on the long file name of the project name. So change the name to something shorter.
  4. If it still complains about running because of D3D problems, then edit the properties of the windows version and make sure the HiDef settings are off.


The input manager is also only wired up for gesture input from the Windows Mobile. So you have to figure out what controls you are going to use on the XBOX controller and then have a look at the Input Manager and then edit the input manager to allow for these. The engine is well coded in general with State maintained quite well. Input enum values give rise to changes in game state in the game engine.

Here is an alternative Keyboard manager function – only up and space are programmed to react here. Space changes the game engine state represented by

public enum GameState { Splash, Play, Win, TryAgain, Loading }

see Engine.cs

private Input HandleKeyInput(Game.Engine.GameState gameState)

Input input = Input.None;
keyboardStateCurrent = Keyboard.GetState();
input = Input.Up;
input = Input.Select;
keyboardStatePrevious = keyboardStateCurrent;
return input;


and here is the appropriate change to the PlayerInput to switch from the gesture managed code to keyboard managed

public Input PlayerInput(Microsoft.Xna.Framework.GameTime gameTime)
Input input = Input.None;
//#if !WINDOWS || !XBOX
//input = HandleTouchInput(XNAGameStudioIrelandChallenge.Engine.Game.Engine.GameState.TryAgain);
input = HandleKeyInput(XNAGameStudioIrelandChallenge.Engine.Game.Engine.GameState.TryAgain);
return input;

This game engine and scene manager are well written. The tile editor concept you have covered with Neil. With minor changes you should be able to load your own content and change the behaviour of the game.

An amended copy of the engine code with the changes detailed above can be found in here. You will have to logged in to windows Live and a member of the year 2 Games Group to access this folder.

There is nothing in this project that you cannot do in XNA 3.1 I think. To import it you’ll need to open up a new project and import all the folders and code into the VS 2008 project with XNA 3.1. It’s a pain but doable.

I will blog more on the structure of the Game in a later post.

Thursday, 17 February 2011

Understanding GameTime - Shawn Hargreaves Blog - Site Home - MSDN Blogs


Understanding GameTime - Shawn Hargreaves Blog - Site Home - MSDN Blogs

Shawn Hargreaves explains how the timing of the Game Loop in XNA works and how it can be affected.

Monday, 14 February 2011

XNA A common Sprite Batch as a Game service

In a previous Post it was pointed out that using game components can lead to multiple sprite batches in each component. A solution to this was presented to me by Neil Gannon and I am applying it here to multiple components were each component just has a sprite batch draw event and each component

public class Game1 : Microsoft.Xna.Framework.Game
MovingCircle myCircle;
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;



Then in the game initialize function we create the sprite batch as a service and we set up the moving service components. The Constructor for the Moving Circle Component accesses the Game Components and adds itself. Hence the commented out line  below. Again Neil’s style.

protected override void Initialize()
spriteBatch = new SpriteBatch(GraphicsDevice);
this.Services.AddService(typeof(SpriteBatch), spriteBatch);
for (int i = 0; i < 9; i++)
myCircle = new MovingCircle(this);

Now to the Game Draw event.

protected override void Draw(GameTime gameTime)

Note: The base.Draw(gameTime);  call before the End() call allows all the Draw() events of the components to be marshalled before the sprite batch End() call commits the draws to the graphics device.

The Code for the Moving Circle Class is thus….


public class MovingCircle : Microsoft.Xna.Framework.DrawableGameComponent
SpriteBatch spriteBatch;
Texture2D movingCircle;
Vector2 movingCirclePosition;
Vector2 Target;



public MovingCircle(Game game)

            : base(game)


            spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

            game.Components.Add(this); // Add yourself


// The component adds its own content

protected override void LoadContent()
movingCircle = Game.Content.Load<Texture2D>("MovingCircle");
movingCirclePosition = new Vector2(LocalRand.RMinMax(0,600), LocalRand.RMinMax(0,400));
Target = new Vector2(LocalRand.RMinMax(0, 600), LocalRand.RMinMax(0, 400));

// Update the moving Circle    
public override void Update(GameTime gameTime)
movingCirclePosition = Vector2.Lerp(movingCirclePosition, Target, 0.1f);
if (Vector2.Distance(movingCirclePosition, Target) < 0.2)
Random r = new Random();
movingCirclePosition = Target;
Target = new Vector2(r.Next(600), r.Next(400));
// Line up the Draw() for this component
public override void Draw(GameTime gameTime)
spriteBatch.Draw(movingCircle, movingCirclePosition, Color.Red);
The Result


The Code is here

Thursday, 10 February 2011

Searching through a Game component collection

Generally you access the game component collection or the services to find a game component. But what if you want to find a collection within a collection. And what if you only wanted to find the components within a range of the player.

We will look at the Game components collection here. We can use generics and the fact that the Components are a collection using Linq to find the relevant components. Say we have a player class and we want to see all the moving circles around it.

Firstly a generic method in the player class to return a collection of specified components

List<T> FindComponent<T>()
return Game.Components.OfType<T>().ToList<T>();


1. This code is called from a game component or you could put it in a static class that can be accessed from anywhere in the game, that has access to the Game object.

Then you can have another method to return the appropriate List of objects.


public List<MovingImage> AroundPlayerPos()

return FindComponent<MovingImage>();

This method can then be further modified to search through the collection using Linq to further qualify what is returned to the calling method.

XNA Game services

I read a good analogy recently explaining the concepts of Game Components and Game services. Game components are like the plumbing (or sewage depending on how good your code is) system underneath a city that is your game. The components are interconnected through the Game class, but can exist independently to certain extent. Game Services are like have a Large Zeppelin over the city with rope ladders that can you can use to grab elements that you need to reference in your game objects. You climb up the rope ladder and grab the type of object that you want. Game services allow you to share objects registered as Game services across Game components. It is reserved for items that a Game Component might need to preform a function

Here is how to use game services. You set up your objects in the main game

Say you want to use a common sprite batch that is set up in your main Game class

spriteBatch = new SpriteBatch(GraphicsDevice);

this.Services.AddService(typeof(SpriteBatch), spriteBatch);

the typeof function says what kind of ladder you will be using to access the service later.

then in the class that you want to retrieve it from (in this case a game drawable component) 

public ChaseImage(Game game, Texture2D CircleImage)
: base(game)
sp = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
image = CircleImage;
spriteFrame = new Rectangle(0,0, image.Width/4, image.Height/4);
imagePosition = new Vector2(LocalRand.RMinMix(0, 40), LocalRand.RMinMix(0, 40));

then you can use the shared sprite batch to draw the all components thus

public override void Draw(GameTime gameTime)
sp.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise,null, SpriteScale);

sp.Draw(image, spriteFrame, null, new Color(new Vector4(1,1,1,0.02f)),1.0f,Vector2.Zero,SpriteEffects.None,1);




  1. If you have a collection of similar services all with the same typeof you could use the generic search code presented in the previous post to retrieve each of the services.

  2. This method only uses one sprite batch but still has multiple calls to the sprite batch for each component drawn.

Tuesday, 8 February 2011

Converting a class to a DrawableGameComponent

Given the original code for a sprite class the source of which is provide here was provided by Neil Gannon.

   1: class Sprite

   2: {

   3: public Sprite(Texture2D texture,Vector2 userPosition, int framecount)

   4:         {

   5:             spriteImage = texture;

   6:             position = userPosition;

   7:             numberOfFrames = framecount;

   8:             spriteHeight = spriteImage.Height;

   9:             spriteWidth = spriteImage.Width / framecount;


  11:         }

  12: public void UpdateAnimation(GameTime gametime) {}

  13: public void Draw(SpriteBatch spriteBatch) {}


  15: }


We need to sub class the class off DrawableGameComponent and then add an override for update and draw. These must be public overrides to match the method declarations in the the DrawableGameComponent Class. The game component needs it’s own sprite batch to draw to the graphics card and hence it needs to see the Games Graphics Device. But thankfully this is exposed in the component model for the game class. The Class becomes

   1: class Sprite : DrawableGameComponent

   2:     {

   3:         //sprite texture and position

   4:         Texture2D spriteImage;

   5:         Vector2 position;

   6:         SpriteBatch spriteBatch;

   7:         //the number of frames in the sprite sheet

   8:         //the current fram in the animation

   9:         //the time between frames

  10:         int numberOfFrames = 0;

  11:         int currentFrame = 0;

  12:         int mililsecindsBetweenFrames = 100;


  14:         //the width and height of our texture

  15:         int spriteWidth = 0;

  16:         int spriteHeight = 0;


  18:         //the source of our image within the sprite sheet to draw

  19:         Rectangle sourceRectangle;


  21:         float timer = 0f;


  23:         public Sprite(Game G, Texture2D texture,Vector2 userPosition, int framecount) :base(G)

  24:         {

  25:             spriteBatch = new SpriteBatch(G.GraphicsDevice);

  26:             spriteImage = texture;

  27:             position = userPosition;

  28:             numberOfFrames = framecount;

  29:             spriteHeight = spriteImage.Height;

  30:             spriteWidth = spriteImage.Width / framecount;


  32:         }


  34:         public override void Update(GameTime gametime)

  35:         {

  36:             timer += (float)gametime.ElapsedGameTime.Milliseconds;


  38:             //if the timer is greater then the time between frames, then animate

  39:                     if (timer > mililsecindsBetweenFrames)

  40:                     {

  41:                         //moce to the next frame

  42:                         currentFrame++;


  44:                         //if we have exceed the number of frames

  45:                         if (currentFrame > numberOfFrames – 1)

  46:                         {

  47:                             currentFrame = 0;

  48:                         }

  49:                         //reset our timer

  50:                         timer = 0f;

  51:                     }

  52:             //set the source to be the current frame in our animation

  53:                     sourceRectangle = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight);

  54:                     base.Update(gametime);

  55:             }




  59:         public override void Draw(GameTime gameTime)

  60:         {

  61:             //draw the sprite , specify the postion and source for the image withtin the sprite sheet

  62:             spriteBatch.Begin(SpriteBlendMode.AlphaBlend);

  63:             spriteBatch.Draw(spriteImage, position,sourceRectangle, Color.White);

  64:             spriteBatch.End();

  65:             base.Draw(gameTime);

  66:         }

  67: }


  1. The constructor has to have a reference to the game that creates it and must pass it on to the Super class.

  2. We can use this reference in the constructor to access the Graphics device reference to create the sprite batch. We can alternatively access the publicly exposed Game property Game.GraphicsDevice and indeed you can reference the public exposed properties of the Game class from anywhere in the game components.

  3. You must remember to add the created sprites as components in the Game class when we create the sprite

   1: rollingImage = Content.Load<Texture2D>(@"Images\rolling");

   2: rolling = new Sprite(this,rollingImage, new Vector2(500, 50), 4);

   3: this.Components.Add(rolling);

Now we can subclass the Sprite Class and create two different types of Sprite game components. The player Sprite class will have an update method that will override the sprite update and deal with the input from the input device and the CGC Sprite Class will move around the screen.

We’ll look at those classes next time.


It’s worth looking at the forum discussion here about the ins and outs of using multiple sprite batches or a single sprite batch for drawing components. So it would seem from the comments made by Shaun Hargreaves in the forum discussion referenced above that although components give good modularity and structure to games, but at the cost of efficiency in committing to the graphics device as multiple sprite batches in components seem not to be a good idea from a performance perspective!! So for multiple instances a Drawable game component may not be a good idea?? So back to the drawing  board eh? It would be a good idea to change the XNA framework so that it batches sprite batch (yeah I know – a bit of a recursive definition) and have a common end() method for them. The solution suggested is to have a Sprite Manager. But I thought that’s what a sprite batch should do!!

At that stage we might consider making the Sprite Class abstract as we would probably not want to create instances of it after that subclass action.

Thursday, 3 February 2011

XNA GameEngine Development Series « Running on Empty

Not for the Faint Hearted XNA GameEngine Development Series « Running on Empty but a good source for ideas.