The Resource Manager

To begin drawing images, playing music, and saving user preferences, we first need to be able to load textures and sound samples, and to read and write binary files. That is the first responsibility of the Resource Manager. The second responsibility is to unload resources when they are no longer needed, to make room for others.

First we must decide what formats to use for our textures and sound samples, and how we will distribute them with our application. Next, we will have to write code to load those resources when needed, and unload them when they are not being used.

Texture Format

For the texture format, the iPhone prefers PNG files. The PNG file format provides compressed textures with decent quality as well as the option of alpha blending for transparent pixels.

As we will discuss in the next chapter, we will be using OpenGL ES as our graphics API. To use a texture with OpenGL, we first have to load the texture file, uncompress it into a buffer, and use that buffer to generate a GL_TEXTURE_2D texture. To keep all of this simple so that we can stay focused on game programming, we are going to need a class to represent OpenGL textures, which we will name GLTexture.

A number of example applications distributed by Apple during the iPhone SDK Beta are no longer available, and one of them provided a class that acted as a simple 2D texture interface for OpenGL ES. We have tracked the class down and modified it for use in our framework.

In addition to providing GLTexture ...

Get iPhone Game Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.