iPhone XML and Texture Atlas example use

I thought it about time I gave something back to the community, after making use of all the great free scripts etc.

In my iPhone game, I make heavy use of GUISpriteUI with a texture atlas, and I first started manually coding up sprite locations within my atlas. For anyone who hasn’t done this, its a royal pain in the a**e, especially when the ordering changes within the atlas.

Thankfully, there is an excellent Texture Atlas tool called Zwoptex (http://zwoptexapp.com/) - additional info here (cocos2d-iphone.org).

This can export the locations in an Apple Property List file. This file is really just an XML file in disguise, so rename it as “xxx.xml” and you can see its contents.

Now, parsing XML is not particularly easy using strings etc., and since System.XML requires lots of resource, its a bit overkill - it might even be unavailable on iPhone Basic?

So, Java to the rescue. Namely, NanoXML (http://nanoxml.sourceforge.net/orig/). I’ve converted the Lite version to C#, which is ideal for mobile devices. I take no credit, the NanoXML guys did all the work and made it available.

I also use XML in other areas of my game, such as building dialogue options, questions and answers etc., and its so easy to use. To read in the XML file and getting a Hashtable in return, I simply do:

TextAsset asset = Resources.Load("controls-atlas-coords", typeof(TextAsset)) as TextAsset; 
if (asset == null) 
{ 
  Debug.Log("Unable to find any plist files."); 
  return;
}
else 
{
  XMLToHashtable converter = new XMLToHashtable();
  Hashtable ht = converter.convertPropertyListXMLToHashtable(asset.text);

  // the frames element contains the co-ords, storing it in my member variable
  atlasHt = (Hashtable)ht["frames"];
}

I then just get the details of the file via the filename of the image I want to use, viz:

private ArrayList getFrameValueForKey(string keyName)
{
  Hashtable ht = (Hashtable)atlasHt[keyName];
  string str = (string)ht["frame"];
    	
  // now turn this string into an array of individual values -  
  // there should be 2 2d arrays
  string formatString = "{{%i, %i}, {%i, %i}}";

  // use the TextScanner class from the wiki
  ArrayList values = TextScanner.Scan(formatString, str);
  return values;
}

and in the function I want to use it:

ArrayList frame = getFrameValueForKey("brake.png");

This then returns a 4 (hopefully!) element array consisting of the location (x,y) and size (w,h) of the image (the values are still stored as strings, so they have to be cast to ints when using them). This makes it much easier to update your sprites/texture atlas and refer to them in code.

I have attached the XML parser code and my XMLtoHashtable conversion utility for converting plist xml files. I leave any other uses up to the user.

I hope its of use to some people.

Ian.

356588–12401–$xmltohashtable_309.cs (2.36 KB)
356588–12402–$xmlparseexception_827.cs (2.53 KB)
356588–12403–$xmlelement_201.cs (94.4 KB)

Oh…Mark it.

Thx for sharing this… might come in handy for my next project for level-definition parsing…