magic numbers: logo.cpp

This commit is contained in:
2025-09-17 13:01:43 +02:00
parent 66566913f6
commit 577510ff8c
3 changed files with 105 additions and 73 deletions

View File

@@ -28,38 +28,38 @@ Logo::Logo()
dest_.y = param.game.game_area.center_y - jail_texture_->getHeight() / 2;
since_sprite_->setPosition(SDL_FRect{
static_cast<float>((param.game.width - since_texture_->getWidth()) / 2),
static_cast<float>(83 + jail_texture_->getHeight() + 5),
static_cast<float>(SINCE_SPRITE_Y_OFFSET + jail_texture_->getHeight() + LOGO_SPACING),
static_cast<float>(since_texture_->getWidth()),
static_cast<float>(since_texture_->getHeight())});
since_sprite_->setY(dest_.y + jail_texture_->getHeight() + 5);
since_sprite_->setY(dest_.y + jail_texture_->getHeight() + LOGO_SPACING);
since_sprite_->setSpriteClip(0, 0, since_texture_->getWidth(), since_texture_->getHeight());
since_texture_->setColor(0x00, 0x00, 0x00);
since_texture_->setColor(SPECTRUM_BLACK.r, SPECTRUM_BLACK.g, SPECTRUM_BLACK.b);
// Crea los sprites de cada linea
for (int i = 0; i < jail_texture_->getHeight(); ++i) {
auto temp = std::make_unique<Sprite>(jail_texture_, 0, i, jail_texture_->getWidth(), 1);
temp->setSpriteClip(0, i, jail_texture_->getWidth(), 1);
const int POS_X = (i % 2 == 0) ? param.game.width + (i * 3) : -jail_texture_->getWidth() - (i * 3);
auto temp = std::make_unique<Sprite>(jail_texture_, 0, i, jail_texture_->getWidth(), SPRITE_LINE_HEIGHT);
temp->setSpriteClip(0, i, jail_texture_->getWidth(), SPRITE_LINE_HEIGHT);
const int POS_X = (i % 2 == 0) ? param.game.width + (i * LINE_OFFSET_FACTOR) : -jail_texture_->getWidth() - (i * LINE_OFFSET_FACTOR);
temp->setX(POS_X);
temp->setY(dest_.y + i);
jail_sprite_.push_back(std::move(temp));
}
// Inicializa el vector de colores
color_.emplace_back(0x00, 0x00, 0x00); // Black
color_.emplace_back(0x00, 0x00, 0xd8); // Blue
color_.emplace_back(0xd8, 0x00, 0x00); // Red
color_.emplace_back(0xd8, 0x00, 0xd8); // Magenta
color_.emplace_back(0x00, 0xd8, 0x00); // Green
color_.emplace_back(0x00, 0xd8, 0xd8); // Cyan
color_.emplace_back(0xd8, 0xd8, 0x00); // Yellow
color_.emplace_back(0xFF, 0xFF, 0xFF); // Bright white
// Inicializa el vector de colores con la paleta ZX Spectrum
color_.emplace_back(SPECTRUM_BLACK);
color_.emplace_back(SPECTRUM_BLUE);
color_.emplace_back(SPECTRUM_RED);
color_.emplace_back(SPECTRUM_MAGENTA);
color_.emplace_back(SPECTRUM_GREEN);
color_.emplace_back(SPECTRUM_CYAN);
color_.emplace_back(SPECTRUM_YELLOW);
color_.emplace_back(SPECTRUM_WHITE);
}
// Destructor
Logo::~Logo() {
jail_texture_->setColor(255, 255, 255);
since_texture_->setColor(255, 255, 255);
jail_texture_->setColor(RESET_COLOR.r, RESET_COLOR.g, RESET_COLOR.b);
since_texture_->setColor(RESET_COLOR.r, RESET_COLOR.g, RESET_COLOR.b);
Audio::get()->stopAllSounds();
Audio::get()->stopMusic();
}
@@ -78,24 +78,30 @@ void Logo::checkInput() {
GlobalInputs::check();
}
// Maneja la reproducción del sonido del logo
void Logo::handleSound() {
static bool sound_triggered = false;
if (!sound_triggered && elapsed_time_ms_ >= SOUND_TRIGGER_TIME_MS) {
Audio::get()->playSound("logo.wav");
sound_triggered = true;
}
}
// Gestiona el logo de JAILGAMES
void Logo::updateJAILGAMES(float delta_time) {
if (counter_ == 30) {
Audio::get()->playSound("logo.wav");
}
if (elapsed_time_ms_ > SOUND_TRIGGER_TIME_MS) {
const float PIXELS_TO_MOVE = LOGO_SPEED_PX_PER_MS * delta_time;
if (counter_ > 30) {
const float pixels_to_move = SPEED * delta_time;
for (int i = 0; i < (int)jail_sprite_.size(); ++i) {
for (size_t i = 0; i < jail_sprite_.size(); ++i) {
if (jail_sprite_[i]->getX() != dest_.x) {
if (i % 2 == 0) {
jail_sprite_[i]->incX(-pixels_to_move);
jail_sprite_[i]->incX(-PIXELS_TO_MOVE);
if (jail_sprite_[i]->getX() < dest_.x) {
jail_sprite_[i]->setX(dest_.x);
}
} else {
jail_sprite_[i]->incX(pixels_to_move);
jail_sprite_[i]->incX(PIXELS_TO_MOVE);
if (jail_sprite_[i]->getX() > dest_.x) {
jail_sprite_[i]->setX(dest_.x);
}
@@ -105,48 +111,41 @@ void Logo::updateJAILGAMES(float delta_time) {
}
// Comprueba si ha terminado el logo
if (counter_ == END_LOGO_COUNTER_MARK + POST_LOGO_DURATION) {
if (elapsed_time_ms_ >= END_LOGO_TIME_MS + POST_LOGO_DURATION_MS) {
Section::name = Section::Name::INTRO;
}
}
// Gestiona el color de las texturas
void Logo::updateTextureColors() {
constexpr int INC = 4;
void Logo::updateTextureColors(float delta_time) {
// Manejo de 'sinceTexture'
for (int i = 0; i <= 7; ++i) {
if (counter_ == SHOW_SINCE_SPRITE_COUNTER_MARK + INC * i) {
for (int i = 0; i <= MAX_SINCE_COLOR_INDEX; ++i) {
const float target_time = SHOW_SINCE_SPRITE_TIME_MS + COLOR_CHANGE_INTERVAL_MS * i;
if (elapsed_time_ms_ >= target_time && elapsed_time_ms_ - delta_time < target_time) {
since_texture_->setColor(color_[i].r, color_[i].g, color_[i].b);
}
}
// Manejo de 'jailTexture' y 'sinceTexture' en el fade
for (int i = 0; i <= 6; ++i) {
if (counter_ == INIT_FADE_COUNTER_MARK + INC * i) {
jail_texture_->setColor(color_[6 - i].r, color_[6 - i].g, color_[6 - i].b);
since_texture_->setColor(color_[6 - i].r, color_[6 - i].g, color_[6 - i].b);
for (int i = 0; i <= MAX_FADE_COLOR_INDEX; ++i) {
const float target_time = INIT_FADE_TIME_MS + COLOR_CHANGE_INTERVAL_MS * i;
if (elapsed_time_ms_ >= target_time && elapsed_time_ms_ - delta_time < target_time) {
jail_texture_->setColor(color_[MAX_FADE_COLOR_INDEX - i].r, color_[MAX_FADE_COLOR_INDEX - i].g, color_[MAX_FADE_COLOR_INDEX - i].b);
since_texture_->setColor(color_[MAX_FADE_COLOR_INDEX - i].r, color_[MAX_FADE_COLOR_INDEX - i].g, color_[MAX_FADE_COLOR_INDEX - i].b);
}
}
}
// Actualiza las variables
void Logo::update(float delta_time) {
static float logic_accumulator = 0.0f;
logic_accumulator += delta_time;
elapsed_time_ms_ += delta_time; // Acumula el tiempo transcurrido
// Ejecutar lógica a 60 FPS (cada 16.67ms) para mantener consistencia en counter_ y colores
constexpr float LOGIC_FRAME_TIME = 1000.0f / 60.0f;
Screen::get()->update(); // Actualiza el objeto screen
Audio::update(); // Actualiza el objeto audio
if (logic_accumulator >= LOGIC_FRAME_TIME) {
Screen::get()->update(); // Actualiza el objeto screen
updateTextureColors(); // Actualiza los colores de las texturas
++counter_; // Gestiona el contador
logic_accumulator -= LOGIC_FRAME_TIME;
}
updateJAILGAMES(delta_time); // Actualiza el logo de JAILGAMES con delta-time real
Audio::update();
handleSound(); // Maneja la reproducción del sonido
updateTextureColors(delta_time); // Actualiza los colores de las texturas
updateJAILGAMES(delta_time); // Actualiza el logo de JAILGAMES
}
// Dibuja en pantalla
@@ -190,7 +189,7 @@ void Logo::renderJAILGAMES() {
sprite->render();
}
if (counter_ >= SHOW_SINCE_SPRITE_COUNTER_MARK) {
if (elapsed_time_ms_ >= SHOW_SINCE_SPRITE_TIME_MS) {
since_sprite_->render();
}
}