Download iPhone Game Development

Transcript
needed for each level and which are not. You can simply purge resources when leaving
one level and load only the resources necessary for the next level.
To do this, we provide purge functions for each resource:
[g_ResManager purgeTextures];
[g_ResManager purgeSounds];
Simply call the purge functions to release resources when you know you don’t need
them, such as when you are changing GameStates.
Resource management can be very complex. Consider a system that
automatically releases old resources that have not been used recently to
make room for new ones. This system would be beneficial in reducing
the amount of redundant loading and unloading between state changes,
as long as there was enough memory to go around.
In fact, this idea is very close to garbage collection, of which there are
many types of implementations. However, it can be very hard to do well,
and since it is unnecessary in our case, we will avoid the complexity in
our framework.
The Render Engine
There are several APIs for drawing in the iPhone SDK, each useful for different purposes. Cocoa uses Objective-C and focuses on manipulating high-level GUI objects such
as UIImage, UIColor, and UIFont. Quartz Core Graphics uses C and is great for drawing
vector graphics such as lines, gradients, and shading, and for rendering PDF content.
However, most games require a high-performance rendering API, which is why
OpenGL ES is available.
OpenGL ES is a very low-level API focused on 3D rendering, so we are going to have
to build up some code to bridge the gap between what we want to do—draw 2D animated sprites—and what OpenGL ES can do: render textures onto 3D geometry.
2D Graphics with a 3D API
At this point, you might be asking yourself: “How are we going to use OpenGL to draw
2D sprites? I thought OpenGL was for 3D!”
Without getting into the math, 2D is a subset of 3D. That means anything you can do
in 2D you can also do in 3D. Specifically, imagine a table with a piece of paper on it.
The table and the paper are 3D. However, if you point a camera straight down at the
paper, the camera sees what looks like a 2D scene. That is essentially what you will be
doing with your 2D game. Meanwhile, when you get to the 3D example, you can use
the full power of OpenGL ES with very little additional work, since it’s already set up.
80 | Chapter 3: The Framework