From ff4e356013a9d889c02de8930b08bade42c27f61 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Fri, 14 Mar 2025 20:28:04 +0100 Subject: [PATCH] Treballant en la intro --- source/global_events.cpp | 5 +- source/intro.cpp | 114 +++++++++++++++++++++++++++++---------- source/intro.h | 27 ++++++++-- source/texture.cpp | 5 +- source/tiled_bg.cpp | 23 ++++---- source/tiled_bg.h | 7 +++ source/utils.cpp | 7 +++ source/utils.h | 1 + 8 files changed, 140 insertions(+), 49 deletions(-) diff --git a/source/global_events.cpp b/source/global_events.cpp index fa036d0..56879e1 100644 --- a/source/global_events.cpp +++ b/source/global_events.cpp @@ -2,6 +2,7 @@ #include "section.h" #include "mouse.h" #include "resource.h" +#include namespace globalEvents { @@ -16,12 +17,12 @@ namespace globalEvents return; case SDL_RENDER_DEVICE_RESET: case SDL_RENDER_TARGETS_RESET: - Resource::get()->reloadTextures(); + //Resource::get()->reloadTextures(); break; case SDL_WINDOWEVENT: if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { - Resource::get()->reloadTextures(); + //Resource::get()->reloadTextures(); } break; default: diff --git a/source/intro.cpp b/source/intro.cpp index 8bce81e..c78c141 100644 --- a/source/intro.cpp +++ b/source/intro.cpp @@ -36,6 +36,7 @@ Intro::Intro() // Configura el fondo tiled_bg_->setSpeed(0.3f); + tiled_bg_->setColor(Color(bg_color_, bg_color_, bg_color_)); } // Comprueba los eventos @@ -66,7 +67,6 @@ void Intro::updateScenes() if (sprites_.at(0)->hasFinished() && !texts_.at(0)->hasFinished()) { texts_.at(0)->setEnabled(true); - tiled_bg_->stopGracefully(); } // Segundo texto de la primera imagen @@ -195,16 +195,19 @@ void Intro::updateScenes() if (!texts_.at(8)->hasFinished()) { texts_.at(8)->setEnabled(true); - tiled_bg_->stopGracefully(); } // Acaba el último texto - if (sprites_.at(5)->hasFinished() && texts_.at(8)->hasFinished()) + if (texts_.at(8)->hasFinished()) { texts_.at(8)->setEnabled(false); - JA_StopMusic(); - section::name = section::Name::TITLE; - section::options = section::Options::TITLE_1; + } + + // Acaba la ultima imagen + if (sprites_.at(5)->hasFinished() && texts_.at(8)->hasFinished()) + { + state_ = IntroState::POST; + state_start_time_ = SDL_GetTicks(); } break; @@ -226,14 +229,18 @@ void Intro::update() // Actualiza el fondo tiled_bg_->update(); - // Actualiza los sprites - updateSprites(); + switch (state_) + { + case IntroState::SCENES: + updateSprites(); + updateTexts(); + updateScenes(); + break; - // Actualiza los textos - updateTexts(); - - // Actualiza las escenas de la intro - updateScenes(); + case IntroState::POST: + updatePostState(); + break; + } // Actualiza el objeto screen Screen::get()->update(); @@ -255,14 +262,17 @@ void Intro::render() // Dibuja el fondo tiled_bg_->render(); - SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0, 0, 0, 128); - SDL_RenderFillRect(Screen::get()->getRenderer(), ¶m.game.game_area.rect); - - // Dibuja los sprites - renderSprites(); - - // Dibuja los textos - renderTexts(); + switch (state_) + { + case IntroState::SCENES: + { + renderSprites(); + renderTexts(); + break; + } + case IntroState::POST: + break; + } // Vuelca el contenido del renderizador en pantalla Screen::get()->render(); @@ -316,7 +326,8 @@ void Intro::initSprites() sprites_.at(2)->addPath(-SPRITE_HEIGHT, Y_DEST, PathType::VERTICAL, X_DEST, 40, easeOutQuint, 0); sprites_.at(3)->addPath(param.game.height, Y_DEST, PathType::VERTICAL, X_DEST, 800, easeOutQuint, 0); sprites_.at(4)->addPath(-SPRITE_HEIGHT, Y_DEST, PathType::VERTICAL, X_DEST, 70, easeOutElastic, 0); - sprites_.at(5)->addPath(param.game.width, X_DEST, PathType::HORIZONTAL, Y_DEST, 400, easeOutQuint, 0); + sprites_.at(5)->addPath(param.game.width, X_DEST, PathType::HORIZONTAL, Y_DEST, 400, easeOutQuad, 300); + sprites_.at(5)->addPath(X_DEST, -SPRITE_WIDTH, PathType::HORIZONTAL, Y_DEST, 80, easeInElastic, 0); // Constantes const int BORDER = 4; @@ -334,20 +345,20 @@ void Intro::initSprites() auto shadow_texture = std::make_shared(Screen::get()->getRenderer()); shadow_texture->createBlank(SHADOW_SPRITE_WIDTH, SHADOW_SPRITE_HEIGHT, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET); shadow_texture->setBlendMode(SDL_BLENDMODE_BLEND); - + auto temp = SDL_GetRenderTarget(Screen::get()->getRenderer()); shadow_texture->setAsRenderTarget(Screen::get()->getRenderer()); - + SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0xFF, 0x00, 0x00, 0xFF); SDL_RenderClear(Screen::get()->getRenderer()); - + SDL_Rect rect = {BORDER / 2, BORDER / 2, SPRITE_WIDTH, SPRITE_HEIGHT}; auto texture = Resource::get()->getTexture(TEXTURE_LIST.at(i))->getSDLTexture(); SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_MOD); SDL_RenderCopy(Screen::get()->getRenderer(), texture, nullptr, &rect); SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE); - - shadow_texture->setAlpha(160); + + //shadow_texture->setAlpha(160); SDL_SetRenderTarget(Screen::get()->getRenderer(), temp); shadow_textures.push_back(shadow_texture); @@ -368,7 +379,8 @@ void Intro::initSprites() shadow_sprites_.at(2)->addPath(-SHADOW_SPRITE_WIDTH, S_X_DEST, PathType::HORIZONTAL, S_Y_DEST, 40, easeOutQuint, 0); shadow_sprites_.at(3)->addPath(param.game.width, S_X_DEST, PathType::HORIZONTAL, S_Y_DEST, 800, easeOutQuint, 0); shadow_sprites_.at(4)->addPath(param.game.height, S_Y_DEST, PathType::VERTICAL, S_X_DEST, 70, easeOutElastic, 0); - shadow_sprites_.at(5)->addPath(-SHADOW_SPRITE_HEIGHT, S_Y_DEST, PathType::VERTICAL, S_X_DEST, 400, easeOutQuint, 0); + shadow_sprites_.at(5)->addPath(-SHADOW_SPRITE_HEIGHT, S_Y_DEST, PathType::VERTICAL, S_X_DEST, 400, easeOutQuad, 300); + shadow_sprites_.at(5)->addPath(S_X_DEST, param.game.width, PathType::HORIZONTAL, S_Y_DEST, 80, easeInElastic, 0); } // Inicializa los textos @@ -465,4 +477,48 @@ void Intro::renderTexts() { text->render(); } -} \ No newline at end of file +} + +// Actualiza el estado POST +void Intro::updatePostState() +{ + const Uint32 ELAPSED_TIME = SDL_GetTicks() - state_start_time_; + + switch (post_state_) + { + case IntroPostState::STOP_BG: + // EVENTO: Detiene el fondo después de 2 segundos + if (ELAPSED_TIME >= 1000) + { + tiled_bg_->stopGracefully(); + + // Modifica el color del fondo hasta llegar a blanco + if (bg_color_ < 255) + { + ++bg_color_; + tiled_bg_->setColor(Color(bg_color_, bg_color_, bg_color_)); + } + } + + // Cambia de estado si el fondo se ha detenido y recuperado el color + if (tiled_bg_->isStopped() && bg_color_ == 255) + { + post_state_ = IntroPostState::END; + state_start_time_ = SDL_GetTicks(); + } + break; + + case IntroPostState::END: + // Finaliza la intro después de 3 segundos + if (ELAPSED_TIME >= 2000) + { + JA_StopMusic(); + section::name = section::Name::TITLE; + section::options = section::Options::TITLE_1; + } + break; + + default: + break; + } +} diff --git a/source/intro.h b/source/intro.h index 4cbdb44..52d0156 100644 --- a/source/intro.h +++ b/source/intro.h @@ -7,8 +7,8 @@ #include "path_sprite.h" // Para PathSprite #include "writer.h" // Para Writer #include "tiled_bg.h" -class Text; // lines 8-8 -class Texture; // lines 9-9 +class Text; // lines 8-8 +class Texture; // lines 9-9 /* Esta clase gestiona un estado del programa. Se encarga de mostrar la secuencia @@ -19,6 +19,18 @@ class Texture; // lines 9-9 class Intro { private: + enum class IntroState + { + SCENES, + POST, + }; + + enum class IntroPostState + { + STOP_BG, + END, + }; + // Objetos std::vector> sprites_; // Vector con los sprites inteligentes para los dibujos de la intro std::vector> shadow_sprites_; // Vector con los sprites inteligentes para las sombras @@ -26,8 +38,12 @@ private: std::unique_ptr tiled_bg_; // Variables - Uint32 ticks_ = 0; // Contador de ticks para ajustar la velocidad del programa - int scene_ = 0; // Indica que escena está activa + Uint32 ticks_ = 0; // Contador de ticks para ajustar la velocidad del programa + int scene_ = 0; // Indica que escena está activa + IntroState state_ = IntroState::SCENES; // Estado + IntroPostState post_state_ = IntroPostState::STOP_BG; // Estado + Uint32 state_start_time_; + Uint8 bg_color_ = 96; // Actualiza las variables del objeto void update(); @@ -62,6 +78,9 @@ private: // Dibuja los textos void renderTexts(); + // Actualiza el estado POST + void updatePostState(); + public: // Constructor Intro(); diff --git a/source/texture.cpp b/source/texture.cpp index 23dbe15..303b696 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -380,7 +380,4 @@ void Texture::setPalette(int palette) } // Obtiene el renderizador -SDL_Renderer *Texture::getRenderer() -{ - return renderer_; -} \ No newline at end of file +SDL_Renderer *Texture::getRenderer(){ return renderer_;} \ No newline at end of file diff --git a/source/tiled_bg.cpp b/source/tiled_bg.cpp index 59a4402..fab5de6 100644 --- a/source/tiled_bg.cpp +++ b/source/tiled_bg.cpp @@ -19,19 +19,22 @@ TiledBG::TiledBG(SDL_Rect pos, TiledBGMode mode) // Rellena la textura con el contenido fillTexture(); - mode_ = TiledBGMode::DIAGONAL; - - switch (mode) + // Inicializa variables + switch (mode_) { + case TiledBGMode::STATIC: + window_ = {0, 0, pos_.w, pos_.h}; + speed_ = 0.0f; + break; case TiledBGMode::DIAGONAL: - window_ = {0, 0, pos_.w, pos_.h}; - break; - + window_ = {0, 0, pos_.w, pos_.h}; + break; case TiledBGMode::CIRCLE: - window_ = {128, 96, pos_.w, pos_.h}; - break; - + window_ = {128, 128, pos_.w, pos_.h}; + speed_ = 0.8f; + break; default: + window_ = {0, 0, pos_.w, pos_.h}; break; } @@ -104,7 +107,7 @@ void TiledBG::update() const int INDEX = static_cast(desp_) % 360; window_.x = 128 + (static_cast(sin_[(INDEX + 270) % 360] * 128)); - window_.y = 96 + (static_cast(sin_[(360 - INDEX) % 360] * 96)); + window_.y = 128 + (static_cast(sin_[(360 - INDEX) % 360] * 96)); break; } default: diff --git a/source/tiled_bg.h b/source/tiled_bg.h index 818dd09..58a0ea5 100644 --- a/source/tiled_bg.h +++ b/source/tiled_bg.h @@ -2,6 +2,7 @@ #include // Para SDL_Rect #include // Para SDL_Renderer, SDL_Texture +#include "utils.h" // Modos de funcionamiento para el tileado de fondo enum class TiledBGMode : int @@ -67,4 +68,10 @@ public: // Detiene el desplazamiento de forma ordenada void stopGracefully() { stopping_ = true; } + + // Cambia el color de la textura + void setColor(Color color) { SDL_SetTextureColorMod(canvas_, color.r, color.g, color.b); } + + // Indica si está parado + bool isStopped() const { return speed_ == 0.0f; } }; \ No newline at end of file diff --git a/source/utils.cpp b/source/utils.cpp index 36a8a61..a89d92d 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -248,6 +248,13 @@ double easeInOutExpo(double t) return t == 0 ? 0 : (t == 1 ? 1 : (t < 0.5 ? pow(2, 20 * t - 10) / 2 : (2 - pow(2, -20 * t + 10)) / 2)); } +// Función de suavizado (easeInElastic) +double easeInElastic(double t) +{ + return t == 0 ? 0 : (t == 1 ? 1 : -pow(2, 10 * t - 10) * sin((t * 10 - 10.75) * (2 * M_PI) / 3)); +} + + // Función de suavizado double easeOutBounce(double t) { diff --git a/source/utils.h b/source/utils.h index 7d3523a..4c37253 100644 --- a/source/utils.h +++ b/source/utils.h @@ -177,6 +177,7 @@ double easeInOut(double t); double easeInOutExpo(double t); double easeOutBounce(double t); double easeOutElastic(double t); +double easeInElastic(double t); // Comprueba si una vector contiene una cadena bool stringInVector(const std::vector &vec, const std::string &str);