How to load tiles(as layers) for maps

BlitzPlus Forums/BlitzPlus Programming/How to load tiles(as layers) for maps

here ... I dug-up this old (Blitz+) example :) it's prolly at this forum already, but I can't find it anymore.

No types anywhere, I hate types ..

; size of your desktop
Global sw=ClientWidth(Desktop())
Global sh=ClientHeight(Desktop())

; a fullscreen window
Global APP=CreateWindow("very basic but ample MAP tutorial :)",0,0,sw,sh,0,0)

; a 640x480 canvas
Global APPcanvas=CreateCanvas(0,0,640,480,APP)
; make the canvas 'stretchable'
SetGadgetLayout APPcanvas,1,0,1,0
; make it the size of the parent-window (fullscreen)
SetGadgetShape APPcanvas,0,0,sw,sh


; make room for 4 32x32 tiles, and a 32x32 tile for the player
Global TILES=CreateImage(128,32)
Global PLAYER=CreateImage(32,32)

; make a map (32x32 tiles)
Dim MAPgfx(32,32),MAPwalk(32,32) ; MAPwalk defines where you can walk, independent of the gfx

PrepareGFX()
PrepareMAP()

Global quit=False
Global cameraX,cameraY,playerX=5,playerY=5,grid=False,coords=False


SetBuffer CanvasBuffer(APPcanvas)

If MAPwalk(playerX+cameraX,playerY+cameraY)=1
	drawmap()
	Notify "change some factors in PrepareMAP(), or change playerX and/or playerY coords"
	quit=True
EndIf



Drawmap()


timer=CreateTimer(20) ; 20 fps

;-------------------------------------------------------------
Repeat
	WaitEvent()
	If EventID()=$803 quit=True ; alt f4=quit
	If EventID()=$4001 updategame()
Until quit

; today we are not lazy as usual, and we actually FREE the images&stuff :)
FreeImage TILES
FreeImage PLAYER
FreeGadget APP
End

;-------------------------------------------------------------

Function updategame()

	If KeyDown(200) ; up
		If MAPwalk(cameraX+playerX,cameraY+PlayerY-1)=0
			playerY=playerY-1
			If playerY<4
				cameraY=cameraY-1
				If cameraY<0
					cameraY=0
				Else
					playerY=4
				EndIf
			EndIf
		EndIf
		Drawmap()
	EndIf
	
	If KeyDown(208) ; dn
		If MAPwalk(cameraX+playerX,cameraY+PlayerY+1)=0
			playerY=playerY+1
			If playerY>10
				cameraY=cameraY+1
				If cameraY>17
					cameraY=17
				Else
					playerY=10
				EndIf
			EndIf
		EndIf
		Drawmap()
	EndIf
	
	If KeyDown(203) ; left
		If MAPwalk(cameraX+playerX-1,cameraY+PlayerY)=0
			playerX=playerX-1
			If playerX<4
				cameraX=cameraX-1
				If cameraX<0
					cameraX=0
				Else
					playerX=4
				EndIf
			EndIf
		EndIf
		Drawmap()
	EndIf
	
	If KeyDown(205) ; right
		If MAPwalk(cameraX+playerX+1,cameraY+PlayerY)=0
			playerX=playerX+1
			If playerX>15
				cameraX=cameraX+1
				If cameraX>12
					cameraX=12
				Else
					playerX=15
				EndIf
			EndIf
		EndIf
		Drawmap()
	EndIf
	
	If KeyHit(28) ; enter
		grid=1-grid ; toggle between 0 and 1
		Drawmap()
	EndIf

	If KeyHit(14) ; backspace
		coords=1-coords ; toggle between 0 and 1
		Drawmap()
	EndIf

End Function

Function Drawmap()
	Color 0,0,0
	For y=0 To 14 ; 15x32 = 480
		For x=0 To 19 ; 20 x 32 = 640
			DrawBlockRect TILES,x*32,y*32,MAPgfx(cameraX+x,cameraY+y)*32,0,32,32 ; draw a part of an image  [[tile1][tile2][etc][..]]
			If grid
				Rect x*32,y*32,33,33,False
			EndIf
		Next
	Next
	DrawImage PLAYER,playerX*32,playerY*32
	
	If coords
		Color 0,0,0
		Text 20,20,playerX
		Text 20,30,playerY
		Text 40,20,cameraX
		Text 40,30,cameraY
		Color 164,175,186
		Rect 58,15,24,32,True
		Color 0,0,0
		Text 60,20,cameraX+playerX
		Text 60,30,cameraY+playerY
	EndIf
	
	FlipCanvas APPcanvas
End Function


Function PrepareMAP()
	; prepare a map

	; nothing special to learn here, it's just a quick way to generate a map, usually you design your map in a map editor
	
	For y=0 To 31
		For x=0 To 31
			MAPgfx(x,y)=1 ; wall

			If y>0 And y<31 ; only draw inside the outer walls
				If x>0 And x<31
					MAPgfx(x,y)=0
					
					; plant trees
					treefactor=Rnd(0,10)
					If treefactor>9
						MAPgfx(x,y)=3
					EndIf
					
					; build walls
					longwallfactor=Rnd(0,90)
					If longwallfactor>89
						px=Rnd(1,30)
						py=Rnd(1,20)
						For t=0 To 6
							MAPgfx(px,py+t)=1
						Next
						px=Rnd(1,20)
						py=Rnd(1,30)
						For t=0 To 6
							MAPgfx(px+t,py)=1
						Next
					EndIf
					
					; let the elephants urinate here :)
					waterfactor=Rnd(0,50)
					If waterfactor>49
						px=Rnd(1,24)
						py=Rnd(1,24)
						For tx=0 To 3
							For ty=0 To 3
								MAPgfx(px+tx,py+ty)=2
							Next
						Next
					EndIf
				EndIf
				
			EndIf
		Next
	Next
	; prepare the walkmap: 0=walk, 1=don't walk
	For y=0 To 31
		For x=0 To 31
			Select MAPgfx(x,y)
				Case 0 ; grass
					MAPwalk(x,y)=0
				Case 1 ; wall
					MAPwalk(x,y)=1
				Case 2 ; water
					MAPwalk(x,y)=1
				Case 3 ; tree
					MAPwalk(x,y)=1
			End Select
		Next
	Next
End Function

Function PrepareGFX()
	
	; nothing special to learn here, it's just a quick way to generate gfx, usually you design your gfx in a gfx editor
	
	SetBuffer ImageBuffer(TILES)

	; grass (and grass for the tree)
	For x=0 To 31
		For y=0 To 31
			Color Rnd(0,32),Rnd(160,255),Rnd(64,128)
			Plot x,y
			Plot 96+x,y
		Next
	Next
	
	; wall
	Color 192,128,80
	Rect 32,0,32,32,True
	
	Color 96,96,96
	For y=0 To 31 Step 8
		Rect 32,y,32,2,True
	Next
	For y=0 To 3
		yy= (y Mod 2)
		For x=(yy*8) To 16+(yy*8) Step 16
			Rect 32+x,y*8,2,8,True
		Next
	Next
	
	; water
	
	Color 0,0,255
	Rect 64,0,32,32,True
	For t=0 To 128
		c=Rnd(0,3)
		If c<1 Color 255,255,255
		If c>=1 Color 128,128,255
		x=Rnd(0,31)
		y=Rnd(0,31)
		Plot 64+x,y
	Next
	
	; tree
	
	Color 200,80,0
	Rect 96+14,24,4,8,True
	
	Color 0,128,0
	Oval 96+4,0,24,24,True
	
	; player  (well .. sortof :)
	SetBuffer ImageBuffer(PLAYER)
	
	Color 255,255,255
	Oval 8,8,16,16,True
	Oval 12,4,8,8,True
	Rect 12,16,2,16,True
	Rect 18,16,2,16,True
	Color 1,1,1
	Plot 14,6
	Plot 17,6
	
End Function