From 5603e1cd09bf2b59c2cabcbef6ad93381ec1ea51 Mon Sep 17 00:00:00 2001
From: Jan Grewe <jan@faked.org>
Date: Tue, 30 May 2023 03:40:37 +0200
Subject: [PATCH] add end screen

---
 src/display.cpp    | 24 +++++++++++++++++++++++-
 src/display.h      |  2 ++
 src/gameoflife.cpp | 25 ++++++++++++++++++-------
 src/gameoflife.h   |  2 ++
 src/main.cpp       |  1 +
 5 files changed, 46 insertions(+), 8 deletions(-)

diff --git a/src/display.cpp b/src/display.cpp
index c627dab..54efa26 100644
--- a/src/display.cpp
+++ b/src/display.cpp
@@ -10,18 +10,21 @@ const uint8_t kDmaBufferRows = 2;                             // known working:
 const uint8_t kPanelType = SMARTMATRIX_HUB75_64ROW_MOD32SCAN; // use SMARTMATRIX_HUB75_16ROW_MOD8SCAN for common 16x32 panels
 const uint8_t kMatrixOptions = (SMARTMATRIX_OPTIONS_NONE);    // see http://docs.pixelmatix.com/SmartMatrix for options
 const uint8_t kBackgroundLayerOptions = (SM_BACKGROUND_OPTIONS_NONE);
+const uint8_t kIndexedLayerOptions = (SM_INDEXED_OPTIONS_NONE);
 
-const int defaultBrightness = (10 * 255) / 100;
+const int defaultBrightness = (brightnessPercent * 255) / 100;
 
 rgb24 colorWhite = {0xff, 0xff, 0xff};
 rgb24 colorBlack = {0x00, 0x00, 0x00};
 
 SMARTMATRIX_ALLOCATE_BUFFERS(matrix, kMatrixWidth, kMatrixHeight, kRefreshDepth, kDmaBufferRows, kPanelType, kMatrixOptions);
 SMARTMATRIX_ALLOCATE_BACKGROUND_LAYER(backgroundLayer, kMatrixWidth, kMatrixHeight, COLOR_DEPTH, kBackgroundLayerOptions);
+SMARTMATRIX_ALLOCATE_INDEXED_LAYER(indexedLayer, kMatrixWidth, kMatrixHeight, COLOR_DEPTH, kIndexedLayerOptions);
 
 void setupDisplay()
 {
   matrix.addLayer(&backgroundLayer);
+  matrix.addLayer(&indexedLayer);
   matrix.begin();
   matrix.setBrightness(defaultBrightness);
   backgroundLayer.enableColorCorrection(true);
@@ -42,6 +45,25 @@ void displayLoop()
   }
 }
 
+void fadeOut(int ticks)
+{
+  const uint transitionTime = 5000;
+  unsigned long currentMillis = millis();
+
+  while (millis() - currentMillis < transitionTime)
+  {
+    char evolutions[16];
+    sprintf(evolutions, "Evolutions: %d", ticks);
+    char msg[] = "Life has ended";
+    indexedLayer.setFont(font3x5);
+    indexedLayer.drawString((matrix.getScreenWidth() / 2) - (strlen(msg) * 4) / 2, (matrix.getScreenHeight() / 2) - 2 - 5 , 1, msg);
+    indexedLayer.drawString((matrix.getScreenWidth() / 2) - (strlen(evolutions) * 4) / 2, (matrix.getScreenHeight() / 2) + 2, 1, evolutions);
+    indexedLayer.swapBuffers();
+    indexedLayer.fillScreen(0);
+  }
+  indexedLayer.swapBuffers();
+}
+
 void clearDisplay()
 {
   backgroundLayer.fillScreen({0, 0, 0});
diff --git a/src/display.h b/src/display.h
index 777b26d..ffb9579 100644
--- a/src/display.h
+++ b/src/display.h
@@ -6,9 +6,11 @@
 #include <SmartMatrix.h>
 
 extern bool runGame;
+extern int brightnessPercent;
 
 void setupDisplay();
 void displayLoop();
+void fadeOut(int ticks);
 void clearDisplay();
 
 #endif
diff --git a/src/gameoflife.cpp b/src/gameoflife.cpp
index 67c70ab..a9c7554 100644
--- a/src/gameoflife.cpp
+++ b/src/gameoflife.cpp
@@ -15,14 +15,14 @@ int noEvolutionTicks;
 
 void setupGameOfLife()
 {
-  randomSeed(analogRead(34));
-  createRandomMatrix(g);
-  for (int i = 0; i < 10; i++)
-    addGlider(random(SCREEN_HEIGHT), random(SCREEN_WIDTH), g);
-  clearDisplay();
   currentTick = 0;
   cellsAliveBefore = 0;
   noEvolutionTicks = 0;
+  randomSeed(analogRead(34));
+  createRandomMatrix(g);
+  for (int i = 0; i < 10; i++) {
+    addGlider(random(SCREEN_HEIGHT), random(SCREEN_WIDTH), g);
+  }
 }
 
 void createRandomMatrix(int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH])
@@ -97,7 +97,7 @@ void gameOfLife(int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH])
 
   if (noEvolutionTicks > noEvolutionTicksLimit)
   {
-    setupGameOfLife();
+    endGame();
   }
 }
 
@@ -118,6 +118,17 @@ void addGlider(int i1, int j1, int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH])
 
 void gameLoop()
 {
-  if (runGame)
+  if (runGame) {
     gameOfLife(g);
+  }
+}
+
+void endGame() {
+  fadeOut(currentTick - noEvolutionTicksLimit);
+  resetGame();
+}
+
+void resetGame() {
+  clearDisplay();
+  setupGameOfLife();
 }
diff --git a/src/gameoflife.h b/src/gameoflife.h
index 3b84e76..cc7b9fa 100644
--- a/src/gameoflife.h
+++ b/src/gameoflife.h
@@ -17,5 +17,7 @@ void createRandomMatrix(int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]);
 void gameOfLife(int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]);
 void addGlider(int i1, int j1, int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]);
 void gameLoop();
+void endGame();
+void resetGame();
 
 #endif
diff --git a/src/main.cpp b/src/main.cpp
index d30ed7a..02936ab 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -5,6 +5,7 @@
 #include "gameoflife.h"
 
 int noEvolutionTicksLimit = 100;
+int brightnessPercent = 20;
 
 void setup()
 {
-- 
GitLab