diff --git a/source/credits.cpp b/source/credits.cpp index 57fb9c3..1b605cc 100644 --- a/source/credits.cpp +++ b/source/credits.cpp @@ -44,13 +44,13 @@ Credits::Credits() fade_in_->setColor(fade_color.r, fade_color.g, fade_color.b); fade_in_->setType(FadeType::FULLSCREEN); - fade_in_->setPost(50); + fade_in_->setPostDuration(50); fade_in_->setMode(FadeMode::IN); fade_in_->activate(); fade_out_->setColor(0, 0, 0); fade_out_->setType(FadeType::FULLSCREEN); - fade_out_->setPost(400); + fade_out_->setPostDuration(400); initPlayers(); SDL_SetTextureBlendMode(text_texture_, SDL_BLENDMODE_BLEND); diff --git a/source/fade.cpp b/source/fade.cpp index 06f61ce..f411546 100644 --- a/source/fade.cpp +++ b/source/fade.cpp @@ -30,15 +30,15 @@ void Fade::init() { type_ = FadeType::CENTER; mode_ = FadeMode::OUT; - enabled_ = false; - finished_ = false; counter_ = 0; r_ = 0; g_ = 0; b_ = 0; a_ = 0; - post_duration_ = 20; + post_duration_ = 0; post_counter_ = 0; + pre_duration_ = 0; + pre_counter_ = 0; num_squares_width_ = param.fade.num_squares_width; num_squares_height_ = param.fade.num_squares_height; fade_random_squares_delay_ = param.fade.random_squares_delay; @@ -48,15 +48,14 @@ void Fade::init() // Resetea algunas variables para volver a hacer el fade sin perder ciertos parametros void Fade::reset() { - enabled_ = false; - finished_ = false; + state_ = FadeState::NOT_ENABLED; counter_ = 0; } // Pinta una transición en pantalla void Fade::render() { - if (enabled_ || finished_) + //if (state_ != FadeState::NOT_ENABLED) { SDL_RenderCopy(renderer_, backbuffer_, nullptr, nullptr); } @@ -65,7 +64,20 @@ void Fade::render() // Actualiza las variables internas void Fade::update() { - if (enabled_) + if (state_ == FadeState::PRE) + { + // Actualiza el contador + if (pre_counter_ == pre_duration_) + { + state_ = FadeState::FADING; + } + else + { + pre_counter_++; + } + } + + if (state_ == FadeState::FADING) { switch (type_) { @@ -79,7 +91,7 @@ void Fade::update() // Comprueba si ha terminado if (counter_ >= 255 / 4) { - finished_ = true; + state_ = FadeState::POST; } break; @@ -110,7 +122,7 @@ void Fade::update() // Comprueba si ha terminado if ((counter_ * 4) > param.game.height) { - finished_ = true; + state_ = FadeState::POST; a_ = 255; } break; @@ -143,7 +155,7 @@ void Fade::update() // Comprueba si ha terminado if (counter_ * fade_random_squares_mult_ / fade_random_squares_delay_ >= num_squares_width_ * num_squares_height_) { - finished_ = true; + state_ = FadeState::POST; } break; @@ -189,39 +201,47 @@ void Fade::update() } else { - finished_ = true; + state_ = FadeState::POST; } break; } + default: + break; } - - if (finished_) - { - // Actualiza el contador - post_counter_ == post_duration_ ? enabled_ = false : post_counter_++; - - // Deja el backbuffer_ todo del mismo color - cleanBackbuffer(r_, g_, b_, a_); - } - counter_++; } + + if (state_ == FadeState::POST) + { + // Actualiza el contador + if (post_counter_ == post_duration_) + { + state_ = FadeState::FINISHED; + } + else + { + post_counter_++; + } + + // Deja el backbuffer_ todo del mismo color + cleanBackbuffer(r_, g_, b_, a_); + } } // Activa el fade void Fade::activate() { // Si ya está habilitado, no hay que volverlo a activar - if (enabled_) + if (state_ != FadeState::NOT_ENABLED) { return; } - enabled_ = true; - finished_ = false; + state_ = FadeState::PRE; counter_ = 0; post_counter_ = 0; + pre_counter_ = 0; switch (type_) { @@ -326,8 +346,14 @@ void Fade::cleanBackbuffer(Uint8 r, Uint8 g, Uint8 b, Uint8 a) int Fade::calculateValue(int min, int max, int current) { if (current < min) + { return 0; + } + if (current > max) + { return 100; + } + return static_cast(100.0 * (current - min) / (max - min)); } \ No newline at end of file diff --git a/source/fade.h b/source/fade.h index b41dc6d..4fee30c 100644 --- a/source/fade.h +++ b/source/fade.h @@ -21,6 +21,16 @@ enum class FadeMode : Uint8 OUT = 1, }; +// Estados del objeto +enum class FadeState : Uint8 +{ + NOT_ENABLED = 0, + PRE = 1, + FADING = 2, + POST = 3, + FINISHED = 4, +}; + // Clase Fade class Fade { @@ -30,22 +40,23 @@ private: SDL_Texture *backbuffer_; // Textura para usar como backbuffer con SDL_TEXTUREACCESS_TARGET // Variables - FadeType type_; // Tipo de fade a realizar - FadeMode mode_; // Modo de fade a realizar - Uint16 counter_; // Contador interno - bool enabled_; // Indica si el fade está activo - bool finished_; // Indica si ha terminado la transición - Uint8 r_, g_, b_, a_; // Colores para el fade - SDL_Rect rect1_; // Rectangulo usado para crear los efectos de transición - SDL_Rect rect2_; // Rectangulo usado para crear los efectos de transición - int num_squares_width_; // Cantidad total de cuadraditos en horizontal para el FadeType::RANDOM_SQUARE - int num_squares_height_; // Cantidad total de cuadraditos en vertical para el FadeType::RANDOM_SQUARE - std::vector square_; // Vector con los indices de los cuadrados para el FadeType::RANDOM_SQUARE - int fade_random_squares_delay_; // Duración entre cada pintado de cuadrados - 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 + FadeType type_; // Tipo de fade a realizar + FadeMode mode_; // Modo de fade a realizar + FadeState state_ = FadeState::NOT_ENABLED; // Estado actual del objeto + Uint16 counter_; // Contador interno + Uint8 r_, g_, b_, a_; // Colores para el fade + SDL_Rect rect1_; // Rectangulo usado para crear los efectos de transición + SDL_Rect rect2_; // Rectangulo usado para crear los efectos de transición + int num_squares_width_; // Cantidad total de cuadraditos en horizontal para el FadeType::RANDOM_SQUARE + int num_squares_height_; // Cantidad total de cuadraditos en vertical para el FadeType::RANDOM_SQUARE + std::vector square_; // Vector con los indices de los cuadrados para el FadeType::RANDOM_SQUARE + int fade_random_squares_delay_; // Duración entre cada pintado de cuadrados + int fade_random_squares_mult_; // Cantidad de cuadrados que se pintaran cada vez + int post_duration_ = 0; // Duración posterior del fade tras finalizar + int post_counter_ = 0; // Contador para la duración posterior + int pre_duration_ = 0; // Duración previa del fade antes de iniciar + int pre_counter_ = 0; // Contador para la duración previa + int value_ = 0; // Estado actual del fade entre 0 y 100 // Inicializa las variables void init(); @@ -80,11 +91,12 @@ public: // Getters int getValue() const { return value_; } - bool isEnabled() const { return enabled_; } - bool hasEnded() const { return !enabled_ && finished_; } + bool isEnabled() const { return state_ != FadeState::NOT_ENABLED; } + bool hasEnded() const { return state_ == FadeState::FINISHED; } // Setters void setType(FadeType type) { type_ = type; } void setMode(FadeMode mode) { mode_ = mode; } - void setPost(int value) { post_duration_ = value; } + void setPostDuration(int value) { post_duration_ = value; } + void setPreDuration(int value) { pre_duration_ = value; } }; \ No newline at end of file diff --git a/source/game.cpp b/source/game.cpp index 835f3cf..6fe04ed 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -68,13 +68,14 @@ Game::Game(int player_id, int current_stage, bool demo) scoreboard_ = Scoreboard::get(); fade_in_->setColor(fade_color.r, fade_color.g, fade_color.b); - fade_in_->setPost(0); + fade_in_->setPreDuration(demo_.enabled ? 80 : 0); + fade_in_->setPostDuration(0); 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_->setPostDuration(param.fade.post_duration); fade_out_->setType(FadeType::VENETIAN); background_->setPos(param.game.play_area.rect); @@ -1490,7 +1491,8 @@ void Game::handleFireInput(const std::shared_ptr &player, BulletType bul JA_PlaySound(Resource::get()->getSound("bullet.wav")); // Establece un tiempo de espera para el próximo disparo. - const int cooldown = player->isPowerUp() ? 5 : options.game.autofire ? 10 : 7; + const int cooldown = player->isPowerUp() ? 5 : options.game.autofire ? 10 + : 7; player->setFireCooldown(cooldown); } } @@ -1678,7 +1680,7 @@ void Game::initDemo(int player_id) for (int i = 0; i < rand() % 3; ++i) player->giveExtraHit(); - player->setInvulnerable(false); + player->setInvulnerable(true); } // Deshabilita los sonidos diff --git a/source/hiscore_table.cpp b/source/hiscore_table.cpp index fd1df20..36b393b 100644 --- a/source/hiscore_table.cpp +++ b/source/hiscore_table.cpp @@ -45,7 +45,7 @@ HiScoreTable::HiScoreTable() background_->setTransition(0.8f); fade_->setColor(fade_color.r, fade_color.g, fade_color.b); fade_->setType(FadeType::RANDOM_SQUARE); - fade_->setPost(param.fade.post_duration); + fade_->setPostDuration(param.fade.post_duration); fade_->setMode(fade_mode_); fade_->activate(); @@ -197,7 +197,7 @@ void HiScoreTable::checkEvents() reloadTextures(); } } - + // Comprueba el cursor Mouse::handleEvent(event); } diff --git a/source/instructions.cpp b/source/instructions.cpp index 6be937d..d08e25e 100644 --- a/source/instructions.cpp +++ b/source/instructions.cpp @@ -42,7 +42,7 @@ Instructions::Instructions() // Inicializa objetos fade_->setColor(fade_color.r, fade_color.g, fade_color.b); fade_->setType(FadeType::FULLSCREEN); - fade_->setPost(param.fade.post_duration); + fade_->setPostDuration(param.fade.post_duration); fade_->setMode(FadeMode::IN); fade_->activate(); diff --git a/source/section.h b/source/section.h index 91b7649..732fc67 100644 --- a/source/section.h +++ b/source/section.h @@ -5,30 +5,31 @@ namespace section // Secciones del programa enum class Name { - INIT = 0, - LOGO = 1, - INTRO = 2, - TITLE = 3, - GAME = 4, - HI_SCORE_TABLE = 5, - GAME_DEMO = 6, - INSTRUCTIONS = 7, - CREDITS = 8, - QUIT = 9, + INIT, + LOGO, + INTRO, + TITLE, + GAME, + HI_SCORE_TABLE, + GAME_DEMO, + INSTRUCTIONS, + CREDITS, + QUIT, }; // Opciones para la sección enum class Options { - GAME_PLAY_1P = 0, - GAME_PLAY_2P = 1, - TITLE_1 = 2, - TITLE_2 = 3, - QUIT_WITH_KEYBOARD = 4, - QUIT_WITH_CONTROLLER = 5, - QUIT_FROM_EVENT = 6, - RELOAD = 7, - NONE = 8, + GAME_PLAY_1P, + GAME_PLAY_2P, + TITLE_TIME_OUT, + TITLE_1, + TITLE_2, + QUIT_WITH_KEYBOARD, + QUIT_WITH_CONTROLLER, + QUIT_FROM_EVENT, + RELOAD, + NONE, }; // Variables para el Attract Mode diff --git a/source/title.cpp b/source/title.cpp index 5c06fef..f66e037 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -42,7 +42,7 @@ Title::Title() mini_logo_sprite_->setX(param.game.game_area.center_x - mini_logo_sprite_->getWidth() / 2); fade_->setColor(fade_color.r, fade_color.g, fade_color.b); fade_->setType(FadeType::RANDOM_SQUARE); - fade_->setPost(param.fade.post_duration); + fade_->setPostDuration(param.fade.post_duration); Resource::get()->getTexture("smb2.gif")->setPalette(1); // Asigna valores a otras variables @@ -79,14 +79,16 @@ void Title::update() fade_->update(); if (fade_->hasEnded()) { - if (post_fade_ == -1) + if (selection_ == section::Options::TITLE_TIME_OUT) { + // El menu ha hecho time out section::name = next_section_; } else { + // Se ha pulsado para jugar section::name = section::Name::GAME; - section::options = post_fade_ == 1 ? section::Options::GAME_PLAY_1P : section::Options::GAME_PLAY_2P; + section::options = selection_; JA_StopMusic(); } } @@ -122,8 +124,10 @@ void Title::update() if (counter_ == param.title.title_duration) { + // El menu ha hecho time out + fade_->setPostDuration(0); fade_->activate(); - post_fade_ = -1; + selection_ = section::Options::TITLE_TIME_OUT; } break; @@ -295,7 +299,18 @@ void Title::checkInput() { JA_PlaySound(Resource::get()->getSound("game_start.wav")); JA_FadeOutMusic(1500); - post_fade_ = controller.player_id; + switch (controller.player_id) + { + case 1: + selection_ = section::Options::GAME_PLAY_1P; + break; + case 2: + selection_ = section::Options::GAME_PLAY_2P; + break; + default: + selection_ = section::Options::TITLE_TIME_OUT; + break; + } state_ = TitleState::START_HAS_BEEN_PRESSED; counter_ = 0; return; diff --git a/source/title.h b/source/title.h index 763c4d4..138e22a 100644 --- a/source/title.h +++ b/source/title.h @@ -9,10 +9,7 @@ class Sprite; // lines 9-9 class Text; // lines 10-10 class Texture; // lines 11-11 class TiledBG; // lines 12-12 -namespace section -{ - enum class Name; -} +#include "section.h" // Textos constexpr const char TEXT_COPYRIGHT[] = "@2020,2025 JailDesigner"; @@ -57,12 +54,12 @@ private: std::unique_ptr define_buttons_; // Objeto para definir los botones del joystic // Variable - int counter_ = 0; // Temporizador para la pantalla de titulo - Uint32 ticks_ = 0; // Contador de ticks para ajustar la velocidad del programa - section::Name next_section_; // Indica cual es la siguiente sección a cargar cuando termine el contador del titulo - int post_fade_ = 0; // Opción a realizar cuando termina el fundido - int num_controllers_; // Número de mandos conectados - TitleState state_; // Estado en el que se encuentra la sección + int counter_ = 0; // Temporizador para la pantalla de titulo + Uint32 ticks_ = 0; // Contador de ticks para ajustar la velocidad del programa + section::Name next_section_; // Indica cual es la siguiente sección a cargar cuando termine el contador del titulo + section::Options selection_ = section::Options::TITLE_TIME_OUT; // Opción elegida en el titulo + int num_controllers_; // Número de mandos conectados + TitleState state_; // Estado en el que se encuentra la sección // Actualiza las variables del objeto void update();