Main Menu

Parameter Bucket

Started by kevin, July 16, 2020, 11:52:53 AM

Previous topic - Next topic

kevin


Parameter Bucket

     This example uses a bank as a type of memory bucket to hold lists of Parameter pairs.  You can alter the parameters type and add your own read/write functions, but the idea here to set up a structure than the user recreated every frame without actually destorying it.  So at the end of each frame after we write a bunch of nonsense data into the bucket we just reset the buckets internal pointer to the start and keep going..
   



PlayBASIC Code: [Select]
      // Set up the Bucket, which is just a global bank we're
// poking directly into
AllocParamBucket()


// ---------------------------------------------------------
do
// ---------------------------------------------------------

Cls 0

TestCount++
ink $ff0000
print "Running Test #"+Str$(TestCount)
ink -1
print fps()

// Allocate the FIRST item in this list
MyList= NewParamBucketList()

// Remember this offset and Set the data fields
CurrentOffset=MyList
// Set the data at this offset
SetParamToBucket(CurrentOffset,"Hello","World")

// Add some more dummy data to the list
for lp =0 to rndrange(1,100)
// add a new item to end of this list
CurrentOffset =AddParamToBucket(CurrentOffset)
// write data into it
SetParamToBucket(CurrentOffset,"Hello"+str$(lp),"World"+str$(lp))
next

// Display This List
ShowParamBucketList(MyList)



// reset the Bucket to it's initial state...
// Next frame we're over writing the same memory
ResetParamBucket()


sync
loop spacekey()=true





type tParamBucket
Bank
BankPtr
BankSize
CurrentOffset
EndType

Dim ParamBucket as tParamBucket


type tParams
LinkOffset ; Bytes to next item in this list
Name$
Value$
EndType



Function AllocParamBucket()

Dim ParamBucket as tParamBucket
ParamBucket = new tParamBucket

size = $10000
Bank=newBank(Size)
if Bank

paramBucket.bank = Bank
paramBucket.BankPtr = getBankPtr(Bank)
paramBucket.BankSize = Size

ResetParamBucket()

endif
EndFunction



Function ResetParamBucket()
paramBucket.CurrentOffset = 16
EndFunction

Function NewParamBucketList()

if paramBucket.bank
ListOffset = ParamBucket.CurrentOffset

dim Link as tparams pointer
Link =ParamBucket.BankPtr + ListOffset

// Force The List to next Object to be ZERO
Link.LinkOffset = 0
Link.Name$ =""
Link.Value =""
// Move the current pointer past this
ParamBucket.CurrentOffset+=sizeof(tparams)
endif
EndFunction ListOffset





Function AddParamToBucket(ListOffset)

if paramBucket.bank

// check we're within the bucket size for the entry link
if range(ListOffset,1,ParamBucket.BankSize)

// Check if this is the last lilnk ??
dim Search as tparams pointer
Search =ParamBucket.BankPtr + ListOffset

// step to the end of this list
While Search.LinkOffset <>0
// Move the search pointer to the next link in this list
Search =ParamBucket.BankPtr +Search.LinkOffset
EndWhile

// Get the current free item from the global
NewParamOffset = ParamBucket.CurrentOffset

// Point the last fragment at this new fragment
Search.LinkOffset = NewParamOffset


// Reused Search pointer to init this new fragment
Search =ParamBucket.BankPtr +Search.LinkOffset

// Flush this fragment out to it's empty state
Search.LinkOffset = 0
Search.Value = ""
Search.Name = ""

// Move the current pointer past this
ParamBucket.CurrentOffset+=sizeof(tparams)

Login required to view complete source code