From c757886430d1063d910e501490a179fc9a4ae0b4 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Fri, 14 Mar 2025 14:06:14 +0100 Subject: [PATCH] Treballant en la intro --- source/director.cpp | 9 +++--- source/intro.cpp | 40 ++++++++++++++++---------- source/intro.h | 3 +- source/tiled_bg.cpp | 68 ++++++++++++++++++++++++++++++++++++++------- source/tiled_bg.h | 25 +++++++++++++---- 5 files changed, 111 insertions(+), 34 deletions(-) diff --git a/source/director.cpp b/source/director.cpp index de3c446..ec33190 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -9,9 +9,6 @@ #include // Para SDL_SCANCODE_0, SDL_SCANCODE_DOWN #include // Para SDL_bool, Uint32 #include // Para errno, EEXIST, EACCES, ENAMETOO... -#ifndef _WIN32 -#include // Para getpwuid, passwd -#endif #include // Para printf, perror #include // Para mkdir, stat, S_IRWXU #include // Para getuid @@ -45,6 +42,10 @@ #include "title.h" // Para Title #include "utils.h" // Para Overrides, overrides +#ifndef _WIN32 +#include // Para getpwuid, passwd +#endif + // Constructor Director::Director(int argc, const char *argv[]) { @@ -52,7 +53,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::LOGO; + section::name = section::Name::INTRO; #else // NORMAL GAME section::name = section::Name::LOGO; section::options = section::Options::NONE; diff --git a/source/intro.cpp b/source/intro.cpp index a83a325..8bce81e 100644 --- a/source/intro.cpp +++ b/source/intro.cpp @@ -17,10 +17,12 @@ #include "utils.h" // Para Zone, BLOCK, Color, bg_color #include "writer.h" // Para Writer #include "global_events.h" +#include "tiled_bg.h" #include // Constructor Intro::Intro() + : tiled_bg_(std::make_unique(param.game.game_area.rect, TiledBGMode::DIAGONAL)) { // Inicializa variables section::name = section::Name::INTRO; @@ -31,6 +33,9 @@ Intro::Intro() // Inicializa los textos initTexts(); + + // Configura el fondo + tiled_bg_->setSpeed(0.3f); } // Comprueba los eventos @@ -61,6 +66,7 @@ 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 @@ -165,7 +171,7 @@ void Intro::updateScenes() shadow_sprites_.at(4)->enable(); // Primer texto de la quinta imagen - if (sprites_.at(4)->hasFinished() && !texts_.at(7)->hasFinished()) + if (!texts_.at(7)->hasFinished()) { texts_.at(7)->setEnabled(true); } @@ -189,6 +195,7 @@ void Intro::updateScenes() if (!texts_.at(8)->hasFinished()) { texts_.at(8)->setEnabled(true); + tiled_bg_->stopGracefully(); } // Acaba el último texto @@ -216,8 +223,8 @@ void Intro::update() // Actualiza el contador de ticks ticks_ = SDL_GetTicks(); - // Actualiza el objeto screen - Screen::get()->update(); + // Actualiza el fondo + tiled_bg_->update(); // Actualiza los sprites updateSprites(); @@ -228,6 +235,9 @@ void Intro::update() // Actualiza las escenas de la intro updateScenes(); + // Actualiza el objeto screen + Screen::get()->update(); + // Actualiza las variables de globalInputs globalInputs::update(); } @@ -242,6 +252,12 @@ void Intro::render() // Limpia la pantalla Screen::get()->clean(bg_color); + // 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(); @@ -295,7 +311,7 @@ void Intro::initSprites() sprites_.push_back(std::move(sprite)); } - sprites_.at(0)->addPath(-SPRITE_WIDTH, X_DEST, PathType::HORIZONTAL, Y_DEST, 100, easeInOutExpo, 0); + sprites_.at(0)->addPath(-SPRITE_WIDTH - 10, X_DEST, PathType::HORIZONTAL, Y_DEST, 100, easeInOutExpo, 0); sprites_.at(1)->addPath(param.game.width, X_DEST, PathType::HORIZONTAL, Y_DEST, 100, easeOutBounce, 0); 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); @@ -318,26 +334,22 @@ 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_SetTextureAlphaMod(texture, 128); SDL_RenderCopy(Screen::get()->getRenderer(), texture, nullptr, &rect); SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE); - - // SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0x54, 0x31, 0x49, 0xFF); - SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0xFF, 0xFF, 0xFF, 192); - //SDL_RenderFillRect(Screen::get()->getRenderer(), &rect); + + shadow_texture->setAlpha(160); SDL_SetRenderTarget(Screen::get()->getRenderer(), temp); - shadow_textures.push_back(shadow_texture); } @@ -351,7 +363,7 @@ void Intro::initSprites() shadow_sprites_.push_back(std::move(sprite)); } - shadow_sprites_.at(0)->addPath(param.game.height, S_Y_DEST, PathType::VERTICAL, S_X_DEST, 100, easeInOutExpo, 0); + shadow_sprites_.at(0)->addPath(param.game.height + 10, S_Y_DEST, PathType::VERTICAL, S_X_DEST, 100, easeInOutExpo, 0); shadow_sprites_.at(1)->addPath(-SHADOW_SPRITE_HEIGHT, S_Y_DEST, PathType::VERTICAL, S_X_DEST, 100, easeOutBounce, 0); 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); diff --git a/source/intro.h b/source/intro.h index d631223..4cbdb44 100644 --- a/source/intro.h +++ b/source/intro.h @@ -6,6 +6,7 @@ #include // Para vector #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 @@ -22,7 +23,7 @@ private: 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 std::vector> texts_; // Textos de la intro - std::unique_ptr shadow_sprite_; // Sprite para la sombra de las imagenes + std::unique_ptr tiled_bg_; // Variables Uint32 ticks_ = 0; // Contador de ticks para ajustar la velocidad del programa diff --git a/source/tiled_bg.cpp b/source/tiled_bg.cpp index ab5ed8a..59a4402 100644 --- a/source/tiled_bg.cpp +++ b/source/tiled_bg.cpp @@ -11,9 +11,6 @@ TiledBG::TiledBG(SDL_Rect pos, TiledBGMode mode) : renderer_(Screen::get()->getRenderer()), pos_(pos), - // Coloca la ventana que recorre el mosaico de fondo de manera que coincida - // con el mosaico que hay pintado en el titulo al iniciar - window_({128, 96, pos_.w, pos_.h}), mode_(mode == TiledBGMode::RANDOM ? static_cast(rand() % 2) : mode) { // Crea la textura para el mosaico de fondo @@ -22,6 +19,22 @@ TiledBG::TiledBG(SDL_Rect pos, TiledBGMode mode) // Rellena la textura con el contenido fillTexture(); + mode_ = TiledBGMode::DIAGONAL; + + switch (mode) + { + case TiledBGMode::DIAGONAL: + window_ = {0, 0, pos_.w, pos_.h}; + break; + + case TiledBGMode::CIRCLE: + window_ = {128, 96, pos_.w, pos_.h}; + break; + + default: + break; + } + // Inicializa los valores del vector con los valores del seno for (int i = 0; i < 360; ++i) { @@ -72,22 +85,57 @@ void TiledBG::render() // Actualiza la lógica de la clase void TiledBG::update() { + updateDesp(); + updateStop(); + switch (mode_) { case TiledBGMode::DIAGONAL: - { // El tileado de fondo se desplaza en diagonal - ++window_.x %= TILE_WIDTH_; - ++window_.y %= TILE_HEIGHT_; + { + // El tileado de fondo se desplaza en diagonal + window_.x = static_cast(desp_) % TILE_WIDTH_; + window_.y = static_cast(desp_) % TILE_HEIGHT_; + break; } case TiledBGMode::CIRCLE: - { // El tileado de fondo se desplaza en circulo - ++counter_ %= 360; - window_.x = 128 + (int(sin_[(counter_ + 270) % 360] * 128)); - window_.y = 96 + (int(sin_[(360 - counter_) % 360] * 96)); + { + // El tileado de fondo se desplaza en circulo + 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)); break; } default: break; } +} + +// Detiene el desplazamiento de forma ordenada +void TiledBG::updateStop() +{ + if (stopping_) + { + const int UMBRAL = 20 * speed_; // Ajusta este valor según la precisión deseada + + // Desacelerar si estamos cerca de completar el ciclo (ventana a punto de regresar a 0) + if (window_.x >= TILE_WIDTH_ - UMBRAL) + { + speed_ /= 1.05f; // Reduce gradualmente la velocidad + + // Asegura que no baje demasiado + if (speed_ < 0.1f) + { + speed_ = 0.1f; + } + } + + // Si estamos en 0, detener + if (window_.x == 0) + { + speed_ = 0.0f; + stopping_ = false; // Desactivamos el estado de "stopping" + } + } } \ No newline at end of file diff --git a/source/tiled_bg.h b/source/tiled_bg.h index b0cde03..818dd09 100644 --- a/source/tiled_bg.h +++ b/source/tiled_bg.h @@ -31,15 +31,24 @@ private: SDL_Texture *canvas_; // Textura donde dibujar el fondo formado por tiles // Variables - SDL_Rect pos_; // Posición y tamaño del mosaico - SDL_Rect window_; // Ventana visible para la textura de fondo del titulo - int counter_ = 0; // Contador - TiledBGMode mode_; // Tipo de movimiento del mosaico - double sin_[360]; // Vector con los valores del seno precalculados + SDL_Rect pos_; // Posición y tamaño del mosaico + SDL_Rect window_; // Ventana visible para la textura de fondo del titulo + int counter_ = 0; // Contador + TiledBGMode mode_; // Tipo de movimiento del mosaico + double sin_[360]; // Vector con los valores del seno precalculados + float desp_ = 0.0f; // Desplazamiento aplicado + float speed_ = 1.0f; // Incremento que se añade al desplazamiento a cada bucle + bool stopping_ = false; // Indica si se está deteniendo // Rellena la textura con el contenido void fillTexture(); + // Actualiza el desplazamiento + void updateDesp() { desp_ += speed_; } + + // Detiene el desplazamiento de forma ordenada + void updateStop(); + public: // Constructor TiledBG(SDL_Rect pos, TiledBGMode mode); @@ -52,4 +61,10 @@ public: // Actualiza la lógica de la clase void update(); + + // Establece la velocidad + void setSpeed(float speed) { speed_ = speed; } + + // Detiene el desplazamiento de forma ordenada + void stopGracefully() { stopping_ = true; } }; \ No newline at end of file