afegit un mini-notificador

This commit is contained in:
2026-04-13 16:27:57 +02:00
parent 58cacf7bda
commit 86323a0e56
2 changed files with 59 additions and 8 deletions

View File

@@ -6,8 +6,9 @@
#include <iostream> // for basic_ostream, operator<<, cout, endl #include <iostream> // for basic_ostream, operator<<, cout, endl
#include <string> // for basic_string, char_traits, string #include <string> // for basic_string, char_traits, string
#include "asset.h" // for Asset
#include "mouse.hpp" // for Mouse::cursorVisible, Mouse::lastMouseMoveTime #include "mouse.hpp" // for Mouse::cursorVisible, Mouse::lastMouseMoveTime
class Asset; #include "text.h" // for Text, TXT_CENTER, TXT_COLOR, TXT_STROKE
// Constructor // Constructor
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options_t *options) { 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; gameCanvasHeight = options->gameHeight;
borderWidth = options->borderWidth * 2; borderWidth = options->borderWidth * 2;
borderHeight = options->borderHeight * 2; borderHeight = options->borderHeight * 2;
notificationLogicalWidth = gameCanvasWidth;
notificationLogicalHeight = gameCanvasHeight;
iniFade(); iniFade();
iniSpectrumFade(); iniSpectrumFade();
@@ -44,12 +43,18 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options
// Establece el modo de video // Establece el modo de video
setVideoMode(options->videoMode); setVideoMode(options->videoMode);
// Inicializa variables // Inicializa el sistema de notificaciones
notifyActive = false; 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 // Destructor
Screen::~Screen() { Screen::~Screen() {
delete notificationText;
SDL_DestroyTexture(gameCanvas); SDL_DestroyTexture(gameCanvas);
} }
@@ -66,6 +71,10 @@ void Screen::start() {
// Vuelca el contenido del renderizador en pantalla // Vuelca el contenido del renderizador en pantalla
void Screen::blit() { 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 // Vuelve a dejar el renderizador en modo normal
SDL_SetRenderTarget(renderer, nullptr); SDL_SetRenderTarget(renderer, nullptr);
@@ -346,4 +355,29 @@ void Screen::updateFX() {
void Screen::renderFX() { void Screen::renderFX() {
renderFade(); renderFade();
renderSpectrumFade(); 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);
} }

View File

@@ -2,10 +2,12 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <string> // for string
#include <vector> // for vector #include <vector> // for vector
#include "utils.h" // for color_t #include "utils.h" // for color_t
class Asset; class Asset;
class Text;
// Tipos de filtro // Tipos de filtro
constexpr int FILTER_NEAREST = 0; constexpr int FILTER_NEAREST = 0;
@@ -29,9 +31,17 @@ class Screen {
int borderHeight; // Anltura del borde int borderHeight; // Anltura del borde
SDL_Rect dest; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o ventana 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 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 // Notificaciones - una sola activa, sin apilación ni animaciones
int notificationLogicalHeight; // Alto lógico de las notificaciones en relación al tamaño de pantalla 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 // Variables - Efectos
bool fade; // Indica si esta activo el efecto de fade bool fade; // Indica si esta activo el efecto de fade
@@ -124,4 +134,11 @@ class Screen {
// Dibuja los efectos // Dibuja los efectos
void renderFX(); 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();
}; };