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_->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);

View File

@@ -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;
}
counter_++;
}
if (finished_)
if (state_ == FadeState::POST)
{
// Actualiza el contador
post_counter_ == post_duration_ ? enabled_ = false : post_counter_++;
if (post_counter_ == post_duration_)
{
state_ = FadeState::FINISHED;
}
else
{
post_counter_++;
}
// Deja el backbuffer_ todo del mismo color
cleanBackbuffer(r_, g_, b_, a_);
}
counter_++;
}
}
// 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<int>(100.0 * (current - min) / (max - min));
}

View File

@@ -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
{
@@ -32,9 +42,8 @@ private:
// Variables
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
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
@@ -43,8 +52,10 @@ private:
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_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 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
@@ -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; }
};

View File

@@ -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> &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

View File

@@ -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();

View File

@@ -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();

View File

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

View File

@@ -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;

View File

@@ -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";
@@ -60,7 +57,7 @@ private:
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
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