migrat instructions a delta_time pur
This commit is contained in:
@@ -44,7 +44,7 @@ Director::Director(int argc, std::span<char*> argv) {
|
|||||||
Section::name = Section::Name::GAME;
|
Section::name = Section::Name::GAME;
|
||||||
Section::options = Section::Options::GAME_PLAY_1P;
|
Section::options = Section::Options::GAME_PLAY_1P;
|
||||||
#elif _DEBUG
|
#elif _DEBUG
|
||||||
Section::name = Section::Name::GAME;
|
Section::name = Section::Name::INSTRUCTIONS;
|
||||||
Section::options = Section::Options::GAME_PLAY_1P;
|
Section::options = Section::Options::GAME_PLAY_1P;
|
||||||
#else // NORMAL GAME
|
#else // NORMAL GAME
|
||||||
Section::name = Section::Name::LOGO;
|
Section::name = Section::Name::LOGO;
|
||||||
|
|||||||
@@ -49,8 +49,8 @@ Instructions::Instructions()
|
|||||||
fade_->setMode(Fade::Mode::IN);
|
fade_->setMode(Fade::Mode::IN);
|
||||||
fade_->activate();
|
fade_->activate();
|
||||||
|
|
||||||
// Inicializa las líneas con un retraso progresivo de 50 ms
|
// Inicializa las líneas con un retraso progresivo
|
||||||
lines_ = initializeLines(256);
|
lines_ = initializeLines(256, LINE_START_DELAY_S);
|
||||||
|
|
||||||
// Rellena la textura de texto
|
// Rellena la textura de texto
|
||||||
fillTexture();
|
fillTexture();
|
||||||
@@ -278,39 +278,53 @@ void Instructions::run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Método para inicializar las líneas
|
// Método para inicializar las líneas
|
||||||
auto Instructions::initializeLines(int height) -> std::vector<Line> {
|
auto Instructions::initializeLines(int height, float line_delay) -> std::vector<Line> {
|
||||||
std::vector<Line> lines;
|
std::vector<Line> lines;
|
||||||
for (int y = 0; y < height; y++) {
|
for (int y = 0; y < height; y++) {
|
||||||
int direction = (y % 2 == 0) ? -1 : 1; // Pares a la izquierda, impares a la derecha
|
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;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Método para mover las líneas con suavizado
|
// Método para mover las líneas con suavizado (usando delta_time puro)
|
||||||
auto Instructions::moveLines(std::vector<Line>& lines, int width, float duration, Uint32 start_delay) -> bool {
|
auto Instructions::moveLines(std::vector<Line>& lines, int width, float duration, float delta_time) -> bool {
|
||||||
Uint32 current_time = SDL_GetTicks();
|
|
||||||
bool all_lines_off_screen = true;
|
bool all_lines_off_screen = true;
|
||||||
|
|
||||||
for (auto& line : lines) {
|
for (auto& line : lines) {
|
||||||
// Establecer start_time en el primer cuadro de animación
|
// Verificar si la línea ha superado su tiempo de retraso
|
||||||
if (line.start_time == 0) {
|
if (!line.started) {
|
||||||
line.start_time = current_time + line.y * start_delay;
|
line.delay_time -= delta_time;
|
||||||
}
|
if (line.delay_time <= 0.0F) {
|
||||||
|
line.started = true;
|
||||||
float elapsed_time = (current_time - line.start_time) / 1000.0F; // Convertir a segundos
|
line.accumulated_time = 0.0F;
|
||||||
if (elapsed_time < 0) {
|
} else {
|
||||||
all_lines_off_screen = false; // Si aún no se debe mover esta línea, no están todas fuera de pantalla
|
all_lines_off_screen = false; // Aún hay líneas esperando para empezar
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (elapsed_time >= duration) {
|
|
||||||
continue; // Si la línea ha salido de los límites, no la muevas más
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float t = elapsed_time / duration;
|
// Si la línea ya ha completado su movimiento, saltarla
|
||||||
|
if (line.accumulated_time >= duration) {
|
||||||
|
continue; // Esta línea ya terminó
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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);
|
float smooth_factor = easeInOutQuint(t);
|
||||||
line.x = line.direction * smooth_factor * width;
|
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;
|
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
|
// Gestiona la textura con los graficos
|
||||||
void Instructions::updateBackbuffer(float delta_time) {
|
void Instructions::updateBackbuffer(float delta_time) {
|
||||||
// Establece la ventana del backbuffer (convertir elapsed_time_ a equivalente de counter)
|
// Establece la ventana del backbuffer usando velocidad en pixels por segundo
|
||||||
float counter_equivalent = elapsed_time_ * 60.0F; // Convertir segundos a equivalente frame para UI
|
// El scroll comienza desde (param.game.height + 100) y desciende a 0
|
||||||
view_.y = std::max(0.0F, param.game.height - counter_equivalent + 100);
|
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
|
// Verifica si view_.y == 0 y gestiona el temporizador
|
||||||
if (view_.y == 0) {
|
if (view_.y == 0.0F) {
|
||||||
if (!start_delay_triggered_) {
|
if (!start_delay_triggered_) {
|
||||||
// Activa el temporizador si no ha sido activado
|
// Activa el temporizador si no ha sido activado
|
||||||
start_delay_triggered_ = true;
|
start_delay_triggered_ = true;
|
||||||
@@ -341,7 +356,7 @@ void Instructions::updateBackbuffer(float delta_time) {
|
|||||||
start_delay_timer_ += delta_time;
|
start_delay_timer_ += delta_time;
|
||||||
if (start_delay_timer_ >= START_DELAY_S) {
|
if (start_delay_timer_ >= START_DELAY_S) {
|
||||||
// Han pasado los segundos de retraso, mover líneas
|
// Han pasado los segundos de retraso, mover líneas
|
||||||
all_lines_off_screen_ = moveLines(lines_, 320, LINE_MOVE_DURATION_S, static_cast<Uint32>(LINE_START_DELAY_MS));
|
all_lines_off_screen_ = moveLines(lines_, 320, LINE_MOVE_DURATION_S, delta_time);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,13 +29,16 @@ struct Line { // Almacena información de línea animada
|
|||||||
int y; // Coordenada Y de la línea
|
int y; // Coordenada Y de la línea
|
||||||
float x; // Coordenada X inicial (usamos float para mayor precisión en el suavizado)
|
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
|
int direction; // Dirección de movimiento: -1 para izquierda, 1 para derecha
|
||||||
Uint32 start_time{0}; // Tiempo de inicio del movimiento
|
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
|
// Constructor de Line
|
||||||
Line(int y, float x, int direction)
|
Line(int y, float x, int direction, float delay)
|
||||||
: y(y),
|
: y(y),
|
||||||
x(x),
|
x(x),
|
||||||
direction(direction) {}
|
direction(direction),
|
||||||
|
delay_time(delay) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Clase Instructions
|
// 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 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 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_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 ---
|
// --- Objetos y punteros ---
|
||||||
SDL_Renderer* renderer_; // El renderizador de la ventana
|
SDL_Renderer* renderer_; // El renderizador de la ventana
|
||||||
@@ -86,8 +90,8 @@ class Instructions {
|
|||||||
void fillBackbuffer(); // Rellena el backbuffer
|
void fillBackbuffer(); // Rellena el backbuffer
|
||||||
void iniSprites(); // Inicializa los sprites de los items
|
void iniSprites(); // Inicializa los sprites de los items
|
||||||
void updateSprites(); // Actualiza los sprites
|
void updateSprites(); // Actualiza los sprites
|
||||||
static auto initializeLines(int height) -> std::vector<Line>; // Inicializa las líneas animadas
|
static auto initializeLines(int height, float line_delay) -> std::vector<Line>; // Inicializa las líneas animadas
|
||||||
static auto moveLines(std::vector<Line>& lines, int width, float duration, Uint32 start_delay) -> bool; // Mueve las líneas (ya usa tiempo real)
|
static auto moveLines(std::vector<Line>& 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<Line>& lines); // Renderiza las líneas
|
static void renderLines(SDL_Renderer* renderer, SDL_Texture* texture, const std::vector<Line>& lines); // Renderiza las líneas
|
||||||
void updateBackbuffer(float delta_time); // Gestiona la textura con los gráficos
|
void updateBackbuffer(float delta_time); // Gestiona la textura con los gráficos
|
||||||
auto calculateDeltaTime() -> float; // Calcula el tiempo transcurrido desde el último frame
|
auto calculateDeltaTime() -> float; // Calcula el tiempo transcurrido desde el último frame
|
||||||
|
|||||||
Reference in New Issue
Block a user