In the latest Oxygen, I have included four versions of Big Bang, starting with procedural code and ending up with dynamic OOP.
Space for the set of particles is dynamically allocated, and array indexes are not required to access the properties of each particle. This makes the code considerably cleaner and more efficient.
Garbage collection is also automatic because standard string space is used to contain the particles.
projects/GDIWindow/BigBang4.o2bas
include "window.inc"
Window "Big Bang", 640, 480, 2
sys xscreen =640, yscreen =480, i, sz
'Font 12,24,0,"courier"
sys anzahl=1000
single xmitte = xscreen/2, ymitte = yscreen/2
'=================
'SINE/COSINE TABLE
'=================
indexbase 0
single sinus[360],cosinus[360]
double st=pi/180
'
for i=0 to <360
sinus(i) = sin(st*i)
cosinus(i)= cos(st*i)
next
'=============
class Particle
'=============
single x,y,diam,speed
sys w,sz
byte red,green,blue,alpha
sys pob
string sbuf
method index(sys i)
pob=strptr(sbuf)+i*sizeof particle
end method
method populate(sys e)
sys i,b
sbuf=nuls e*sizeof particle
pob=strptr sbuf
for i=0 to <e
rebirth
pob+=sizeof particle
next
end method
method rebirth()
@this = pob
x = xmitte
y = ymitte
w = rand(0,359)
speed = rand(1,100)*.1
diam = rand(10,25)*.1
red = rand(40,250)
green = rand(40,250)
blue = rand(40,250)
end method
method outside() as sys
@this=pob
if x<0 or y<0 or x>=xscreen or y>=yscreen then return 1
end method
method move()
@this=pob
single aSpeed, adist
adist=hypot(xmitte-x,ymitte-y)
aspeed=adist*speed*.002+1
x += cosinus(w)*aspeed
y += sinus(w)*aspeed
sz=1+adist*diam*.015
end method
method show()
@this=pob
Oval x, y, sz, sz, red, green, blue
end method
end class
particle pa
pa.populate anzahl
PlayWav "cosmobumm.wav",0,1
While Key(27)=0
ClsColor 2,2,2
for i=0 to <anzahl
pa.index i
pa.move
if pa.outside then
pa.rebirth
else
pa.show
end if
next
'
Events
FlipBuffer
WaitFPS 80
Wend
WinEnd
Charles