Ahhh, you sir are a genius! It seems so obvious now. Now I peak at 320 fps. (my cpu is also taxed at 90% for 5 days straight now running decryption) Oops, back down to 280fps after making some other changes.
Particles will grow after colliding, the smaller one is destroyed. Each particle contains its own color. I've also attempted to fade the particles out as they near the edge of the screen, but there still seems to be some flicker right as they go off the edge.
REM ***********************************************
REM Title: Gamespace
REM Author: Phaelax
REM Downloaded from: http://dbcc.zimnox.com/
REM ***********************************************
#CONSTANT SCREEN_WIDTH = 1280
#CONSTANT SCREEN_HEIGHT = 720
#CONSTANT PARTICLE_COUNT = 100
#CONSTANT BOUNDARY_THRESHOLD = 50.0
SetWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT, 0 )
setVirtualResolution(SCREEN_WIDTH, SCREEN_HEIGHT)
SetSyncRate(0, 0)
// Coin properties
Type ParticleUDT
x as float
y as float
dx as float
dy as float
alpha as float
speed as float
size as float
process as integer
baseColor as integer
color as integer
EndType
// particle array
global particles as ParticleUDT[PARTICLE_COUNT]
for i = 0 to PARTICLE_COUNT-1
particles[i].x = random(0, SCREEN_WIDTH)
particles[i].y = random(0, SCREEN_HEIGHT)
a = random(0, 360)
particles[i].dx = cos(a)
particles[i].dy = sin(a)
particles[i].speed = random(1,6) / 10.0
particles[i].size = 2
//particles[i].baseColor = makeColor(random(64,255),random(64,255),random(64,255))
if random(0,1) = 1
particles[i].baseColor = makeColor(216,9,219)
else
particles[i].baseColor = makeColor(5,151,230)
endif
next i
// particle movement speed
speed as float = 0.4
// Foreground and background colors need components specified
// to mimic alpha blending through linear interpolation of the
// two colors
// background color
br = 3
bg = 1
bb = 38
setClearColor(br, bg, bb)
white = makeColor(255,255,255)
repeat
lineCount = 0
// Loop through all coins
for i = 0 to PARTICLE_COUNT-2
particles[i].x = particles[i].x + particles[i].dx*particles[i].speed
particles[i].y = particles[i].y + particles[i].dy*particles[i].speed
// If particle is near edge of screen, fade it out
w# = 1
if particles[i].x <= BOUNDARY_THRESHOLD then w# = 1.0 - particles[i].x / BOUNDARY_THRESHOLD
if particles[i].x >= SCREEN_WIDTH - BOUNDARY_THRESHOLD then w# = (particles[i].x - (SCREEN_WIDTH - BOUNDARY_THRESHOLD)) / BOUNDARY_THRESHOLD
if particles[i].y <= BOUNDARY_THRESHOLD then w# = min(w#, 1.0 - particles[i].y / BOUNDARY_THRESHOLD)
if particles[i].y >= SCREEN_HEIGHT - BOUNDARY_THRESHOLD then w# = min(w#, (particles[i].y - (SCREEN_HEIGHT - BOUNDARY_THRESHOLD)) / BOUNDARY_THRESHOLD)
if w# < 0 then w# = 0
if w# < 1
pr = GetColorRed(particles[i].baseColor)
pg = GetColorGreen(particles[i].baseColor)
pb = GetColorBlue(particles[i].baseColor)
r = pr + (br-pr)*w#
g = pg + (bg-pg)*w#
b = pb + (bb-pb)*w#
particles[i].color = makeColor(r,g,b)
else
particles[i].color = particles[i].baseColor
endif
drawEllipse(particles[i].x, particles[i].y, particles[i].size, particles[i].size, particles[i].color, particles[i].color, 1)
if getRawKeyState(32) = 1 then drawEllipse(particles[i].x, particles[i].y, particles[i].size, particles[i].size, white, white, 0)
killParticle = 0
// Connect particles to neighbors
for j = i+1 to PARTICLE_COUNT-1
if i <> j and particles[j].process <> processFlag
// Join particles if within 150px
d = (particles[i].x - particles[j].x) ^ 2 + (particles[i].y - particles[j].y)^2
if d <= 22500
// Merge particles if collide
if d <= (particles[i].size + particles[j].size)^2
if particles[i].size > particles[j].size
particles[i].size = particles[i].size + 2
killParticle(particles[j])
else
particles[j].size = particles[j].size + 2
killParticle(particles[i])
endif
//exit
else
// Fade the line between particles.
// Gets brighter the closer they are.
t# = d / 22500.0
if t# > 1 then t# = 1
if t# < 0 then t# = 0
drawParticleJoint(particles[i], particles[j], t#, br, bg, bb)
inc lineCount
endif
endif
endif
next j
particles[i].process = processFlag
if (particles[i].x < 0 or particles[i].x > SCREEN_WIDTH or particles[i].y < 0 or particles[i].y > SCREEN_HEIGHT)
killParticle(particles[i])
endif
next i
processFlag = 1 - processFlag
print(screenfps())
//print("Lines: "+str(lineCount))
sync()
until getRawKeyPressed(27) = 1 // Press ESC to exit
end
function killParticle(p ref as ParticleUDT)
// If 1, create particle on left or right edge of screen
if random(0,1) = 1
p.x = random(0, 1) * SCREEN_WIDTH
p.y = random(1, SCREEN_HEIGHT)
else
// create particle on top or bottom of screen
p.x = random(1, SCREEN_WIDTH)
p.y = random(0, 1) * SCREEN_HEIGHT
endif
a = random(0, 360)
p.dx = cos(a)
p.dy = sin(a)
p.speed = random(1,6) / 10.0
p.size = 2
//p.baseColor = makeColor(random(64,255),random(64,255),random(64,255))
endfunction
function min(a as float, b as float)
if a < b then exitfunction a
endfunction b
function drawParticleJoint(ap as ParticleUDT, bp as ParticleUDT, t as float, br as integer, bg as integer, bb as integer)
ir = GetColorRed(ap.color)
ig = GetColorGreen(ap.color)
ib = GetColorBlue(ap.color)
jr = GetColorRed(bp.color)
jg = GetColorGreen(bp.color)
jb = GetColorBlue(bp.color)
iar = ir + (br-ir)*t
iag = ig + (bg-ig)*t
iab = ib + (bb-ib)*t
jar = jr + (br-jr)*t
jag = jg + (bg-jg)*t
jab = jb + (bb-jb)*t
ic = makeColor(iar, iag, iab)
jc = makeColor(jar, jag, jab)
drawLine(ap.x, ap.y, bp.x, bp.y, ic, jc)
endfunction