LoginLogin

Def codes for making codes

Root / General / [.]

randoCreated:
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.

CHKPROJECT: Check if the project named P$ exists.
...
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.
It's probably not a good idea to accept [SYS] and [DEFAULT] since none of the other file commands do.

Here is one:
DEF GET(ARRAY, OFFSET)
DIM TEMP[3]
COPY TEMP,  0,  ARRAY, OFFSET,1
RETURN TEMP[0]
END
this 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

May not see why it's useful. get()(once updated) can determine the type of array(string/number) by getting the first element.

...Whatโ€™s the point? Canโ€™t you just do
 ? ARRAY[ELEMENT]

No, that won't work. doing this:
DIM A[20, 20]
?A[20]
Syntax error

May not see why it's useful. get()(once updated) can determine the type of array(string/number) by getting the first element.
How are you going to get the first element if you don't know the type already? You can't use COPY since you have to know the type when creating the temporary array.

It seemed like dfrost was going to run the first (!!) element of the input array through their 67% accuracy typeof command to make a destination array of the correct type...

No, that won't work. doing this:
DIM A[20, 20]
?A[20]
Syntax error
Its not that hard to just use correct syntax.
?ARRAY[ELEMENT,ELEMENT]

No, that won't work. doing this:
DIM A[20, 20]
?A[20]
Syntax error
Its not that hard to just use correct syntax.
?ARRAY[ELEMENT,ELEMENT]
I think the idea is that you can use GET if you don't know how many dimensions the array has.

maybe I can copy it to a string array... you can "stringize" numbers so that they can fit into it While I'm at I might as well post this:
DEF TOSTRING(X)
IF X*0&&X*0==X*0 THEN RETURN X
RETURN STR$(X)
END

DEF DESTROY_ARR A[]
 FOR I%=LEN(A[])-1 TO 0 STEP -1
  TRASH%=POP(A[])
  NEXT
 END
This 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 DESTROY_ARR A[]
 FOR I%=LEN(A[])-1 TO 0 STEP -1
  TRASH%=POP(A[])
  NEXT
 END
This 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 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
END
The 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
END
I 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.

DEF DESTROY_ARR A[]
 FOR I%=LEN(A[])-1 TO 0 STEP -1
  TRASH%=POP(A[])
  NEXT
 END
This 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 DESTORY_ARR A
 WHILE LEN(A)
  IF POP(A) THEN ENDIF
  WEND
 END
Or you could just do this:
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
END
Do you know what this means? a new ARYOP function for FREE

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
END
Do you know what this means? a new ARYOP function for FREE
But with less speed.
Or you could just do this:
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.
In my case that's perfect. I've only needed to clear numerical arrays so far.

i know there has to be a way to create faster graphics(like a custom GPU). How come none of us compile our knowledge into the same project?