Sprite Sheet
As described earlier, a sprite is a single image that is used (and reused) to represent an individual element onscreen, such as the player’s character, an enemy, and so on. As you can imagine, having many individual sprites within a game and having a separate file foreach sprite, which is then loaded into the game could be difficult to manage.To simplify sprite management and, more importantly, improve performance, it is common to store more than one sprite in a single image. Images that hold more than a single sprite are called sprite sheets.
The idea is that a single image contains a series of sprite images in the form of a grid. Each sprite can then accessed by using its column and row location within the larger im-age. Simple sprite sheets contain individual sprites, each with the same dimensions.This makes it easy to access a specific sprite on that sheet using its row and column location. For example, if we had a sprite sheet that contained sprites whose dimensions were all 40 pixels square, it would take a simple calculation to work out in pixels where a given spritewas using its row and column; for example:
x = column * 40
y = row * 40
More complex sprite sheets, or packed sprite sheets (like the one shown in Figure 2.3), are images that contain a number of smaller sub-images, each of which could be another sprite sheet or image.As you’ll notice by looking at Figure 2.3, a packed sprite sheet can contain a number of different sized sprites.
Figure 2.3
These more complex sprite sheets mean you cannot perform a fixed calculation as seen already to work out where inside the sprite sheet a specific sprite may be. Instead, youneed to include acontrol file, which does two things:
1. It defines the location of each sprite within the sprite sheet.
2. It defines a key for each sprite in the sprite sheet (for example, the name of the sprite).
Control files can be either plain text or XML, and are often created when the sprite sheet is created.The data contained in the control sheet can be populated manually (that is, you sort out the coordinates for each sprite in the sprite sheet), or using a tool such as Zwoptex3, which can generate the sprite sheet and control file from separate images.When your game launches, the control file is parsed so the game knows where the sprites are located within the sprite sheet.
texture atlas(纹理地图集)
Creating a sprite sheet is basically the same as creating a sprite.You can use your favorite graphics package to place individual sprites into a larger image. Care must be taken to ensure that each sprite occupies the correct location within the sprite sheet. Because this can be a complex exercise, using tools to help with their creation is a recommended approach.