From 7d62b3a339d58538fec2dab1797608febe98f8fc Mon Sep 17 00:00:00 2001 From: Sergio Valor Martinez Date: Tue, 15 Nov 2022 12:30:08 +0100 Subject: [PATCH] Creada la clase Notify --- source/common/notify.cpp | 91 ++++++++++++++++++++++++++++++++++++++++ source/common/notify.h | 60 ++++++++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 source/common/notify.cpp create mode 100644 source/common/notify.h diff --git a/source/common/notify.cpp b/source/common/notify.cpp new file mode 100644 index 0000000..b7801d0 --- /dev/null +++ b/source/common/notify.cpp @@ -0,0 +1,91 @@ +#include "notify.h" +#include +#include + +// Constructor +Notify::Notify(SDL_Renderer *renderer, std::string bitmapFile, std::string textFile) +{ + // Inicializa variables + this->renderer = renderer; + bgColor = {32, 32, 32}; + + // Crea objetos + text = new Text(bitmapFile, textFile, renderer); +} + +// Destructor +Notify::~Notify() +{ + // Libera la memoria de los objetos + delete text; + + for (auto notification : notifications) + { + delete notification.sprite; + delete notification.texture; + } +} + +// Dibuja las notificaciones por pantalla +void Notify::render() +{ + for (int i = 0; i < (int)notifications.size(); ++i) + { + notifications.at(i).sprite->render(); + } +} + +// Actualiza el estado de las notificaiones +void Notify::update() +{ + for (int i = 0; i < (int)notifications.size(); ++i) + { + notifications.at(i).counter++; + } + + clearFinishedNotifications(); +} + +// Elimina las notificaciones finalizadas +void Notify::clearFinishedNotifications() +{ + for (int i = (int)notifications.size() - 1; i >= 0; --i) + { + if (notifications.at(i).counter >= 300) + { + delete notifications.at(i).sprite; + delete notifications.at(i).texture; + notifications.erase(notifications.begin() + i); + } + } +} + +// Muestra una notificación de texto por pantalla; +void Notify::showText(std::string text) +{ + // Crea constantes + const int width = this->text->lenght(text) + (this->text->getCharacterSize() * 2); + const int height = this->text->getCharacterSize() * 2; + const int desp = this->text->getCharacterSize(); + + // Crea la notificacion + notification_t n; + + // inicializa variables + n.counter = 0; + n.state = ns_rising; + n.text = text; + + // Crea la textura + n.texture = new Texture(renderer); + n.texture->createBlank(renderer, width, height, SDL_TEXTUREACCESS_TARGET); + n.texture->setAsRenderTarget(renderer); + n.texture->setBlendMode(SDL_BLENDMODE_BLEND); + this->text->writeDX(TXT_CENTER | TXT_STROKE, desp, 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); + + // Añade la notificación a la lista + notifications.push_back(n); +} \ No newline at end of file diff --git a/source/common/notify.h b/source/common/notify.h new file mode 100644 index 0000000..8c06751 --- /dev/null +++ b/source/common/notify.h @@ -0,0 +1,60 @@ +#pragma once + +#include +#include "text.h" +#include "texture.h" +#include "sprite.h" +#include "utils.h" +#include + +#ifndef NOTIFY_H +#define NOTIFY_H + +class Notify +{ +private: + enum notification_state_e + { + ns_rising, + ns_stay, + ns_vanishing + }; + + struct notification_t + { + std::string text; + int counter; + notification_state_e state; + Texture *texture; + Sprite *sprite; + }; + + // Objetos y punteros + SDL_Renderer *renderer; // El renderizador de la ventana + Text *text; // Objeto para dibujar texto + + // Variables + color_t bgColor; // Color de fondo de las notificaciones + std::vector notifications; // La lista de notificaciones activas + + // Dibuja las notificaciones por pantalla + void render(); + + // Actualiza el estado de las notificaiones + void update(); + + // Elimina las notificaciones finalizadas + void clearFinishedNotifications(); + +public: + // Constructor + Notify(SDL_Renderer *renderer, std::string bitmapFile, std::string textFile); + + // Destructor + ~Notify(); + + // Muestra una notificación de texto por pantalla; + void showText(std::string text); +}; + +#endif