2d游戏引擎:构建一个2D地图引擎



尽管事实上建立个地图引擎是件再简单不过事,但是我还是经常收到来自区别人们各种邮件,询问我应该如何样去做,或者是我原来有关建立个地图引擎是简单不过事情想法是吧,也可能是这些向我求问人本身很懒吧(你说呢?).似乎大家都对这个主题比较感兴趣,这章我将详尽地讲解如何建立个高效2D地图引擎.

瓷砖是游戏编程中专用术语,用来产生2D游戏中场景,每个瓷砖都对应种区别地表结构;比如,海洋,草地,岩石,沙滩.虽然在3D游戏编程中使用了更为先进和生动场景生成技术,可是运用瓷砖还是能在2D游戏中生成个较生动和有趣场景.相比起来,2D中这种技术更为容易掌握和应用(而和3D游戏产生效果是),并且运行得更为快捷.

其实在所有工作中,最主要考虑原因还是瓷砖大小.如果瓷砖太大,用它来表现地表可能就不细致,反的如果太小,游戏速度将因此而拖慢(同样大小地面,瓷砖小将使用过多数量瓷砖),并且考虑到降低问题本身复杂性方面,用这些瓷砖组合起来地图大小要正好是屏幕倍数.比如,X数量瓷砖总宽度刚好和y个屏幕宽度相等.下面提供了张简单对应表:

屏幕大小 瓷砖设置方式                                  实际要绘制总瓷砖数         备注

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 tiles high =    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

注意到在某些解决思路方法中有些瓷砖组合并不和屏幕大小真正切合;这表明你将需要些额外像素来填充这些地图(瓷砖组合)边缘空出来屏幕部分.我在上面表中已经用"Red"注明这些需要避免出现空白解决思路方法(和接下来"Green,Yellow"组成"备注"项),用"Green"注明了那些合适解决思路方法,用"Yellow"注明了那些需要在必要时候才应该使用解决思路方法.

如果你不打算用上面这些解决思路方法中任何种或者在你使用DX窗口模式下情况下,上面这张表对你是没有意义;但是大体上这些解决思路方法和其它解决思路方法都是(都按同个公式来计算),都遵循个通用原则:
只有那些瓷砖数在1000-5000的间解决思路方法都是可选,多于这个数目瓷砖数将会使要形成地图引擎运行速度打折扣.

实际组合瓷砖形成地图思路方法很简单;这里有介绍 2种主要用到思路方法:

运行时,组合瓷砖生成地图内容到个单独绘图页上-因此这种思路方法只涉及到个生成过程.如果采用这种思路方法,以上提供表项将完全无可用.并且,采用这种思路方法可能会对接下来Z轴转换和遮盖处理带来困难.

运行时,在个循环里生成地图.这会在很大上降低帧播放速率(fps,frame per second),在快机器上会更明显.但是在些优化工作后可以达到个合理播放速率.这种思路方法允许你轻松地生成动画,进行混合处理,透明处理.



我们在接下来中将使用个简单循环来组合生成地图;以下实现组合生成地图过程可以说是在当前运行时完成,也可以说是在文章最后面链接那个中进行:

Sub RenderMap(NumTilesX as eger,NumTilesY as eger, TileWidth as eger,TileHeight as eger _
_ TileSourcesurf as DirectDrawSurface7, TileDestSurf as DirectDrawSurface7)

Dim X as eger, Y as eger, r as RECT, retVal as long

For X = 0 to NumTilesX
For Y = 0 to NumTilesY
'Create our Rectangle.
r.Left = 'Left coordinate for Tile on source surface
r.Top = 'Top coordinate for Tile on Destination surface
r.Right = r.Left + TileWidth
r.Bottom = r.Top + TileHeight
'This is where we copy the tile from the source to the destination
retVal = TileDestSurf.BltFast((X * TileWidth), (Y * TileHeight), TileSourceSurf, r, DDBLTFAST_WAIT)
Next Y
Next X

End Sub 
 
以上将完成个生成地图过程.我们可以对它作些改变以加快地图在每个循环里被生成速度.

Remove the Multiplications. There are 6 dferent maths functions in the above code. This could possibly be cut down. Using Multiplication and/or division is quite costly on a loop basis. The easiest way that this can be done is by using a lookup table. On the first loop we calculate ALL the coordinates and store them in an .gif' />, then on subsequent loops we just look at this .gif' /> and the calculation will be done; this is known as a lookup table. 
移除乘法运算(而改用加法或使用其它办法).以上用到了6个区别数学.实际上这可以被精简.使用乘法运算和逻辑运算将在个循环主体里花费不少系统占用.最直观和简单思路方法是使用个对照表.
Only Draw  necessary. This is the simplest way of speeding things up. The Fruitworld game does this, all the tiles are represented by a number in an .gif' />,  this .gif' /> has changed since the last loop it will draw it, otherwise it moves onto the next iteration. This can easily be combined with the lookup table suggested above. 
只在需要绘制时候进行实际绘制.这是最让人信服提速思路方法了.游戏"水果世界"源代码使用了这个思路方法,所有瓷砖都对应个索引值被存放在里以供,
The other aspect of using tiles is actually drawing them. The most important thing is that they join together perfectly - and you cant see the joins between them, this is called a seamless pattern, and you can use PaShop Pro to generate one for you. The next aspect is what they look like; although detail is important, there is a fine line. Assuming that you are drawing generic landscape (Grass, Sand, Water) - you dont want the user's eye to be focused on it; the grass, water and sand are the background, not the foreground.
使用瓷砖另外个方面是实际绘制它们.最重要事是要保证它们的间是无缝拼合,你至少不能看到明显缝隙,这就是人们常说"无缝拼接"
Tags:  2d引擎 2d地图编辑器 2d地图 2d游戏引擎

延伸阅读

最新评论

发表评论