Author Topic: Big Bang with objects  (Read 2858 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
Big Bang with objects
« on: May 01, 2012, 02:39:19 AM »
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

Code: [Select]
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

Peter

  • Guest
Re: Big Bang with objects
« Reply #1 on: May 01, 2012, 06:06:07 AM »
Hi Charles,

This is not BigBang, it is FlyStar.
Origial BigBang is here..

There is a bug in your latest Oxygen.dll. Fmod.inc will not run!
I got a weird error message. "EAX" ???

Code: [Select]
include "win64.inc"
include "fmod.inc"

Window ">_<", 640, 480, 0
Font 12,24,0,"courier"
InitSound
ShowMouse 0

sys anzahl=1000,color,single aSpeed  
sys xscreen=640,yscreen=480, i  
sys xmitte = xscreen/2, ymitte = yscreen/2

Dim x(1000) as single
Dim y(1000) as single

Dim winkel(1000) as single
Dim speed(1000)  as single
Dim sinus(360)   as single
Dim cosinus(360) as single

for i=1 to anzahl
   x(i) = xmitte
   y(i) = ymitte
   winkel(i)= Rand(1,360)
   speed(i) = Rand(1,3)
next

for i=0 to 360
   sinus(i)  = sin(rad(i))
   cosinus(i)= cos(rad(i))
next

Function xMove(single x,winkel,speed) as single
return x + Cosinus(winkel)*speed
End Function

Function yMove(single y,winkel,speed) as single
return y + Sinus(winkel)*speed
End Function

Function Distance(single x1,y1,x2,y2) as single
sys dx, dy
dx = x1-x2: dy = y1-y2
return abs(sqr(dx*dx+dy*dy))
End Function

sys song,snd,devil
devil=LoadIcon "devil.ico"
snd =LoadSound "cosmobumm.wav"
song=LoadMusic "0.mod"
PlayMusic song
PlaySound snd

While Key(27)=0
ClsColor 2,2,2
SetIcon devil,xMouse-32,yMouse-48,64,64,0
for i=0 to anzahl
   aSpeed = Distance (xmitte, ymitte, x(i), y(i)) /200 * speed(i)+1
   x(i) = xMove(x(i), winkel(i), aSpeed)
   y(i) = yMove(y(i), winkel(i), aSpeed)
   if x(i) < 0 Or x(i) > xscreen Or y(i) < 0 Or y(i) > yscreen
      x(i) = xmitte
      y(i) = ymitte
      winkel(i)= Rand(1,360)
      speed(i) = Rand(1,3)
   end if
   color = speed(i)*100 - 55
   RoundBox x(i), y(i),6,6,6,6,color, color, color
next
xmitte = xMouse
ymitte = yMouse
Text 200,8,"END OF THE WORLD",255,255,255
FlipBuffer
WaitFor 80
Wend
FreeSound
FreeMusic song
WinEnd
« Last Edit: May 01, 2012, 06:15:24 AM by peter »

Charles Pegge

  • Guest
Re: Big Bang with objects
« Reply #2 on: May 01, 2012, 11:07:31 AM »
Sorry Peter.

I am pulling the facility for consolidating small string literals. This was causing the problem. Optimisation too far!

Charles

« Last Edit: May 03, 2012, 04:27:51 AM by Charles Pegge »

kryton9

  • Guest
Re: Big Bang with objects
« Reply #3 on: May 03, 2012, 06:59:23 PM »
Cool demos Charles.