From 86323a0e56a1e019eb12e406364b2715e718741d Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Mon, 13 Apr 2026 16:27:57 +0200 Subject: [PATCH] afegit un mini-notificador --- source/screen.cpp | 44 +++++++++++++++++++++++++++++++++++++++----- source/screen.h | 23 ++++++++++++++++++++--- 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/source/screen.cpp b/source/screen.cpp index 3a65ed7..1e02a13 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -6,8 +6,9 @@ #include // for basic_ostream, operator<<, cout, endl #include // for basic_string, char_traits, string +#include "asset.h" // for Asset #include "mouse.hpp" // for Mouse::cursorVisible, Mouse::lastMouseMoveTime -class Asset; +#include "text.h" // for Text, TXT_CENTER, TXT_COLOR, TXT_STROKE // Constructor Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options_t *options) { @@ -21,8 +22,6 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options gameCanvasHeight = options->gameHeight; borderWidth = options->borderWidth * 2; borderHeight = options->borderHeight * 2; - notificationLogicalWidth = gameCanvasWidth; - notificationLogicalHeight = gameCanvasHeight; iniFade(); iniSpectrumFade(); @@ -44,12 +43,18 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options // Establece el modo de video setVideoMode(options->videoMode); - // Inicializa variables - notifyActive = false; + // Inicializa el sistema de notificaciones + notificationText = new Text(asset->get("8bithud.png"), asset->get("8bithud.txt"), renderer); + notificationMessage = ""; + notificationTextColor = {0xFF, 0xFF, 0xFF}; + notificationOutlineColor = {0x00, 0x00, 0x00}; + notificationEndTime = 0; + notificationY = 2; } // Destructor Screen::~Screen() { + delete notificationText; SDL_DestroyTexture(gameCanvas); } @@ -66,6 +71,10 @@ void Screen::start() { // Vuelca el contenido del renderizador en pantalla void Screen::blit() { + // Dibuja la notificación activa sobre el gameCanvas antes de presentar + SDL_SetRenderTarget(renderer, gameCanvas); + renderNotification(); + // Vuelve a dejar el renderizador en modo normal SDL_SetRenderTarget(renderer, nullptr); @@ -346,4 +355,29 @@ void Screen::updateFX() { void Screen::renderFX() { renderFade(); renderSpectrumFade(); +} + +// Muestra una notificación en la línea superior durante durationMs +void Screen::notify(const std::string &text, color_t textColor, color_t outlineColor, Uint32 durationMs) { + notificationMessage = text; + notificationTextColor = textColor; + notificationOutlineColor = outlineColor; + notificationEndTime = SDL_GetTicks() + durationMs; +} + +// Limpia la notificación actual +void Screen::clearNotification() { + notificationEndTime = 0; + notificationMessage.clear(); +} + +// Dibuja la notificación activa (si la hay) sobre el gameCanvas +void Screen::renderNotification() { + if (SDL_GetTicks() >= notificationEndTime) { + return; + } + notificationText->writeDX(TXT_CENTER | TXT_COLOR | TXT_STROKE, + gameCanvasWidth / 2, notificationY, + notificationMessage, 1, + notificationTextColor, 1, notificationOutlineColor); } \ No newline at end of file diff --git a/source/screen.h b/source/screen.h index 0069eb3..9a793bb 100644 --- a/source/screen.h +++ b/source/screen.h @@ -2,10 +2,12 @@ #include +#include // for string #include // for vector #include "utils.h" // for color_t class Asset; +class Text; // Tipos de filtro constexpr int FILTER_NEAREST = 0; @@ -29,9 +31,17 @@ class Screen { int borderHeight; // Anltura del borde SDL_Rect dest; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o ventana color_t borderColor; // Color del borde añadido a la textura de juego para rellenar la pantalla - bool notifyActive; // Indica si hay notificaciones activas - int notificationLogicalWidth; // Ancho lógico de las notificaciones en relación al tamaño de pantalla - int notificationLogicalHeight; // Alto lógico de las notificaciones en relación al tamaño de pantalla + + // Notificaciones - una sola activa, sin apilación ni animaciones + Text *notificationText; // Fuente 8bithud dedicada a las notificaciones + std::string notificationMessage; // Texto a mostrar + color_t notificationTextColor; // Color del texto + color_t notificationOutlineColor; // Color del outline + Uint32 notificationEndTime; // SDL_GetTicks() hasta el cual se muestra + int notificationY; // Fila vertical en el canvas virtual + + // Dibuja la notificación activa (si la hay) sobre el gameCanvas + void renderNotification(); // Variables - Efectos bool fade; // Indica si esta activo el efecto de fade @@ -124,4 +134,11 @@ class Screen { // Dibuja los efectos void renderFX(); + + // Muestra una notificación en la línea superior del canvas durante durationMs. + // Sobrescribe cualquier notificación activa (sin apilación). + void notify(const std::string &text, color_t textColor, color_t outlineColor, Uint32 durationMs); + + // Limpia la notificación actual + void clearNotification(); };