Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dynamic Arrays in User Defined Types
#11
So, I could do it like this?


Code:
Type ObjType
Num_Surfaces as Integer
Start_Id as Integer ptr
End_Id as Integer ptr
Surface_Id as Integer ptr
End Type

Dim Entity(1 to Total_Entities) as ObjType

Sub LoadOBJ(Entity as ObjType)

'Get Entity.Num_Surfaces from file...
  Entity.Start_Id=Callocate(Entity.Num_Surfaces*Len(Entity.Start_Id))
End Sub

Then when I want to use the new array in a sub...

Code:
Sub Draw_Entity( Entity as ObjType)

For S = 1 to Entity.Num_Surfaces
  For P = Entity.Start_Id[S] to Entity.End_Id[S]
    
  Next
Next
End Sub

Does that look right?


EDIT:
Ok, that seems to work good, but what about these?

Code:
RGBA(1 to 50, 3) as Single ''heh oops!
Ambient(1 to 50, 3) as Single
Diffuse(1 to 50, 3) as Single
Specular(1 to 50, 3) as Single
Emission(1 to 50, 3) as Single

How do I allocate a matrix in this method?


Would I just do something like this?


Code:
Type Surface_Properties_Type
RGBA(3) as Single ''heh oops!
Ambient(3) as Single
Diffuse(3) as Single
Specular(3) as Single
Emission(3) as Single
End Type


Type ObjType
Num_Surfaces as Integer
Start_Id as Integer ptr
End_Id as Integer ptr
Surface_Id as Integer ptr
Surface_Properties as Surface_Properties_Type ptr
End Type

Dim Entity(1 to Total_Entities) as ObjType


Then....


Code:
Some_Sub(Entity as ObjType)

Entity.Surface_Properties =  Callocate(Entity.Num_Surfaces*Len(Entity.Surface_Properties))

End Sub


Some_Other_Sub(Entity as ObjType)

Entity.Surface_Properties[S].RGBA(1) = 'Get From File

End Sub



It's kinda hard to get used to... Is it even right?
Reply
#12
Pretty much, although you need to subtract 1 from the Num_Surfaces

Code:
For S = 1 to Entity.Num_Surfaces  - 1
Reply
#13
One more question... Tongue

Do I need to deallocate these, or will it be automatic? I've tested a few things and I didn't notice any memory leaks... at least this Ram-Booster thing I'm using didn't report any...

Is it automatic deallocation when the program ens then?
Reply
#14
Quote:Is it automatic deallocation when the program ens then?
Windows might do that, but I'm pretty sure you're supposed to deallocate them.
Reply
#15
All this raised another question. Here's a piece of code:

Code:
TYPE SomeType
     ThisField  AS LONG
     ThisOtherField AS STRING  
END TYPE

' Static array for the sake of test, but
' Question would apply to dynamic arrays
' declared like this too.
DIM ThisArray(1 to 100) AS SomeType
DIM Sorted(1 to 100)    AS SomeType

' Assuming I have a function I did called SortArray()
Sorted = SortArray(ThisArray())

Can I do this? Or, do I need to pass the address of ThisArray like with the @ symbol ?
hen they say it can't be done, THAT's when they call me ;-).

[Image: kaffee.gif]
[Image: mystikshadows.png]

need hosting: http://www.jc-hosting.net
All about ASCII: http://www.ascii-world.com
Reply
#16
You need to pass the address. Have the function return a pointer. Include crt.bi with your program and use MemCpy to copy the return pointer into the new array.

Code:
TYPE SomeType
     ThisField  AS LONG
     ThisOtherField AS STRING
END TYPE

'For MemCpy
'$Include: "crt.bi"

' Static array for the sake of test, but
' Question would apply to dynamic arrays
' declared like this too.
DIM ThisArray(1 to 100) AS SomeType
DIM Sorted(1 to 100)    AS SomeType

' Assuming I have a function I did called SortArray()
MemCpy @Sorted(1), SortArray(ThisArray()), 100 * Len(SomeType)
Reply
#17
I was trying to rebuild the gl program I made using pointers and I ran into trouble. I made this for a small example... Can anyone show me what I'm doing wrong? I'm not able to access those Callocated arrays from main. :???:


Code:
Type Vector3DType
   X as Single
   Y as Single
   Z as Single
End Type

Type Main_Type
   Indices as Integer
End Type

Type Alloc_Type
   Arr(3) as Vector3DType
End Type

Declare Sub Make_Vals( mArray as Main_Type, aArray as Alloc_Type ptr)  
Declare Sub Print_Vals( mArray as Main_Type, aArray as Alloc_Type ptr )
Declare Sub Kill_Vals(aArray as Alloc_Type ptr )
  
Dim Main_Array(1 to 10) as Main_Type
Dim Alloc_Array(1 to 10) as Alloc_Type Ptr



   Make_Vals Main_Array(1), Alloc_Array(1)
   Print_Vals Main_Array(1), Alloc_Array(1)


sleep 500
CLS


   Dim aArray as Alloc_Type ptr
   aArray = @Alloc_Array(1)
  
   For i = 1 to Main_Array(1).Indices
      Print aArray[i].arr(1).X
      Print aArray[i].arr(1).Y
      Print aArray[i].arr(1).Z
   Next



Sleep 500
Kill_Vals Alloc_Array(1)




Sub Make_Vals( mArray as Main_Type, aArray as Alloc_Type ptr )
   mArray.Indices = 100
   aArray = Callocate(mArray.Indices * Len(Alloc_Type))
  
   For i = 1 to mArray.Indices
      aArray[i].arr(1).X = rnd*3
      aArray[i].arr(1).Y = rnd*3
      aArray[i].arr(1).Z = rnd*3
   Next
End Sub


Sub Print_Vals( mArray as Main_Type, aArray as Alloc_Type ptr )
   For i = 1 to mArray.Indices
      Print aArray[i].arr(1).X
      Print aArray[i].arr(1).Y
      Print aArray[i].arr(1).Z
   Next
End Sub


Sub Kill_Vals(aArray as Alloc_Type ptr )
   Deallocate(aArray)
End Sub
Reply
#18
A better way is to do away with arrays and use straight pointers.

ie:

Code:
type vector
   x as single
   y as single
   z as single
end type

type model
    numverts as integer
    vertex as vector
end type

dim vb as model ptr

vb->numverts = 5000
vb->vertex = allocate(vb->numverts *LEN(vector))

'to access each element...

for i = 0 to vb->numverts -1
     vb->vertex[i].x = rnd
     vb->vertex[i].y = rnd
     vb->vertex[i].z = rnd
next i

deallocate vb->vertex
deallocate vb

That way, you're treating the pointer like a structure
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply
#19
Ok, but what if you wanted to have vb as an array of models? How do you access that structure in main?


Like this...
Code:
Dim vb(1 to Num_Models) as model ptr
Reply
#20
Code:
vb(vbindex)->verterx[vertexindex].x
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)