A Bank of Types

BlitzMax Forums/BlitzMax Programming/A Bank of Types

Leiden(Posted 2006) [#1]
Since banks are allocated in a continuous block of memory (so I've heard) they would be a superb way of passing stuff to C and back. Is it possible to create a Bank of Types and if so how would you do it?


ImaginaryHuman(Posted 2006) [#2]
Local MyBank:TBank=CreateBank(1024)
Local MyBuf:Byte Ptr=BankBuf(MyBank)

Type MyType1
   Field ThingA:Byte
   Field ThingB:Int
   '5 bytes
End Type

Type MyType2
   Field ThingC:Float
   Field ThingD:Short
   '6 bytes
End Type

Local MyDummy1:MyType1=New MyType1
Local MyDummy2:MyType2=New MyType2
MyDummy1=MyBuf
MyDummy2=MyBuf+8
MyDummy1.ThingA=4
MyDummy1.ThingB=12
MyDummy2.ThingC=16.3
MyDummy2.ThingD=14

Print MyBuf[0] ' prints MyDummy1.ThingA (byte)
Local MyIntPtr:Int Ptr=MyBuf+1
Print MyIntPtr[0] ' prints MyDummy1.ThingB (int at offset 1)
MyIntPtr=MyBuf+8
Print MyIntPtr[0] ' prints MyDummy2.ThingC (float)
Local MyShortPtr:Short Ptr=MyBuf+12
Print MyShortPtr[0] ' prints MyDummy2.ThingD (short)


Just a stab at it.. untested.

You can either do that, trying to map Max-friendly instances of real types onto the memory bank, or you could just use a bank by itself...

Local MyBank:TBank=CreateBank(1024)
Local MyBuf:Byte Ptr=BankBuf(MyBank)
Local MyShortPtr:Short Ptr
Local MyIntPtr:Int Ptr
MyBuf[0]=5 ' store a byte
MyBuf[1]=5 ' store a byte in the upper byte of the integer (thing b)
MyIntPtr=MyBuf+1
MyIntPtr[0]=5 ' store an int at 1 byte past the start

etc

??????

I don't think you can do VarPtr(variable)=BankBuf+Offset, although that'd be nice


Chris C(Posted 2006) [#3]
I tend to do this... -edit- far too often and it looks like a kludge!
	For Local i:Int=0 Until MaxContacts
		PokeInt(ContactBank,(i*ContactSize)+OdeMode,b.mode)
		PokeDouble(ContactBank,(i*ContactSize)+OdeMu,b.mu)
		PokeDouble(ContactBank,(i*ContactSize)+OdeMu2,b.mu2)
		PokeDouble(ContactBank,(i*ContactSize)+Odefdir1x,b.fdirx)
		PokeDouble(ContactBank,(i*ContactSize)+Odefdir1y,b.fdiry)
		PokeDouble(ContactBank,(i*ContactSize)+Odefdir1z,b.fdirz)
		PokeDouble(ContactBank,(i*ContactSize)+Odebounce,b.bounce)
		PokeDouble(ContactBank,(i*ContactSize)+Odebouncevel,b.bouncevel)
		PokeDouble(ContactBank,(i*ContactSize)+Odesofterp,b.softerp)
		PokeDouble(ContactBank,(i*ContactSize)+Odesoftcfm,b.softcfm)
		PokeDouble(ContactBank,(i*ContactSize)+Odemotion1,b.motion1)
		PokeDouble(ContactBank,(i*ContactSize)+Odemotion2,b.motion2)
		PokeDouble(ContactBank,(i*ContactSize)+Odeslip1,b.slip1)
		PokeDouble(ContactBank,(i*ContactSize)+Odeslip2,b.slip2)
	
	Next