2D Map Saving/Loading

Blitz3D Forums/Blitz3D Beginners Area/2D Map Saving/Loading

elseano(Posted 2007) [#1]
Hi, haven't been using blitz for a while and I just started again. I'm trying to create a tile-based map system for a platformer type game, and it's not really working. Can anyone help?

Thanks in advance, here's the code:

Global tnumber=0
Global maprx
Global mapry
Global maprmode

Graphics 640,480,32,1

SetBuffer BackBuffer()

ClsColor 134,156,175

;load tileset
tiles1=LoadAnimImage("C:/a/Co/Tiles/_A_TilesTest.bmp",32,32,0,16)
MaskImage tiles1,255,0,255

;;;;;;;;;;;;;

While Not KeyHit(1)

	Cls
	
	If KeyHit(200) And tnumber<15 Then tnumber=tnumber+1
	If KeyHit(208) And tnumber>0 Then tnumber=tnumber-1
	
	If KeyHit(57) Then
	
		SaveMap()
		Text 10,290,"Save"
		
	EndIf
	
	If KeyDown(51) Then
	
		LoadMap()
		Text 10,290,"Load"
		
	EndIf 
	
	Text 10,180,maprx
	Text 10,200,mapry
	Text 10,220,maprmode
	
	Color 45,200,75
	Rect MouseX(),MouseY(),32,32,0
	
	Text 10,10,MouseX()/32
	Text 10,30,MouseY()/32
	
	
	If MouseDown(1) Then
	
		hello.tile = New tile
		hello\xnum = MouseX()/32
		hello\ynum = MouseY()/32
		hello\mode = tnumber
		
	EndIf
	
	For hello.tile = Each tile
	
	
		DrawImage tiles1,hello\xnum*32,hello\ynum*32,hello\mode
				
		If ImageRectOverlap(tiles1,hello\xnum*32,hello\ynum*32,MouseX(),MouseY(),24,24) And MouseDown(2) Then
		
			Delete hello
			
		EndIf
	
	Next

	Flip
	
Wend

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Type tile

;position within grid
Field xnum
Field ynum

;type of tile: ground, water or danger
Field mode

End Type

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Function SaveMap()

	For hello.tile = Each tile
	
		mapout=WriteFile("C:\a\mapdata.txt")
	
		WriteInt(mapout,hello\xnum)
		WriteInt(mapout,hello\ynum)
		WriteInt(mapout,hello\mode)

	Next

End Function

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Function LoadMap()

	mapin=ReadFile("C:\a\mapdata.txt")
	
		maprx=ReadInt(mapin)
		mapry=ReadInt(mapin)
		maprmode=ReadInt(mapin)
		
		CloseFile(mapin)

End Function



Stevie G(Posted 2007) [#2]
Something like this ... sorry not tested - just typed into the post ..


Function SaveMap()

  Count = 0
  for t.tile = each tile
    Count = Count + 1
  next

  mapout = writefile( "Blah" )
  writeint( mapout, count )
  for t.tile = each tile
    writeint( mapout, t\xnum)
    writeint( mapout , t\ynum)
    writeint( mapout, t\mode )
  next

  closefile mapout



function LoadMap()

  mapin = readfile( "Blah" )
  count = readint( mapin )
  for l = 1 to count
      t.tile = new tile
      t\xnum = readint( mapin)
      t\ynum = readint( mapin)
      t\mode = readint( mapin)
  next

  closefile mapin

end function




elseano(Posted 2007) [#3]
Ahh, that makes so much sense :)

Thanks