Game Development Reference
In-Depth Information
even use triple buffering , in which three color buffers are used instead of two.
Triple buffering can help smooth out the frame rate if there is high volatility, but
at the cost of increased input lag.
Sprites
A sprite is a 2D visual object within the game world that can be drawn using
a single image on any given frame. Typically sprites are used to represent char-
acters and other dynamic objects. For simple games, sprites might also be used
for backgrounds, though there are more efficient approaches, especially for static
backgrounds. Most 2D games will have dozens if not hundreds of sprites, and for
mobile games the sprites often make up the majority of the overall download size
of the game. Because of this, it's important to try to use sprites as efficiently as
possible.
The first decision one has to make with sprites is the image format in which they
will be stored. This will vary depending on the platform as well as memory con-
straints. APNGfilemaytakeuplessspace,buthardwaretypically cannotnatively
draw PNGs, so they must be converted when loaded by the game. A TGA file
can usually be drawn directly, but TGA file sizes are typically massive. On iOS
devices, the preferred file format is PVR, because it is a compressed image format
that can also be drawn natively by the hardware.
The process of loading an image file into memory can also vary greatly depending
on the platform and framework. For frameworks such as SDL, XNA, and cocos2d,
functionality for loading many file formats is built in. But if you are writing a
2D game from scratch, one relatively simple cross-platform library is stb_image.c
( http://nothings.org/stb_image.c ) , which can load several file formats, including
JPEG and PNG. Because it's written in portable C code, it will work in C, C++,
and Objective-C programs.
Drawing Sprites
Suppose you have a basic 2D scene with a background image and a character in
the center. The simplest approach to drawing this scene would be to first draw the
background image and then draw the character. This is much like how a painter
would paint the scene on a canvas, and because of this, the approach is known as
the painter's algorithm . In the painter's algorithm, all the sprites in a scene are
sorted from back to front (see Figure 2.3 ) . When it's time to render the scene, the
presorted scene can then be traversed in order so that it can be drawn appropri-
ately.
Search WWH ::




Custom Search