pass an array to a procedure

Everything else that doesn't fall into one of the other PB categories.
doodlemunch
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Apr 05, 2005 11:20 pm

pass an array to a procedure

Post by doodlemunch »

how do i pass an array to a procedure? i want to access all the float numbers i got in an array how can i pass the array to the procedure? i tried with @value at the procedure values but it gave me an error when trying to compile
Xombie
Addict
Addict
Posts: 898
Joined: Thu Jul 01, 2004 2:51 am
Location: Tacoma, WA
Contact:

Post by Xombie »

Is this to a dll? Because otherwise, all arrays are global in PB. You can just access it like normal within your procedure. No need to pass anything.
doodlemunch
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Apr 05, 2005 11:20 pm

Post by doodlemunch »

i have an array with 4 values, they are color values (r,g,b etc). floats.

i have a procedure which needs the array to be passed because inside the procedure i call a dll which needs the array info (the colors).

i cant seem to pass the array correctly. my color is taken as green although what im sending is red.. and even if i have 0,0,0 its green anyway, i think im sending the pointer or something. how can i work arround this? is it even possible? how can i "fake" the array to the dll if not?

thanks!!!!
doodlemunch
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Apr 05, 2005 11:20 pm

Post by doodlemunch »

I did it !!!
:lol: thanks anyway !
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

I don't know if I got your question right but maybe this helps (?)

Code: Select all

Procedure showArray(*array.l)
  For i=0 To 10
    Debug PeekB(*array) ; PeekW(), PeekL(), PeekF()
    *array+1  ; +2, +4
  Next 
EndProcedure

Dim myArray.b(10)
For i=0 To 10
  myArray(i) = Random($FF)
Next

For i=0 To 10
  Debug myArray(i) 
Next

showArray(@myArray())
I guess there will be more elegant ways for sure...


EDIT: Ah... too late again... didn't see your last response. ;)
Good programmers don't comment their code. It was hard to write, should be hard to read.
doodlemunch
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Apr 05, 2005 11:20 pm

Post by doodlemunch »

but if I use a for then I must call the dll function many times? this wont work!

and i managed to make it work but somehow it might be sending integers or something because i cant seem to mix the values, if i send 1,0,0 i get full red, thats ok ! but if i send 0.1,0,0 i still get full red! weird!!! if i send 0.1,0.1,0.5 i dont get a blueish color at all, its white!.

this is like what im doing:

Code: Select all

;my array
Dim Color.f(4)
  Color(0) = R.f
  Color(1) = G.f
  Color(2) = b.f
  Color(3) = 0.0
  

;this is my function
Procedure MyFunction(*value.f )
  Dim TempArray.f(4)
  TempArray() = *value.f
  CallFunctionFast(*thefunction,  TempArray() )

EndProcedure

;now im calling my function
MyFunction(  Color() )

traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

Now that I see what you're up to, why don't you simply pass the pointer on?
I mean, an array is nothing else than an "easy to handle memoryarea".

BTW: You're working with pointers here, a pointer can't be a float.
It should be MyFunction(*value.l).
Good programmers don't comment their code. It was hard to write, should be hard to read.
doodlemunch
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Apr 05, 2005 11:20 pm

Post by doodlemunch »

looks like the dll function is not recieving the floats or something (yes im saying this again because i think its happening). does anyone know what could be causing this mess?? the values dont get "mixed" :cry: :cry: :cry: :cry: :cry: (the colors are plain colors, even if I call 0.5,0.0,0.1 I dont get the desired color).
doodlemunch
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Apr 05, 2005 11:20 pm

Post by doodlemunch »

oh didnt see your post.
if i do that will the values remain floats and be passed like floats and everyone will be happy? (ill go test).

edit: no it doesnt work still :cry: :cry:
doodlemunch
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Apr 05, 2005 11:20 pm

Post by doodlemunch »

can someone help me with this?
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Are you sure the colour array you are using should be 'float' types?

I only ask because colours are normally represented by 'long' datatypes.

I'm probably barking up the wrong tree completely!
I may look like a mule, but I'm not a complete ass.
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

doodlemunch:

What DLL is that you're using?
Does it work outside your procedure, like

Code: Select all

CallFunctionFast(*thefunction,  @yourArray())
?

If it does, it should also work like this from your procedure:

Code: Select all

Procedure MyFunction(*colorArray.l )
  CallFunctionFast(*thefunction,  *colorArray)
EndProcedure
There's no need to copy the values to a new array.

srod wrote:I only ask because colours are normally represented by 'long' datatypes.
Hmm... last time I checked, OpenGL and DirectX were considered 'normal'.
Both are using floats as color-values ;) SCNR :twisted:
Good programmers don't comment their code. It was hard to write, should be hard to read.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Ah, not a great one for graphics me! Was talking with experience of PB and basic GDI only. Just making sure the problem was nothing obvious; usually is in my case!

I second your comments about the arrays and their being no need to copy the elements across to a new array etc.
I may look like a mule, but I'm not a complete ass.
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

srod wrote:Just making sure the problem was nothing obvious; usually is in my case!
You know I justed wanted to banter you about this ;)
Good programmers don't comment their code. It was hard to write, should be hard to read.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

I consider myself well and truly 'bantered' on that score! :D
I may look like a mule, but I'm not a complete ass.
Post Reply