From 6c8f231b348e83d4ea6401bc0ef7ef260137c658 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Tue, 23 Sep 2025 12:10:16 +0200 Subject: [PATCH] revisat instructions.cpp --- source/director.cpp | 2 +- source/sections/instructions.cpp | 48 +++++++++++++++++--------------- source/sections/instructions.h | 12 ++++++-- 3 files changed, 36 insertions(+), 26 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 b154006..3bc1c9b 100644 --- a/source/sections/instructions.cpp +++ b/source/sections/instructions.cpp @@ -88,24 +88,24 @@ void Instructions::iniSprites() { void Instructions::updateSprites() { SDL_FRect src_rect = {0, 0, param.game.item_size, param.game.item_size}; - // Disquito - src_rect.y = param.game.item_size * (((counter_ + 12) / 36) % 2); + // Disquito (desplazamiento 12/60 = 0.2s) + src_rect.y = param.game.item_size * (static_cast((elapsed_time_ + 0.2f) / SPRITE_ANIMATION_CYCLE_S) % 2); sprites_[0]->setSpriteClip(src_rect); - // Gavina - src_rect.y = param.game.item_size * (((counter_ + 9) / 36) % 2); + // Gavina (desplazamiento 9/60 = 0.15s) + src_rect.y = param.game.item_size * (static_cast((elapsed_time_ + 0.15f) / SPRITE_ANIMATION_CYCLE_S) % 2); sprites_[1]->setSpriteClip(src_rect); - // Pacmar - src_rect.y = param.game.item_size * (((counter_ + 6) / 36) % 2); + // Pacmar (desplazamiento 6/60 = 0.1s) + src_rect.y = param.game.item_size * (static_cast((elapsed_time_ + 0.1f) / SPRITE_ANIMATION_CYCLE_S) % 2); sprites_[2]->setSpriteClip(src_rect); - // Time Stopper - src_rect.y = param.game.item_size * (((counter_ + 3) / 36) % 2); + // Time Stopper (desplazamiento 3/60 = 0.05s) + src_rect.y = param.game.item_size * (static_cast((elapsed_time_ + 0.05f) / SPRITE_ANIMATION_CYCLE_S) % 2); sprites_[3]->setSpriteClip(src_rect); - // Coffee - src_rect.y = param.game.item_size * (((counter_ + 0) / 36) % 2); + // Coffee (sin desplazamiento) + src_rect.y = param.game.item_size * (static_cast(elapsed_time_ / SPRITE_ANIMATION_CYCLE_S) % 2); sprites_[4]->setSpriteClip(src_rect); } @@ -205,11 +205,11 @@ void Instructions::fillBackbuffer() { // Actualiza las variables void Instructions::update(float delta_time) { - Screen::get()->update(); // Actualiza el objeto screen + elapsed_time_ += delta_time; // Incrementa el tiempo transcurrido + Screen::get()->update(); // Actualiza el objeto screen - counter_++; // Incrementa el contador updateSprites(); // Actualiza los sprites - updateBackbuffer(); // Gestiona la textura con los graficos + updateBackbuffer(delta_time); // 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 @@ -255,7 +255,7 @@ void Instructions::checkInput() { // 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_); + const float delta_time = static_cast(current_time - last_time_) / 1000.0f; // Convertir ms a segundos last_time_ = current_time; return delta_time; } @@ -324,23 +324,27 @@ void Instructions::renderLines(SDL_Renderer *renderer, SDL_Texture *texture, con } // Gestiona la textura con los graficos -void Instructions::updateBackbuffer() { - // Establece la ventana del backbuffer - view_.y = std::max(0.0F, param.game.height - counter_ + 100); +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); // Verifica si view_.y == 0 y gestiona el temporizador if (view_.y == 0) { if (!start_delay_triggered_) { // Activa el temporizador si no ha sido activado start_delay_triggered_ = true; - start_delay_time_ = SDL_GetTicks(); - } else if (SDL_GetTicks() - start_delay_time_ >= 4000) { - // Han pasado tres segundos, mover líneas - all_lines_off_screen_ = moveLines(lines_, 320, 1.0F, 5); + start_delay_timer_ = 0.0f; + } else { + 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)); + } } } - // Comprueba si el contador ha llegado al final + // Comprueba si todas las líneas han terminado if (all_lines_off_screen_) { Section::name = Section::Name::TITLE; Section::options = Section::Options::TITLE_1; diff --git a/source/sections/instructions.h b/source/sections/instructions.h index 354b2b7..6918d82 100644 --- a/source/sections/instructions.h +++ b/source/sections/instructions.h @@ -50,6 +50,12 @@ class Instructions { void run(); private: + // --- Constantes de tiempo (en segundos) --- + 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) + // --- Objetos y punteros --- SDL_Renderer *renderer_; // El renderizador de la ventana SDL_Texture *texture_; // Textura fija con el texto @@ -62,14 +68,14 @@ class Instructions { std::unique_ptr fade_; // Objeto para renderizar fades // --- Variables --- - int counter_ = 0; // Contador para manejar el progreso en la pantalla de instrucciones + float elapsed_time_ = 0.0f; // Tiempo transcurrido (segundos) 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 std::vector lines_; // Vector que contiene las líneas animadas en la pantalla bool all_lines_off_screen_ = false; // Indica si todas las líneas han salido de la pantalla - Uint32 start_delay_time_ = 0; // Tiempo de inicio del retraso para mover las líneas + float start_delay_timer_ = 0.0f; // Timer para retraso antes de mover líneas (segundos) bool start_delay_triggered_ = false; // Bandera para determinar si el retraso ha comenzado // --- Métodos internos --- @@ -84,6 +90,6 @@ class Instructions { 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 - void updateBackbuffer(); // Gestiona la textura con los gráficos + void updateBackbuffer(float delta_time); // Gestiona la textura con los gráficos float calculateDeltaTime(); // Calcula el tiempo transcurrido desde el último frame }; \ No newline at end of file