Skip to content

Commit 7657c90

Browse files
committed
GoL - Uses cIndex more consistently
1 parent aea68df commit 7657c90

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

wled00/FX.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5186,6 +5186,7 @@ uint16_t mode_2Dgameoflife(void) { // Written by Ewoud Wijma, inspired by https:
51865186
uint16_t &generation = SEGENV.aux0; //rename aux0 readability (not needed)
51875187
CRGB bgColor = SEGCOLOR(1);
51885188
CRGB color;
5189+
uint16_t cIndex;
51895190

51905191
if (SEGENV.call == 0) {
51915192
SEGMENT.setUpLeds();
@@ -5198,15 +5199,16 @@ uint16_t mode_2Dgameoflife(void) { // Written by Ewoud Wijma, inspired by https:
51985199
random16_set_seed(strip.now>>2); //seed the random generator
51995200
//Setup Grid
52005201
for (int x = 0; x < cols; x++) for (int y = 0; y < rows; y++) {
5202+
cIndex = y * cols + x;
52015203
if (random8() < 82) { // ~32% chance of being alive
5202-
setBitValue(cells, y * cols + x, true);
5203-
setBitValue(futureCells, y * cols + x, true);
5204+
setBitValue(cells, cIndex, true);
5205+
setBitValue(futureCells, cIndex, true);
52045206
color = SEGMENT.color_from_palette(random8(), false, PALETTE_SOLID_WRAP, 0);
52055207
SEGMENT.setPixelColorXY(x,y,!SEGMENT.check1?color : RGBW32(color.r, color.g, color.b, 0));
52065208
}
52075209
else {
5208-
setBitValue(cells, y * cols + x, false);
5209-
setBitValue(futureCells, y * cols + x, false);
5210+
setBitValue(cells, cIndex, false);
5211+
setBitValue(futureCells, cIndex, false);
52105212
if (SEGMENT.check2) continue; //overlay
52115213
SEGMENT.setPixelColorXY(x,y, !SEGMENT.check1?bgColor : RGBW32(bgColor.r, bgColor.g, bgColor.b, 0));
52125214
}
@@ -5231,7 +5233,8 @@ uint16_t mode_2Dgameoflife(void) { // Written by Ewoud Wijma, inspired by https:
52315233
if (SEGMENT.check2) {
52325234
for (int x = 0; x < cols; x++) for (int y = 0; y < rows; y++) {
52335235
//redraw foreground/alive
5234-
if (getBitValue(cells, y * cols + x)) {
5236+
cIndex = y * cols + x;
5237+
if (getBitValue(cells, cIndex)) {
52355238
color = SEGMENT.getPixelColorXY(x,y);
52365239
SEGMENT.setPixelColorXY(x,y, !SEGMENT.check1?color : RGBW32(color.r, color.g, color.b, 0));
52375240
}
@@ -5244,7 +5247,8 @@ uint16_t mode_2Dgameoflife(void) { // Written by Ewoud Wijma, inspired by https:
52445247
//Recolor live cells if palette/color changed
52455248
if (SEGMENT.color_from_palette(0, false, PALETTE_SOLID_WRAP, 0) != *prevColor){
52465249
for (int x = 0; x < cols; x++) for (int y = 0; y < rows; y++) {
5247-
if (getBitValue(cells, y * cols + x)) {
5250+
cIndex = y * cols + x;
5251+
if (getBitValue(cells, cIndex)) {
52485252
SEGMENT.setPixelColorXY(x,y, SEGMENT.color_from_palette(random8(), false, PALETTE_SOLID_WRAP, 0));
52495253
}
52505254
}
@@ -5253,8 +5257,7 @@ uint16_t mode_2Dgameoflife(void) { // Written by Ewoud Wijma, inspired by https:
52535257

52545258
//Update Game of Life
52555259
bool cellChanged = false; // Detect still live and dead grids
5256-
//cell index and coordinates
5257-
uint16_t cIndex;
5260+
//cell coordinates
52585261
uint16_t cX;
52595262
uint16_t cY;
52605263
//Loop through all cells. Count neighbors, apply rules, setPixel
@@ -5273,7 +5276,7 @@ uint16_t mode_2Dgameoflife(void) { // Written by Ewoud Wijma, inspired by https:
52735276
cX = (x+i+cols) % cols;
52745277
cY = (y+j+rows) % rows;
52755278
}
5276-
cIndex = cY * cols + cX;
5279+
cIndex = cY * cols + cX; //neighbor cell index
52775280
// count neighbors and store upto 3 neighbor colors
52785281
if (getBitValue(cells, cIndex)) { //if alive
52795282
neighbors++;
@@ -5286,17 +5289,18 @@ uint16_t mode_2Dgameoflife(void) { // Written by Ewoud Wijma, inspired by https:
52865289
}
52875290

52885291
// Rules of Life
5289-
bool cellValue = getBitValue(cells, y * cols + x);
5292+
cIndex = y * cols + x; //current cell index
5293+
bool cellValue = getBitValue(cells, cIndex);
52905294
if ((cellValue) && (neighbors < 2 || neighbors > 3)) {
52915295
// Loneliness or overpopulation
52925296
cellChanged = true;
5293-
setBitValue(futureCells, y * cols + x, false);
5297+
setBitValue(futureCells, cIndex, false);
52945298
// blur/turn off dying cells
52955299
if (!SEGMENT.check2) SEGMENT.setPixelColorXY(x,y, blend(SEGMENT.getPixelColorXY(x,y), bgColor, map(SEGMENT.custom1, 0, 255, 255, 0)));
52965300
}
52975301
else if (!(cellValue) && (neighbors == 3)) {
52985302
// Reproduction
5299-
setBitValue(futureCells, y * cols + x, true);
5303+
setBitValue(futureCells, cIndex, true);
53005304
cellChanged = true;
53015305
// find dominant color and assign it to a cell
53025306
// no longer storing colors, if parent dies the color is lost

0 commit comments

Comments
 (0)