From 9e8c5e13df9506f124f91c1d9b6682c3afdda399 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Tue, 28 Oct 2025 10:10:47 +0100 Subject: [PATCH] migrat Notifier a time based --- source/core/rendering/screen.cpp | 11 +++++++++-- source/core/rendering/screen.hpp | 3 ++- source/game/scenes/game_over.cpp | 2 +- source/game/scenes/logo.cpp | 10 +++++----- source/game/scenes/title.cpp | 2 +- source/game/ui/notifier.cpp | 19 ++++++++++++------- source/game/ui/notifier.hpp | 9 ++++++--- 7 files changed, 36 insertions(+), 20 deletions(-) diff --git a/source/core/rendering/screen.cpp b/source/core/rendering/screen.cpp index 8dd7c75..678f64d 100644 --- a/source/core/rendering/screen.cpp +++ b/source/core/rendering/screen.cpp @@ -199,10 +199,17 @@ void Screen::toggleShaders() { initShaders(); } -// Actualiza la lógica de la clase +// Actualiza la lógica de la clase (versión antigua para escenas no migradas) void Screen::update() { fps_.calculate(SDL_GetTicks()); - Notifier::get()->update(); + Notifier::get()->update(0.016f); // Asume ~60 FPS (16ms por frame) + Mouse::updateCursorVisibility(); +} + +// Actualiza la lógica de la clase (versión nueva con delta_time para escenas migradas) +void Screen::update(float delta_time) { + fps_.calculate(SDL_GetTicks()); + Notifier::get()->update(delta_time); Mouse::updateCursorVisibility(); } diff --git a/source/core/rendering/screen.hpp b/source/core/rendering/screen.hpp index 1caf7f2..1f8ce8c 100644 --- a/source/core/rendering/screen.hpp +++ b/source/core/rendering/screen.hpp @@ -150,7 +150,8 @@ class Screen { void render(); // Actualiza la lógica de la clase - void update(); + void update(); // Para escenas no migradas (sin delta_time) + void update(float delta_time); // Para escenas migradas (con delta_time) // Establece el modo de video void setVideoMode(bool mode); diff --git a/source/game/scenes/game_over.cpp b/source/game/scenes/game_over.cpp index 439a480..e187b5f 100644 --- a/source/game/scenes/game_over.cpp +++ b/source/game/scenes/game_over.cpp @@ -62,7 +62,7 @@ void GameOver::update() { tv_sprite_->update(delta); // Actualiza el objeto Screen - Screen::get()->update(); + Screen::get()->update(delta); } // Dibuja el final en pantalla diff --git a/source/game/scenes/logo.cpp b/source/game/scenes/logo.cpp index 70f4a5e..8f9b8b3 100644 --- a/source/game/scenes/logo.cpp +++ b/source/game/scenes/logo.cpp @@ -180,11 +180,11 @@ void Logo::update() { // Obtener delta time desde el último frame const float DELTA_TIME = delta_timer_->tick(); - checkInput(); // Comprueba las entradas - updateState(DELTA_TIME); // Actualiza el estado y gestiona transiciones - updateJAILGAMES(DELTA_TIME); // Gestiona el logo de JAILGAME - updateTextureColors(); // Gestiona el color de las texturas - Screen::get()->update(); // Actualiza el objeto Screen + checkInput(); // Comprueba las entradas + updateState(DELTA_TIME); // Actualiza el estado y gestiona transiciones + updateJAILGAMES(DELTA_TIME); // Gestiona el logo de JAILGAME + updateTextureColors(); // Gestiona el color de las texturas + Screen::get()->update(DELTA_TIME); // Actualiza el objeto Screen } // Dibuja en pantalla diff --git a/source/game/scenes/title.cpp b/source/game/scenes/title.cpp index 650d08a..271f20b 100644 --- a/source/game/scenes/title.cpp +++ b/source/game/scenes/title.cpp @@ -176,7 +176,7 @@ void Title::update() { checkInput(); // Actualiza la pantalla - Screen::get()->update(); + Screen::get()->update(current_delta_); // Actualiza el estado actual updateState(current_delta_); diff --git a/source/game/ui/notifier.cpp b/source/game/ui/notifier.cpp index 5528f26..7cae24e 100644 --- a/source/game/ui/notifier.cpp +++ b/source/game/ui/notifier.cpp @@ -15,6 +15,7 @@ #include "core/resources/resource.hpp" // Para Resource #include "external/jail_audio.h" // Para JA_PlaySound #include "game/options.hpp" // Para Options, options, NotificationPosition +#include "utils/delta_timer.hpp" // Para DeltaTimer #include "utils/utils.hpp" // Para PaletteColor // [SINGLETON] @@ -39,6 +40,7 @@ auto Notifier::get() -> Notifier* { Notifier::Notifier(const std::string& icon_file, const std::string& text) : icon_surface_(!icon_file.empty() ? Resource::get()->getSurface(icon_file) : nullptr), text_(Resource::get()->getText(text)), + delta_timer_(std::make_unique()), bg_color_(Options::notifications.color), stack_(false), has_icons_(!icon_file.empty()) {} @@ -51,7 +53,7 @@ void Notifier::render() { } // Actualiza el estado de las notificaiones -void Notifier::update() { +void Notifier::update(float delta_time) { for (auto& notification : notifications_) { // Si la notificación anterior está "saliendo", no hagas nada if (!notifications_.empty() && ¬ification != ¬ifications_.front()) { @@ -63,10 +65,11 @@ void Notifier::update() { switch (notification.state) { case Status::RISING: { - const int DIRECTION = 1; - notification.rect.y += DIRECTION; + const float DISPLACEMENT = SLIDE_SPEED * delta_time; + notification.rect.y += DISPLACEMENT; - if (notification.rect.y == notification.y) { + if (notification.rect.y >= notification.y) { + notification.rect.y = notification.y; notification.state = Status::STAY; notification.start_time = SDL_GetTicks(); } @@ -81,10 +84,12 @@ void Notifier::update() { } case Status::VANISHING: { - const int DIRECTION = -1; - notification.rect.y += DIRECTION; + const float DISPLACEMENT = SLIDE_SPEED * delta_time; + notification.rect.y -= DISPLACEMENT; - if (notification.rect.y == notification.y - notification.travel_dist) { + const float TARGET_Y = notification.y - notification.travel_dist; + if (notification.rect.y <= TARGET_Y) { + notification.rect.y = TARGET_Y; notification.state = Status::FINISHED; } break; diff --git a/source/game/ui/notifier.hpp b/source/game/ui/notifier.hpp index ef5019b..a2a59ee 100644 --- a/source/game/ui/notifier.hpp +++ b/source/game/ui/notifier.hpp @@ -8,6 +8,7 @@ class SurfaceSprite; // lines 8-8 class Surface; // lines 10-10 class Text; // lines 9-9 +class DeltaTimer; // lines 11-11 // Constantes constexpr Uint32 DEFAULT_NOTIFICATION_DURATION = 2000; @@ -24,6 +25,7 @@ class Notifier { // Constantes static constexpr float ICON_SIZE = 16.0F; static constexpr float PADDING_OUT = 0.0F; + static constexpr float SLIDE_SPEED = 120.0F; // Pixels per second for slide animations // [SINGLETON] Objeto notifier static Notifier* notifier; @@ -60,8 +62,9 @@ class Notifier { explicit Notification() = default; }; - std::shared_ptr icon_surface_; // Textura para los iconos de las notificaciones - std::shared_ptr text_; // Objeto para dibujar texto + std::shared_ptr icon_surface_; // Textura para los iconos de las notificaciones + std::shared_ptr text_; // Objeto para dibujar texto + std::unique_ptr delta_timer_; // Timer for frame-independent animations // Variables Uint8 bg_color_; // Color de fondo de las notificaciones @@ -97,7 +100,7 @@ class Notifier { void render(); // Actualiza el estado de las notificaiones - void update(); + void update(float delta_time); // Muestra una notificación de texto por pantalla void show(std::vector texts, NotificationText text_is = NotificationText::LEFT, Uint32 display_duration = DEFAULT_NOTIFICATION_DURATION, int icon = -1, bool can_be_removed = true, const std::string& code = std::string());