I made a multi sprite entity creation, deletion, and rendering group of DEFs and I'd chuck them into a spoiler here but they are far from complete, a bit messy, and need performance enhancement.
Def codes for making codes
randoCreated:
CHKPROJECT: Check if the project named P$ exists.It's probably not a good idea to accept [SYS] and [DEFAULT] since none of the other file commands do....This is probably the simplest way to do this check, but it's the most effective. If you give it a name that can't possibly be a valid project name it'll just tell you false, anyway.
Here is one:
DEF GET(ARRAY, OFFSET) DIM TEMP[3] COPY TEMP, 0, ARRAY, OFFSET,1 RETURN TEMP[0] ENDthis command can get any element of an array regardless of size, number of dimensions, (and maybe type) I'm pretty sure that it's a type mismatch this could REALLY be useful in making a TYPEOF() function
...Whatโs the point? Canโt you just do
? ARRAY[ELEMENT]
No, that won't work. doing this:Its not that hard to just use correct syntax.DIM A[20, 20] ?A[20]Syntax error
?ARRAY[ELEMENT,ELEMENT]
DEF DESTROY_ARR A[] FOR I%=LEN(A[])-1 TO 0 STEP -1 TRASH%=POP(A[]) NEXT ENDThis is a simple function which removes all elements from a single-dimension array. The FOR loop is "backwards" to help improve program speed when worked with large arrays in complex loops.
Here's a slightly simpler version:DEF DESTROY_ARR A[] FOR I%=LEN(A[])-1 TO 0 STEP -1 TRASH%=POP(A[]) NEXT ENDThis is a simple function which removes all elements from a single-dimension array. The FOR loop is "backwards" to help improve program speed when worked with large arrays in complex loops.
DEF DESTORY_ARR A WHILE LEN(A) IF POP(A) THEN ENDIF WEND END
Yeah that one's better.
Yes, these are a bit higher-level functions, but considering all the debugging you'll go though to implement 32-bit-sensitive algorithms, I just saved you weeks of debugging.
Took me a year and a half to figure this out in SmileBASIC.
'BOUND A NUMBER INTO 32 BITS (FROM -2147483648 TO 2147483647) DEF BOUND(N) WHILE N>2147483647 N=N-4294967296 WEND WHILE N<-2147483648 N=N+4294967296 WEND RETURN N END 'USING >> WILL ADD 1S TO THE LEFT SIDE OF NEGATIVE NUMBERS (WHICH TAKES FOREVER TO DEBUG) 'ZERO-FILLING RIGHT SHIFT, AKA THE >>> OPERATOR IN JAVASCRIPT DEF U_RSHIFT(A,B) A=BOUND(A) IF A<0 THEN A=A>>1 AND 2147483647 OR &H40000000 RETURN A>>B-1 ELSE RETURN A>>B END 'FINALLY A BITWISE ROTATION SHIFT IN SMILEBASIC! DEF ROTR32(A,B) A=BOUND(A) RETURN U_RSHIFT(A,B) OR A<<32-B ENDThe leftmost bit of every number determines if its positive (0) or negative (1). So when you do bit operations on negative numbers, the results go crazy spawning a bunch of ones on the left side, this helps you manage that leftmost bit to make use of the full 32 bits. In most situations, negative inputs now return positive outputs (as expected), unless if another one fills up the left bit.
I didn't realize SmileBASIC actually uses saturating math when going from floats to integers, so it was more difficult than expected, but I managed to improve the above functions a bit:
VAR I32=2147483648 VAR U32=4294967296 DEF BOUND(N) RETURN N-FLOOR((N+I32)/U32)*U32 END DEF U_RSHIFT(A,B) A=BOUND(A) RETURN A>>B AND NOT (-1<<32-B) END 'Unchanged from original DEF ROTR32(A,B) A=BOUND(A) RETURN U_RSHIFT(A,B) OR A<<32-B ENDI think personally I'd stick to using integer variables as much as possible, and have BOUND(A) be something explicitly done when passing numbers in only when needed, rather than have it always be run in U_RSHIFT and ROTR32, but that's just my personal taste.
Or you could just do this:Here's a slightly simpler version:DEF DESTROY_ARR A[] FOR I%=LEN(A[])-1 TO 0 STEP -1 TRASH%=POP(A[]) NEXT ENDThis is a simple function which removes all elements from a single-dimension array. The FOR loop is "backwards" to help improve program speed when worked with large arrays in complex loops.DEF DESTORY_ARR A WHILE LEN(A) IF POP(A) THEN ENDIF WEND END
DEF FREE() DIM NEW[0] RETURN NEW END FREE OUT ARRAY 'or ARRAY=FREE()The garbage collector will free the memory occupied by the array once there are no variables that point to it. Keep in mind that this can change the type of the array so if you want to use the array again you'll need different FREE functions for each type.
Here is a bunch of random code:
DEF VOID A: END DEF BLANKARRAY() DIM A[0] RETURN A END DIM BLANKARRAY$() DIM A$[0] RETURN A$ DEF GET(ARRAY, INDEX) DIM TEMP[3] COPY TEMP,ARRAY,INDEX,1 RETURN TEMP[0] END DEF SET ARRAY, INDEX,VALUE FILL ARRAY,VALUE,INDEX,1 END DEF ForEach ARRAY, FUNCTION VAR I,L = LEN(ARRAY) - 1 FOR I = 0 TO L CALL FUNCTION, GET(ARRAY,I),I NEXT END DEF INCARR ARRAY, ITEM, INDEX, STEP SET ARRAY, INDEX,INDEX*STEP ENDDo you know what this means? a new ARYOP function for FREE
Here is a bunch of random code:But with less speed.DEF VOID A: END DEF BLANKARRAY() DIM A[0] RETURN A END DIM BLANKARRAY$() DIM A$[0] RETURN A$ DEF GET(ARRAY, INDEX) DIM TEMP[3] COPY TEMP,ARRAY,INDEX,1 RETURN TEMP[0] END DEF SET ARRAY, INDEX,VALUE FILL ARRAY,VALUE,INDEX,1 END DEF ForEach ARRAY, FUNCTION VAR I,L = LEN(ARRAY) - 1 FOR I = 0 TO L CALL FUNCTION, GET(ARRAY,I),I NEXT END DEF INCARR ARRAY, ITEM, INDEX, STEP SET ARRAY, INDEX,INDEX*STEP ENDDo you know what this means? a new ARYOP function for FREE
Or you could just do this:In my case that's perfect. I've only needed to clear numerical arrays so far.DEF FREE() DIM NEW[0] RETURN NEW END FREE OUT ARRAY 'or ARRAY=FREE()The garbage collector will free the memory occupied by the array once there are no variables that point to it. Keep in mind that this can change the type of the array so if you want to use the array again you'll need different FREE functions for each type.