Qbasicnews.com

Full Version: Dynamic Arrays in User Defined Types
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Hmmm.... This wont run. :???:


Code:
Type Surface_Properties
RGBA(3) as Single  
Spec(3) as Single  
End Type

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

Type Model_Type
   Num_Verts as Integer
   Num_Faces as Integer
   Vec as Vector3DType
   Surf as Surface_Properties
end Type


Dim Model(1 to 10) as Model_Type Ptr


Model(1)->Num_Verts = 100
Model(1)->Vec = allocate(Model(1)->Num_Verts * Len(Vector3DType))
Ptr.

Code:
Vec as Vector3DType ptr

You forgot to declare as Ptr
Big Grin
He he he... now this crashes. :roll:


Code:
Type Surface_Properties
RGBA(3) as Single  
Spec(3) as Single  
End Type

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

Type Model_Type
   Num_Verts as Integer
   Num_Faces as Integer
   Num_Surfaces as Integer
   Vec as Vector3DType Ptr
   Surf as Surface_Properties ptr
end Type


Dim Model(1 to 10) as Model_Type ptr


Model(1)->Num_Verts = 100
Model(1)->Num_Surfaces = 3
Model(1)->Vec = Callocate(Model(1)->Num_Verts * Len(Vector3DType))
Model(1)->Surf = Callocate(Model(1)->Num_Surfaces * Len(Surface_Properties))
You forgot to allocate model_type ptr

Code:
Dim Model(1 to 10) as Model_Type ptr

for i = 1 to 10
   model(i) = allocate(Len(Model_type))
next i
Oh, Thanks!!! I didn't know I had to do that too. :rotfl:

EDIT:

It seems to work good and that's a nice clean structure. Thanks again. Tongue
BTW, for Matrices you can also use a 2D pointer array. This is what I'm using in my bootleg 3d math library

Code:
#define MATRIX4X4 Single Ptr Ptr

'PtrArray2D part of Extra Lib (library of extra functions I'm working on.)
Function PtrArray2D(Byval rows As Integer, Byval cols As Integer,Byval bytes As Integer) As Any Ptr Ptr
         Dim tempptr As Any Ptr Ptr
         tempptr = Callocate(rows * bytes)
         For i = 0 To rows - 1
             tempptr[i] = Callocate(cols * bytes)
         Next i
         PtrArray2D = tempptr
End Function

Dim global As MATRIX4X4

global = PtrArray2D(4, 4, Len(Single))

global[1][1] = 24.653
Pages: 1 2 3