Afegits estats al fade

Afegida opció de prefade
El modo demo ja comença a meitat del "meollo"
This commit is contained in:
2025-01-04 13:40:22 +01:00
parent 06eb05f065
commit 7b8f16610a
9 changed files with 139 additions and 86 deletions

View File

@@ -44,13 +44,13 @@ Credits::Credits()
fade_in_->setColor(fade_color.r, fade_color.g, fade_color.b); fade_in_->setColor(fade_color.r, fade_color.g, fade_color.b);
fade_in_->setType(FadeType::FULLSCREEN); fade_in_->setType(FadeType::FULLSCREEN);
fade_in_->setPost(50); fade_in_->setPostDuration(50);
fade_in_->setMode(FadeMode::IN); fade_in_->setMode(FadeMode::IN);
fade_in_->activate(); fade_in_->activate();
fade_out_->setColor(0, 0, 0); fade_out_->setColor(0, 0, 0);
fade_out_->setType(FadeType::FULLSCREEN); fade_out_->setType(FadeType::FULLSCREEN);
fade_out_->setPost(400); fade_out_->setPostDuration(400);
initPlayers(); initPlayers();
SDL_SetTextureBlendMode(text_texture_, SDL_BLENDMODE_BLEND); SDL_SetTextureBlendMode(text_texture_, SDL_BLENDMODE_BLEND);

View File

@@ -30,15 +30,15 @@ void Fade::init()
{ {
type_ = FadeType::CENTER; type_ = FadeType::CENTER;
mode_ = FadeMode::OUT; mode_ = FadeMode::OUT;
enabled_ = false;
finished_ = false;
counter_ = 0; counter_ = 0;
r_ = 0; r_ = 0;
g_ = 0; g_ = 0;
b_ = 0; b_ = 0;
a_ = 0; a_ = 0;
post_duration_ = 20; post_duration_ = 0;
post_counter_ = 0; post_counter_ = 0;
pre_duration_ = 0;
pre_counter_ = 0;
num_squares_width_ = param.fade.num_squares_width; num_squares_width_ = param.fade.num_squares_width;
num_squares_height_ = param.fade.num_squares_height; num_squares_height_ = param.fade.num_squares_height;
fade_random_squares_delay_ = param.fade.random_squares_delay; 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 // Resetea algunas variables para volver a hacer el fade sin perder ciertos parametros
void Fade::reset() void Fade::reset()
{ {
enabled_ = false; state_ = FadeState::NOT_ENABLED;
finished_ = false;
counter_ = 0; counter_ = 0;
} }
// Pinta una transición en pantalla // Pinta una transición en pantalla
void Fade::render() void Fade::render()
{ {
if (enabled_ || finished_) //if (state_ != FadeState::NOT_ENABLED)
{ {
SDL_RenderCopy(renderer_, backbuffer_, nullptr, nullptr); SDL_RenderCopy(renderer_, backbuffer_, nullptr, nullptr);
} }
@@ -65,7 +64,20 @@ void Fade::render()
// Actualiza las variables internas // Actualiza las variables internas
void Fade::update() 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_) switch (type_)
{ {
@@ -79,7 +91,7 @@ void Fade::update()
// Comprueba si ha terminado // Comprueba si ha terminado
if (counter_ >= 255 / 4) if (counter_ >= 255 / 4)
{ {
finished_ = true; state_ = FadeState::POST;
} }
break; break;
@@ -110,7 +122,7 @@ void Fade::update()
// Comprueba si ha terminado // Comprueba si ha terminado
if ((counter_ * 4) > param.game.height) if ((counter_ * 4) > param.game.height)
{ {
finished_ = true; state_ = FadeState::POST;
a_ = 255; a_ = 255;
} }
break; break;
@@ -143,7 +155,7 @@ void Fade::update()
// Comprueba si ha terminado // Comprueba si ha terminado
if (counter_ * fade_random_squares_mult_ / fade_random_squares_delay_ >= num_squares_width_ * num_squares_height_) if (counter_ * fade_random_squares_mult_ / fade_random_squares_delay_ >= num_squares_width_ * num_squares_height_)
{ {
finished_ = true; state_ = FadeState::POST;
} }
break; break;
@@ -189,39 +201,47 @@ void Fade::update()
} }
else else
{ {
finished_ = true; state_ = FadeState::POST;
} }
break; 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_++; 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 // Activa el fade
void Fade::activate() void Fade::activate()
{ {
// Si ya está habilitado, no hay que volverlo a activar // Si ya está habilitado, no hay que volverlo a activar
if (enabled_) if (state_ != FadeState::NOT_ENABLED)
{ {
return; return;
} }
enabled_ = true; state_ = FadeState::PRE;
finished_ = false;
counter_ = 0; counter_ = 0;
post_counter_ = 0; post_counter_ = 0;
pre_counter_ = 0;
switch (type_) 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) int Fade::calculateValue(int min, int max, int current)
{ {
if (current < min) if (current < min)
{
return 0; return 0;
}
if (current > max) if (current > max)
{
return 100; return 100;
}
return static_cast<int>(100.0 * (current - min) / (max - min)); return static_cast<int>(100.0 * (current - min) / (max - min));
} }

View File

@@ -21,6 +21,16 @@ enum class FadeMode : Uint8
OUT = 1, OUT = 1,
}; };
// Estados del objeto
enum class FadeState : Uint8
{
NOT_ENABLED = 0,
PRE = 1,
FADING = 2,
POST = 3,
FINISHED = 4,
};
// Clase Fade // Clase Fade
class Fade class Fade
{ {
@@ -30,22 +40,23 @@ private:
SDL_Texture *backbuffer_; // Textura para usar como backbuffer con SDL_TEXTUREACCESS_TARGET SDL_Texture *backbuffer_; // Textura para usar como backbuffer con SDL_TEXTUREACCESS_TARGET
// Variables // Variables
FadeType type_; // Tipo de fade a realizar FadeType type_; // Tipo de fade a realizar
FadeMode mode_; // Modo de fade a realizar FadeMode mode_; // Modo de fade a realizar
Uint16 counter_; // Contador interno FadeState state_ = FadeState::NOT_ENABLED; // Estado actual del objeto
bool enabled_; // Indica si el fade está activo Uint16 counter_; // Contador interno
bool finished_; // Indica si ha terminado la transición Uint8 r_, g_, b_, a_; // Colores para el fade
Uint8 r_, g_, b_, a_; // Colores para el fade SDL_Rect rect1_; // Rectangulo usado para crear los efectos de transición
SDL_Rect rect1_; // Rectangulo usado para crear los efectos de transición SDL_Rect rect2_; // 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_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
int num_squares_height_; // Cantidad total de cuadraditos en vertical para el FadeType::RANDOM_SQUARE std::vector<SDL_Rect> square_; // Vector con los indices de los cuadrados para el FadeType::RANDOM_SQUARE
std::vector<SDL_Rect> 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_delay_; // Duración entre cada pintado de cuadrados int fade_random_squares_mult_; // Cantidad de cuadrados que se pintaran cada vez
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_duration_; // Duración posterior del fade tras finalizar int post_counter_ = 0; // Contador para la duración posterior
int post_counter_; // Contador para la duración posterior int pre_duration_ = 0; // Duración previa del fade antes de iniciar
int value_ = 0; // Estado actual del fade entre 0 y 100 int pre_counter_ = 0; // Contador para la duración previa
int value_ = 0; // Estado actual del fade entre 0 y 100
// Inicializa las variables // Inicializa las variables
void init(); void init();
@@ -80,11 +91,12 @@ public:
// Getters // Getters
int getValue() const { return value_; } int getValue() const { return value_; }
bool isEnabled() const { return enabled_; } bool isEnabled() const { return state_ != FadeState::NOT_ENABLED; }
bool hasEnded() const { return !enabled_ && finished_; } bool hasEnded() const { return state_ == FadeState::FINISHED; }
// Setters // Setters
void setType(FadeType type) { type_ = type; } void setType(FadeType type) { type_ = type; }
void setMode(FadeMode mode) { mode_ = mode; } 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; }
}; };

View File

@@ -68,13 +68,14 @@ Game::Game(int player_id, int current_stage, bool demo)
scoreboard_ = Scoreboard::get(); scoreboard_ = Scoreboard::get();
fade_in_->setColor(fade_color.r, fade_color.g, fade_color.b); 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_->setType(FadeType::RANDOM_SQUARE);
fade_in_->setMode(FadeMode::IN); fade_in_->setMode(FadeMode::IN);
fade_in_->activate(); fade_in_->activate();
fade_out_->setColor(fade_color.r, fade_color.g, fade_color.b); 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); fade_out_->setType(FadeType::VENETIAN);
background_->setPos(param.game.play_area.rect); background_->setPos(param.game.play_area.rect);
@@ -1490,7 +1491,8 @@ void Game::handleFireInput(const std::shared_ptr<Player> &player, BulletType bul
JA_PlaySound(Resource::get()->getSound("bullet.wav")); JA_PlaySound(Resource::get()->getSound("bullet.wav"));
// Establece un tiempo de espera para el próximo disparo. // 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); player->setFireCooldown(cooldown);
} }
} }
@@ -1678,7 +1680,7 @@ void Game::initDemo(int player_id)
for (int i = 0; i < rand() % 3; ++i) for (int i = 0; i < rand() % 3; ++i)
player->giveExtraHit(); player->giveExtraHit();
player->setInvulnerable(false); player->setInvulnerable(true);
} }
// Deshabilita los sonidos // Deshabilita los sonidos

View File

@@ -45,7 +45,7 @@ HiScoreTable::HiScoreTable()
background_->setTransition(0.8f); background_->setTransition(0.8f);
fade_->setColor(fade_color.r, fade_color.g, fade_color.b); fade_->setColor(fade_color.r, fade_color.g, fade_color.b);
fade_->setType(FadeType::RANDOM_SQUARE); fade_->setType(FadeType::RANDOM_SQUARE);
fade_->setPost(param.fade.post_duration); fade_->setPostDuration(param.fade.post_duration);
fade_->setMode(fade_mode_); fade_->setMode(fade_mode_);
fade_->activate(); fade_->activate();

View File

@@ -42,7 +42,7 @@ Instructions::Instructions()
// Inicializa objetos // Inicializa objetos
fade_->setColor(fade_color.r, fade_color.g, fade_color.b); fade_->setColor(fade_color.r, fade_color.g, fade_color.b);
fade_->setType(FadeType::FULLSCREEN); fade_->setType(FadeType::FULLSCREEN);
fade_->setPost(param.fade.post_duration); fade_->setPostDuration(param.fade.post_duration);
fade_->setMode(FadeMode::IN); fade_->setMode(FadeMode::IN);
fade_->activate(); fade_->activate();

View File

@@ -5,30 +5,31 @@ namespace section
// Secciones del programa // Secciones del programa
enum class Name enum class Name
{ {
INIT = 0, INIT,
LOGO = 1, LOGO,
INTRO = 2, INTRO,
TITLE = 3, TITLE,
GAME = 4, GAME,
HI_SCORE_TABLE = 5, HI_SCORE_TABLE,
GAME_DEMO = 6, GAME_DEMO,
INSTRUCTIONS = 7, INSTRUCTIONS,
CREDITS = 8, CREDITS,
QUIT = 9, QUIT,
}; };
// Opciones para la sección // Opciones para la sección
enum class Options enum class Options
{ {
GAME_PLAY_1P = 0, GAME_PLAY_1P,
GAME_PLAY_2P = 1, GAME_PLAY_2P,
TITLE_1 = 2, TITLE_TIME_OUT,
TITLE_2 = 3, TITLE_1,
QUIT_WITH_KEYBOARD = 4, TITLE_2,
QUIT_WITH_CONTROLLER = 5, QUIT_WITH_KEYBOARD,
QUIT_FROM_EVENT = 6, QUIT_WITH_CONTROLLER,
RELOAD = 7, QUIT_FROM_EVENT,
NONE = 8, RELOAD,
NONE,
}; };
// Variables para el Attract Mode // Variables para el Attract Mode

View File

@@ -42,7 +42,7 @@ Title::Title()
mini_logo_sprite_->setX(param.game.game_area.center_x - mini_logo_sprite_->getWidth() / 2); 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_->setColor(fade_color.r, fade_color.g, fade_color.b);
fade_->setType(FadeType::RANDOM_SQUARE); fade_->setType(FadeType::RANDOM_SQUARE);
fade_->setPost(param.fade.post_duration); fade_->setPostDuration(param.fade.post_duration);
Resource::get()->getTexture("smb2.gif")->setPalette(1); Resource::get()->getTexture("smb2.gif")->setPalette(1);
// Asigna valores a otras variables // Asigna valores a otras variables
@@ -79,14 +79,16 @@ void Title::update()
fade_->update(); fade_->update();
if (fade_->hasEnded()) if (fade_->hasEnded())
{ {
if (post_fade_ == -1) if (selection_ == section::Options::TITLE_TIME_OUT)
{ {
// El menu ha hecho time out
section::name = next_section_; section::name = next_section_;
} }
else else
{ {
// Se ha pulsado para jugar
section::name = section::Name::GAME; 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(); JA_StopMusic();
} }
} }
@@ -122,8 +124,10 @@ void Title::update()
if (counter_ == param.title.title_duration) if (counter_ == param.title.title_duration)
{ {
// El menu ha hecho time out
fade_->setPostDuration(0);
fade_->activate(); fade_->activate();
post_fade_ = -1; selection_ = section::Options::TITLE_TIME_OUT;
} }
break; break;
@@ -295,7 +299,18 @@ void Title::checkInput()
{ {
JA_PlaySound(Resource::get()->getSound("game_start.wav")); JA_PlaySound(Resource::get()->getSound("game_start.wav"));
JA_FadeOutMusic(1500); 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; state_ = TitleState::START_HAS_BEEN_PRESSED;
counter_ = 0; counter_ = 0;
return; return;

View File

@@ -9,10 +9,7 @@ class Sprite; // lines 9-9
class Text; // lines 10-10 class Text; // lines 10-10
class Texture; // lines 11-11 class Texture; // lines 11-11
class TiledBG; // lines 12-12 class TiledBG; // lines 12-12
namespace section #include "section.h"
{
enum class Name;
}
// Textos // Textos
constexpr const char TEXT_COPYRIGHT[] = "@2020,2025 JailDesigner"; constexpr const char TEXT_COPYRIGHT[] = "@2020,2025 JailDesigner";
@@ -57,12 +54,12 @@ private:
std::unique_ptr<DefineButtons> define_buttons_; // Objeto para definir los botones del joystic std::unique_ptr<DefineButtons> define_buttons_; // Objeto para definir los botones del joystic
// Variable // Variable
int counter_ = 0; // Temporizador para la pantalla de titulo int counter_ = 0; // Temporizador para la pantalla de titulo
Uint32 ticks_ = 0; // Contador de ticks para ajustar la velocidad del programa 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::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 section::Options selection_ = section::Options::TITLE_TIME_OUT; // Opción elegida en el titulo
int num_controllers_; // Número de mandos conectados int num_controllers_; // Número de mandos conectados
TitleState state_; // Estado en el que se encuentra la sección TitleState state_; // Estado en el que se encuentra la sección
// Actualiza las variables del objeto // Actualiza las variables del objeto
void update(); void update();