Skip to content

Commit d00126b

Browse files
committed
added option to use fast color add, may improve performance a little
also fixed a bug in fire animation
1 parent 7d6965d commit d00126b

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

wled00/FX.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8387,19 +8387,22 @@ uint16_t mode_particlefire(void)
83878387
// initialize new flame: set properties of source
83888388
// from time to time, chang the flame position
83898389
// make some of the flames small and slow to add a bright base
8390+
8391+
if (random8(40) == 0) //from time to time, change flame position (about once per second)
8392+
{
8393+
if (SEGMENT.check1)
8394+
{ // wrap around in X direction, distribute randomly
8395+
flames[i].source.x = random16(PS_MAX_X);
8396+
}
8397+
else
8398+
{ // no wrapping
8399+
flames[i].source.x = PS_P_RADIUS * 3 + random16(PS_MAX_X - (PS_P_RADIUS * 6)); // distribute randomly but not close to the corners
8400+
}
8401+
}
8402+
83908403
if (i < (numFlames - (cols >> 1)))
83918404
{ // all but the last few are normal flames
8392-
if (random8(40) == 0)
8393-
{
8394-
if (SEGMENT.check1)
8395-
{ // wrap around in X direction, distribute randomly
8396-
flames[i].source.x = random16(PS_MAX_X);
8397-
}
8398-
else
8399-
{ // no wrapping
8400-
flames[i].source.x = PS_P_RADIUS * 3 + random16(PS_MAX_X - (PS_P_RADIUS * 6)); // distribute randomly but not close to the corners
8401-
}
8402-
}
8405+
84038406
flames[i].source.y = -1 * PS_P_RADIUS; // set the source below the frame so particles alredy spread a little when the appear
84048407
flames[i].source.vx = 0; // (rand() % 3) - 1;
84058408
flames[i].source.vy = 0;
@@ -8459,7 +8462,7 @@ uint16_t mode_particlefire(void)
84598462

84608463
return FRAMETIME;
84618464
}
8462-
static const char _data_FX_MODE_PARTICLEFIRE[] PROGMEM = "Particle Fire@Speed,Intensity,Base Flames,Wind Speed, Color Mode, WrapX;;!;012;sx=100,ix=120,c1=16,c2=128,c3=0,o1=0";
8465+
static const char _data_FX_MODE_PARTICLEFIRE[] PROGMEM = "Particle Fire@Speed,Intensity,Base Flames,Wind Speed, Color Scheme, WrapX;;!;012;sx=100,ix=120,c1=16,c2=128,c3=0,o1=0";
84638466
/*syntax for json configuration string:
84648467
@A,B,C,D,E,F,G,H;I,J,K;L;M;N mark commas and semicolons
84658468
A - speed

wled00/FXparticleSystem.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ void Particle_Gravity_update(PSparticle *part, bool wrapX, bool bounceX, bool bo
335335
// render particles to the LED buffer (uses palette to render the 8bit particle color value)
336336
// if wrap is set, particles half out of bounds are rendered to the other side of the matrix
337337
// saturation is color saturation, if not set to 255, hsv instead of palette is used (palette does not support saturation)
338-
void ParticleSys_render(PSparticle *particles, uint16_t numParticles, bool wrapX, bool wrapY)
338+
void ParticleSys_render(PSparticle *particles, uint16_t numParticles, bool wrapX, bool wrapY, bool fastcoloradd)
339339
{
340340

341341
const uint16_t cols = strip.isMatrix ? SEGMENT.virtualWidth() : 1;
@@ -351,6 +351,7 @@ void ParticleSys_render(PSparticle *particles, uint16_t numParticles, bool wrapX
351351
CRGB baseRGB;
352352
uint16_t i;
353353
uint8_t brightess; // particle brightness, fades if dying
354+
354355

355356
// go over particles and update matrix cells on the way
356357
for (i = 0; i < numParticles; i++)
@@ -388,7 +389,7 @@ void ParticleSys_render(PSparticle *particles, uint16_t numParticles, bool wrapX
388389
x--; // shift x to next pixel left, will overflow to 255 if 0
389390
dx = dx + (PS_P_RADIUS >> 1);
390391
}
391-
else // if jump has ocurred, fade pixel out
392+
else // if jump has ocurred
392393
{
393394
dx = dx - (PS_P_RADIUS >> 1); // adjust dx so pixel fades
394395
}
@@ -400,7 +401,6 @@ void ParticleSys_render(PSparticle *particles, uint16_t numParticles, bool wrapX
400401
}
401402
else
402403
{
403-
// adjust dy so pixel fades
404404
dy = dy - (PS_P_RADIUS >> 1);
405405
}
406406

@@ -429,7 +429,7 @@ void ParticleSys_render(PSparticle *particles, uint16_t numParticles, bool wrapX
429429
// calculate the intensity with linear interpolation
430430
intensity = ((uint32_t)((PS_P_RADIUS)-dx) * ((PS_P_RADIUS)-dy) * (uint32_t)brightess) >> PS_P_SURFACE; // divide by PS_P_SURFACE to distribute the energy
431431
// scale the particle base color by the intensity and add it to the pixel
432-
SEGMENT.addPixelColorXY(x, rows - y - 1, baseRGB.scale8(intensity));
432+
SEGMENT.addPixelColorXY(x, rows - y - 1, baseRGB.scale8(intensity), fastcoloradd);
433433
}
434434
// bottom right;
435435
x++;
@@ -441,7 +441,7 @@ void ParticleSys_render(PSparticle *particles, uint16_t numParticles, bool wrapX
441441
if (x < cols && y < rows)
442442
{
443443
intensity = ((uint32_t)dx * ((PS_P_RADIUS)-dy) * (uint32_t)brightess) >> PS_P_SURFACE; // divide by PS_P_SURFACE to distribute the energy
444-
SEGMENT.addPixelColorXY(x, rows - y - 1, baseRGB.scale8(intensity));
444+
SEGMENT.addPixelColorXY(x, rows - y - 1, baseRGB.scale8(intensity), fastcoloradd);
445445
}
446446
// top right
447447
y++;
@@ -453,7 +453,7 @@ void ParticleSys_render(PSparticle *particles, uint16_t numParticles, bool wrapX
453453
if (x < cols && y < rows)
454454
{
455455
intensity = ((uint32_t)dx * dy * (uint32_t)brightess) >> PS_P_SURFACE; // divide by PS_P_SURFACE to distribute the energy
456-
SEGMENT.addPixelColorXY(x, rows - y - 1, baseRGB.scale8(intensity));
456+
SEGMENT.addPixelColorXY(x, rows - y - 1, baseRGB.scale8(intensity), fastcoloradd);
457457
}
458458
// top left
459459
x--;
@@ -467,7 +467,7 @@ void ParticleSys_render(PSparticle *particles, uint16_t numParticles, bool wrapX
467467
if (x < cols && y < rows)
468468
{
469469
intensity = ((uint32_t)((PS_P_RADIUS)-dx) * dy * (uint32_t)brightess) >> PS_P_SURFACE; // divide by PS_P_SURFACE to distribute the energy
470-
SEGMENT.addPixelColorXY(x, rows - y - 1, baseRGB.scale8(intensity));
470+
SEGMENT.addPixelColorXY(x, rows - y - 1, baseRGB.scale8(intensity), fastcoloradd);
471471
}
472472
}
473473
}
@@ -665,7 +665,7 @@ void PartMatrix_addHeat(uint8_t col, uint8_t row, uint16_t heat)
665665
// check if there is heat left over
666666
if (newcolorvalue == 255)
667667
{ // there cannot be a leftover if it is not full
668-
heat = heat - (255 - currentcolor[i]); // heat added is difference from current red value to full red value, subtract it from the inital heat value so heat is the remaining heat not added yet
668+
heat = heat - (255 - currentcolor[i]); // heat added is difference from current value to full value, subtract it from the inital heat value so heat is the remaining heat not added yet
669669
// this cannot produce an underflow since we never add more than the initial heat value
670670
}
671671
else

wled00/FXparticleSystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void Particle_attractor(PSparticle *particle, PSparticle *attractor, uint8_t *co
7474
void Particle_Move_update(PSparticle *part);
7575
void Particle_Bounce_update(PSparticle *part, const uint8_t hardness);
7676
void Particle_Gravity_update(PSparticle *part, bool wrapX, bool bounceX, bool bounceY, const uint8_t hardness);
77-
void ParticleSys_render(PSparticle *particles, uint16_t numParticles, bool wrapX, bool wrapY);
77+
void ParticleSys_render(PSparticle *particles, uint16_t numParticles, bool wrapX, bool wrapY, bool fastcoloradd = false);
7878
void FireParticle_update(PSparticle *part, bool wrapX, bool WrapY);
7979
void ParticleSys_renderParticleFire(PSparticle *particles, uint16_t numParticles, bool wrapX);
8080
void PartMatrix_addHeat(uint8_t col, uint8_t row, uint16_t heat);

0 commit comments

Comments
 (0)