Game Development Reference
In-Depth Information
Bob's texture, render Bobs, bind the coin texture, render coins, and so on). We can do it more
effectively by putting multiple images into a single texture. And that's a texture atlas : a single
texture containing multiple images. We only need to bind that texture once, and we can then
render any entity types for which there is an image in the atlas. That saves some state change
overhead and increases performance. Figure 8-23 shows such a texture atlas.
Figure 8-23. A texture atlas
There are three objects in Figure 8-23 : a cannon, a cannonball, and Bob. The grid is not part
of the texture; it's only there to illustrate how we usually create texture atlases manually.
The texture atlas is 64×64 pixels in size, and each grid is 32×32 pixels. The cannon takes up
two cells, the cannonball a little less than one-quarter of a cell, and Bob a single cell. Now, if you
look back at how you defined the bounds (and graphical rectangles) of the cannon, cannonball,
and targets, you will notice that the relation of their sizes to each other is very similar to what
you have in the grid. The target is 0.5×0.5 m in your world and the cannon is 0.2×0.2 m. In our
texture atlas, Bob takes up 32×32 pixels and the cannonball a little less than 16×16 pixels. The
relationship between the texture atlas and the object sizes in our world should be clear: 32 pixels
in the atlas equals 0.5 m in our world. Now the cannon was 1×1 m in our original example,
but we can, of course, change this. According to our texture atlas, in which the cannon takes
up 64×32 pixels, we should let our cannon have a size of 1×0.5 m in our world. Wow, that is
exceptionally easy, isn't it?
So why choose 32 pixels to match 1 meter in your world? Remember that textures must have
power-of-two widths and heights. Using a power-of-two pixel unit like 32 to map to 0.5 m in your
world is a convenient way for the artist to cope with the restriction on texture sizes. It also makes
it easier to get the size relations of different objects in our world right in the pixel art.
Note that there's nothing keeping us from using more pixels per world unit. We could choose 64
pixels or 50 pixels to match 0.5 m in your world. So what's a good pixel-to-meters size, then? That
again depends on the screen resolution at which the game will run. Let's do some calculations.
Our cannon world is bounded by (0,0) in the bottom-left corner and (9.6,4.8) in the top-left
corner. This is mapped to our screen. Let's figure out how many pixels per world unit we have on
the screen of a low-end device with 480×320 pixels in landscape mode:
pixelsPerUnitX = screenWidth / worldWidth = 480 / 9.6 = 50 pixels / meter
pixelsPerUnitY = screenHeight / worldHeight = 320 / 6.4 = 50 pixels / meter
 
Search WWH ::




Custom Search