From b1c3e0d2af189cb8cfab12846eb0fc2a1a5c4c32 Mon Sep 17 00:00:00 2001 From: Sergio Valor Martinez Date: Tue, 15 Nov 2022 13:47:39 +0100 Subject: [PATCH] Trabajando en la animacion de las notificaciones --- source/common/notify.cpp | 50 +++++++++++++++++++++++++++++++++++++--- source/common/notify.h | 7 ++++-- source/game.cpp | 9 ++++++++ source/intro.cpp | 1 + source/logo.cpp | 41 ++++++++++++++++++++++++++++++-- source/title.cpp | 3 +++ 6 files changed, 104 insertions(+), 7 deletions(-) diff --git a/source/common/notify.cpp b/source/common/notify.cpp index 67359c2..fa6cfc6 100644 --- a/source/common/notify.cpp +++ b/source/common/notify.cpp @@ -7,7 +7,8 @@ Notify::Notify(SDL_Renderer *renderer, std::string bitmapFile, std::string textF { // Inicializa variables this->renderer = renderer; - bgColor = {32, 32, 32}; + bgColor = {64, 64, 64}; + waitTime = 300; // Crea objetos text = new Text(bitmapFile, textFile, renderer); @@ -41,6 +42,48 @@ void Notify::update() for (int i = 0; i < (int)notifications.size(); ++i) { notifications.at(i).counter++; + + // Comprueba los estados + if (notifications.at(i).state == ns_rising) + { + const float step = (notifications.at(i).counter / (float)notifications.at(i).texture->getHeight()); + const int alpha = 255 * step; + + notifications.at(i).rect.y++; + //notifications.at(i).texture->setAlpha(alpha); + + if (notifications.at(i).rect.y == 0) + { + notifications.at(i).state = ns_stay; + //notifications.at(i).texture->setAlpha(255); + notifications.at(i).counter = 0; + } + } + + else if (notifications.at(i).state == ns_stay) + { + if (notifications.at(i).counter == waitTime) + { + notifications.at(i).state = ns_vanishing; + notifications.at(i).counter = 0; + } + } + else if (notifications.at(i).state == ns_vanishing) + { + + const float step = (notifications.at(i).counter / (float)notifications.at(i).texture->getHeight()); + const int alpha = 255 * step; + + notifications.at(i).rect.y--; + //notifications.at(i).texture->setAlpha(alpha); + + if (notifications.at(i).rect.y == -notifications.at(i).texture->getHeight()) + { + notifications.at(i).state = ns_finished; + } + } + + notifications.at(i).sprite->setRect(notifications.at(i).rect); } clearFinishedNotifications(); @@ -51,7 +94,7 @@ void Notify::clearFinishedNotifications() { for (int i = (int)notifications.size() - 1; i >= 0; --i) { - if (notifications.at(i).counter >= 300) + if (notifications.at(i).state == ns_finished) { delete notifications.at(i).sprite; delete notifications.at(i).texture; @@ -75,6 +118,7 @@ void Notify::showText(std::string text) n.counter = 0; n.state = ns_rising; n.text = text; + n.rect = {0, -height, width, height}; // Crea la textura n.texture = new Texture(renderer); @@ -86,7 +130,7 @@ void Notify::showText(std::string text) this->text->writeDX(TXT_CENTER | TXT_STROKE, width / 2, desp / 2, text, 1, {255, 255, 255}, 1, {0, 0, 0}); // Crea el sprite - n.sprite = new Sprite({0, 0, width, height}, n.texture, renderer); + n.sprite = new Sprite(n.rect, n.texture, renderer); // Añade la notificación a la lista notifications.push_back(n); diff --git a/source/common/notify.h b/source/common/notify.h index d75fa02..3b14bde 100644 --- a/source/common/notify.h +++ b/source/common/notify.h @@ -17,7 +17,8 @@ private: { ns_rising, ns_stay, - ns_vanishing + ns_vanishing, + ns_finished }; struct notification_t @@ -27,6 +28,7 @@ private: notification_state_e state; Texture *texture; Sprite *sprite; + SDL_Rect rect; }; // Objetos y punteros @@ -35,6 +37,7 @@ private: // Variables color_t bgColor; // Color de fondo de las notificaciones + int waitTime; // Tiempo que se ve la notificación std::vector notifications; // La lista de notificaciones activas // Elimina las notificaciones finalizadas @@ -46,7 +49,7 @@ public: // Actualiza el estado de las notificaiones void update(); - + // Constructor Notify(SDL_Renderer *renderer, std::string bitmapFile, std::string textFile); diff --git a/source/game.cpp b/source/game.cpp index 2131ef0..50567dd 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -2703,6 +2703,9 @@ void Game::update() // Actualiza el contador de ticks ticks = SDL_GetTicks(); + // Actualiza las notificaciones + screen->updateNotifier(); + // Actualiza el contador de juego counter++; @@ -3286,6 +3289,9 @@ void Game::updatePausedGame() // Actualiza el contador de ticks ticks = SDL_GetTicks(); + // Actualiza las notificaciones + screen->updateNotifier(); + if (leavingPauseMenu) { if (pauseCounter > 0) @@ -3447,6 +3453,9 @@ void Game::updateGameOverScreen() // Actualiza el contador de ticks ticks = SDL_GetTicks(); + // Actualiza las notificaciones + screen->updateNotifier(); + // Actualiza la lógica del menu gameOverMenu->update(); diff --git a/source/intro.cpp b/source/intro.cpp index f086079..c786b10 100644 --- a/source/intro.cpp +++ b/source/intro.cpp @@ -408,6 +408,7 @@ void Intro::update() // Actualiza las escenas de la intro updateScenes(); + // Actualiza las notificaciones screen->updateNotifier(); } } diff --git a/source/logo.cpp b/source/logo.cpp index d7caa4f..e494315 100644 --- a/source/logo.cpp +++ b/source/logo.cpp @@ -60,8 +60,42 @@ void Logo::checkEventHandler() // Cualquier tecla pulsada if ((eventHandler->type == SDL_KEYDOWN) || (eventHandler->type == SDL_JOYBUTTONDOWN)) { - section.name = PROG_SECTION_TITLE; - section.subsection = TITLE_SECTION_1; + switch (eventHandler->key.keysym.scancode) + { + case SDL_SCANCODE_F: + screen->switchVideoMode(); + texture->reLoad(); + break; + + case SDL_SCANCODE_F1: + screen->setWindowSize(1); + texture->reLoad(); + break; + + case SDL_SCANCODE_F2: + screen->setWindowSize(2); + texture->reLoad(); + break; + + case SDL_SCANCODE_F3: + screen->setWindowSize(3); + texture->reLoad(); + break; + + case SDL_SCANCODE_F4: + screen->setWindowSize(4); + texture->reLoad(); + break; + + case SDL_SCANCODE_F5: + screen->showText("Conectado a Jailers.net"); + break; + + default: + section.name = PROG_SECTION_TITLE; + section.subsection = TITLE_SECTION_1; + break; + } } } } @@ -94,6 +128,9 @@ void Logo::update() // Comprueba si ha terminado el logo checkLogoEnd(); + + // Actualiza las notificaciones + screen->updateNotifier(); } } diff --git a/source/title.cpp b/source/title.cpp index 39cbfdd..56fa410 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -210,6 +210,9 @@ void Title::update() // Actualiza el contador de ticks ticks = SDL_GetTicks(); + // Actualiza las notificaciones + screen->updateNotifier(); + switch (section.subsection) { // Sección 1 - Titulo desplazandose