Dragging/moving across a massive tile based map by Drakkheim18th Oct 2010 18:35
|
---|
Summary Demonstration of how to right click and drag across a large (2000 x 2000) map by only displaying the sprites that are on the screen. Description Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com `Scolling a massive (4 million tile) map `Hold Right Mouse button and Drag to move map Set display mode 800,600,32,VSyncOn dim map(2000,2000)as integer ` create our map array cls `make some animated sprites for the display on the screen for counter = 1 to 143 create animated sprite counter, "tile1.png", 2,2,1 NEXT makemap() `populate the map array mouseoffsetX = 0 `the offset of the current drag mouseoffsety = 0 startDragX = 0 `the mouse position at the sart of this click-drag startDragY = 0 true = 1 `annoying globals false = 0 dragstart = 0 bclicking = false `a mode checker tilesize = 64 `the size of our map tiles (we're using square ones here) globalxoffset = 64000 `start at the middle of the map globalyoffset = 64000 tick = timer() fpcount = 0 draw sprites first do `start the main loop if mouseclick() = 2 if bclicking = false hide mouse bclicking = true startDragX = globalxoffset + mousex() startDragY = globalyoffset + mouseY() else `were already dragging mouseoffsetx = startdragx - mousex() mouseoffsety = startdragy - mousey() endif else if bclicking = true bclicking = false show mouse globalxoffset = mouseoffsetx globalyoffset = mouseoffsety endif ENDIF drawmap(mouseoffsetx,mouseoffsety,tilesize,tilesize) sync if (timer() < tick+1000) fpcount = fpcount +1 else tick= timer() fps = fpcount fpcount =0 ENDIF text 10,25,str$(fps) loop end `------------functions below----------------- function drawmap(offsetx,offsety,tilesizex,tilesizey) topx = 0 topy = 0 toptilex = offsetx / tilesizex toptiley = offsety / tilesizey spacex = offsetx mod tilesizex spacey = offsety mod tilesizey targetsprite = 1 for y = 0 to 11 for x = 0 to 13 `TODO: Add bounds Checking tgtx = topx+(x*tilesizex)-spacex tgty = topy+(y*tilesizey)-spacey sprite targetsprite,tgtx,tgty,1 if sprite frame(targetsprite) <> map(y+toptiley,x+toptilex) set sprite frame targetsprite,map(y+toptiley,x+toptilex) endif targetsprite = targetsprite +1 next next endfunction function makemap() for x = 0 to 2000 for y = 0 to 2000 map(y,x) = rnd(3) +1 next NEXT ENDFUNCTION |