In the last section, you might have noticed that we displayed an image using the SPImage object, but used an SPTexture object to set the button's image. What is the difference between an image and a texture?
A texture is just the data that describes an image — like the file that is saved on your digital camera. You can't show anybody that file alone. It's all zeros and ones, after all. You need some device that can display the image, like your iPod.
In Sparrow, that device is SPImage. SPImage has another constructor that makes it more clear what happens.
// create an image from a texture: SPTexture *texture = [SPTexture textureWithContentsOfFile: @"face.png"]; SPImage *image = [SPImage imageWithTexture:texture]; // or do the same in one line: SPImage *image = [SPImage imageWithContentsOfFile:@"face.png"];
Every texture you create takes up precious memory. So, if you need to display something multiple times simultaneously, load the texture only once, and use different SPImage objects to display them.
Even if all the previous samples created textures directly from PNG files, a real application should not do that. Here's why.
By using a texture atlas, you avoid both texture switches and the power-of-two limitation. All textures are within one big “super-texture”, and Sparrow takes care that the correct part of this texture is displayed. (Some other frameworks call this feature “Sprite Sheets”.)
To find out more about the advantages of texture atlases, I recommend you have look at this great (and funny!) video created by Andreas Löw, the author of TexturePacker.
In Sparrow, a texture atlas is very easy to use, so you should definitely take advantage of that.
The positions of each sub-texture are defined in an XML file like this one:
<TextureAtlas imagePath="atlas.png"> <SubTexture name="moon" x="0" y="0" width="30" height="30"/>; <SubTexture name="jupiter" x="30" y="0" width="65" height="78"/>; ... </TextureAtlas>;
You don't have to create a texture atlas manually. You can use the following tools to create a texture atlas:
sparrow/util/atlas_generator
. The README
file in the same folder shows you how to install and use it.So, now you have a texture atlas. But how do you use it?
// load the atlas SPTextureAtlas *atlas = [SPTextureAtlas atlasWithContentsOfFile:@"atlas.xml"]; // display a sub-texture SPTexture *jupiterTexture = [atlas textureByName:@"jupiter"]; SPImage *jupiterImage = [SPImage imageWithTexture:jupiterTexture];
Create the atlas only once when the game is initialized, and reference it throughout your game.
It's as simple as that!
In the samples above, we've always used PNG files for our textures. Sparrow supports more file formats, though, and each has specific advantages and disadvantages. Describing all of that would go beyond the scope of this manual, though.
Once you've mastered the basics of Sparrow, however, you should definitely have a look at this tutorial: Texture Formats, Best Practices. It teaches you all you need to know about the file formats of textures.
Next Section: Event Handling