Sunday, September 17, 2006 10:15 AM
by
garykac
Separating your code and data
In the previous post, I talked about creating border art and using it in your game. One problem with the approach shown is that the description of the texture contents - the sprite locations and boundaries - is stored in the game code. This means that if you want to update the border artwork for a future version of your game, you need to make parallel code modifications and then recompile.
A nicer way of doing this is to keep the description of the texture contents in a file alongside the texture. This would allow you to change the appearance of your game without needing to recompile - useful if you're programming by yourself, and crucial if you're developing the game where someone else is helping out with the artwork.
This approach is supported in the XNAExtras class library by allowing texture and sprite information to be loaded from an XML file. A simple descriptor file is:
<?xml version="1.0" encoding="UTF-8" ?>
<texinfo>
<textures>
<texture name="whiteball" src="white ball.png" size="64x64" />
</textures>
<sprites>
<sprite name="red" tex="whiteball" pos="50,50" tint="#ff0000" />
</sprites>
</texinfo>
This XML file creates a single sprite based on the "white ball.png" texture, adds a red tint and positions the sprite at (50,50). While perhaps a bit of overkill for this simple texture, once you start having multiple sprites in the same texture (as you should), these descriptor files can be quite useful.
To load a texture definition file into your game, simply use:
sprites.LoadSprites("spriteinfo.xml");
Where sprites is your game's XSpriteManager. If you want to interact with this sprite in your code (for example, to move it around the screen), you can ask the XSpriteManager to return a reference to it using:
XSprite s = sprites.GetSprite("red") as XSprite;
You can find working examples of LoadSprites in the TextBoxDemo and XSpritesDemo projects (included with the XNAExtras download).

To assist with creating these "texture information" files, I've created an XML Schema Definition (XSD) file that you can use to perform basic validation. This XSD file (TextureInfo.xsd) is included as part of XNAExtras and is located in the "XML Schema" directory. To use it, copy the XSD file into the same directory as your XML file and replace your
<texinfo>
tag at the beginning of the file with the following mess:
<texinfo
xmlns="http://xna.microsoft.com/textureinfo"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xna.microsoft.com/textureinfo textureinfo.xsd"
>
Now if you open your XML file in a application that supports XSD (like Visual C# Express), you'll get validation squiggles. It doesn't perform a complete validation of the file, but it will catch most basic errors/typos. If you want to keep the XSD files in a separate directory, replace "textureinfo.xsd" in the "xsi:schemaLocation" attribute with a path+filename.

Beyond making your code more manageable, there's another aspect to this code/data separation that can benefit your work: Your art assets become more shareable with others. If you're thinking "Why would I want to make it easier for people to steal my art?", try to step back and think about the XNA gaming community as a whole.
While it means that others can more easily make use of your assets, it also means that you can more easily make use of the assets that others create. This sharing is crucial to developing and maintaining a thriving game development community. Yes, some people will "steal" and not give credit, but that sort of thing will happen no matter what you do (unless you don't release your game at all).
As mentioned earlier, exposing your art like this makes it much easier to be replaced/updated. While this is obviously good for you, it can also enable a modding community to build up around your game - people (even those with no programminng experience) can now take your game and make new skins without messing with the code. How cool would that be?