Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
89 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

  Contents

 Introduction
 What the Heck
 is a Tile?

 Drawing the Tiles
 Conclusion

 Printable version

 


  The Series

 Part 1
 Part 2

 

Drawing the Tiles

Finally, we can now create a function to turn that little char array filled with 1’s and 2’s into a graphical, DirectX display. Make sure you have DirectX already set up and running. I will go through each piece of code "NeHe style," since he has inspired me to write a tutorial and because his tutorials are always the easiest to understand.

#define TILE_SIZE 32 #define WORLD_SIZEX 12 #define WORLD_SIZEY 12

Here we defined the tile size as 32*32 pixels, and the world size with 12 tiles high and 12 tiles wide.

void draw_tiles(void) { int tile; int x; int y; RECT tile_src;

tile is going to be used later in determining if the tile map[y][x], for example, has an ID# of 1 or 2 when we go through the loop of the map array. x and y are the two placeholder variables used when looping through the array and drawing the tile at its appropriate location. The RECT tile_src is going to specify which graphic is going to be used at each tile location, pointing at the offscreen surface where the tile graphic is kept.

for (y = 0; y < WORLD_SIZEY; y++) { for (x = 0; x < WORLD_SIZEX; x++) { tile = map[y][x]; // tile now stores the ID of this particular tile

In this piece of code, two "for" loops are established that go through our map array and capture the ID# for each tile, which is stored in tile.

Note: Our function goes through each tile one by one and blits each tile one by one. It is because of page flipping that it seems as if all of them are being blitted at the same time.

tile_src.left = (tile – 1) * TILE_SIZE; tile_src.top = 0; tile_src.right = tile * TILE_SIZE; tiel_src.bottom = TILE_SIZE;

The tile_src RECT is now set up, depending on the tile ID stored in tile for the tile at map[y][x];

BltFast(x * TILE_SIZE, y * TILE_SIZE, lpddsoffscreen, &tile_src, NULL); } } }

Now we blit the tile at the correct screen coordinates, which would be the x and y variable positions * 32 pixels. I used lpddsoffscreen as the name of the DirectX surface where my bitmap is loaded to. Yours might be loaded to a differently named surface.




Next : Conclusion