# Which will used cache the bitmap index the current tile

140 5. Multi-Dimensional Arrays

1: void DrawTilemap( int p_x, int p_y )

2: {

3: int x, y, z;

4: int bx = p_x;

5: int by = p_y;

6: int index;

7: for( z = 0; z < LAYERS; z++ )

8: {

9: bx = p_x;

10: by = p_y;

11: for( y = 0; y < MAPHEIGHT; y++ )

12: {

13: for( x = 0; x < MAPWIDTH; x++ )

14: {

15: index = g_tilemap.Get( x, y, z );

16: if( index != -1 )

17: {

18: SDLBlit( g_tiles[index], g_window, bx, by ); 19: }

20: bx += TILESIZE;

21: }

22: bx = p_x;

23: by += TILESIZE;

24: }

25: }

26: }

**Team LRN**

The outermost loop, starting on line 7, loops through each layer, starting with the base layer. Every time a new layer is started, the loop resets bx and by to the original values because each layer is drawn directly on top of the previous layer.

Starting at line 15, I determine if the tile should be drawn and then draw it. In the first tilemap demonstration, I assumed that every tile will be a valid tile. However, I cannot do that this time, because many of the tiles on some of the layers might be �1, which is invalid. So on line 15, I get the index of the current tile, and if it isn’t invalid, I continue to draw it. If it is invalid, I don’t draw anything.