Reading tilemap data

BlitzMax Forums/BlitzMax Beginners Area/Reading tilemap data

Nightsight(Posted 2008) [#1]
I have a question about how to import tile mapdata.

The following example (just the type, it will not run on its own) is taken from Sloan Kelly's BlitzMax book, it will read the tilemap data from an external file with the numbers 0-9. The problem is how do I get it to accept numbers from 10 and up. I need to be able to just insert an comma between the numbers, and be able to take a number with more than two digits. Any suggetions? It does not need to use the example below


Type TMap

Field image:TImage
Field blocks:TList = CreateList()

Method LoadBlocks(img:String)
image = LoadAnimImage(img, 25, 25, 0, 4)
End Method

Method LoadMap(map:String)
x:Int = 0
y:Int = 0

ClearList(blocks)

file = OpenFile(map)
While Not Eof(file)

'Cls
'DrawText("Processing Map", 50, 50)
'DrawText("X = " + X, 50, 60)
'DrawText("Y = " + Y, 50, 70)
'Flip


s:String = ReadLine(file)
'If s:String <> "," Then '''' egen
For i:Int = 1 To Len(s)
block:TBlock = New TBlock
block.X = x
block.Y = y
block.Index = Int(Mid(s, i, 1)) - 1

blocks.AddLast(block)

x = x + 25

If x >= 800
x = 0
y = y + 25
End If

Next
'EndIf ''''egen
Wend

End Method

Method DrawMap()
For b:TBlock = EachIn blocks
DrawImage(image, b.X, b.Y, b.Index)
Next
End Method


Function Create:TMap(path:String, image:String)
o:TMap = New TMap
o.LoadBlocks(image)
o.LoadMap(path)
Return o
End Function


End Type


Yan(Posted 2008) [#2]
Strict

Local myString$ = "10, 20, 30, 40, 50"
Local myArrayOfStrings$[] = myString$.Split(",")

For Local thisString$ = EachIn myArrayOfStrings$ 
	Print thisString$.Trim()
Next

Print
Print (myArrayOfStrings$[0]) + " + " + Int(myArrayOfStrings$[1]) + " = " + (int(myArrayOfStrings$[0]) + Int(myArrayOfStrings$[1]))



Nightsight(Posted 2008) [#3]
Thanks, I will look into it later. (i'm making pixels right now ;- ) )


CS_TBL(Posted 2008) [#4]
btw, BMax already has a TMap object, which isn't related to mapdata like you're doing. So at some point you may get into problems..


Amon(Posted 2008) [#5]
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



Nightsight(Posted 2008) [#6]
Thanks alot Amon. Again I will look into it. (i'm also making the graphics for a remake/inspired of HERO for the C64, and the graphics is going ok for the moment. So then I get tired of drawing in a day or two I will look into the coding.)


- CS TBL, I have not played to much with it, but the example (from which it comes from, works without any problems. But I'll probably change it 'name' anyway, just to be sure.