From e2fd470ad39944f5ddb02847b0be3cddf29e7344 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Tue, 16 Sep 2025 12:23:02 +0200 Subject: [PATCH] =?UTF-8?q?migraci=C3=B3=20a=20delta=20time?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/director.cpp | 2 +- source/sections/instructions.cpp | 33 ++++++++++++++++++++------------ source/sections/instructions.h | 7 ++++--- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/source/director.cpp b/source/director.cpp index bbc9c43..65ed1a2 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -42,7 +42,7 @@ Director::Director(int argc, std::span argv) { Section::name = Section::Name::GAME; Section::options = Section::Options::GAME_PLAY_1P; #elif _DEBUG - Section::name = Section::Name::HI_SCORE_TABLE; + Section::name = Section::Name::INSTRUCTIONS; Section::options = Section::Options::GAME_PLAY_1P; #else // NORMAL GAME Section::name = Section::Name::LOGO; diff --git a/source/sections/instructions.cpp b/source/sections/instructions.cpp index 7aeb64c..b154006 100644 --- a/source/sections/instructions.cpp +++ b/source/sections/instructions.cpp @@ -204,18 +204,15 @@ void Instructions::fillBackbuffer() { } // Actualiza las variables -void Instructions::update() { - if (SDL_GetTicks() - ticks_ > param.game.speed) { - ticks_ = SDL_GetTicks(); // Actualiza el contador de ticks - Screen::get()->update(); // Actualiza el objeto screen +void Instructions::update(float delta_time) { + Screen::get()->update(); // Actualiza el objeto screen - counter_++; // Incrementa el contador - updateSprites(); // Actualiza los sprites - updateBackbuffer(); // Gestiona la textura con los graficos - tiled_bg_->update(); // Actualiza el mosaico de fondo - fade_->update(); // Actualiza el objeto "fade" - fillBackbuffer(); // Rellena el backbuffer - } + counter_++; // Incrementa el contador + updateSprites(); // Actualiza los sprites + updateBackbuffer(); // Gestiona la textura con los graficos + tiled_bg_->update(delta_time); // Actualiza el mosaico de fondo + fade_->update(delta_time); // Actualiza el objeto "fade" + fillBackbuffer(); // Rellena el backbuffer Audio::update(); } @@ -255,12 +252,24 @@ void Instructions::checkInput() { GlobalInputs::check(); } +// Calcula el tiempo transcurrido desde el último frame +float Instructions::calculateDeltaTime() { + const Uint64 current_time = SDL_GetTicks(); + const float delta_time = static_cast(current_time - last_time_); + last_time_ = current_time; + return delta_time; +} + // Bucle para la pantalla de instrucciones void Instructions::run() { + last_time_ = SDL_GetTicks(); Audio::get()->playMusic("title.ogg"); + while (Section::name == Section::Name::INSTRUCTIONS) { + const float delta_time = calculateDeltaTime(); + checkInput(); - update(); + update(delta_time); checkEvents(); // Tiene que ir antes del render render(); } diff --git a/source/sections/instructions.h b/source/sections/instructions.h index 3fa0692..354b2b7 100644 --- a/source/sections/instructions.h +++ b/source/sections/instructions.h @@ -63,7 +63,7 @@ class Instructions { // --- Variables --- int counter_ = 0; // Contador para manejar el progreso en la pantalla de instrucciones - Uint64 ticks_ = 0; // Contador de ticks para ajustar la velocidad del programa + Uint64 last_time_ = 0; // Último timestamp para calcular delta-time SDL_FRect view_; // Vista del backbuffer que se va a mostrar por pantalla SDL_FPoint sprite_pos_ = {0, 0}; // Posición del primer sprite en la lista float item_space_ = 2.0; // Espacio entre los items en pantalla @@ -73,7 +73,7 @@ class Instructions { bool start_delay_triggered_ = false; // Bandera para determinar si el retraso ha comenzado // --- Métodos internos --- - void update(); // Actualiza las variables + void update(float delta_time); // Actualiza las variables void render(); // Pinta en pantalla static void checkEvents(); // Comprueba los eventos static void checkInput(); // Comprueba las entradas @@ -82,7 +82,8 @@ class Instructions { void iniSprites(); // Inicializa los sprites de los items void updateSprites(); // Actualiza los sprites static auto initializeLines(int height) -> std::vector; // Inicializa las líneas animadas - static auto moveLines(std::vector &lines, int width, float duration, Uint32 start_delay) -> bool; // Mueve las líneas + static auto moveLines(std::vector &lines, int width, float duration, Uint32 start_delay) -> bool; // Mueve las líneas (ya usa tiempo real) static void renderLines(SDL_Renderer *renderer, SDL_Texture *texture, const std::vector &lines); // Renderiza las líneas void updateBackbuffer(); // Gestiona la textura con los gráficos + float calculateDeltaTime(); // Calcula el tiempo transcurrido desde el último frame }; \ No newline at end of file