Description:
This sample shows how to implement sprite sheets, combining many separate sprite images into a single larger texture that will be more efficient for the graphics card.
New For XNA Game Studio 3.1: This sample has been updated for Game Studio 3.1 to make use of the new automatic .xnb serialization mechanism. The ContentTypeWriter and ContentTypeReader helper classes used by previous versions of Game Studio are no longer required (although they still work the same way as before if they are included). If these helpers are not provided, the Content Pipeline will now automatically serialize custom data types using reflection. This sample has been simplified by removing the ContentTypeWriter and ContentTypeReader classes, as these are no longer required.
Sample Overview
Manually combining sprite images into larger sheets works well if you have just a few sprites, but it quickly becomes a burden as your game grows larger. When you have hundreds of sprites, it can be laborious having to manually pack them all into a single sprite sheet texture, and then remember where you put each image so you know what source rectangle to pass to SpriteBatch.Draw in your game code.
This sample automates the process of creating sprite sheets by using a custom content processor. You provide an XML file listing any number of individual bitmap files, one per sprite. The processor reads all these bitmaps, packs them into a single larger texture, and saves this new texture along with information recording what source rectangle should be used for each sprite. You can then look up your sprites by name rather than having to remember the specific coordinates for each image.