Main Menu

Basic FIle Formats

Started by kevin, January 18, 2010, 07:49:34 AM

Previous topic - Next topic

kevin

 Basic FIle Formats

 This example we're looking at a very basic framework for how we can store different types of data in a larger chunk of data and then retrieve it.  Although this first example just looks at how we can use ID makers to tell us where a particular  section of the data begins.

  In this example we're using 4 character strings, packed into a 32bit integer as our ID.  This allows us to avoid 'string' comparisons when decoding the data later on.   While it's largely irrelevant in this example given it's so small, but in larger chunks of data with lots of the section types, it can add a that little bit of extra overhead.  



PlayBASIC Code: [Select]
   // This Examples Requires either PB1.64k or PBFX1.76 or higher

FORMID =TextToLong("FORM")
DUDEID =TextToLong("DUDE")
ENDID =0


Bank=NewBank(1000)
PokeBankInt Bank,0,FormID
PokeBankInt Bank,4,DudeID

print PeekBankString(Bank,0,4)
print PeekBankString(Bank,4,4)


// Scan through them
Pos=0
repeat
ID = PeekBankInt(BAnk,pos)

Select ID
case FORMID
print "Found FORM"
pos+=4
Continue

case DUDEID
print "Found DUDE"
pos+=4
Continue

endselect

pos+=4
until ID =EndID


Sync
waitkey


// Converts a 4 character string into 32bit integer
psub TextToLong(T$)
ThisChr = mid(t$,4) * $1000000
ThisChr+= mid(t$,3) * $10000
ThisChr+= mid(t$,2) * $100
ThisChr+= mid(t$,1)
endpsub ThisCHR