Map based games
BlitzMax Forums/BlitzMax Beginners Area/Map based games
| ||
Hi, I am kind of think in out loud and wondering what is best, or even normal prodedure with building map/tile based games. There is only so much each frame cycle can do before it has an effect on how the game runs, so I am looking for advice on how to keep the map drawing at optimal performance to allow for other events within the same cycle. In the main loop (if the map is bigger than the visible screen), is it best to: a. iterate through the tileset/map (see pseudo) and draw the whole map each frame (also in the loop, checking each tile for player/tile collisions) b. iterate through the tileset/map (see pseudo, would need modifying) and draw only what is viewable (again, checking each tile for player/tile collisions) c. Before the main loop, draw the map, copy it to an image or something, and then in the main loop draw that image (checking collisions based on a color mask?) I am favouring "b", but as game development is new to me, I am wondering if this the correct way or what other approaches there are. 'Pseudo (very rough and straight from my head): Type TMap Field x:Int Field y:Int ... Function DrawMap(sLevel) RestoreData sLevel For Local iCols:Int = 0 to 6 For Local iRows:Int = 0 to 3 Read Local iTile:Int DrawImage( TImage, iCols * 32, iRows, iTile) ' TImage is pointing to imagearray (LoadAnimImage...) Next Next End Function End Type #level_1 DefData 1, 2, 2, 2, 2, 2, 3 ... DefData 1, 2, 2, 2, 2, 2, 3 #level_2 DefData 1, 2, 2, 2, 2, 2, 3 ... DefData 1, 2, 2, 2, 2, 2, 3 Many thanks, |
| ||
I am working on a scrolling tilemap demo. generally, only draw what is visible to the user! and favor arrays over lists |
| ||
always go for b |
| ||
and only check the tiles in the vicinity of the player for collision |
| ||
Type tmapdata Field mapw:Int Field maph:Int Field data:Int[32,32] End Type Type tmapviewer Field camerax:Int Field cameray:Int Field vieww:Int=16 Field viewh:Int=12 Field map:tmapdata Method drawmap() Local x:Int Local y:Int Local v:Int Local s$ For y=0 To viewh-1 s="" For x=0 To vieww-1 v=map.data[ x +camerax , y +cameray ] s:+RSet(v,3)+" " Next DebugLog s Next End Method Method movecamerato(x:Int,y:Int) If x<0 x=0 If y<0 y=0 If (x+vieww)>map.mapw x=map.mapw-vieww If (y+viewh)>map.maph y=map.maph-viewh camerax=x cameray=y End Method End Type Local mymap:tmapdata=New tmapdata mymap.mapw=32 mymap.maph=32 ' fill 'r up For Local y:Int=0 To 31 For Local x:Int=0 To 31 mymap.data[x,y]=Rnd(255) Next Next For y=9 To 11 For x=9 To 11 mymap.data[x,y]=0 Next Next mymap.data[31,31]=0 Local myviewer:tmapviewer=New tmapviewer myviewer.map=mymap ' attach our map to the viewer myviewer.drawmap ; DebugLog "---------------" myviewer.movecamerato 4,5 myviewer.drawmap ; DebugLog "---------------" myviewer.movecamerato 34,35 myviewer.drawmap ; DebugLog "---------------" Notify 0 End A thingy I was experimenting with lately was to not include the actual drawing in the mapviewer. Instead I'd send out an event that commands the gfx-object (holding the image with tiles usually) to draw itself on a canvas opened/set by the mapviewer. This way the mapviewer can draw any type o' map.. but that's perhaps all too advanced for now orso :P |
| ||
CS_TBL I think I follow you, although you are correct.. a little advanced for now, but definately worth following up. Thanks :) Everyone else, many thanks! It confirms I am on the right path. |