This blog is no longer updated. Feel free to copy any useful information to other blogs or wikis as this site may not exist for much longer. Thanks.


Sprite Sheet Packer Tool – XNA GS Example

October 7th, 2009 | 6 comments

Since I designed the Sprite Sheet Packer to be as agnostic as possible in regards to what you might use the output for, some people may not know how to read that file in for use in their XNA GS game. So here’s a quick run down.

First, add both the outputted PNG and TXT file to your content project. Right click on the TXT file and choose the Properties option. Find the “Build Action” property and set that to None and the “Copy to Output Directory” property to “Copy if Newer”. This will copy the text file to your outputted folder without trying to build it as content.

Next, we read in the file. Here’s a snippet from the downloadable sample that shows how that can be achieved:

Dictionary<string, Rectangle> spriteSourceRectangles =
   new Dictionary<string, Rectangle>();

// open a StreamReader to read the index
string path = Path.Combine(
   StorageContainer.TitleLocation,
   "Content\\Sprites.txt");
using (StreamReader reader = new StreamReader(path))
{
   // while we're not done reading...
   while (!reader.EndOfStream)
   {
      // get a line
      string line = reader.ReadLine();

      // split at the equals sign
      string[] sides = line.Split('=');

      // trim the right side and split based on spaces
      string[] rectParts = sides[1].Trim().Split(' ');

      // create a rectangle from those parts
      Rectangle r = new Rectangle(
         int.Parse(rectParts[0]),
         int.Parse(rectParts[1]),
         int.Parse(rectParts[2]),
         int.Parse(rectParts[3]));

      // add the name and rectangle to the dictionary
      spriteSourceRectangles.Add(sides[0].Trim(), r);
   }
}

Pretty simple, right? Then you can just use the name of a sprite to get the source rectangle and draw that sprite. To see it in action, download the sample and give it a go.


Possibly Related Posts

(Automatically Generated)
Sprite Sheet Packer Tool
Sprite Sheet Packer Tool Released!
sspack your images
Editing The Default XNA Game Studio Project Templates
Pong in F# with XNA Game Studio

  1. October 22nd, 2009 at 23:15
    Quote | #1

    I used your above sample along with your latest version of your Sprite Sheet Packer and I think your Sprite Sheet Packer isn’t packing the sprite sheet properly. I uploaded this zip file so you can see for yourself and maybe identify the problem easier. My zip file includes all 30 images for my stickman animation that I’m using, along with the output.jpg (the outputted spritesheet that contains errors). I left the output.txt from your Sprite Sheet packer as well.

    I used media fire free online file sharing service to upload the file, here it is, http://www.mediafire.com/?ymymvjjrmgm

  2. October 22nd, 2009 at 23:15
    Quote | #2

    I used your above sample along with your latest version of your Sprite Sheet Packer and I think your Sprite Sheet Packer isn’t packing the sprite sheet properly. I uploaded this zip file so you can see for yourself and maybe identify the problem easier. My zip file includes all 30 images for my stickman animation that I’m using, along with the output.jpg (the outputted spritesheet that contains errors). I left the output.txt from your Sprite Sheet packer as well.

    I used media fire free online file sharing service to upload the file, here it is, http://www.mediafire.com/?ymymvjjrmgm

  3. October 22nd, 2009 at 23:17
    Quote | #3

    I forgot to mention, you probably have to load up the spritesheet in your sample and press the up arrow key many times real fast to see that the stickman isn’t in the proper order. Would be nice if your spritesheet packer tool had a preview that we can click the play button to watch the spritesheet in action being played forward. A looping option would also be cool, but if it’s asking too much, I understand.

  4. October 22nd, 2009 at 23:17
    Quote | #4

    I forgot to mention, you probably have to load up the spritesheet in your sample and press the up arrow key many times real fast to see that the stickman isn’t in the proper order. Would be nice if your spritesheet packer tool had a preview that we can click the play button to watch the spritesheet in action being played forward. A looping option would also be cool, but if it’s asking too much, I understand.

  5. October 22nd, 2009 at 23:23
    Quote | #5

    After looking at the output.txt file I noticed that it just needs to be sorted into the right order I think hehe

  6. October 22nd, 2009 at 23:23
    Quote | #6

    After looking at the output.txt file I noticed that it just needs to be sorted into the right order I think hehe

Comments are closed.