Treballant en la intro

This commit is contained in:
2025-03-14 20:28:04 +01:00
parent c757886430
commit ff4e356013
8 changed files with 140 additions and 49 deletions

View File

@@ -2,6 +2,7 @@
#include "section.h"
#include "mouse.h"
#include "resource.h"
#include <iostream>
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:

View File

@@ -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(), &param.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<Texture>(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();
}
}
}
// 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;
}
}

View File

@@ -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<std::unique_ptr<PathSprite>> sprites_; // Vector con los sprites inteligentes para los dibujos de la intro
std::vector<std::unique_ptr<PathSprite>> shadow_sprites_; // Vector con los sprites inteligentes para las sombras
@@ -26,8 +38,12 @@ private:
std::unique_ptr<TiledBG> 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();

View File

@@ -380,7 +380,4 @@ void Texture::setPalette(int palette)
}
// Obtiene el renderizador
SDL_Renderer *Texture::getRenderer()
{
return renderer_;
}
SDL_Renderer *Texture::getRenderer(){ return renderer_;}

View File

@@ -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<int>(desp_) % 360;
window_.x = 128 + (static_cast<int>(sin_[(INDEX + 270) % 360] * 128));
window_.y = 96 + (static_cast<int>(sin_[(360 - INDEX) % 360] * 96));
window_.y = 128 + (static_cast<int>(sin_[(360 - INDEX) % 360] * 96));
break;
}
default:

View File

@@ -2,6 +2,7 @@
#include <SDL2/SDL_rect.h> // Para SDL_Rect
#include <SDL2/SDL_render.h> // 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; }
};

View File

@@ -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)
{

View File

@@ -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<std::string> &vec, const std::string &str);