Readline

BlitzPlus Forums/BlitzPlus Programming/Readline

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Here's how I'd do it... Feel free to write your own. It would probably be better for you to do that so you can get a feel for banks. Also, remember that a bank is just a section of memory, so you have to count in bytes and this also means that there is no REALLY good way to get to the data unless you treat it like Blitz treats an array. I used banks because I'm pretty sure that you can change the sizes of blitz basic arrays easily to accommodate for levels of different sizes.

AppTitle("Level Example")
Graphics 800,600,16,2
SetBuffer(BackBuffer())
SeedRnd(MilliSecs())

;Sizes of Various data types in bytes
Const IntegerSize=4
Const ShortSize=2

MyLevel=GenRandLevel(10,5)
If Not MyLevel Then RuntimeError("Could not create level!")
If Not writelevel("test.lvl",MyLevel) Then RuntimeError("Could not write level!")
FreeBank(MyLevel)
MyLevel=loadlevel("test.lvl")
If Not mylevel Then RuntimeError("Could not reload level!")
Cls
DrawLevel(MyLevel)
Flip
WaitKey()

Function GenRandLevel(xsize,ysize) ;Generates a random level and stores it into a bank
	levelbank=CreateBank(2*IntegerSize+xsize*ysize*shortsize);2 integers plus a rectangle of shorts to store the pieces
	If Not levelbank Then Return 0
	;Save the size of the level (maximum for both x and y is 2^16-1 [the size of the positive portion of an integer])
	PokeInt(levelbank,0,xsize)
	PokeInt(levelbank,IntegerSize,ysize)
	DebugLog("Level Sizes are: " + xsize +","+ysize) ;Just for a little information
	For IcountRands=IntegerSize*2 To (ShortSize*xsize*ysize)+IntegerSize*2-1 Step ShortSize
									;skip the first 8 bytes due to the level sizes being there
									;use step 2 because a short takes up two bytes
									;Subtract one because it is an offset which start counting at 0
		PokeShort(levelbank,IcountRands,Rand(0,4)) ;A short allows for 2^16-1 different pieces
	Next
	Return levelbank
End Function

Function drawlevel(levelbank) ;Draws the generated level
	;Get the Level Sizes
	xsize=PeekInt(levelbank,0)
	ysize=PeekInt(levelbank,IntegerSize) ;Skip IntegerSize number of bytes to get past the x coordinate
	DebugLog("Drawing Level of size " + xsize +"," + ysize) ;Again, just for info
	If BankSize(levelbank) <> xsize*ysize*2+8 Then RuntimeError("Level Corrupted!") ;Make sure the the file loaded ok
	For ycoord=0 To ysize-1 ;Start counting at 0 because we don't want to skip the first row of pieces
		For xcoord=0 To xsize-1 ;start counting at 0 because we don't want to skip the first piece
			piecetype=PeekShort(levelbank,ShortSize*(xcoord+ycoord*xsize)+IntegerSize*2)
			;Draw the Pieces
			Select piecetype
			Case 0
				Text (xcoord)*15,(ycoord)*15,"A"
			Case 1
				Text (xcoord)*15,(ycoord)*15,"B"
			Case 2
				Text (xcoord)*15,(ycoord)*15,"C"
			Case 3
				Text (xcoord)*15,(ycoord)*15,"D"
			Case 4
				Text (xcoord)*15,(ycoord)*15,"E"
			Default
				;This is for pieces that we don't have images for
				Text (xcoord)*15,(ycoord)*15,"X"
				DebugLog("Unknown piece: " + piecetype)
			End Select
		Next
	Next
End Function

Function loadlevel(loadfile$)
	levelhandle=ReadFile(loadfile)
	If Not levelhandle Then Return 0
	levelbank=CreateBank(FileSize(loadfile))
	ReadBytes(levelbank,levelhandle,0,FileSize(loadfile))
	CloseFile(levelhandle)
	Return levelbank
End Function

Function writelevel(savefile$,levelbank)
	levelhandle=WriteFile(savefile)
	If Not levelhandle Then Return False
	WriteBytes(levelbank,levelhandle,0,BankSize(levelbank))
	CloseFile(levelhandle)
	Return True
End Function