Porcelated tiles in DirectX: Part 1
Author: Martin Estevao (lpsoftware) Translation: Codehunter
prerequisites
There is a good foundation for C / C and DirectX knowledge. I hope readers should be able to get something through this guide. I assume that the reader can use DirectDraw and load the image file to a remote surface surface. In this article, I no longer describe how to load the position.
Throughout the entire article, I will use Windows / DirectX technology, but if you use DOS or other platforms, don't worry, all demonstration methods are easy to transplant :)
Note: All my code cannot be absolutely no bug, and my technology may not be the most effective, but I will do my best. Enjoy it together!
What is the tile?
A tile is a small picture, which is a small block bitmap that can be reset. It has a copy of a small piece of different appearance to a surface to form your game world. Compared with the single bitmap, "Tile" saves a lot of memory by creating a virtual bitmap. Now, ordinary tiles are 32 * 32 or 16 * 16 pixel size, while the map ranges from several tiles to hundreds of tiles.
At the initialization or setting of the game, we can load the map from the ASCII text file, which is very similar to that, "1" is a special tile, "2" position is another tile, and so on. "" "" "",,,,,,,,,,
The map can also be defined as a character array, but this is not the first choice. Here, I will use this format, although I strongly recommends loading a map from the isolated I / O file.
Many developers like to redefine his tiles with classes or structures, and add some tags to specify different "sports" properties, extended items are used to place special tiles such as movable tiles and multiple tiles, but for Let us only post the tile to the screen, and there is a little overhanging using tiles.
We will define our tile combination as shown below, a 12 * 12 tile map placed in a 2-dimensional character array. Char Map [12] [12] = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}, {2, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 2}, {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2}, {2, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2}, {2, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1, 2}, {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2}, {2, 1, 1, 1 , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2}, {2, 1, 1 , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2), {2, 2 , 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}};
In this format, we can access each tile with MAP [X_Tile] [Y_TYLE]. In this case, a tile of a value of "2" is a wall, or a solid tile, the character in the game can't cross. The ID # tile "1" represents the region where you can active. We can define the type of tile, as long as the value of MAP [X_TILE] [Y_TILE] is as follows: map [x_tile] [y_tile] = new_id; Next We want to create a bitmap, it stores an image of each tile . You can also create your own tiles, or to penetrate from others' RPG :) It is like this. (Picture slightly)
Note: In the function of drawing tiles, this format is only working because RECT is set. If you prefer to use other ways to set up the tiles in your bitmap, you must set up RECT
Finally, we can create a function to turn "1" and "2" in the character array into images and display it with DirectX. You must be sure that you have set up DirectX and can run. I will use every code "Nehe Style" because he gives me the inspiration of my guide, and his guide is always so easy to understand.
#define tile_size 32 # Define World_sizex 12 # Define World_sizey 12
Here I defined 32 * 32 pixel tiles, the size of the map was 12 * 12 tiles.
Void Draw_tiles (void) {Int Tile; INT X; INT Y;
Rect tile_src;
Tile will be used later, used to determine the value of MAP [Y] [x], give an example, when we run to the map by this loop to the map array, MAP [Y] [x] has an ID # value of 1 or 2 Put Tile.x, y is the two variables to traverse the entire array and draw a tile in the appropriate position. The Rect Tile_SRC will be used to specify the position of each tile, pointing to the position of the tile image will be drawn away from the slave page.
For (y = 0; y This code uses two "for" loops to traverse the map arbitrarily and captures the ID # of each tile and stores in the TILE variable. Note: Our function takes a picking with each tile and a picking one painting to the surface, because it seems to be painted simultaneously on the page. TILE_SRC.LEFT = (TILE? 1) * TILE_SIZE; TILE_SRC.TOP = 0; TILE_SRC.Right = Tile * Tile_Size; TIEL_SRC.BOTTOM = Tile_Size The setting of Tile_SRC RECT depends on the ID value of the tile stored in the TILE. Bltfast (x * tile_size, y * tile_size, lpddsoffscreen, & tile_src, null);}}} Now let's draw the tiles to the corresponding position of the screen, this is used to use x, y variables by 32 pixels. I use lpddsoffscreen as the name of the surface, you can use a different name. in conclusion This is my first article published in GameDev.Net, I plan to write more articles in the future. If the article helps anyone in DireceX or a porcelated tile, I will feel very happy. If there is any mistake in the article, if you like me hate I, please send me a mail. If this article is helpful, you have created a game, please tell me. Next article The next article will be based on this article and include techniques for rolling large maps, techniques, crash testing and animated tiles.