11-13-2023, 02:18 AM
For multiple arrays within a UDT you could do this.
Code: (Select All)
' Program to create an array of Doubles in a UDT
DIM AS LONG i
DIM AS DOUBLE v
CONST cMAXELEMENTS = 10
TYPE tARRAYS
array0 AS _MEM
array1 AS _MEM
array2 AS _MEM
END TYPE
DIM AS tARRAYS object
SCREEN 12: _FONT 8
' initialize the array
createObject object.array0, cMAXELEMENTS
createObject object.array1, cMAXELEMENTS
createObject object.array2, cMAXELEMENTS
PRINT "ARRAY 0 ARRAY 1 ARRAY 2"
' set the array to random values
PRINT "Create initial Values"
FOR i = 0 TO cMAXELEMENTS
v = INT(RND * 5)
setElement object.array0, i, v
PRINT "index:"; i; "->"; v; " ";
v = INT(RND * 5)
setElement object.array1, i, v
PRINT "index:"; i; "->"; v; " ";
v = INT(RND * 5)
setElement object.array2, i, v
PRINT "index:"; i; "->"; v
NEXT
' retrieve values from array
PRINT "Retrieve Values"
FOR i = 0 TO cMAXELEMENTS
PRINT "index:"; i; "->"; getElement(object.array0, i); " ";
PRINT "index:"; i; "->"; getElement(object.array1, i); " ";
PRINT "index:"; i; "->"; getElement(object.array2, i)
NEXT
'resize array
resizeObject object.array0, cMAXELEMENTS * 2
resizeObject object.array1, cMAXELEMENTS * 2
resizeObject object.array2, cMAXELEMENTS * 2
' retrieve values from array
PRINT "Double the size of the array"
FOR i = 0 TO cMAXELEMENTS * 2
PRINT "index:"; i; "->"; getElement(object.array0, i); " ";
PRINT "index:"; i; "->"; getElement(object.array1, i); " ";
PRINT "index:"; i; "->"; getElement(object.array2, i)
NEXT
'resize array
resizeObject object.array0, cMAXELEMENTS / 2
resizeObject object.array1, cMAXELEMENTS / 2
resizeObject object.array2, cMAXELEMENTS / 2
' retrieve values from array
PRINT "Half the size of the array"
FOR i = 0 TO cMAXELEMENTS / 2
PRINT "index:"; i; "->"; getElement(object.array0, i); " ";
PRINT "index:"; i; "->"; getElement(object.array1, i); " ";
PRINT "index:"; i; "->"; getElement(object.array2, i)
NEXT
SUB createObject (o AS _MEM, size AS LONG)
' make array one larger than number of elements
o = _MEMNEW((size + 1) * 8) ' 8 is the number of bytes in a double
END SUB
SUB resizeObject (o AS _MEM, size AS LONG)
DIM AS _MEM old
DIM AS LONG iter
' Copy old _MEM to a temp _MEM
old = o
' Create new array of the new size
createObject o, size
' Copy data from old array to new array
iter = 0: DO WHILE iter < o.SIZE AND iter < old.SIZE
_MEMPUT o, o.OFFSET + iter, _MEMGET(old, old.OFFSET + iter, _BYTE) AS _BYTE
iter = iter + 1: LOOP
' free old array
_MEMFREE old
END SUB
FUNCTION getElement# (o AS _MEM, element AS LONG)
getElement = _MEMGET(o, o.OFFSET + (element * 8), DOUBLE)
END FUNCTION
SUB setElement (o AS _MEM, element AS LONG, v AS DOUBLE)
_MEMPUT o, o.OFFSET + (element * 8), v AS DOUBLE
END SUB
2D physics engine https://github.com/mechatronic3000/fzxNGN
Untitled Rouge-like https://github.com/mechatronic3000/Untitled-Rougelike
QB Pool https://github.com/mechatronic3000/QBPool
Untitled Rouge-like https://github.com/mechatronic3000/Untitled-Rougelike
QB Pool https://github.com/mechatronic3000/QBPool