Game Development Reference
In-Depth Information
Click here to view code image
// This assumes we have an 2D array [row][column]
of all the segments
for int i = 0, i < vCount , i++
// Is this the correct row?
if ( camera.y - segments [ i ][0]. y ) < screenHeight
for int j = 0, j < hCount , j ++
// Is this the correct column?
if ( camera.x - segments [ i ][ j ]. x ) <
screenWidth
// This is the top left visible segment
end
loop
end
loop
Tile Maps
Suppose you are creating a 2D top-down action roleplaying game in the vein of
The Legend of Zelda . There are massive outdoor areas and dungeons, so you need
some way to draw these levels. A naïve approach might be to simply create a
uniquetexturetorepresenteverysinglescene.Butthismightduplicateagreatdeal
of content because the same tree or rock might appear several times in one scene.
And to make matters worse, those trees or rocks may show up in several differ-
ent scenes, as well. This means using a unique texture for every scene may end up
wasting a great deal of memory.
Tile maps solvethisproblem bypartitioning theworldintosquares(orotherpoly-
gons) of equal size. Each square then references the sprite that is at that location in
the grid. The reference can often just be a numeric lookup into the tile set , or set
of all the possible tiles. So if the tree is at index 0 in the tile set, every square that
has a tree can be represented by the number 0. Figure 2.9 demonstrates a sample
scene that uses tile maps.
Search WWH ::




Custom Search