From 1acbe1d097f922a9d0a022e096991ef54373107e Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Thu, 23 Oct 2025 18:59:37 +0200 Subject: [PATCH] migrat instructions a delta_time pur --- source/director.cpp | 2 +- source/sections/instructions.cpp | 63 ++++++++++++++++++++------------ source/sections/instructions.hpp | 26 +++++++------ 3 files changed, 55 insertions(+), 36 deletions(-) diff --git a/source/director.cpp b/source/director.cpp index 79f7560..9b6cda3 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -44,7 +44,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::GAME; + 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 47be9f3..61cd69d 100644 --- a/source/sections/instructions.cpp +++ b/source/sections/instructions.cpp @@ -49,8 +49,8 @@ Instructions::Instructions() fade_->setMode(Fade::Mode::IN); fade_->activate(); - // Inicializa las líneas con un retraso progresivo de 50 ms - lines_ = initializeLines(256); + // Inicializa las líneas con un retraso progresivo + lines_ = initializeLines(256, LINE_START_DELAY_S); // Rellena la textura de texto fillTexture(); @@ -278,39 +278,53 @@ void Instructions::run() { } // Método para inicializar las líneas -auto Instructions::initializeLines(int height) -> std::vector { +auto Instructions::initializeLines(int height, float line_delay) -> std::vector { std::vector lines; for (int y = 0; y < height; y++) { int direction = (y % 2 == 0) ? -1 : 1; // Pares a la izquierda, impares a la derecha - lines.emplace_back(y, 0.0F, direction); + float delay = y * line_delay; // Retraso progresivo basado en la línea + lines.emplace_back(y, 0.0F, direction, delay); } return lines; } -// Método para mover las líneas con suavizado -auto Instructions::moveLines(std::vector& lines, int width, float duration, Uint32 start_delay) -> bool { - Uint32 current_time = SDL_GetTicks(); +// Método para mover las líneas con suavizado (usando delta_time puro) +auto Instructions::moveLines(std::vector& lines, int width, float duration, float delta_time) -> bool { bool all_lines_off_screen = true; for (auto& line : lines) { - // Establecer start_time en el primer cuadro de animación - if (line.start_time == 0) { - line.start_time = current_time + line.y * start_delay; + // Verificar si la línea ha superado su tiempo de retraso + if (!line.started) { + line.delay_time -= delta_time; + if (line.delay_time <= 0.0F) { + line.started = true; + line.accumulated_time = 0.0F; + } else { + all_lines_off_screen = false; // Aún hay líneas esperando para empezar + continue; + } } - float elapsed_time = (current_time - line.start_time) / 1000.0F; // Convertir a segundos - if (elapsed_time < 0) { - all_lines_off_screen = false; // Si aún no se debe mover esta línea, no están todas fuera de pantalla - continue; - } - if (elapsed_time >= duration) { - continue; // Si la línea ha salido de los límites, no la muevas más + // Si la línea ya ha completado su movimiento, saltarla + if (line.accumulated_time >= duration) { + continue; // Esta línea ya terminó } - float t = elapsed_time / duration; + // Acumular tiempo y calcular posición + line.accumulated_time += delta_time; + + if (line.accumulated_time >= duration) { + // La línea ha completado su movimiento + line.accumulated_time = duration; + } else { + // La línea aún se está moviendo + all_lines_off_screen = false; + } + + // Calcular posición con suavizado + float t = line.accumulated_time / duration; float smooth_factor = easeInOutQuint(t); line.x = line.direction * smooth_factor * width; - all_lines_off_screen = false; // Si alguna línea aún se está moviendo, no están todas fuera de pantalla } return all_lines_off_screen; @@ -327,12 +341,13 @@ void Instructions::renderLines(SDL_Renderer* renderer, SDL_Texture* texture, con // Gestiona la textura con los graficos void Instructions::updateBackbuffer(float delta_time) { - // Establece la ventana del backbuffer (convertir elapsed_time_ a equivalente de counter) - float counter_equivalent = elapsed_time_ * 60.0F; // Convertir segundos a equivalente frame para UI - view_.y = std::max(0.0F, param.game.height - counter_equivalent + 100); + // Establece la ventana del backbuffer usando velocidad en pixels por segundo + // El scroll comienza desde (param.game.height + 100) y desciende a 0 + float scroll_offset = elapsed_time_ * SCROLL_SPEED_PPS; + view_.y = std::max(0.0F, (param.game.height + 100.0F) - scroll_offset); // Verifica si view_.y == 0 y gestiona el temporizador - if (view_.y == 0) { + if (view_.y == 0.0F) { if (!start_delay_triggered_) { // Activa el temporizador si no ha sido activado start_delay_triggered_ = true; @@ -341,7 +356,7 @@ void Instructions::updateBackbuffer(float delta_time) { start_delay_timer_ += delta_time; if (start_delay_timer_ >= START_DELAY_S) { // Han pasado los segundos de retraso, mover líneas - all_lines_off_screen_ = moveLines(lines_, 320, LINE_MOVE_DURATION_S, static_cast(LINE_START_DELAY_MS)); + all_lines_off_screen_ = moveLines(lines_, 320, LINE_MOVE_DURATION_S, delta_time); } } } diff --git a/source/sections/instructions.hpp b/source/sections/instructions.hpp index 4c75b9a..3ef730b 100644 --- a/source/sections/instructions.hpp +++ b/source/sections/instructions.hpp @@ -25,17 +25,20 @@ class TiledBG; */ // --- Estructuras --- -struct Line { // Almacena información de línea animada - int y; // Coordenada Y de la línea - float x; // Coordenada X inicial (usamos float para mayor precisión en el suavizado) - int direction; // Dirección de movimiento: -1 para izquierda, 1 para derecha - Uint32 start_time{0}; // Tiempo de inicio del movimiento +struct Line { // Almacena información de línea animada + int y; // Coordenada Y de la línea + float x; // Coordenada X inicial (usamos float para mayor precisión en el suavizado) + int direction; // Dirección de movimiento: -1 para izquierda, 1 para derecha + float accumulated_time{0}; // Tiempo acumulado desde que empezó la animación (segundos) + float delay_time{0}; // Tiempo de retraso antes de comenzar la animación (segundos) + bool started{false}; // Indica si la línea ha comenzado a moverse // Constructor de Line - Line(int y, float x, int direction) + Line(int y, float x, int direction, float delay) : y(y), x(x), - direction(direction) {} + direction(direction), + delay_time(delay) {} }; // Clase Instructions @@ -53,7 +56,8 @@ class Instructions { static constexpr float SPRITE_ANIMATION_CYCLE_S = 36.0F / 60.0F; // Ciclo de animación sprites (≈0.6s) static constexpr float START_DELAY_S = 4.0F; // Retraso antes de mover líneas (4s) static constexpr float LINE_MOVE_DURATION_S = 1.0F; // Duración movimiento líneas (1s) - static constexpr float LINE_START_DELAY_MS = 5.0F; // Retraso entre líneas (5ms) + static constexpr float LINE_START_DELAY_S = 0.005F; // Retraso entre líneas (5ms = 0.005s) + static constexpr float SCROLL_SPEED_PPS = 60.0F; // Velocidad de scroll (60 pixels por segundo) // --- Objetos y punteros --- SDL_Renderer* renderer_; // El renderizador de la ventana @@ -86,9 +90,9 @@ class Instructions { void fillBackbuffer(); // Rellena el backbuffer 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 (ya usa tiempo real) - static void renderLines(SDL_Renderer* renderer, SDL_Texture* texture, const std::vector& lines); // Renderiza las líneas + static auto initializeLines(int height, float line_delay) -> std::vector; // Inicializa las líneas animadas + static auto moveLines(std::vector& lines, int width, float duration, float delta_time) -> bool; // Mueve las líneas usando delta_time puro + static void renderLines(SDL_Renderer* renderer, SDL_Texture* texture, const std::vector& lines); // Renderiza las líneas void updateBackbuffer(float delta_time); // Gestiona la textura con los gráficos auto calculateDeltaTime() -> float; // Calcula el tiempo transcurrido desde el último frame }; \ No newline at end of file