Reading tilemap data

BlitzMax Forums/BlitzMax Beginners Area/Reading tilemap data

Just knocked up this code which should help you. :)

SuperStrict

Graphics 800, 600

Global MapWidth:Int = 800 / 50 '=16
Global MapHeight:Int = 600 / 50 ' =12

Global MapArray:TTile[MapWidth, MapHeight] 

Type TTile
	Field x:Int
	Field y:Int
	Field TileID:Int
	
	Global List:TList = New TList
	
	Function Create:TTile() 
		Return New TTile
	End Function
	
	Function LoadArrayDataFromFile() 
		Local fileStream:TStream
		filestream = ReadFile("MapData.txt") 
		For Local y:Int = 0 Until MapHeight
			For Local x:Int = 0 Until MapWidth
				MapArray[x, y].TileID = ReadInt(Filestream) 
			Next
		Next
		CloseFile Filestream
	End Function
	
	
	Function SaveArrayDataToFile() 
		Local FileStream:TStream
		FileStream = WriteFile("MapData.txt") 
		For Local y:Int = 0 Until MapHeight
			For Local x:Int = 0 Until MapWidth
				WriteInt(FileStream, MapArray[x, y].TileID) 
			Next
		Next
		CloseFile filestream
	End Function
	
	Function ClearMapArray() 
		For Local x:Int = 0 Until MapWidth
			For Local y:Int = 0 Until MapHeight
				MapArray[x, y].TileID = 0
			Next
		Next
	End Function
	
	Function DrawMapData() 
		For Local x:Int = 0 Until MapWidth
			For Local y:Int = 0 Until MapHeight
				DrawText MapArray[x, y].TileID, x * 50, y * 50
			Next
		Next
	End Function
	
	Function FillArrayWithRandomNumbers() 
		For Local y:Int = 0 Until MapHeight
			For Local x:Int = 0 Until MapWidth
				MapArray[x, y] = TTile.Create() 
				MapArray[x, y].TileID = Rand(1, 1000) 
 
			Next
		Next
	End Function
	
End Type


TTile.FillArrayWithRandomNumbers()  ' We fill the Map Array with random number

TTile.SaveArrayDataToFile()  ' We save whats in the MapArray to a file

TTile.ClearMapArray()   ' We set the maparray to 0. We will then read back in to the array the data from the file.

TTile.LoadArrayDataFromFile()  ' we load back the array data from file.

While Not KeyHit(KEY_ESCAPE) 
	Cls
		
		TTile.DrawMapData
		
	Flip
Wend