How to load tiles(as layers) for maps

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

Apollonius(Posted 2004) [#1]
I forgot how to load tiles and display using data 0,0,0,0,....

its using FOR but I forgot could anyone give me an example?


CS_TBL(Posted 2004) [#2]
make a map using data ..

a minimap of 4x4 is like this:

data 1,1,1,1
data 1,0,0,1
data 1,0,0,1
data 1,1,1,1

read it in a 2d array, or bank (if you know how to use a bank in a 2d array-style)

when you draw your map, the number in the data-lines (1 or 0 in this example) refers to one of these: (1) an offset in a large image containing misc tiles (walls, trees, paths etc.) or (2) a unique iamge-handle with 1 tile.

then using a 'for y for x next next' loop draw your map

but also try old pages in the beginner-area (around page 18..22) I'm sure there're already plenty of examples.


CS_TBL(Posted 2004) [#3]
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



LarsG(Posted 2004) [#4]

No types anywhere, I hate types ..



You must be nuts!! lol


CS_TBL(Posted 2004) [#5]
hmm no nuts today.. .. I just hate types, that's all :)

don't you hate something too? sprots? spinache? spam? :)


LarsG(Posted 2004) [#6]
I hate spam.. both types.. I have to agree on that..


CS_TBL(Posted 2004) [#7]
both 'types' :)


sswift(Posted 2004) [#8]
CS:
I hated types too. And you might nto believe me, but you will grow to love them. The sooner you learn how to use them effectively, the better. They make a lot of stuff much easier.


CS_TBL(Posted 2004) [#9]
I like the 'idea' of types, but I don't like the way Blitz offers them. Here's one area in which a c++ class looks cleaner to me! (prolly the only area)

I go with banks instead.., needs a little more thinking ahead, but the end-result looks cleaner than a type.


Apollonius(Posted 2004) [#10]
dunno what a bank is or what it looks like nor what it eats in winter :|


CS_TBL(Posted 2004) [#11]
a bank is just a piece of memory..

bla=CreateBank(1000)

now you have 1000 bytes of memory for you to abuse.. (to store variables/large maps etc.)

A bank is the way to make your own commands that look like normal blitz commands, instead of types that look like types.

See the help for CreateBank, PokeByte, PeekByte, PokeShort, PeekShort, PokeInt, PeekInt, PokeFloat, PeekFloat, BankSize, ResizeBank and FreeBank.

for maps, instead of storing 1 byte per maptile in a 2d array like this: dim map(100,100)

you could use a bank, like this: map=CreateBank(100*100)

in this case:
memlocation 0..99 equals x:0..99 / y:0
memlocation 100..199 equals x:0..99 / y:1
memlocation 200..299 equals x:0..99 / y:2

etc.

however, only when you use bytes.. if you use shorts (=2 bytes) then you need a bank twice this size and then:

memlocation 0..199 step 2 equals x:0..99 / y:0
memlocation 200..399 step 2 equals x:0..99 / y:1

etc.