Hi!
Here is an attempt at running a "mainloop" for all objects at once, like in python. Interesting for gaming or GUI design. Objects of different types get created and deleted dynamically. It's also a solution to get an abstract class functionnality in GLb, without C++.
Have good fun!
// --------------------------------- //
// Project: TYPE_1 (pure abstract classes in C++ failed)
// Start: Saturday, July 20, 2013
// IDE Version: 10.244
// --------------------------------- //
// Project: TYPE_2
// Start: Tuesday, July 16, 2013
// IDE Version: 10.244
// SETCURRENTDIR("Media") // go to media files
SETSCREEN 500,500,0
SYSTEMPOINTER FALSE
//LIMITFPS 30
GLOBAL oblist%[] AS ob // like a pure abstract class, an array of various objects.
//GLOBAL collist%[] ; DIMDATA collist%[],0xcc66ff,0x00bb55, 0x0055ff,0xcc7777, 0x777777
TYPE ob
shape$ ="sq" // is the TYPE of each object, in fact.
// used later in a "switch" to apply the correct drawing function,
// like a function member of each object in C++.
color% = 0x666666 //default color
val1=0 ; val2=0 ; val3=0 // generic, use varies depending on the type of object.
px ; py ; vx ; vy
// weapons% =0
intelligence% = 4 // (bonus included, haha)
dizziness% =1
bounces%=4 // max. bounces allowed, the deletion criterium.
ENDTYPE
start:
FOR bzzz% = 1 TO 440 ; new_ob() ; NEXT // create n objects
GLOBAL t0%, t% , stamp%
LOCAL ciao%
t0=GETTIMERALL() ; t=t0+2000
mainloop://____run_all_objects_at_once__________________._____________________
WHILE TRUE
FOREACH z IN oblist[]
ciao=animate(z)
IF ciao >0 THEN DELETE z // [death]
//DELETE must be inside a loop, otherwise you need to know
// the (int) position of object z inside the array oblist[]. How do you get that?
// Please post me the answer if you know it.
NEXT
PRINT LEN(oblist[]), 5,5
SHOWSCREEN
stamp=GETTIMERALL() // MUST be GLOBAL apparently, too tricky otherwise.
IF stamp >t
t=t+500+RND(700) //1789+RND(1984)
new_ob() // [birth]
// DRAWRECT 10,10,222,222, RGB(255,255,0) // visualize creation time
ENDIF
WEND
//______________________________________________________._____________________
FUNCTION new_ob:
LOCAL z AS ob // new, inherits default values
// z.color = collist[RND(4)]
z.color = bOR(RND(0xffffff), ASL(0x000000f0,8*RND(2)))
// avoiding dark or unsaturated colors.
IF RND(1) // if shape is sq
z.px =250.0 ; z.py =250.0
z.vx =rnddr2() ; z.vy =rnddr2()
z.val1=6+RND(30) ; z.val2=6+RND(30)
// more t.b.c.
ELSE
z.px =250.0 ; z.py =250.0
z.vx =rnddr2() ; z.vy =rnddr2()
z.shape$="tri" ; z.val1=RND(84)-42
ENDIF
DIMPUSH oblist[], z
ENDFUNCTION
FUNCTION animate: z AS ob
INLINE
//moves, common, C-style:
z.px +=z.vx ; z.py+=z.vy;
if(z.px <50)
{z.vx =rnddr(); z.vy=rnddr2() ; z.px=50; z.bounces-=1 ; if (z.bounces <1) {return(7);}}
if(z.py <50)
{z.vy =rnddr(); z.vx=rnddr2() ; z.py=50; z.bounces-=1 ; if (z.bounces <1) {return(7);}}
if(z.px >450)
{z.vx = -rnddr(); z.vy=rnddr2(); z.px =450; z.bounces-=1 ; if (z.bounces <1) {return(7);}}
if(z.py >450)
{z.vy = -rnddr(); z.vx=rnddr2(); z.py=450; z.bounces-=1 ; if (z.bounces <1) {return(7);}}
ENDINLINE
//switch to apply the right function:
IF z.shape$ = "sq" // "according to the type" like an abstract class.
DRAWRECT z.px,z.py,z.val1,z.val2,z.color
ELSEIF z.shape$="tri"
STARTPOLY -1,1 //no texture, iMode=tri
POLYVECTOR z.px, z.py-z.val1, 0,0, z.color
POLYVECTOR z.px+10, z.py, 0,0, z.color
POLYVECTOR z.px-10, z.py, 0,0, z.color
ENDPOLY
ELSE
// another default type of object.
ENDIF // ...should be usable to make a custom GUI.
RETURN 0 // i.e. "don't delete this element"
ENDFUNCTION
FUNCTION rnddr:
RETURN (RND(3000)+5)/1000.0
ENDFUNCTION
FUNCTION rnddr2:
IF (RND(1)) ; RETURN rnddr()
ELSE
RETURN -rnddr()
ENDIF
ENDFUNCTION