Sprite Sheet Packer includes two ways for XNA games to consume the files:

1) Use the XML exporter which saves into the XML Intermediate Format. Add the XML file to your content project and use Content.Load<Dictionary<string, Rectangle>> to load the file as a dictionary mapping the image name to the source rectangle.

2) Use the TXT exporter and read the file in manually. Below is one way to handle reading in this file:

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);
   }
}

Last edited Jul 30, 2010 at 1:54 AM by SimReality, version 5

Comments

thejomo Mar 18, 2013 at 10:37 PM 
Thank you for such a great program! In the description above, it says there is a downloadable sample, but I wasn't able to find on on codeplex, do you have a link for me?

Thanks