From f2ea6dbdb9b6129595b9bea271994739eceffae2 Mon Sep 17 00:00:00 2001 From: Sergio Valor Martinez Date: Thu, 25 May 2023 12:13:57 +0200 Subject: [PATCH] demo: Posibilidad de modificar el efecto de fuego con el teclado --- main.cpp | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/main.cpp b/main.cpp index fea0a78..01e72f3 100644 --- a/main.cpp +++ b/main.cpp @@ -57,11 +57,12 @@ int gradCurrentColor = 192; // Color actual más alto del degradado int gradBreathDirection = 0; // Indica si gradCurrentColor crece o decrece // Variables para el efecto de fuego -const int fireScreenWidth = 320; // Ancho del efecto de fuego -const int fireScreenHeight = 180; // Alto del efecto de fuego -Uint32 fire[fireScreenHeight][fireScreenWidth]; // Buffer con el fuego -ColorRGB palette[256]; // Paleta de color para el fuego -const int fireDesp = 240 - fireScreenHeight + 3; // Fila donde comienza a dibujarse el efecto de fuego +const int fireScreenWidth = 320; // Ancho del efecto de fuego +const int fireScreenHeight = 180; // Alto del efecto de fuego +int fire[fireScreenHeight][fireScreenWidth]; // Buffer con el fuego +ColorRGB palette[256]; // Paleta de color para el fuego +const int fireDesp = 240 - fireScreenHeight + 3; // Fila donde comienza a dibujarse el efecto de fuego +int fireModifier = 28; // Valor para modificar el efecto de fuego (0-30) // Inicializa las opciones void initOptions(); @@ -255,7 +256,7 @@ void initSprite() void initFire() { // Intializes random number generator - srand((unsigned) SDL_GetTicks()); + srand((unsigned)SDL_GetTicks()); // Inicializa el buffer de fuego for (int y = 0; y < fireScreenHeight; ++y) @@ -340,6 +341,16 @@ void checkEvents() screen->incWindowSize(); break; + case SDL_SCANCODE_F3: + --fireModifier; + fireModifier = std::max(0, fireModifier); + break; + + case SDL_SCANCODE_F4: + ++fireModifier; + fireModifier = std::min(30, fireModifier); + break; + case SDL_SCANCODE_N: screen->showNotification("Ejemplo de notificacion", "con 2 lineas de texto", 0); break; @@ -433,21 +444,25 @@ void updateGradient() // Actualiza el efecto de fuego void updateFire() { - // randomize the bottom row of the fire buffer const int w = fireScreenWidth; const int h = fireScreenHeight; + const int mod = 157 - fireModifier; + // for (int x = 0; x < w; ++x) + // const int max = 10; + // const int mod = max - abs((counter % (max * 2)) - max); + // for (int x = 0; x < w; ++x) + // fire[h - 1][x] = 0; + // for (int x = mod; x < w - mod; ++x) + // randomize the bottom row of the fire buffer for (int x = 0; x < w; ++x) - //fire[h - 1][x] = abs(32768 + rand()) % 256; + // fire[h - 1][x] = abs(32768 + rand()) % 256; fire[h - 1][x] = rand() % 256; // do the fire calculations for every pixel, from top to bottom for (int y = 0; y < h - 1; ++y) for (int x = 0; x < w; ++x) { fire[y][x] = - ((fire[(y + 1) % h][(x - 1 + w) % w] + fire[(y + 1) % h][(x) % w] + fire[(y + 1) % h][(x + 1) % w] + fire[(y + 2) % h][(x) % w]) * 32) / 129; - //fire[y][x] = std::min(255,(int)fire[y][x]); - //fire[y][x] = std::max(0,(int)fire[y][x]); - + ((fire[(y + 1) % h][(x - 1 + w) % w] + fire[(y + 1) % h][(x) % w] + fire[(y + 1) % h][(x + 1) % w] + fire[(y + 2) % h][(x) % w]) * 32) / mod; } } @@ -529,7 +544,7 @@ void renderFire() for (int y = 0; y < fireScreenHeight; ++y) for (int x = 0; x < fireScreenWidth; ++x) { - const ColorRGB c = palette[fire[y][x]]; + const ColorRGB c = palette[std::min(255, std::max(0, fire[y][x]))]; SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, 255); SDL_RenderDrawPoint(renderer, x, y + fireDesp); } @@ -565,6 +580,7 @@ void renderText() debugText->writeDX(TXT_CENTER | TXT_COLOR | TXT_STROKE, options->screen.nativeWidth / 2, text->getCharacterSize() * 11, controllerName, 1, {240, 240, 240}, 1, {0, 0, 192}); debugText->writeDX(TXT_CENTER | TXT_COLOR | TXT_STROKE, options->screen.nativeWidth / 2, text->getCharacterSize() * 13, inputPressed, 1, {240, 240, 240}, 1, {0, 0, 192}); debugText->writeDX(TXT_CENTER | TXT_COLOR | TXT_STROKE, options->screen.nativeWidth / 2, options->screen.nativeHeight - (text->getCharacterSize() * 2), "Pulsa 'ESCAPE' para terminar el programa", 1, {240, 240, 240}, 1, {0, 0, 192}); + debugText->writeDX(TXT_CENTER | TXT_COLOR | TXT_STROKE, options->screen.nativeWidth / 2, text->getCharacterSize() * 17, "Pulsa 'F3' o 'F4' para modificar el fuego: " + to_string(fireModifier), 1, {240, 240, 240}, 1, {0, 0, 192}); } // Dibuja los elementos del programa en pantalla