Build a 2D map engine

xiaoxiao2021-03-06  15

http://216.5.163.53/directx4vb/tutorials/directx7/dd_tiles.asp

Although in fact, a map engine is just a simple matter, but I still receive a variety of emails from different people, ask how I should do it, or I originally established a map engine is simple. However, the idea of ​​things is wrong, or maybe these people who ask me itself is very lazy, you said?). Because everyone is more interested in this topic, this chapter I will explain well Establish a high effect 2D map engine.

Tiles are dedicated terms in game programming to produce scenes in 2D games, each of which corresponds to a different surface structure; such as ocean, grass, rock, beach. Although it is used in 3D game programming Advanced and vivid scene generation technology, but use tiles to generate a more vivid and interesting scene in 2D games. Compared to this technology, this technology is easier to master and applied (and generated with 3D games) The effect is the same), and it is more fast.

In fact, in all work, the most important consideration is still the size of the tile. If the tile is too large, the surface to express it may not be meticulous, and if it is too small, the game speed will be slow (because the same size is Ground, tile tendri uses excessive number of tiles), and considering how to reduce the complexity of the problem itself, the map size combined with these tiles should be just the multiple of the screen. For example, the total width of the X quantity is just right with Y The width of the screen is equal. The following provides a simple corresponding table:

Screen size tile set method actually draws total porcelain tile number notes

640x480 5x5 128 tiles wide, 96 tiles high = 12,288 tiles to be drawn Red 10x10 64 tiles wide, 48 tiles high = 3062 tiles to be drawn Green 16x16 40 tiles wide, 30 tiles high = 1200 tiles to be drawn Green 32x32 20 tiles wide , 15 tiles high = 300 tiles to be drawn Yellow 800x600 5x5 160 tiles wide, 120 tiles high = 19,200 tiles to be drawn Red 10x10 80 tiles wide, 60 tiles high = 4800 tiles to be drawn Green 16x16 50 tiles wide, 37.5 tiles high = 1850 TILES to Be Drawn Green 32x32 25 Tiles Wide, 18.75 TILESHIGH = 468 TILES to Be DRAWN YELLOW

1024x768 5x5 204.8 tiles wide, 153.6 tiles high = 31,212 tiles to be drawn Red 10x10 102.4 tiles wide, 76.8 tiles high = 7752 tiles to be drawn Red 16x16 64 tiles wide, 48 tile high = 3062 tiles to be drawn Green 32x32 32 tiles wide , 24 tiles high = 768 TILES to Be Drawn Yellow Notes notice in some solutions, the combination of tiles is not true to the screen size; this shows that you will need some extra pixels to fill the edges of these maps (combination of tiles) Space-out screen part. I have used "red" in the table above. These needs to avoid blank solutions (with the next "Green, Yellow" form a "note" item), with "Green" Note Made the appropriate solution, using "Yellow" to indicate the solution that needs to be used when necessary.

If you don't plan to use any of these solutions or in the case where you use DX's window mode, the above table is meaningless; but in general, these solutions are all in other solutions. The same (all according to the same formula), all follow a general principle: only those solutions between 1000-5000 in 1000-5000 are optional, more than this number of tiles will be formed The running speed of the map engine is discounting.

The actual combination of tile forming a map is very simple; here is the two main methods:

When the program is running, the combination tile generates the map content to a separate draw page - so this approach only involves a generation process. If this method is used, the above-mentioned items will be completely available. And, use this The method may bring difficulties to the next Z-axis conversion and cover processing.

The program is running at a loop. This will reduce the play rate (FPS, FRME PER Second) on a very large program, which will be more obvious on the fast machine. But after some optimization, you can reach a Reasonable play rate. This method allows you to easily generate animations, mixing, transparent processing.

We will use a simple loop in the next program to combine a map; the following implementation combination generates a map process can be said to be completed at the current program, or it is also the program in the last side of the article. ongoing:

Sub Rendermap (Numtilesx As INTEGER, NUMTILESY AS INTEGER, TILEWIDTH AS INTEGER, TILEHEIGHT AS INTEGER, TILESIGHT AS INTEGER __ TILESOURESURSURF As DirectDrawsurface7, TileDestsurf as DirectDrawsurface7)

DIM X AS INTEGER, Y AS INTEGER, R AS RECT, RETVAL AS LONG

For X = 0 to NumTilesXFor Y = 0 to NumTilesY'Create our Rectangle.r.Left = 'Left coordinate for Tile on source surfacer.Top =' Top coordinate for Tile on Destination surfacer.Right = r.Left TileWidthr.Bottom = r.Top TileHeight'This is where we copy the tile from the source to the destinationretVal = TileDestSurf.BltFast (int (X * tileWidth), int (Y * tileHeight), TileSourceSurf, r, DDBLTFAST_WAIT) Next YNext xEnd Sub the above function A process that generates a map will be completed. We can make some changes to it to speed up the speed of the map being generated in each cycle.

转载请注明原文地址:https://www.9cbs.com/read-46504.html

New Post(0)