Input: model upate/poll finalitzat
This commit is contained in:
369
source/sections/title.cpp
Normal file
369
source/sections/title.cpp
Normal file
@@ -0,0 +1,369 @@
|
||||
#include "title.h"
|
||||
#include <SDL3/SDL_events.h> // Para SDL_PollEvent, SDL_Event, SDL_KEYDOWN
|
||||
#include <SDL3/SDL_keycode.h> // Para SDLK_1, SDLK_2, SDLK_3, SDLK_4, SDLK_5
|
||||
#include <SDL3/SDL_timer.h> // Para SDL_GetTicks
|
||||
#include <stddef.h> // Para size_t
|
||||
#include <string> // Para char_traits, operator+, basic_string
|
||||
#include <vector> // Para vector
|
||||
#include "define_buttons.h" // Para DefineButtons
|
||||
#include "fade.h" // Para Fade, FadeType
|
||||
#include "game_logo.h" // Para GameLogo
|
||||
#include "global_inputs.h" // Para check, update
|
||||
#include "input.h" // Para Input, InputAction, INPUT_DO_NOT_ALLOW_R...
|
||||
#include "audio.h" // Para JA_GetMusicState, JA_FadeOutMusic, JA_...
|
||||
#include "lang.h" // Para getText
|
||||
#include "global_events.h" // Para handleEvent
|
||||
#include "notifier.h" // Para Notifier
|
||||
#include "options.h" // Para OptionsController, Options, options
|
||||
#include "param.h" // Para Param, param, ParamGame, ParamTitle
|
||||
#include "resource.h" // Para Resource
|
||||
#include "screen.h" // Para Screen
|
||||
#include "section.h" // Para Options, Name, name, AttractMode, options
|
||||
#include "sprite.h" // Para Sprite
|
||||
#include "text.h" // Para TEXT_CENTER, TEXT_SHADOW, Text
|
||||
#include "texture.h" // Para Texture
|
||||
#include "tiled_bg.h" // Para TiledBG, TiledBGMode
|
||||
#include "utils.h" // Para Color, Zone, fade_color, no_color, BLOCK
|
||||
#include "ui/service_menu.h"
|
||||
|
||||
// Constructor
|
||||
Title::Title()
|
||||
: text_(Resource::get()->getText("smb2")),
|
||||
fade_(std::make_unique<Fade>()),
|
||||
tiled_bg_(std::make_unique<TiledBG>(param.game.game_area.rect, TiledBGMode::RANDOM)),
|
||||
game_logo_(std::make_unique<GameLogo>(param.game.game_area.center_x, param.title.title_c_c_position)),
|
||||
mini_logo_sprite_(std::make_unique<Sprite>(Resource::get()->getTexture("logo_jailgames_mini.png"))),
|
||||
define_buttons_(std::make_unique<DefineButtons>()),
|
||||
num_controllers_(Input::get()->getNumControllers()),
|
||||
state_(TitleState::LOGO_ANIMATING)
|
||||
{
|
||||
// Configura objetos
|
||||
game_logo_->enable();
|
||||
mini_logo_sprite_->setX(param.game.game_area.center_x - mini_logo_sprite_->getWidth() / 2);
|
||||
fade_->setColor(param.fade.color);
|
||||
fade_->setType(FadeType::RANDOM_SQUARE);
|
||||
fade_->setPostDuration(param.fade.post_duration);
|
||||
Resource::get()->getTexture("smb2.gif")->setPalette(1);
|
||||
|
||||
// Asigna valores a otras variables
|
||||
Section::options = Section::Options::TITLE_1;
|
||||
const bool IS_TITLE_TO_DEMO = (Section::attract_mode == Section::AttractMode::TITLE_TO_DEMO);
|
||||
next_section_ = IS_TITLE_TO_DEMO ? Section::Name::GAME_DEMO : Section::Name::LOGO;
|
||||
Section::attract_mode = IS_TITLE_TO_DEMO ? Section::AttractMode::TITLE_TO_LOGO : Section::AttractMode::TITLE_TO_DEMO;
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Title::~Title()
|
||||
{
|
||||
Resource::get()->getTexture("smb2.gif")->setPalette(0);
|
||||
Audio::get()->stopAllSounds();
|
||||
}
|
||||
|
||||
// Actualiza las variables del objeto
|
||||
void Title::update()
|
||||
{
|
||||
if (SDL_GetTicks() - ticks_ > param.game.speed)
|
||||
{
|
||||
// Actualiza el contador de ticks_
|
||||
ticks_ = SDL_GetTicks();
|
||||
|
||||
// Actualiza el fade
|
||||
updateFade();
|
||||
|
||||
// Actualiza el estado
|
||||
updateState();
|
||||
|
||||
// Actualiza el objeto screen
|
||||
Screen::get()->update();
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja el objeto en pantalla
|
||||
void Title::render()
|
||||
{
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
Screen::get()->start();
|
||||
|
||||
// Limpia la pantalla
|
||||
Screen::get()->clean();
|
||||
|
||||
// Dibuja el mosacico de fondo
|
||||
tiled_bg_->render();
|
||||
|
||||
// Dibuja el logo con el título del juego
|
||||
game_logo_->render();
|
||||
|
||||
constexpr Color shadow = Color(0x14, 0x87, 0xc4);
|
||||
|
||||
if (state_ != TitleState::LOGO_ANIMATING)
|
||||
{
|
||||
// Mini logo
|
||||
const int pos1 = (param.game.height / 5 * 4) + BLOCK;
|
||||
const int pos2 = pos1 + mini_logo_sprite_->getHeight() + 3;
|
||||
mini_logo_sprite_->setY(pos1);
|
||||
mini_logo_sprite_->render();
|
||||
|
||||
// Texto con el copyright
|
||||
text_->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, pos2, TEXT_COPYRIGHT, 1, NO_TEXT_COLOR, 1, shadow);
|
||||
}
|
||||
|
||||
if (state_ == TitleState::LOGO_FINISHED)
|
||||
{
|
||||
// 'PRESS TO PLAY'
|
||||
if (counter_ % 50 > 14 && !define_buttons_->isEnabled())
|
||||
{
|
||||
text_->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, param.title.press_start_position, Lang::getText("[TITLE] PRESS_BUTTON_TO_PLAY"), 1, NO_TEXT_COLOR, 1, shadow);
|
||||
}
|
||||
}
|
||||
|
||||
if (state_ == TitleState::START_HAS_BEEN_PRESSED)
|
||||
{
|
||||
// 'PRESS TO PLAY'
|
||||
if (counter_ % 10 > 4 && !define_buttons_->isEnabled())
|
||||
{
|
||||
text_->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, param.title.press_start_position, Lang::getText("[TITLE] PRESS_BUTTON_TO_PLAY"), 1, NO_TEXT_COLOR, 1, shadow);
|
||||
}
|
||||
}
|
||||
|
||||
// Define Buttons
|
||||
define_buttons_->render();
|
||||
|
||||
// Fade
|
||||
fade_->render();
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
Screen::get()->render();
|
||||
}
|
||||
|
||||
// Comprueba los eventos
|
||||
void Title::checkEvents()
|
||||
{
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event))
|
||||
{
|
||||
if (event.type == SDL_EVENT_KEY_DOWN && event.key.repeat == 0)
|
||||
{
|
||||
switch (event.key.key)
|
||||
{
|
||||
case SDLK_1: // Redefine los botones del mando #0
|
||||
define_buttons_->enable(0);
|
||||
break;
|
||||
|
||||
case SDLK_2: // Redefine los botones del mando #1
|
||||
define_buttons_->enable(1);
|
||||
break;
|
||||
|
||||
case SDLK_3: // Intercambia los mandos entre los dos jugadores
|
||||
swapControllers();
|
||||
break;
|
||||
|
||||
case SDLK_4: // Intercambia la asignación del teclado
|
||||
swapKeyboard();
|
||||
break;
|
||||
|
||||
case SDLK_5: // Muestra la asignación de mandos y teclado
|
||||
showControllers();
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
resetCounter();
|
||||
}
|
||||
|
||||
GlobalEvents::check(event);
|
||||
define_buttons_->checkEvents(event);
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba las entradas
|
||||
void Title::checkInput()
|
||||
{
|
||||
Input::get()->update();
|
||||
|
||||
if (!ServiceMenu::get()->isEnabled())
|
||||
{
|
||||
// Comprueba las entradas solo si no se estan definiendo los botones
|
||||
if (!define_buttons_->isEnabled())
|
||||
{
|
||||
// Comprueba todos los métodos de control
|
||||
for (const auto &CONTROLLER : Options::controllers)
|
||||
{
|
||||
// START
|
||||
if (Input::get()->checkInput(InputAction::START, INPUT_DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index))
|
||||
{
|
||||
if ((state_ == TitleState::LOGO_FINISHED || ALLOW_TITLE_ANIMATION_SKIP) && !fade_->isEnabled())
|
||||
{
|
||||
Audio::get()->playSound("game_start.wav");
|
||||
Audio::get()->fadeOutMusic(1500);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba los inputs que se pueden introducir en cualquier sección del juego
|
||||
if (!define_buttons_->isEnabled())
|
||||
{
|
||||
GlobalInputs::check();
|
||||
}
|
||||
}
|
||||
|
||||
// Bucle para el titulo del juego
|
||||
void Title::run()
|
||||
{
|
||||
while (Section::name == Section::Name::TITLE)
|
||||
{
|
||||
checkInput();
|
||||
update();
|
||||
checkEvents(); // Tiene que ir antes del render
|
||||
render();
|
||||
}
|
||||
}
|
||||
|
||||
// Reinicia el contador interno
|
||||
void Title::resetCounter() { counter_ = 0; }
|
||||
|
||||
// Intercambia la asignación de mandos a los jugadores
|
||||
void Title::swapControllers()
|
||||
{
|
||||
if (Input::get()->getNumControllers() == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Options::swapControllers();
|
||||
showControllers();
|
||||
}
|
||||
|
||||
// Intercambia el teclado de jugador
|
||||
void Title::swapKeyboard()
|
||||
{
|
||||
Options::swapKeyboard();
|
||||
std::string text = Lang::getText("[DEFINE_BUTTONS] PLAYER") + std::to_string(Options::getPlayerWhoUsesKeyboard()) + ": " + Lang::getText("[DEFINE_BUTTONS] KEYBOARD");
|
||||
Notifier::get()->show({text});
|
||||
}
|
||||
|
||||
// Muestra información sobre los controles y los jugadores
|
||||
void Title::showControllers()
|
||||
{
|
||||
// Crea vectores de texto vacíos para un número máximo de mandos
|
||||
constexpr size_t NUM_CONTROLLERS = 2;
|
||||
std::vector<std::string> text(NUM_CONTROLLERS);
|
||||
std::vector<int> player_controller_index(NUM_CONTROLLERS, -1);
|
||||
|
||||
// Obtiene de cada jugador el índice del mando que tiene asignado
|
||||
for (size_t i = 0; i < NUM_CONTROLLERS; ++i)
|
||||
{
|
||||
// Ejemplo: el jugador 1 tiene el mando 2
|
||||
player_controller_index.at(Options::controllers.at(i).player_id - 1) = i;
|
||||
}
|
||||
|
||||
// Genera el texto correspondiente
|
||||
for (size_t i = 0; i < NUM_CONTROLLERS; ++i)
|
||||
{
|
||||
const size_t index = player_controller_index.at(i);
|
||||
if (Options::controllers.at(index).plugged)
|
||||
{
|
||||
text.at(i) = Lang::getText("[DEFINE_BUTTONS] PLAYER") + std::to_string(i + 1) + ": " + Options::controllers.at(index).name;
|
||||
}
|
||||
}
|
||||
|
||||
// Muestra la notificación
|
||||
Notifier::get()->show({text.at(0), text.at(1)});
|
||||
}
|
||||
|
||||
// Actualiza el fade
|
||||
void Title::updateFade()
|
||||
{
|
||||
fade_->update();
|
||||
if (fade_->hasEnded())
|
||||
{
|
||||
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 = selection_;
|
||||
Audio::get()->stopMusic();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el estado
|
||||
void Title::updateState()
|
||||
{
|
||||
// Establece la lógica según el estado
|
||||
switch (state_)
|
||||
{
|
||||
case TitleState::LOGO_ANIMATING:
|
||||
{
|
||||
game_logo_->update();
|
||||
if (game_logo_->hasFinished())
|
||||
{
|
||||
state_ = TitleState::LOGO_FINISHED;
|
||||
Audio::get()->playMusic("title.ogg");
|
||||
}
|
||||
break;
|
||||
}
|
||||
case TitleState::LOGO_FINISHED:
|
||||
{
|
||||
// El contador solo sube si no estamos definiendo botones
|
||||
counter_ = define_buttons_->isEnabled() ? 0 : counter_ + 1;
|
||||
|
||||
// Actualiza el logo con el título del juego
|
||||
game_logo_->update();
|
||||
|
||||
// Actualiza el mosaico de fondo
|
||||
tiled_bg_->update();
|
||||
|
||||
if (counter_ == param.title.title_duration)
|
||||
{
|
||||
// El menu ha hecho time out
|
||||
fade_->setPostDuration(0);
|
||||
fade_->activate();
|
||||
selection_ = Section::Options::TITLE_TIME_OUT;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case TitleState::START_HAS_BEEN_PRESSED:
|
||||
{
|
||||
// Actualiza el logo con el título del juego
|
||||
game_logo_->update();
|
||||
|
||||
// Actualiza el mosaico de fondo
|
||||
tiled_bg_->update();
|
||||
|
||||
if (counter_ == 100)
|
||||
{
|
||||
fade_->activate();
|
||||
}
|
||||
++counter_;
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user