' 2022-04-10 b+ translation of http://sdlbasic.epizy.com/showthread.php?tid=235
' ***************
' Particle System
' ***************
' maximum number of particles slots
' lower this to get more fluent effect
maximum = 100
' Each particle has 6 parameters
' 0 : location on the x-axis
' 1 : location on the y-axis
' 2 : movement on the x-axis
' 3 : movement on the y-axis
' 4 : how old the particle can get
' 5 : period where particle keeps the same color
' ******************************
' Boiler plate for a nice canvas
' ******************************
'setDisplay(700, 400, 32, 1)
f&
= _LoadFont("Arial.ttf", 40) ' everyone has arial?
'setCaption("Particles Example")
'autoback(-2)
'hidemouse
' *************************************************
' pre-cache sin/cos calculations (small speed gain)
' *************************************************
pi = 3.141592654
k = 0
k = k + 1
'initialize text
text_count = 0
t1$ = "Particles Example"
t2$ = "( Cool, isn't it? )"
t3$ = "( useless too )"
t4$ = t2$
sw_text = 0
' *************************************************
' initial particle definition: all particles 'dead'
' *************************************************
particle(i, 4) = 0
'**************************************************
' main loop
'**************************************************
' create new particles
' ********************
' select 7 random particles at each cycle
' play with this value to get the best result on your computer
spawni = rand(maximum)
' if age of the random particle = 0, create one
If particle
(spawni
, 4) = 0 Then
' horizontal location
particle(spawni, 0) = 20
lings = 1
particle(spawni, 0) = 660
lings = 0
' vertical location
particle(spawni, 1) = 350
direction = rand(35) + 157
' horizontal speed vector
particle(spawni, 2) = rand(6)
particle(spawni, 2) = 0 - (rand(6))
' falling speed vector
particle(spawni, 3) = (0 - rand(6)) - 5
' particle age
particle(spawni, 4) = 30 + rand(50)
' color aging
particle(spawni, 5) = particle(spawni, 4) / 5
' update position on ALL live particles
' *************************************
' for all live particles
' calculate the new location
particle(i, 0) = particle(i, 0) + particle(i, 2)
particle(i, 1) = particle(i, 1) + particle(i, 3)
' increase the speed of falling
particle(i, 3) = particle(i, 3) + (rand(100) / 100) / 3
' make the particle older
particle(i, 4) = particle(i, 4) - 1
' if it hits the bottom, make it bounce up
' 0=no bounce, 1= full bounce,no damping
particle(i, 3) = -particle(i, 3) * ((rand(100) / 100) / 2)
' Color determination of the particle
'************************************
' for all live particles
' color it darkred if less than 20% life left
If particle
(i
, 4) < particle
(i
, 5) Then k = darkred
' color it red if more than 20% life left
If particle
(i
, 4) > (particle
(i
, 5)) Then k = red
' color it orange if more than 40% life left
If particle
(i
, 4) > (particle
(i
, 5) * 2) Then k = orange
' color it yellow if more than 60% life left
If particle
(i
, 4) > (particle
(i
, 5) * 3) Then k = yellow
' color it white if more than 80% life left
If particle
(i
, 4) > (particle
(i
, 5) * 4) Then k = white
'----------------------------
' PLOT ALL THE LIVE PARTICLES
'----------------------------
'--- circles ---
'circle(particle(i,0), particle(i,1), 2+rand(4))
'--- filled circles ---
'fillcircle(particle(i,0), particle(i,1), 2+rand(4))
'--- squares ---
'polyline(particle(i,0), particle(i,1), particle(i,0)+6, particle(i,1), particle(i,0)+6, particle(i,1)+6, particle(i,0), particle(i,1)+6, particle(i,0), particle(i,1))
'--- stars ---
'polyline(particle(i,0)+4, particle(i,1), particle(i,0)+8, particle(i,1)+12, particle(i,0), particle(i,1)+4, particle(i,0)+12, particle(i,1)+4, particle(i,0)+2, particle(i,1)+12)
star particle(i, 0), particle(i, 1), 8, 20, 5, 90, k
'--- polygon seems to 'lock-up' ---
'polygon(particle(i,0), particle(i,1), particle(i,0)+6, particle(i,1), particle(i,0)+6, particle(i,1)+6, particle(i,0), particle(i,1)+6, particle(i,0), particle(i,1))
'--- filled squares ---
'bar(particle(i,0), particle(i,1), particle(i,0)+2+rand(4), particle(i,1)+2+rand(4))
'Text 223, 180, 24, t4$, &HFFFF0000
text_count = text_count + 1
t4$ = t3$
sw_text = 0
t4$ = t2$
sw_text = 1
text_count = 0
' x, y are same as for circle,
' rInner is center circle radius
' rOuter is the outer most point of star
' nPoints is the number of points,
' angleOffset = angle offset IN DEGREES, it will be converted to radians in sub
' this is to allow us to spin the polygon of n sides
Dim pAngle
, radAngleOffset
, x1
, y1
, x2
, y2
, x3
, y3
, i
As Long
pAngle
= _D2R(360 / nPoints
): radAngleOffset
= _D2R(angleOffset
) x1
= x
+ rInner
* Cos(radAngleOffset
) y1
= y
+ rInner
* Sin(radAngleOffset
) x2
= x
+ rOuter
* Cos(i
* pAngle
+ radAngleOffset
+ .5 * pAngle
) y2
= y
+ rOuter
* Sin(i
* pAngle
+ radAngleOffset
+ .5 * pAngle
) x3
= x
+ rInner
* Cos((i
+ 1) * pAngle
+ radAngleOffset
) y3
= y
+ rInner
* Sin((i
+ 1) * pAngle
+ radAngleOffset
) ftri x1, y1, x2, y2, x3, y3, K
'triangles leaked
Line (x1
, y1
)-(x2
, y2
), K
Line (x2
, y2
)-(x3
, y3
), K
Line (x3
, y3
)-(x1
, y1
), K
x1 = x3: y1 = y3
'2019-12-16 fix by Steve saves some time with STATIC and saves and restores last dest
_Blend a&
'<<<< new 2019-12-16 fix
'Sub Text (x, y, textHeight, txt$, k As _Unsigned Long)
' Dim fg As _Unsigned Long, cur&, I&, multi, xlen
' fg = _DefaultColor
' 'screen snapshot
' cur& = _Dest
' I& = _NewImage(8 * Len(txt$), 16, 32)
' _Dest I&
' Color k
' _PrintString (0, 0), txt$
' multi = textHeight / 16
' xlen = Len(txt$) * 8 * multi
' _PutImage (x, y)-Step(xlen, textHeight), I&, cur&
' Color fg
' _FreeImage I&
'End Sub