Tuesday, 17 May 2011

Incomplete Federation of Live accounts

I have four Windows Live accounts linked together. But a word of warning. If you want to access files in folders you must give permission on the folder to the linked live accounts. You can edit in the browser from Linked account that you have switched to when initially logged in as another account which would seem to suggest that federation across the accounts works to a certain extent. Sometimes you get an HTTP access error 403 when accessing locations belonging to another Live account that is linked but not directly logged in. This I think is due to improper federation between the Live accounts.

If you are using the office live plug in locally on your PC to download and open a Live sky drive hosted office document for editing, then the account that Office live plug in is setup for needs to have edit permission on the folder and hence the file to be downloaded and opened so all the Live accounts need to have access to folders owned by the other in order for navigation to run smoothly.

Also worth a NOTE: Folder access is inherited from the parent, so if you give access to the account at the top most level then it will be granted for sub folders always.

Friday, 29 April 2011

Jeff Prosise's Blog : Silverlight 4's New Local File System Support and Problems with My Documents in Windows 7

While looking at the new restricted capabilities of Silverlight to deal with Files I came across this post Jeff Prosise's Blog : Silverlight 4's New Local File System Support. I downloaded the sample code an went to run it. It worked impressively in fact for the My Picture example. But I need to access folders in My Documents. So I tried that and oops it broke. The problem is that

string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

in windows 7 maps onto "C:\\Users\\name\\Documents" and the sub folders of that location have My Music, My Pictures and My Videos contained within and you cannot chain through these folders as you do not have permission to do so. So you have to skip over these thus

string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

foreach (string dir in Directory.EnumerateDirectories(path))
if (!dir.Contains("My Music") && !dir.Contains("My Pictures") && !dir.Contains("My Videos"))

Another Gotcha in Windows 7 and file permissions is that if you download a zip file from the web is that certain files will be blocked and you have to unblock each one before you can process the files programmatically.

Tuesday, 12 April 2011

A conveyor belt of sprites

This is an example of a set of wrap around (or Conveyor belt) sprites that can be used in a frogger type game. It is inspired by a problem that a student was having with the technique. It is an example of using a list to hold a list of sprites. It also demonstrates the idea of sub classing as it uses a sprite class that we developed previously. The key to the technique is that the original random spacing of conveyor sprites is maintained once they start moving and once they go off the lefthand side of the viewport you just need to reposition them at their width extent to the right of the viewport. The original random spacing is preserved. The ConveyorSprite class subclasses the Sprite Class which is a Drawable Game component and as such draws itself. The Update method is overridden to update the position of the Conveyor sprite so that it moves across the screen and if it goes off the left then it is repositioned to the right of the viewport. As the new Conveyor sprites are being added to List of Conveyor Sprites in the main game class, the start relative position of each off screen to the right is calculated.

The code can be downloaded at

Wednesday, 6 April 2011

Managing Complex Groups in Windows Live - Group Work for students

The Windows Live Group feature allows you to easily manage Group work for students. I have a group of about 70 students from 4 different courses in First year. These Groups are further split into groups for a game that they are designing and developing in Game Maker. I got them to set up groups with one person hosting the Group on their Sky Drive. I set up a new Windows Live ID to manage the group from my side. The Group owner invites the other members to the newly created group along with myself.

I manage the group by setting up Hotmail folders for each Course and then place the invites from each Game Group into these folders. 


This enables me to keep track of the groups within groups via the Email Invite which has a


Everyone in the Group has read write privileges on the folders in the group. This gives them a means to share content that they are developing for the game. It also allows me to keep an eye on the Progress of the group and comment or advice on the on going work.


There is a One Note web file that the group can use to document the sources of content and keep a group and individual schedule of tasks to be done and completed.

The group can contact me and each other using Instant messenger or by Emailing the group.


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 XNAGameChallange2011ModifiedByPPVS2010.zip 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.

Friday, 28 January 2011

XNA Development: Game Development for the masses

A really good place to start in XNA Development: Game Development for the masses. George uses clear concise examples for 2D sprite based game production. His XNA 4.0 post on Game state is quite uptodate also.

Thursday, 20 January 2011

Multiple Drawable Game Components - App Hub Forums

Good Forum Post on the use of Game Components and Game Services in @ Multiple Drawable Game Components - App Hub Forums

Microsoft abandon Live Blogs

Microsoft have sold out on their Live Blog service by moving it to WordPress and hence I'm now using blogger. If I could take my Live Blog back over to the Live Spaces I would. They have indicated that all Live Blog spaces have to migrate to word press. Unfortunately Word Press does not recognise Live. Also If you have an existing WordPress account that further complicates the transition and if you use the useful live writer on top of that again, WordPress does not recognise some of the plug-ins (such as the excellent code snipit add in). I had a number of bloggs linked to the one WordPress login as it was and this really presents problems both for Live writer and for sharing using the browser button. Added to this is the fact that Word PRess is an absolute Mare to navigate. Seriously it's all over the place and overly functional. Blogger is less functional but easier to maintain and plays nice with Live writer and it's plug ins. Bad descision MS!!

Ok in a moment of Frustration I may have over reacted. The Share Button (provided by Google, go figure), does work with WordPress. But the code snipits do not render properly in WordPress. Compare Bloggers with WordPress for the same entry.

Wednesday, 19 January 2011

XNA Game Components – How Far do they go

Ok. The idea of game components is that they allow you to take logic that would normally be in the game loop out of the game loop. This promotes a certain amount of independence in a class. As an example say we have a image that must move in a game. We’ll make it nice and simple and we’ll create an image that uses Vector2.Lerp to move to a target vector within the screen co-ordinates. Firstly we’ll look at the logic of doing this in the Game loop and then we’ll look at a class that is of type DrawableGameComponent. It’s quite strange that you can only choose a GameComponent Template when you add a new component to a game. This is an oversight by Microsoft in Visual studio.


First we need some class variables.

   1: SpriteBatch spriteBatch;

   2: Texture2D movingCircle;

   3: Vector2 movingCirclePosition;

   4: Vector2 Target;

So lets set up our Image first which is in the Project Content folder and load it into the Texture2D Class Variable and Randomise the initial position

   1: protected override void LoadContent()

   2:         {

   3:             Random r = new Random();

   4:             spriteBatch = new SpriteBatch(GraphicsDevice);

   5:             movingCircle = this.Content.Load<Texture2D>("MovingCircle");

   6:             movingCirclePosition = new Vector2(r.Next(600), r.Next(400));

   7:             Target = new Vector2(r.Next(600), r.Next(400));        }

Now we need a Randomised Vector2 for our Target. We can do this in the LoadContent or the or the initialise section.

Next its on to the update method to move things along so to speak.

   1: protected override void Update(GameTime gameTime)

   2: {

   3:     movingCirclePosition = Vector2.Lerp(movingCirclePosition, Target, 0.01f);

   4:     if (Vector2.Distance(movingCirclePosition, Target) < 0.1)

   5:     {

   6:         Random r = new Random();

   7:         movingCirclePosition = Target;

   8:         Target = new Vector2(r.Next(600), r.Next(400));

   9:     }

  10:     base.Update(gameTime);

  11: }

Then we draw the moving cricle

   1: protected override void Draw(GameTime gameTime)

   2:         {

   3:             GraphicsDevice.Clear(Color.Black);

   4:             spriteBatch.Begin();

   5:             spriteBatch.Draw(movingCircle,movingCirclePosition,Color.White);

   6:             spriteBatch.End();

   7:             base.Draw(gameTime);

   8:         }

and that’s it. One moving circle.

So lets look at the component version. Firstly a component is usually implemented as a class which is a sub-class of the component class. Once again the only component available in the new class template is a game component which does not override the Draw and LoadContent Class.

   1: public class MovingCircle : Microsoft.Xna.Framework.GameComponent

   2:     {

   3:         public MovingCircle(Game game)

   4:             : base(game)

   5:         {

   6:             // TODO: Construct any child components here

   7:         }

So we change GameComponent to DrawableGameComponent and then override the Draw and LoadContent Methods. We can then transfer the variables for moving the circle, keeping positions, drawing the circle sprite into the MovingCircle Class and the code transfers into the override methods such as the LoadContent Method shown in the code Snippet below. NOTE the LoadContent method has to have the same visibility as the DrawableGameComponent LoadContent Method. The other methods have a public visibility. The override for Draw must be public.


   1: protected override void LoadContent()

   2:         {

   3:             Random r = new Random();

   4:             spriteBatch = new SpriteBatch(Game.GraphicsDevice);

   5:             movingCircle = Game.Content.Load<Texture2D>("MovingCircle");

   6:             movingCirclePosition = new Vector2(r.Next(600), r.Next(400));

   7:             Target = new Vector2(r.Next(600), r.Next(400));

   8:         }

   9: public override void Update(GameTime gameTime)

  10:         {

  11:             // TODO: Add your update code here

  12:             movingCirclePosition = Vector2.Lerp(movingCirclePosition, Target, 0.1f);

  13:             if (Vector2.Distance(movingCirclePosition, Target) < 0.2)

  14:             {

  15:                 Random r = new Random();

  16:                 movingCirclePosition = Target;

  17:                 Target = new Vector2(r.Next(600), r.Next(400));

  18:             }

  19:             base.Update(gameTime);

  20:         }


  22:         public override void Draw(GameTime gameTime)

  23:         {

  24:             Game.GraphicsDevice.Clear(Color.Black);

  25:             spriteBatch.Begin();

  26:             spriteBatch.Draw(movingCircle, movingCirclePosition, Color.Red);

  27:             spriteBatch.End();

  28:             base.Draw(gameTime);

  29:         }

Notice that the code changes so that you use Game.GraphicsDevice isntead of the GraphicsDevice variable that was use in the LoadContent function in the Game class presiously

All that remains to do then is to wire up a MovingCircle instance to the Game Loop by adding it as a component to the game class. This is best done in the Initialise class so that the DrawableGameComponent’s LoadConentFunction is called after the DrawableGameComponent is created. The total code for the Game class now looks like this

   1: public class Game1 : Microsoft.Xna.Framework.Game

   2: {

   3:     MovingCircle myMovingCircle;

   4:     GraphicsDeviceManager graphics;

   5:     public Game1()

   6:     {

   7:         graphics = new GraphicsDeviceManager(this);

   8:         Content.RootDirectory = "Content";

   9:     }

  10:     protected override void Initialize()

  11:     {

  12:         myMovingCircle = new MovingCircle(this);

  13:         this.Components.Add(myMovingCircle);

  14:         base.Initialize();

  15:     }

  16:     protected override void LoadContent()

  17:     {         }


  19:     protected override void UnloadContent()

  20:     {

  21:     }

  22:     protected override void Update(GameTime gameTime)

  23:     { base.Update(gameTime);         }


  25:     protected override void Draw(GameTime gameTime)

  26:     {          base.Draw(gameTime);         }

  27: }

When we add the DrawableGameComponent to the Component collection of the Game and this plugs it into game loop.