''FB module for converting *.tmx tile file map (Tiled editor) to FB Image
''helpfull when you are usin image maps in games like worms, lemmings..
''
''by dreamerman 2010
'' :-)

Declare Function Zwroc_stat_proste(mciag_znakow As String, wskaznik1 As String, wskaznik2 As String, dlugosc As Integer) As String
Declare Sub Pobierz_wymiar(ByVal Plik_graficzny As String, ByRef Szer As Integer, ByRef Wys As Integer)
Declare Function Return_filename(ByVal temp_string As String) As String


Type tile_map
	size_width As Integer
	size_height As Integer
	tilesizex As Integer
	tilesizey As Integer
End Type


''custom variable needed in egg_game.bas
Dim Shared As Point_2d tab_platformy(1)


Public Function Convert_tmx_to_image(ByVal tmx_file As String, ByRef map_image As FB.Image Ptr, ByRef err_string As String, ByRef MapSizeX As Integer, ByRef MapSizeY As Integer) As Integer
''main function
''tmx files are coded in xml
Dim As String czs1, czs2, czs3, tile_filename
Dim As Integer czi1, czi2, czi3, czi4, czi5, czi6
ReDim As Integer map_tiles(0, 0)
Dim As Integer map_width, map_height, tile_sizex, tile_sizey, tile_spacing, data_started
Dim As Integer curx, cury, tiles_in_tileset

cury = 0
data_started = -1
Dim As Integer wp1
wp1 = FreeFile()


If Not (Open(tmx_file For Input Encoding "ascii" As #wp1)) Then

	Do Until Eof(wp1)
		Line Input #wp1, czs1
		czs1 = Trim(czs1)	''remove spaces
		''find correct start string -> its before first '='
		czi1 = InStr(czs1, "=")
		czs2 = Left(czs1, czi1-1)
		
		If (Left(czs2, 1) = "<") Then
			''retrieve parameters info
		EndIf
		
		If (data_started=0) Then
			''there are map data only, in simple csv format
			czi1 = 0: czi2 = 1
			''Print "'" & czs1 & "'"
			For curx = 0 To map_width - 1
				czi1 = InStr(czi2, czs1, ",")
				''If curx < 10 And cury > 15 Then Print "'" & Mid(czs1, czi2, czi1-czi2) & "'"	': Sleep 100
				map_tiles(curx, cury) = Val(Mid(czs1, czi2, czi1-czi2))
				czi2 += Len(Str(map_tiles(curx, cury)))+1
				'If cury<20 And  map_tiles(curx, cury) <> 0 Then Print "haj!: " & Str(curx) & "x" & Str(cury) & ", val: " & Str(map_tiles(curx, cury))
			Next
			cury += 1
			If cury = map_height Then
				Data_started = -1
			EndIf
		Else
		Select Case czs2
			Case "<map version"	''here is info about map/tile size and type
				map_width = Val(Zwroc_stat_proste(czs1, "width=" + Chr(34), Chr(34), 10))
				map_height = Val(Zwroc_stat_proste(czs1, "height=" + Chr(34), Chr(34), 10))
				tile_sizex = Val(Zwroc_stat_proste(czs1, "tilewidth=" + Chr(34), Chr(34), 10))
				tile_sizey = Val(Zwroc_stat_proste(czs1, "tileheight=" + Chr(34), Chr(34), 10))
				ReDim map_tiles(map_width-1, map_height-1)
				Print "Map dimensions: '" & Str(map_width) & "x" & Str(map_height) & ", tile: '" & Str(tile_sizex) & "x" & Str(tile_sizey) 
				''Sleep
				MapSizeX = map_width * tile_sizex
				MapSizeY = map_height * tile_sizey
			Case "<data encoding"
				czs3 = Zwroc_stat_proste(czs1, "encoding=" + chr(34), Chr(34), 10)
				If Not czs3 = "csv" Then
					err_string = "Not supported encoding type!"
					Close #wp1
					Return 1
				EndIf
				data_started = 0
			Case "<image source"
				czs3 = Zwroc_stat_proste(czs1, "source=" + chr(34), Chr(34), 10)
				''retrieve only the true file name - not directories
				tile_filename = "data\" & Return_filename(czs3)
				''check if png, if yes then rename to bmp
				If (Right(tile_filename, 4) = ".png") Then tile_filename = Left(tile_filename, Len(tile_filename) - 4) & ".bmp"
			Case "<tileset firstgid"
				tile_spacing = Val(Zwroc_stat_proste(czs1, "spacing=" + Chr(34), Chr(34), 10))
		End Select
		EndIf
		
		
		'Print czs2: Sleep 50
		''WindowTitle(czs1): Sleep 100
	Loop
	
	Close  #wp1
Else
	''error
	Close #wp1 ''be sure that is close
	err_string = "File not opened!": sleep
	Return 1
EndIf

map_image = ImageCreate(MapSizeX, MapSizeY, , 32)
czi1 = 1: czi2 = 1
''map file closed, all tiles reader, now we can render them to our image
Dim temp_surface As FB.Image Ptr
''currently only supports bmp tile set's
Pobierz_wymiar(tile_filename, czi1, czi2)
temp_surface = ImageCreate(czi1, czi2, ,32)
BLoad tile_filename, temp_surface

Print "wymiary: " & Str(czi1) & "x" & Str(czi2)
'Print "tile 3x16: " & Str(map_tiles(3, 16))
'sleep
czi3 = czi1 \ (tile_sizex + tile_spacing)
'Print czi3: Sleep
'If czi3 = 0 Then czi3 = 1
tiles_in_tileset = (czi2 \ (tile_sizey + tile_spacing)) * czi3
''Print "max: " & tiles_in_tileset
''Sleep

For czi1 = 0 To map_width - 1
	For czi2 = 0 To map_height -1
		''
		If Not map_tiles(czi2, czi1) = 0 Then
			czi4 = map_tiles(czi2, czi1)	''tile number
			If czi4 < tiles_in_tileset - 1 Then
				czi5 = czi4 \ czi3
				czi6 = czi4 Mod czi3 - 1
				Put map_image, (czi2*tile_sizex, czi1*tile_sizey), temp_surface, (czi6 * (tile_sizex + tile_spacing), czi5 * (tile_sizey + tile_spacing))- Step (tile_sizex, tile_sizey), PSet
			ElseIf czi4 = tiles_in_tileset - 1 Then
				''start
				tab_platformy(1).X = czi2*tile_sizex'-16
				tab_platformy(1).y = czi1*tile_sizey'-16
			Else
				''end
				tab_platformy(0).X = czi2*tile_sizex'-16
				tab_platformy(0).y = czi1*tile_sizey'-16
			EndIf
		Else
			Line map_image, (czi2*tile_sizex, czi1*tile_sizey)-(czi2*tile_sizex+tile_sizex, czi1*tile_sizey+tile_sizey), &HFFFF00FF, bf
			'Put map_image, (czi2*tile_sizex, czi1*tile_sizey), temp_surface, (czi6 * (tile_sizex + tile_spacing), czi5 * (tile_sizey + tile_spacing))- Step (tile_sizex, tile_sizey), PSet
		EndIf
	Next
Next
'/


ImageDestroy(temp_surface)

Return 0
End Function



Function Zwroc_stat_proste(mciag_znakow As String, wskaznik1 As String, wskaznik2 As String, dlugosc As Integer) As String
	Dim As Integer czi1, czi2, czi3
	Dim As String czs1
	czi3 = Len(wskaznik1)
	''Print "przed !" & mciag_znakow & "! "
	czi1 = InStr(mciag_znakow, wskaznik1)
	czi2 = InStr(czi1+czi3, mciag_znakow, wskaznik2)
	czs1 = Mid(mciag_znakow, czi1+czi3, czi2-czi1-czi3)
	''Print "przed !" & czs1 & "! "
	Return czs1
End Function



Sub Pobierz_wymiar(ByVal Plik_graficzny As String, ByRef Szer As Integer, ByRef Wys As Integer)
	Dim As String czs1
	Dim As Integer wp1
	Dim As UByte Plik_temp(32), Pos_temp
	Dim As Integer czi1, czi2
	
	wp1 = FreeFile
	Open Plik_graficzny For Binary As #wp1
	Get #wp1, 0, Plik_temp(0), 32
	
	czs1 = Right(Plik_graficzny, 3)
	Select Case czs1
		Case "png"
			pos_temp = 12
			czi1 = Plik_temp(pos_temp+4)*16777216 + Plik_temp(pos_temp+5)*65536 + Plik_temp(pos_temp+6)*256 + Plik_temp(pos_temp+7)	''x
			czi2 = Plik_temp(pos_temp+8)*16777216 + Plik_temp(pos_temp+9)*65536 + Plik_temp(pos_temp+10)*256 + Plik_temp(pos_temp+11)	''y
		Case "bmp"
			czi1 = Plik_temp(21)*16777216 + Plik_temp(20)*65536 + Plik_temp(19)*256 + Plik_temp(18)	''x
			czi2 = Plik_temp(25)*16777216 + Plik_temp(24)*65536 + Plik_temp(23)*256 + Plik_temp(22)	''y
		Case Else
			Print "Nie rozpoznany plik!": Sleep 1000
	End Select
	Close #wp1
	
	Szer = czi1
	Wys = czi2
	
End Sub


Function Return_filename(ByVal temp_string As String) As String
Dim As Integer czi1, czi2
Dim As String czs3

czs3 = temp_string				
czi1 = InStr(czs3, "/")
			
Do While czi1>0
	czs3 = Mid(czs3, czi1+1)
	czi1 = InStr(czs3, "/")
Loop

czi1 = InStr(czs3, "\")
Do While czi1>0
	czs3 = Mid(czs3, czi1+1)
	czi1 = InStr(czs3, "\")
Loop

Return czs3
	
End Function

