Declare Sub levelinput(filename As String)
Declare Sub loadlevel()
Declare Sub datapos()
Declare Sub Loadpic()
Declare Sub Waitsub()
Declare Sub Testbug()
Declare Sub Errorlog(text As String)
Declare Sub SetValue()
Declare Sub Figuraction()
Declare Sub Zeichnen()
Declare Sub figurpaint()
Declare Sub resetdata()
Declare Sub Bildcheck()
Declare Sub SetData()

Sub SetData()
	Tabellmax 		= tabellmax + 1
	Spalte			= Len(leveltext(0))
	Zeile          = tabellsize
	Levelbreite		= Spalte * Texturbreite
	Levelhoehe		= Zeile * Texturhoehe
	allHoehe			= Levelhoehe + Texturhoehe
	ReDim preserve textur(0 To texturmax) As Any Ptr
	ReDim preserve texturdata(0 To Zeile+1 , 0 To spalte+1,0 To Tabellmax+1) As Integer
End Sub

Sub Bildcheck()
	For i As Integer = 0 To texturmax
		Texturpfad = "Bin\sources\textur" & i & ".bmp"

		If FileExists(Texturpfad) Then
			Open Texturpfad For Input As #1
			Get #1 , 19 , Texturbreite
			get #1 , 23 , Texturhoehe
			Close #1

			If i > 0 Then
				If Not Texturbreiteold = Texturbreite And Not Texturhoeheold  = texturhoehe Then
					Errorlog("Fehler bei Auflösung der Texturen")
				EndIf
			Else
				Texturbreiteold = Texturbreite
				Texturhoeheold  = texturhoehe
			EndIf
		Else
			Errorlog("Texturen sind nicht vorhanden.")
		EndIf
	Next
End Sub

Sub Loadpic()


	For i As Integer = 0 To texturmax
		ReDim Preserve textur( 0 To i ) As Any Ptr
		textur(i) 	= imagecreate (Texturbreite,Texturhoehe)
		BLoad "bin\sources\textur" & i & ".bmp" 	, textur(i)
	Next
	For i As Integer = 1 To 4
		If FileExists ("Bin\sources\figur" & i & ".bmp") Then
			ReDim preserve figur( 1 To i ) As Any Ptr
			Figur(i) = imagecreate (Texturbreite,Texturhoehe)
			Bload "Bin\sources\figur" & i & ".bmp" 	, Figur(i)
		Else
			Errorlog("Figurtextur wurde nicht gefunden.")
		EndIf
	Next
End Sub

Sub Waitsub()
	Do
		Sleep 10,1
	Loop Until multiKey(&h1C)
End Sub

Sub Testbug()
	Print "Bis hier hin kommts :D"
	sleep
End Sub

Sub Errorlog(Text As String)
	Screen 0
	Dim PrintText As String
	PrintText = Time & Space(1) & Date & Space(1) & Text
	Open "Protokoll.txt" For Append As #1
	Print #1 , PrintText
	Print #1 , ""
	Close #1


	Cls
	Print PrintText
	Print
	Print "Bitte beheben sie dieses Problem. Beenden sie mit Enter"
	Waitsub()
	End
End Sub

Sub SetValue()
	levelcount 		= 1
	texturmax 		= 10
End Sub

Sub Figuraction()
	Dim x As Integer
	Do
		For i As Integer = 1 To 4
			Put (Figurposx,Figurposy - texturhoehe),Figur(i),Trans
			Sleep 50 , 1
		Next
		If x = 2 Then
			Put (Figurposx,Figurposy - texturhoehe),Figur(1),Trans
			Exit do
		EndIf
		x = x + 1
	Loop
End Sub

Sub figurpaint ()
	Put (Figurposx,Figurposy - texturhoehe),Figur(1),Trans
End Sub

Sub Zeichnen()
	Spalte 		= 0
	zeile 		= 0
	Loopzeile 	= 0
	Loopspalte 	= 0
	Line  (0 , 0) - (Levelbreite , allhoehe ) , RGB(192,192,192) , BF

	Do
		Put (Loopspalte,Loopzeile),textur(texturdata(Zeile,Spalte,0)),Trans
		Loopspalte = Loopspalte + texturbreite
		Spalte = Spalte + 1

		Select Case  texturdata(zeile,spalte - 1,1)
			Case 2
				If Spawnpunkt = 0 Then
					Figurposx = Loopspalte - texturbreite
					Figurposy = Loopzeile
					datapos()
					Spawnpunkt = 1
				End If 
		End Select

		If spalte = Len(leveltext(0)) Then
			Loopzeile = Loopzeile + texturhoehe
			Spalte = 0
			Zeile = Zeile + 1
			Loopspalte = 0
		End If
		If zeile = tabellsize  Then
			Spalte = 0
			Zeile = 0
			Loopspalte 	= 0
			Loopzeile 	= 0
			exit Do
		EndIf


	Loop
	If Spawnpunkt = 0 Then
		Errorlog("Level" & levelcount &  " besitzt keinen Spawnpunkt.")
	EndIf

End Sub

Sub datapos()
	dataposx = Figurposx / Texturbreite
	dataposy = Figurposy / Texturhoehe
End Sub

Sub loadlevel()
	Dim Tabellmaxi As Integer
	Do
		For tabellchangei As Integer = tabellchange To leveltextcount - 1
			If leveltext(tabellchangei) = "" Then
				tabellchange = tabellchangei + 1
				Exit For
			EndIf
			For Spalte As Integer = 1 to Len(leveltext(0))
				texturdata(tabellchangei -((Tabellmaxi * tabellsize)),spalte - 1,tabellmaxi) = val(Mid(leveltext(tabellchangei),Spalte,1))
			Next
		Next
		Tabellmaxi = Tabellmaxi + 1
	loop until Tabellmaxi = Tabellmax
End Sub

Sub levelinput(fileName As String)

	If FileExists(filename) Then
		Open filename For Input As #1
		Do

			Line Input #1 , leveltextcheck

			ReDim Preserve leveltext (0 To leveltextcount) As String
			leveltext(leveltextcount) = leveltextcheck
			If  Levelsizecheck = 0 Then
				If leveltext(leveltextcount) = "" Then
					tabellsize = leveltextcount
					Tabellmax = Tabellmax +1
					Levelsizecheck = 1
				EndIf
			endif
			leveltextcount = leveltextcount + 1



		Loop Until Eof(1)
		Close #1
	Else
		Errorlog("Leveldatei wurde nicht gefunden")
	EndIf

End Sub

Sub resetdata()
	Spawnpunkt = 0
	levelsizecheck = 0
	leveltextcount = 0
	tabellsize = 0
	Tabellmax = 0
	tabellchange = 0
	Spalte = 0
	zeile = 0
	Loopzeile = 0
	Loopspalte = 0
End Sub
