Drawing the TilesFinally, 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.
Here we defined the tile size as 32*32 pixels, and the world size with 12 tiles high and 12 tiles wide.
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.
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.
The tile_src RECT is now set up, depending on the tile ID stored in tile for the tile at map[y][x];
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. |
||||||||||||||||||||