Skip to content

Commit 886ea5f

Browse files
committed
GoL - Palette Switching
Recolors all live cells on palette/color change. Before you needed a game restart or mutation over time.
1 parent 631aa35 commit 886ea5f

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

wled00/FX.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5169,14 +5169,16 @@ uint16_t mode_2Dgameoflife(void) { // Written by Ewoud Wijma, inspired by https:
51695169
const uint16_t cols = SEGMENT.virtualWidth();
51705170
const uint16_t rows = SEGMENT.virtualHeight();
51715171
const size_t dataSize = ((SEGMENT.length() + 7) / 8); // round up to nearest byte
5172-
const size_t totalSize = dataSize*2 + sizeof(uint8_t) + sizeof(uint16_t)*2; // 2 byte arrays, 1 uint8_t, 2 uint16_t
5172+
const size_t detectionSize = sizeof(uint8_t) + sizeof(uint16_t)*2; // 1 uint8_t (gliderLen), 2 uint16_t (2 CRCs)
5173+
const size_t totalSize = dataSize * 2 + detectionSize + sizeof(CRGB); //CRGB prevColor
51735174

51745175
if (!SEGENV.allocateData(totalSize)) return mode_static(); //allocation failed
51755176
byte *cells = reinterpret_cast<byte*>(SEGENV.data);
51765177
byte *futureCells = reinterpret_cast<byte*>(SEGENV.data + dataSize);
51775178
uint8_t *gliderLength = reinterpret_cast<uint8_t*>(SEGENV.data + dataSize*2);
51785179
uint16_t *oscillatorCRC = reinterpret_cast<uint16_t*>(SEGENV.data + dataSize*2 + sizeof(uint8_t));
51795180
uint16_t *spaceshipCRC = reinterpret_cast<uint16_t*>(SEGENV.data + dataSize*2 + sizeof(uint8_t) + sizeof(uint16_t));
5181+
CRGB *prevColor = reinterpret_cast<CRGB*>(SEGENV.data + dataSize*2 + detectionSize);
51805182

51815183
uint16_t &generation = SEGENV.aux0; //rename aux0 readability (not needed)
51825184
CRGB bgColor = SEGCOLOR(1);
@@ -5235,6 +5237,17 @@ uint16_t mode_2Dgameoflife(void) { // Written by Ewoud Wijma, inspired by https:
52355237
if (SEGENV.step > strip.now || strip.now - SEGENV.step < FRAMETIME_FIXED * (uint32_t)map(SEGMENT.speed,0,255,64,2)) {
52365238
return FRAMETIME; //skip if not enough time has passed
52375239
}
5240+
5241+
//Recolor live cells if palette/color changed
5242+
if (SEGMENT.color_from_palette(0, false, PALETTE_SOLID_WRAP, 0) != *prevColor){
5243+
for (int x = 0; x < cols; x++) for (int y = 0; y < rows; y++) {
5244+
if (getBitValue(cells, y * cols + x)) {
5245+
SEGMENT.setPixelColorXY(x,y, SEGMENT.color_from_palette(random8(), false, PALETTE_SOLID_WRAP, 0));
5246+
}
5247+
}
5248+
*prevColor = SEGMENT.color_from_palette(0, false, PALETTE_SOLID_WRAP, 0);
5249+
}
5250+
52385251
//Update Game of Life
52395252
bool cellChanged = false; // Detect still live and dead grids
52405253
//cell index and coordinates

0 commit comments

Comments
 (0)