revisat instructions.cpp
This commit is contained in:
@@ -42,7 +42,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::HI_SCORE_TABLE;
|
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;
|
||||||
|
|||||||
@@ -88,24 +88,24 @@ void Instructions::iniSprites() {
|
|||||||
void Instructions::updateSprites() {
|
void Instructions::updateSprites() {
|
||||||
SDL_FRect src_rect = {0, 0, param.game.item_size, param.game.item_size};
|
SDL_FRect src_rect = {0, 0, param.game.item_size, param.game.item_size};
|
||||||
|
|
||||||
// Disquito
|
// Disquito (desplazamiento 12/60 = 0.2s)
|
||||||
src_rect.y = param.game.item_size * (((counter_ + 12) / 36) % 2);
|
src_rect.y = param.game.item_size * (static_cast<int>((elapsed_time_ + 0.2f) / SPRITE_ANIMATION_CYCLE_S) % 2);
|
||||||
sprites_[0]->setSpriteClip(src_rect);
|
sprites_[0]->setSpriteClip(src_rect);
|
||||||
|
|
||||||
// Gavina
|
// Gavina (desplazamiento 9/60 = 0.15s)
|
||||||
src_rect.y = param.game.item_size * (((counter_ + 9) / 36) % 2);
|
src_rect.y = param.game.item_size * (static_cast<int>((elapsed_time_ + 0.15f) / SPRITE_ANIMATION_CYCLE_S) % 2);
|
||||||
sprites_[1]->setSpriteClip(src_rect);
|
sprites_[1]->setSpriteClip(src_rect);
|
||||||
|
|
||||||
// Pacmar
|
// Pacmar (desplazamiento 6/60 = 0.1s)
|
||||||
src_rect.y = param.game.item_size * (((counter_ + 6) / 36) % 2);
|
src_rect.y = param.game.item_size * (static_cast<int>((elapsed_time_ + 0.1f) / SPRITE_ANIMATION_CYCLE_S) % 2);
|
||||||
sprites_[2]->setSpriteClip(src_rect);
|
sprites_[2]->setSpriteClip(src_rect);
|
||||||
|
|
||||||
// Time Stopper
|
// Time Stopper (desplazamiento 3/60 = 0.05s)
|
||||||
src_rect.y = param.game.item_size * (((counter_ + 3) / 36) % 2);
|
src_rect.y = param.game.item_size * (static_cast<int>((elapsed_time_ + 0.05f) / SPRITE_ANIMATION_CYCLE_S) % 2);
|
||||||
sprites_[3]->setSpriteClip(src_rect);
|
sprites_[3]->setSpriteClip(src_rect);
|
||||||
|
|
||||||
// Coffee
|
// Coffee (sin desplazamiento)
|
||||||
src_rect.y = param.game.item_size * (((counter_ + 0) / 36) % 2);
|
src_rect.y = param.game.item_size * (static_cast<int>(elapsed_time_ / SPRITE_ANIMATION_CYCLE_S) % 2);
|
||||||
sprites_[4]->setSpriteClip(src_rect);
|
sprites_[4]->setSpriteClip(src_rect);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -205,11 +205,11 @@ void Instructions::fillBackbuffer() {
|
|||||||
|
|
||||||
// Actualiza las variables
|
// Actualiza las variables
|
||||||
void Instructions::update(float delta_time) {
|
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
|
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
|
tiled_bg_->update(delta_time); // Actualiza el mosaico de fondo
|
||||||
fade_->update(delta_time); // Actualiza el objeto "fade"
|
fade_->update(delta_time); // Actualiza el objeto "fade"
|
||||||
fillBackbuffer(); // Rellena el backbuffer
|
fillBackbuffer(); // Rellena el backbuffer
|
||||||
@@ -255,7 +255,7 @@ void Instructions::checkInput() {
|
|||||||
// Calcula el tiempo transcurrido desde el último frame
|
// Calcula el tiempo transcurrido desde el último frame
|
||||||
float Instructions::calculateDeltaTime() {
|
float Instructions::calculateDeltaTime() {
|
||||||
const Uint64 current_time = SDL_GetTicks();
|
const Uint64 current_time = SDL_GetTicks();
|
||||||
const float delta_time = static_cast<float>(current_time - last_time_);
|
const float delta_time = static_cast<float>(current_time - last_time_) / 1000.0f; // Convertir ms a segundos
|
||||||
last_time_ = current_time;
|
last_time_ = current_time;
|
||||||
return delta_time;
|
return delta_time;
|
||||||
}
|
}
|
||||||
@@ -324,23 +324,27 @@ 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() {
|
void Instructions::updateBackbuffer(float delta_time) {
|
||||||
// Establece la ventana del backbuffer
|
// Establece la ventana del backbuffer (convertir elapsed_time_ a equivalente de counter)
|
||||||
view_.y = std::max(0.0F, param.game.height - counter_ + 100);
|
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
|
// Verifica si view_.y == 0 y gestiona el temporizador
|
||||||
if (view_.y == 0) {
|
if (view_.y == 0) {
|
||||||
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;
|
||||||
start_delay_time_ = SDL_GetTicks();
|
start_delay_timer_ = 0.0f;
|
||||||
} else if (SDL_GetTicks() - start_delay_time_ >= 4000) {
|
} else {
|
||||||
// Han pasado tres segundos, mover líneas
|
start_delay_timer_ += delta_time;
|
||||||
all_lines_off_screen_ = moveLines(lines_, 320, 1.0F, 5);
|
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<Uint32>(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_) {
|
if (all_lines_off_screen_) {
|
||||||
Section::name = Section::Name::TITLE;
|
Section::name = Section::Name::TITLE;
|
||||||
Section::options = Section::Options::TITLE_1;
|
Section::options = Section::Options::TITLE_1;
|
||||||
|
|||||||
@@ -50,6 +50,12 @@ class Instructions {
|
|||||||
void run();
|
void run();
|
||||||
|
|
||||||
private:
|
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 ---
|
// --- Objetos y punteros ---
|
||||||
SDL_Renderer *renderer_; // El renderizador de la ventana
|
SDL_Renderer *renderer_; // El renderizador de la ventana
|
||||||
SDL_Texture *texture_; // Textura fija con el texto
|
SDL_Texture *texture_; // Textura fija con el texto
|
||||||
@@ -62,14 +68,14 @@ class Instructions {
|
|||||||
std::unique_ptr<Fade> fade_; // Objeto para renderizar fades
|
std::unique_ptr<Fade> fade_; // Objeto para renderizar fades
|
||||||
|
|
||||||
// --- Variables ---
|
// --- 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
|
Uint64 last_time_ = 0; // Último timestamp para calcular delta-time
|
||||||
SDL_FRect view_; // Vista del backbuffer que se va a mostrar por pantalla
|
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
|
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
|
float item_space_ = 2.0; // Espacio entre los items en pantalla
|
||||||
std::vector<Line> lines_; // Vector que contiene las líneas animadas en la pantalla
|
std::vector<Line> 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
|
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
|
bool start_delay_triggered_ = false; // Bandera para determinar si el retraso ha comenzado
|
||||||
|
|
||||||
// --- Métodos internos ---
|
// --- Métodos internos ---
|
||||||
@@ -84,6 +90,6 @@ class Instructions {
|
|||||||
static auto initializeLines(int height) -> std::vector<Line>; // Inicializa las líneas animadas
|
static auto initializeLines(int height) -> 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, Uint32 start_delay) -> bool; // Mueve las líneas (ya usa tiempo real)
|
||||||
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(); // 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
|
float calculateDeltaTime(); // Calcula el tiempo transcurrido desde el último frame
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user