From f0836c66f026a05cfd1fcbf5bcfdd73b1e81598e Mon Sep 17 00:00:00 2001 From: Felix Pankratz Date: Fri, 13 Dec 2024 19:22:44 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=A0=20create=20a=20vector=20so=20we=20?= =?UTF-8?q?can=20spawn=20new=20drops=20dynamically=20:3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nametag/src/rain.cpp | 63 ++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/nametag/src/rain.cpp b/nametag/src/rain.cpp index 39ce268..bb920dc 100644 --- a/nametag/src/rain.cpp +++ b/nametag/src/rain.cpp @@ -1,6 +1,7 @@ #include "rain.h" #include "constants.h" #include "overlay.h" +#include #define COLOR_HIGHLIGHT_R 128 #define COLOR_HIGHLIGHT_G 255 @@ -10,19 +11,21 @@ #define COLOR_DEFAULT_G 255 #define COLOR_DEFAULT_B 0 -typedef struct { - bool reached_bottom = false; +//typedef struct { +struct Raindrop { + //bool reached_bottom = false; + bool reached_bottom; + int x; int y; int length; int speed; unsigned long last_moved; -} Raindrop; +} ; -// TODO: Use a dynamic vector instead -Raindrop raindrops[PANEL_WIDTH]; bool rain_initialized = false; uint16_t color_highlight; uint16_t color_default; +std::vector raindrops; void setup_rain(MatrixPanel_I2S_DMA *matrix) { color_highlight = matrix->color565( @@ -35,11 +38,15 @@ void setup_rain(MatrixPanel_I2S_DMA *matrix) { COLOR_DEFAULT_B, COLOR_DEFAULT_G ); - for (int i = 0; i raindrops[x].speed) { // step down a pixel raindrops[x].y++; raindrops[x].last_moved = timestamp; } - if (raindrops[x].y > PANEL_HEIGHT) { - raindrops[x].y = 0; - raindrops[x].reached_bottom = true; - } + if (raindrops[x].y > PANEL_HEIGHT + raindrops[x].length) { + raindrops.push_back({ + false, + raindrops[x].x, + 0 - random(8), + random(20, 28), + random(0, 76), + millis() + }); + raindrops.erase(raindrops.begin() + x); + continue; + } if (raindrops[x].y >= 0) { //draw our pixel - matrix->drawPixel(x, raindrops[x].y, matrix->color565(128, 255, 128)); + matrix->drawPixel(raindrops[x].x, raindrops[x].y, matrix->color565(128, 255, 128)); // draw our trail for (int trail_offset = 1; trail_offset < raindrops[x].length; trail_offset++) { int trail_y = raindrops[x].y - trail_offset; @@ -74,28 +90,25 @@ void draw_rain(MatrixPanel_I2S_DMA *matrix, Mode mode) { ); if (trail_y < 0) { - if (not raindrops[x].reached_bottom) { - break; - } - // we need to count from the bottom up - trail_y = PANEL_HEIGHT + trail_y; + break; } - int pixel_num = (trail_y * PANEL_WIDTH) + x; + int pixel_num = (trail_y * PANEL_WIDTH) + raindrops[x].x; if (overlay[pixel_num / 8] & (1 << (7 - (pixel_num % 8))) and mode != Stealth) { if (mode == HighVis) { - matrix->drawPixel(x, trail_y, color_highlight); + matrix->drawPixel(raindrops[x].x, trail_y, color_highlight); } else { - matrix->drawPixel(x, trail_y, matrix->color565( + matrix->drawPixel(raindrops[x].x, trail_y, matrix->color565( COLOR_HIGHLIGHT_R * brightness, COLOR_HIGHLIGHT_G * brightness, COLOR_HIGHLIGHT_B * brightness )); } } else { - matrix->drawPixel(x, trail_y, fade_color); + matrix->drawPixel(raindrops[x].x, trail_y, fade_color); } } } + x++; } }