Dynamic arrays using OOP or procedural ?

Started by Zlatko Vid, May 29, 2021, 09:29:09 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Zlatko Vid

this is question for Charles ...
and i hope that he will look at it when he can .
from old forum topic :::
QuoteIn 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.

My question will be :
Charles ..can i on similar way using this method (OOP) create array in my interpreter ,,for example ?

Brian Alvarez

 Well, im not charles, but Yes, it is possible to do that with a class.

I would still like something done by the hands of Charles, he knows more about Oxygen Basic
and there is less chance to make a mistake (considering i had to go trial and error).


Charles Pegge

Hi Aurel and Brian,

I have reworked BigBang.o2bas to use 2  classes: particles and particle . It belongs in ProjectsA\Particles\


include "..\GDIWindow\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



'=============
class Particle
'=============
 
  single x,y,xv,yv,diam
  sys    sz
  byte   red,green,blue,alpha

  method rebirth()
  ================
  'create particle characteristics with random values
  single r,a
  x     = xmitte           'x position
  y     = ymitte           'y position
  a     = rand(1,628)*.01  'travel angle
  r     = rand(20,100)*.1  'radius
  xv    = r*cos(a)         'x velocity
  yv    = r*sin(a)         'y velocity
  diam  = rand(10,25)*.1   'diameter
  red   = rand(40,250)     'color component red
  green = rand(40,250)     'color component green
  blue  = rand(20,250)     'color component blue
  end method

  method outside() as sys
  =======================
  if x<0 or y<0 or x>=xscreen or y>=yscreen
    return 1
  endif
  end method

  method move()
  =============
  single adist,a
  adist=hypot(xmitte-x,ymitte-y)
  a=adist*.002+.1
  x += xv*a
  y += yv*a
  sz=adist*diam*.015+2
  end method

  method show()
  =============
  Oval x, y, sz, sz, red, green, blue
  end method

end class

'==============
class Particles
'==============
 
  particle*part
  string sbuf
  int qty

  method index(sys i)
  ===================
  @part=strptr(sbuf)+i*sizeof particle
  end method

  method populate(sys e)
  ======================
  sys i
  sbuf=nuls e*sizeof particle
  @part=strptr sbuf
  for i=0 to <e
    part.rebirth
    @part+=sizeof particle 'stride
  next
  qty=e
  end method

  method action()
  ===============
  sys i
  @part=strptr sbuf
  for i=0 to <qty
    part.move
    if part.outside then
      part.rebirth
    else
      part.show
    end if
    @part+=sizeof particle 'stride
  next
  end method

end class



particles pa
pa.populate anzahl

'PlayWav "..\GDIWindow\cosmobumm.wav",0,1


While Key(27)=0
  ClsColor 2,2,40
  pa.action
  '
  Events
  FlipBuffer
  WaitFPS 60
Wend
WinEnd


Zlatko Vid

Hi Charles
My main question is how is this fast compare to ordinary array accses?

Charles Pegge

I have not measured it but it is less machine code for the CPU and often simplifies the source code.


Eduardo Jorge


Zlatko Vid

Hi Eduardo

I really don't know what might be new ...
only Charles can say that .