From ca2c48ea173ac94379d58b442d81c533992d0ce0 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Mon, 23 Dec 2024 13:56:11 +0100 Subject: [PATCH] Au, paca casa. M'he quedat a mitjes fent un fade de audio sincronitzat amb el fade de video en el titol --- source/director.cpp | 2 +- source/fade.cpp | 12 ++++++++++++ source/fade.h | 7 +++++++ source/game.cpp | 45 ++++++++++++++++++++++----------------------- source/game.h | 3 ++- source/title.cpp | 1 + 6 files changed, 45 insertions(+), 25 deletions(-) diff --git a/source/director.cpp b/source/director.cpp index 7dc5550..ff848db 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -54,7 +54,7 @@ Director::Director(int argc, const char *argv[]) section::name = section::Name::GAME; section::options = section::Options::GAME_PLAY_1P; #elif DEBUG - section::name = section::Name::GAME; + section::name = section::Name::LOGO; #else // NORMAL GAME section::name = section::Name::LOGO; section::attract_mode = section::AttractMode::TITLE_TO_DEMO; diff --git a/source/fade.cpp b/source/fade.cpp index 160dea8..6042fe0 100644 --- a/source/fade.cpp +++ b/source/fade.cpp @@ -100,6 +100,8 @@ void Fade::update() SDL_RenderFillRect(renderer_, &rect1_); SDL_RenderFillRect(renderer_, &rect2_); + + value_ = calculateValue(0, counter_, i); } // Deja el renderizador como estaba @@ -139,6 +141,8 @@ void Fade::update() SDL_SetRenderTarget(renderer_, temp); } + value_ = calculateValue(0, static_cast(num_squares_width_ * num_squares_height_), static_cast(counter_ * fade_random_squares_mult_ / fade_random_squares_delay_)); + // Comprueba si ha terminado if (counter_ * fade_random_squares_mult_ / fade_random_squares_delay_ >= num_squares_width_ * num_squares_height_) { @@ -337,4 +341,12 @@ void Fade::cleanBackbuffer(Uint8 r, Uint8 g, Uint8 b, Uint8 a) // Vuelve a dejar el renderizador como estaba SDL_SetRenderTarget(renderer_, temp); +} + +// Calcula el valor del estado del fade +int Fade::calculateValue(int min, int max, int current) +{ + if (max == 0) + return 0; + return std::clamp(current * 100 / max, 0, 100); } \ No newline at end of file diff --git a/source/fade.h b/source/fade.h index 156ea36..f10950b 100644 --- a/source/fade.h +++ b/source/fade.h @@ -45,6 +45,7 @@ private: int fade_random_squares_mult_; // Cantidad de cuadrados que se pintaran cada vez int post_duration_; // Duración posterior del fade tras finalizar int post_counter_; // Contador para la duración posterior + int value_ = 0; // Estado actual del fade entre 0 y 100 // Inicializa las variables void init(); @@ -52,6 +53,9 @@ private: // Limpia el backbuffer void cleanBackbuffer(Uint8 r, Uint8 g, Uint8 b, Uint8 a); + // Calcula el valor del estado del fade + int calculateValue(int min, int max, int current); + public: // Constructor Fade(); @@ -88,4 +92,7 @@ public: // Establece la duración posterior void setPost(int value); + + // Getters + int getValue() const { return value_; } }; \ No newline at end of file diff --git a/source/game.cpp b/source/game.cpp index c62d80f..b12365a 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -44,7 +44,8 @@ Game::Game(int player_id, int current_stage, bool demo) input_(Input::get()), background_(std::make_unique()), canvas_(SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, param.game.play_area.rect.w, param.game.play_area.rect.h)), - fade_(std::make_unique()), + fade_in_(std::make_unique()), + fade_out_(std::make_unique()), balloon_manager_(std::make_unique()) { // Pasa variables @@ -63,9 +64,15 @@ Game::Game(int player_id, int current_stage, bool demo) Scoreboard::init(); scoreboard_ = Scoreboard::get(); - fade_->setColor(fade_color.r, fade_color.g, fade_color.b); - fade_->setPost(param.fade.post_duration); - fade_->setType(FadeType::VENETIAN); + fade_in_->setColor(fade_color.r, fade_color.g, fade_color.b); + fade_in_->setPost(param.fade.post_duration); + fade_in_->setType(FadeType::RANDOM_SQUARE); + fade_in_->setMode(FadeMode::IN); + fade_in_->activate(); + + fade_out_->setColor(fade_color.r, fade_color.g, fade_color.b); + fade_out_->setPost(param.fade.post_duration); + fade_out_->setType(FadeType::VENETIAN); background_->setPos(param.game.play_area.rect); @@ -213,8 +220,8 @@ void Game::updatePlayers() if (demo_.enabled && allPlayersAreNotPlaying()) { - fade_->setType(FadeType::RANDOM_SQUARE); - fade_->activate(); + fade_out_->setType(FadeType::RANDOM_SQUARE); + fade_out_->activate(); } } @@ -291,23 +298,13 @@ void Game::updateGameOverState() game_over_counter_--; - /* - if ((game_over_counter_ == 250) || (game_over_counter_ == 200) || (game_over_counter_ == 180) || (game_over_counter_ == 120) || (game_over_counter_ == 60)) - { - // Hace sonar aleatoriamente uno de los 4 sonidos de burbujas - const auto index = rand() % 4; - JA_Sound_t *sound[4] = {Resource::get()->getSound("bubble1.wav"), Resource::get()->getSound("bubble2.wav"), Resource::get()->getSound("bubble3.wav"), Resource::get()->getSound("bubble4.wav")}; - JA_PlaySound(sound[index], 0); - } - */ - if (game_over_counter_ == 150) { - fade_->activate(); + fade_out_->activate(); } } - if (fade_->hasEnded()) + if (fade_out_->hasEnded()) { if (game_completed_counter_ > 0) { @@ -945,7 +942,8 @@ void Game::render() scoreboard_->render(); // Dibuja el fade - fade_->render(); + fade_in_->render(); + fade_out_->render(); // Vuelca el contenido del renderizador en pantalla screen_->blit(); @@ -1729,12 +1727,12 @@ void Game::updateDemo() // Activa el fundido antes de acabar con los datos de la demo if (demo_.counter == TOTAL_DEMO_DATA - 200) { - fade_->setType(FadeType::RANDOM_SQUARE); - fade_->activate(); + fade_out_->setType(FadeType::RANDOM_SQUARE); + fade_out_->activate(); } // Si ha terminado el fundido, cambia de sección - if (fade_->hasEnded()) + if (fade_out_->hasEnded()) { section::name = section::Name::HI_SCORE_TABLE; return; @@ -1773,7 +1771,8 @@ void Game::updateGame() Stage::addPower(5); } #endif - fade_->update(); + fade_in_->update(); + fade_out_->update(); updatePlayers(); checkPlayersStatusPlaying(); updateScoreboard(); diff --git a/source/game.h b/source/game.h index dcea838..6f142d4 100644 --- a/source/game.h +++ b/source/game.h @@ -140,7 +140,8 @@ private: std::vector> item_animations_; // Vector con las animaciones de los items std::vector> player_animations_; // Vector con las animaciones del jugador - std::unique_ptr fade_; // Objeto para renderizar fades + std::unique_ptr fade_in_; // Objeto para renderizar fades + std::unique_ptr fade_out_; // Objeto para renderizar fades std::unique_ptr balloon_manager_; // Objeto para gestionar los globos std::vector paths_; // Vector con los recorridos precalculados almacenados diff --git a/source/title.cpp b/source/title.cpp index cb0faff..a35be30 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -75,6 +75,7 @@ void Title::update() // Comprueba el fundido y si se ha acabado fade_->update(); + JA_SetMusicVolume(100 - fade_->getValue()); if (fade_->hasEnded()) { if (post_fade_ == -1)