Pointer question

BlitzMax Forums/BlitzMax Programming/Pointer question

JoshK(Posted 2007) [#1]
This is a question about calling dlls with variable pointers.

I have a function that accepts a variable and changes it, like this:

local texture
glGenTextures(1,VarPtr texture)

And the function sets "texture" to the right value.


Now I have a function where the variable type is a byte ptr. The function is generating an array of vertices:

local buffer:Byte ptr
Function GenVertices(varptr buffer)

The function does not fill in the buffer, it sets the value of the buffer variable to a memory buffer the function allocates.

Then I want to create a static bank from buffer. This has me confused. If I pass the varptr to buffer it crashes. If I pass buffer it crashes. How should I pass this variable?


Brucey(Posted 2007) [#2]
Can't you make buffer a Byte Ptr Ptr ?
something like
Local buffer:Byte Ptr Ptr

Function GenVertices(varptr buffer)

...maybe...


Chris C(Posted 2007) [#3]
local buffer:int ptr ;<- type array holds

function GenVertices(buffer var)


isn't it????


Brucey(Posted 2007) [#4]
Glad we all really seem to know what we are doing, huh? ;-)


Chris C(Posted 2007) [#5]
ROFL - yeah great, lets hope halo finds out and teaches us all somthing....!


gman(Posted 2007) [#6]
chris was on the right track... you need to "var" the param to GenVertices. this allows you to change it:
SuperStrict

Framework BRL.Basic

Local buffer:Byte Ptr

' fill the buffer with 334455
GenVertices(buffer)

Local testout:TBank=CreateStaticBank(buffer,4)
Print(PeekInt(testout,0))

Function GenVertices(buff:Byte Ptr Var)
	' allocate some space
	buff=MemAlloc(4)

	Local testbnk:TBank=CreateBank(4)
	PokeInt(testbnk,0,334455)
	MemCopy(buff,testbnk.buf(),4)	
	testbnk=Null
EndFunction

brucey was on the right track as well as you could use a Byte Ptr Ptr too:
SuperStrict

Framework BRL.Basic

Local buffer:Byte Ptr

' fill the buffer with 334455
GenVertices(Varptr buffer)

Local testout:TBank=CreateStaticBank(buffer,4)
Print(PeekInt(testout,0))

Function GenVertices(buff:Byte Ptr Ptr)
	' allocate some space
	buff[0]=MemAlloc(4)

	Local testbnk:TBank=CreateBank(4)
	PokeInt(testbnk,0,334455)
	MemCopy(buff[0],testbnk.buf(),4)	
	testbnk=Null
EndFunction



ImaginaryHuman(Posted 2007) [#7]
I would think all you do is pass `buffer` as the memory address that you want your static bank to use. OpenGL is going to fill your buffer variable with an address of a memory space, so now buffer=where mem is, so you just give buffer to CreateStaticBank (looks like that's whats in the code above).

If you were to do varptr(buffer) you'd be getting the pointer to the buffer variable itself and not the indirect contents of the buffer, nor the memory space it implies. You can create a static bank to the varptr of a variable, and then use poke/peek to change the variable's value, but that doesn't have anything to do with the contents of the memory space that the variable points to.


JoshK(Posted 2007) [#8]
Byte ptr ptr works.