primer commit

This commit is contained in:
2025-11-23 11:44:31 +01:00
commit 6ada29eaf8
613 changed files with 484459 additions and 0 deletions

778
source/game/scenes/game.cpp Normal file
View File

@@ -0,0 +1,778 @@
#include "game/scenes/game.hpp"
#include <SDL3/SDL.h>
#include <cmath> // Para std::sqrt, std::min
#include <utility>
#include <vector> // Para vector
#include "core/audio/audio.hpp" // Para Audio
#include "core/input/global_inputs.hpp" // Para check
#include "core/input/input.hpp" // Para Input, InputAction, Input::DO_NOT_ALLOW_REPEAT
#include "core/rendering/screen.hpp" // Para Screen
#include "core/rendering/surface.hpp" // Para Surface
#include "core/rendering/text.hpp" // Para Text, Text::CENTER_FLAG, Text::COLOR_FLAG
#include "core/resources/resource_cache.hpp" // Para ResourceRoom, Resource
#include "core/resources/resource_list.hpp" // Para Asset
#include "core/system/global_events.hpp" // Para check
#include "game/gameplay/item_tracker.hpp" // Para ItemTracker
#include "game/gameplay/room.hpp" // Para Room, RoomData
#include "game/gameplay/room_tracker.hpp" // Para RoomTracker
#include "game/gameplay/scoreboard.hpp" // Para Scoreboard::Data, Scoreboard
#include "game/defaults.hpp" // Para Defaults::Music
#include "game/options.hpp" // Para Options, options, Cheat, SectionState
#include "game/scene_manager.hpp" // Para SceneManager
#include "game/ui/notifier.hpp" // Para Notifier, NotificationText, CHEEVO_NO...
#include "utils/defines.hpp" // Para Tile::SIZE, PlayArea::HEIGHT, RoomBorder::BOTTOM
#include "utils/color.hpp" // Para Color
#include "utils/utils.hpp" // Para stringToColor
#ifdef _DEBUG
#include "core/system/debug.hpp" // Para Debug
#endif
// Constructor
Game::Game(Mode mode)
: scoreboard_data_(std::make_shared<Scoreboard::Data>(0, 9, 0, true, 0, SDL_GetTicks(), Options::cheats.jail_is_open == Options::Cheat::State::ENABLED)),
scoreboard_(std::make_shared<Scoreboard>(scoreboard_data_)),
room_tracker_(std::make_shared<RoomTracker>()),
mode_(mode),
#ifdef _DEBUG
current_room_("03.yaml"),
spawn_data_(Player::SpawnData(25 * Tile::SIZE, 21 * Tile::SIZE, 0, 0, 0, Player::State::ON_GROUND, Flip::LEFT))
#else
current_room_("03.yaml"),
spawn_data_(Player::SpawnData(25 * Tile::SIZE, 13 * Tile::SIZE, 0, 0, 0, Player::State::ON_GROUND, Flip::LEFT))
#endif
{
// Crea objetos e inicializa variables
ItemTracker::init();
demoInit();
room_ = std::make_shared<Room>(current_room_, scoreboard_data_);
initPlayer(spawn_data_, room_);
total_items_ = getTotalItems();
createRoomNameTexture();
game_backbuffer_surface_ = std::make_shared<Surface>(Options::game.width, Options::game.height);
changeRoom(current_room_);
SceneManager::current = SceneManager::Scene::GAME;
SceneManager::options = SceneManager::Options::NONE;
}
Game::~Game() {
ItemTracker::destroy();
}
// Comprueba los eventos de la cola
void Game::handleEvents() {
SDL_Event event;
while (SDL_PollEvent(&event)) {
GlobalEvents::handle(event);
#ifdef _DEBUG
handleDebugEvents(event);
#endif
}
}
// Comprueba el teclado
void Game::handleInput() {
Input::get()->update();
// Inputs globales siempre funcionan
if (Input::get()->checkAction(InputAction::TOGGLE_MUSIC, Input::DO_NOT_ALLOW_REPEAT)) {
scoreboard_data_->music = !scoreboard_data_->music;
scoreboard_data_->music ? Audio::get()->resumeMusic() : Audio::get()->pauseMusic();
Notifier::get()->show({"MUSIC " + std::string(scoreboard_data_->music ? "ENABLED" : "DISABLED")});
}
// Durante fade/postfade, solo procesar inputs globales
if (state_ != State::PLAYING) {
GlobalInputs::handle();
return;
}
// Input de pausa solo en estado PLAYING
if (Input::get()->checkAction(InputAction::PAUSE, Input::DO_NOT_ALLOW_REPEAT)) {
togglePause();
Notifier::get()->show({std::string(paused_ ? "GAME PAUSED" : "GAME RUNNING")});
}
GlobalInputs::handle();
}
// Bucle para el juego
void Game::run() {
keepMusicPlaying();
if (!scoreboard_data_->music && mode_ == Mode::GAME) {
Audio::get()->pauseMusic();
}
while (SceneManager::current == SceneManager::Scene::GAME) {
update();
render();
}
if (mode_ == Mode::GAME) {
Audio::get()->stopMusic();
}
}
// Actualiza el juego, las variables, comprueba la entrada, etc.
void Game::update() {
const float DELTA_TIME = delta_timer_.tick();
handleEvents(); // Comprueba los eventos
handleInput(); // Comprueba las entradas
#ifdef _DEBUG
Debug::get()->clear();
#endif
// Dispatch por estado
switch (state_) {
case State::PLAYING:
updatePlaying(DELTA_TIME);
break;
case State::BLACK_SCREEN:
updateBlackScreen(DELTA_TIME);
break;
case State::GAME_OVER:
updateGameOver(DELTA_TIME);
break;
case State::FADE_TO_ENDING:
updateFadeToEnding(DELTA_TIME);
break;
case State::POST_FADE_ENDING:
updatePostFadeEnding(DELTA_TIME);
break;
}
Audio::update(); // Actualiza el objeto Audio
Screen::get()->update(DELTA_TIME); // Actualiza el objeto Screen
#ifdef _DEBUG
updateDebugInfo();
#endif
}
// Actualiza el juego en estado PLAYING
void Game::updatePlaying(float delta_time) {
// Actualiza los objetos
room_->update(delta_time);
switch (mode_) {
case Mode::GAME:
#ifdef _DEBUG
// Maneja el arrastre del jugador con el ratón (debug)
handleDebugMouseDrag(delta_time);
// Si estamos arrastrando, no ejecutar la física normal del jugador
if (!debug_dragging_player_) {
player_->update(delta_time);
}
#else
player_->update(delta_time);
#endif
checkPlayerIsOnBorder();
checkPlayerAndItems();
checkPlayerAndEnemies();
checkIfPlayerIsAlive();
checkEndGame();
checkRestoringJail(delta_time);
break;
case Mode::DEMO:
demoCheckRoomChange(delta_time);
break;
}
scoreboard_->update(delta_time);
keepMusicPlaying();
}
// Actualiza el juego en estado BLACK_SCREEN
void Game::updateBlackScreen(float delta_time) {
state_time_ += delta_time;
// Si se acabaron las vidas Y pasó el threshold → GAME_OVER
if (scoreboard_data_->lives < 0 && state_time_ > GAME_OVER_THRESHOLD) {
transitionToState(State::GAME_OVER);
return;
}
// Si pasó la duración completa → volver a PLAYING
if (state_time_ > BLACK_SCREEN_DURATION) {
// Despausar al salir
player_->setPaused(false);
room_->setPaused(false);
Screen::get()->setBorderColor(room_->getBorderColor());
transitionToState(State::PLAYING);
}
}
// Actualiza el juego en estado GAME_OVER
void Game::updateGameOver(float delta_time) {
// Pequeño delay antes de cambiar escena
state_time_ += delta_time;
if (state_time_ > 0.1F) { // 100ms de delay mínimo
SceneManager::current = SceneManager::Scene::TITLE;
}
}
// Actualiza el juego en estado FADE_TO_ENDING
void Game::updateFadeToEnding(float delta_time) {
// Actualiza room, enemies, items (todo sigue funcionando)
room_->update(delta_time);
// NO actualizar player (congelar movimiento)
// player_->update(delta_time); -- COMENTADO INTENCIONALMENTE
// Actualiza scoreboard
scoreboard_->update(delta_time);
keepMusicPlaying();
// Aplica el fade progresivo al BACKBUFFER (no al renderer de pantalla)
fade_accumulator_ += delta_time;
if (fade_accumulator_ >= FADE_STEP_INTERVAL) {
fade_accumulator_ = 0.0F;
if (game_backbuffer_surface_->fadeSubPalette()) {
// Fade completado, transicionar a POST_FADE
transitionToState(State::POST_FADE_ENDING);
}
}
}
// Actualiza el juego en estado POST_FADE_ENDING
void Game::updatePostFadeEnding(float delta_time) {
// Pantalla negra estática, acumular tiempo
state_time_ += delta_time;
// Después del delay, cambiar a la escena de ending
if (state_time_ >= POST_FADE_DELAY) {
SceneManager::current = SceneManager::Scene::TITLE;
}
}
// Cambia al estado especificado y resetea los timers
void Game::transitionToState(State new_state) {
// Lógica de ENTRADA según el nuevo estado
if (new_state == State::BLACK_SCREEN) {
// Respawn room y player
room_ = std::make_shared<Room>(current_room_, scoreboard_data_);
initPlayer(spawn_data_, room_);
// Pausar ambos
room_->setPaused(true);
player_->setPaused(true);
}
state_ = new_state;
state_time_ = 0.0F;
fade_accumulator_ = 0.0F;
}
// Pinta los objetos en pantalla
void Game::render() {
// Dispatch por estado
switch (state_) {
case State::PLAYING:
renderPlaying();
break;
case State::BLACK_SCREEN:
renderBlackScreen();
break;
case State::GAME_OVER:
renderGameOver();
break;
case State::FADE_TO_ENDING:
renderFadeToEnding();
break;
case State::POST_FADE_ENDING:
renderPostFadeEnding();
break;
}
}
// Renderiza el juego en estado PLAYING (directo a pantalla)
void Game::renderPlaying() {
// Prepara para dibujar el frame
Screen::get()->start();
// Dibuja los elementos del juego en orden
room_->renderMap();
room_->renderEnemies();
room_->renderItems();
if (mode_ == Mode::GAME) {
player_->render();
}
renderRoomName();
#ifdef _DEBUG
if (!Debug::get()->isEnabled()) {
scoreboard_->render();
}
// Debug info
renderDebugInfo();
#else
scoreboard_->render();
#endif
// Actualiza la pantalla
Screen::get()->render();
}
// Renderiza el juego en estado BLACK_SCREEN (pantalla negra)
void Game::renderBlackScreen() {
Screen::get()->start();
auto const COLOR = Color::index(Color::Cpc::BLACK);
Screen::get()->clearSurface(COLOR);
Screen::get()->setBorderColor(COLOR);
Screen::get()->render();
}
// Renderiza el juego en estado GAME_OVER (pantalla negra)
void Game::renderGameOver() {
Screen::get()->start();
Screen::get()->clearSurface(Color::index(Color::Cpc::BLACK));
Screen::get()->render();
}
// Renderiza el juego en estado FADE_TO_ENDING (via backbuffer)
void Game::renderFadeToEnding() {
// 1. Guardar renderer actual
auto previous_renderer = Screen::get()->getRendererSurface();
// 2. Cambiar target a backbuffer
Screen::get()->setRendererSurface(game_backbuffer_surface_);
// 3. Renderizar todo a backbuffer
game_backbuffer_surface_->clear(Color::index(Color::Cpc::BLACK));
room_->renderMap();
room_->renderEnemies();
room_->renderItems();
player_->render(); // Player congelado pero visible
renderRoomName();
scoreboard_->render();
// 4. Restaurar renderer original
Screen::get()->setRendererSurface(previous_renderer);
// 5. Preparar pantalla y volcar backbuffer (fade YA aplicado en update)
Screen::get()->start();
game_backbuffer_surface_->render();
Screen::get()->render();
}
// Renderiza el juego en estado POST_FADE_ENDING (pantalla negra)
void Game::renderPostFadeEnding() {
Screen::get()->start();
Screen::get()->clearSurface(Color::index(Color::Cpc::BLACK));
Screen::get()->render();
}
#ifdef _DEBUG
// Pasa la información de debug
void Game::updateDebugInfo() {
// Debug::get()->add("X = " + std::to_string(static_cast<int>(player_->x_)) + ", Y = " + std::to_string(static_cast<int>(player_->y_)));
// Debug::get()->add("VX = " + std::to_string(player_->vx_).substr(0, 4) + ", VY = " + std::to_string(player_->vy_).substr(0, 4));
// Debug::get()->add("STATE = " + std::to_string(static_cast<int>(player_->state_)));
}
// Pone la información de debug en pantalla
void Game::renderDebugInfo() {
if (!Debug::get()->isEnabled()) {
return;
}
auto surface = Screen::get()->getRendererSurface();
// Borra el área del scoreboard
SDL_FRect rect = {ScoreboardArea::X, ScoreboardArea::Y, ScoreboardArea::WIDTH, ScoreboardArea::HEIGHT};
surface->fillRect(&rect, Color::index(Color::Cpc::BLACK));
// Pinta la rejilla
/*for (int i = 0; i < PlayArea::BOTTOM; i += 8)
{
// Lineas horizontales
surface->drawLine(0, i, PlayArea::RIGHT, i, static_cast<Uint8>(PaletteColor::BRIGHT_BLACK));
}
for (int i = 0; i < PlayArea::RIGHT; i += 8)
{
// Lineas verticales
surface->drawLine(i, 0, i, PlayArea::BOTTOM - 1, static_cast<Uint8>(PaletteColor::BRIGHT_BLACK));
}*/
// Pinta el texto de debug en el área del scoreboard
Debug::get()->setPos({ScoreboardArea::X, ScoreboardArea::Y});
Debug::get()->render();
}
// Comprueba los eventos
void Game::handleDebugEvents(const SDL_Event& event) {
if (event.type == SDL_EVENT_KEY_DOWN && static_cast<int>(event.key.repeat) == 0) {
switch (event.key.key) {
case SDLK_F12:
Debug::get()->toggleEnabled();
Notifier::get()->show({"DEBUG " + std::string(Debug::get()->isEnabled() ? "ENABLED" : "DISABLED")});
room_->redrawMap(); // Redibuja el tilemap para mostrar/ocultar líneas de colisión
Options::cheats.invincible = static_cast<Options::Cheat::State>(Debug::get()->isEnabled());
player_->setColor();
scoreboard_data_->music = !Debug::get()->isEnabled();
scoreboard_data_->music ? Audio::get()->resumeMusic() : Audio::get()->pauseMusic();
break;
case SDLK_R:
Resource::Cache::get()->reload();
break;
case SDLK_W:
changeRoom(room_->getRoom(Room::Border::TOP));
break;
case SDLK_A:
changeRoom(room_->getRoom(Room::Border::LEFT));
break;
case SDLK_S:
changeRoom(room_->getRoom(Room::Border::BOTTOM));
break;
case SDLK_D:
changeRoom(room_->getRoom(Room::Border::RIGHT));
break;
case SDLK_1:
Options::cheats.infinite_lives = Options::cheats.infinite_lives == Options::Cheat::State::ENABLED ? Options::Cheat::State::DISABLED : Options::Cheat::State::ENABLED;
Notifier::get()->show({std::string("INFINITE LIVES ") + (Options::cheats.infinite_lives == Options::Cheat::State::ENABLED ? "ENABLED" : "DISABLED")}, Notifier::Style::DEFAULT, -1, true);
player_->setColor();
break;
case SDLK_2:
Options::cheats.invincible = Options::cheats.invincible == Options::Cheat::State::ENABLED ? Options::Cheat::State::DISABLED : Options::Cheat::State::ENABLED;
Notifier::get()->show({std::string("INVINCIBLE ") + (Options::cheats.invincible == Options::Cheat::State::ENABLED ? "ENABLED" : "DISABLED")}, Notifier::Style::DEFAULT, -1, true);
player_->setColor();
break;
case SDLK_3:
Options::cheats.jail_is_open = Options::cheats.jail_is_open == Options::Cheat::State::ENABLED ? Options::Cheat::State::DISABLED : Options::Cheat::State::ENABLED;
Notifier::get()->show({std::string("JAIL IS OPEN ") + (Options::cheats.jail_is_open == Options::Cheat::State::ENABLED ? "ENABLED" : "DISABLED")}, Notifier::Style::DEFAULT, -1, true);
break;
case SDLK_0:
Screen::get()->toggleDebugInfo();
break;
default:
break;
}
}
}
// Maneja el arrastre del jugador con el ratón (debug)
void Game::handleDebugMouseDrag(float delta_time) {
// Solo funciona si Debug está habilitado
if (!Debug::get()->isEnabled()) {
return;
}
// Obtener estado del ratón (coordenadas de ventana física)
float mouse_x = 0.0F;
float mouse_y = 0.0F;
SDL_MouseButtonFlags buttons = SDL_GetMouseState(&mouse_x, &mouse_y);
// Convertir coordenadas de ventana a coordenadas lógicas del renderer
float render_x = 0.0F;
float render_y = 0.0F;
SDL_RenderCoordinatesFromWindow(Screen::get()->getRenderer(), mouse_x, mouse_y, &render_x, &render_y);
// Restar el offset del borde para obtener coordenadas del área de juego
SDL_FRect dst_rect = Screen::get()->getGameSurfaceDstRect();
float game_x = render_x - dst_rect.x;
float game_y = render_y - dst_rect.y;
// Verificar si los botones están presionados
bool left_button_pressed = (buttons & SDL_BUTTON_LMASK) != 0;
bool right_button_pressed = (buttons & SDL_BUTTON_RMASK) != 0;
// Botón derecho: teleport instantáneo a la posición del cursor
if (right_button_pressed && !debug_dragging_player_) {
player_->setDebugPosition(game_x, game_y);
player_->finalizeDebugTeleport();
} else if (left_button_pressed) {
// Obtener posición actual del jugador
SDL_FRect player_rect = player_->getRect();
float player_x = player_rect.x;
float player_y = player_rect.y;
// Calcular distancia al objetivo
float dx = game_x - player_x;
float dy = game_y - player_y;
float distance = std::sqrt(dx * dx + dy * dy);
// Constantes de velocidad con ease-in (aceleración progresiva)
constexpr float DRAG_SPEED_MIN = 30.0F; // Velocidad inicial (pixels/segundo)
constexpr float DRAG_SPEED_MAX = 600.0F; // Velocidad máxima (pixels/segundo)
constexpr float DRAG_ACCELERATION = 600.0F; // Aceleración (pixels/segundo²)
// Incrementar velocidad con el tiempo (ease-in)
if (!debug_dragging_player_) {
debug_drag_speed_ = DRAG_SPEED_MIN; // Iniciar con velocidad mínima
}
debug_drag_speed_ = std::min(DRAG_SPEED_MAX, debug_drag_speed_ + DRAG_ACCELERATION * delta_time);
if (distance > 1.0F) {
// Calcular el movimiento con la velocidad actual
float move_factor = std::min(1.0F, debug_drag_speed_ * delta_time / distance);
float new_x = player_x + dx * move_factor;
float new_y = player_y + dy * move_factor;
// Mover el jugador hacia la posición del cursor
player_->setDebugPosition(new_x, new_y);
}
debug_dragging_player_ = true;
Debug::get()->add(std::string("X : " + std::to_string(static_cast<int>(player_rect.x))));
Debug::get()->add(std::string("Y : " + std::to_string(static_cast<int>(player_rect.y))));
} else if (debug_dragging_player_) {
// Botón soltado después de arrastrar: finalizar teleport
player_->finalizeDebugTeleport();
debug_dragging_player_ = false;
debug_drag_speed_ = 0.0F; // Reset para el próximo arrastre
}
}
#endif
// Escribe el nombre de la pantalla
void Game::renderRoomName() {
// Dibuja la textura con el nombre de la habitación
room_name_surface_->render(nullptr, &room_name_rect_);
}
// Cambia de habitación
auto Game::changeRoom(const std::string& room_path) -> bool {
// En las habitaciones los limites tienen la cadena del fichero o un 0 en caso de no limitar con nada
if (room_path == "0") {
return false;
}
// Verifica que exista el fichero que se va a cargar
if (!Resource::List::get()->get(room_path).empty()) {
// Crea un objeto habitación nuevo a partir del fichero
room_ = std::make_shared<Room>(room_path, scoreboard_data_);
// Pone el nombre de la habitación en la textura
fillRoomNameTexture();
// Pone el color del marcador en función del color del borde de la habitación
setScoreBoardColor();
if (room_tracker_->addRoom(room_path)) {
// Incrementa el contador de habitaciones visitadas
scoreboard_data_->rooms++;
}
// Pasa la nueva habitación al jugador
player_->setRoom(room_);
// Cambia la habitación actual
current_room_ = room_path;
return true;
}
return false;
}
// Comprueba si el jugador esta en el borde de la pantalla
void Game::checkPlayerIsOnBorder() {
if (player_->isOnBorder()) {
const auto BORDER = player_->getBorder();
const auto ROOM_NAME = room_->getRoom(BORDER);
// Si puede cambiar de habitación, cambia
if (changeRoom(ROOM_NAME)) {
player_->switchBorders();
spawn_data_ = player_->getSpawnParams();
return;
}
// Si ha llegado al fondo y no hay habitación, muere
if (BORDER == Room::Border::BOTTOM) {
killPlayer();
return;
}
}
}
// Comprueba las colisiones del jugador con los enemigos
auto Game::checkPlayerAndEnemies() -> bool {
const bool DEATH = room_->enemyCollision(player_->getCollider());
if (DEATH) {
killPlayer();
}
return DEATH;
}
// Comprueba las colisiones del jugador con los objetos
void Game::checkPlayerAndItems() {
room_->itemCollision(player_->getCollider());
}
// Comprueba si el jugador esta vivo
void Game::checkIfPlayerIsAlive() {
if (!player_->isAlive()) {
killPlayer();
}
}
// Mata al jugador
void Game::killPlayer() {
if (Options::cheats.invincible == Options::Cheat::State::ENABLED) {
return;
}
// Resta una vida al jugador
if (Options::cheats.infinite_lives == Options::Cheat::State::DISABLED) {
--scoreboard_data_->lives;
}
// Sonido
Audio::get()->playSound(Defaults::Sound::HIT, Audio::Group::GAME);
// Transicionar al estado BLACK_SCREEN (el respawn ocurre en transitionToState)
transitionToState(State::BLACK_SCREEN);
}
// Pone el color del marcador en función del color del borde de la habitación
void Game::setScoreBoardColor() {
// Obtiene el color del borde
const Uint8 BORDER_COLOR = room_->getBorderColor();
const bool IS_BLACK = BORDER_COLOR == Color::index(Color::Cpc::BLACK);
// Si el color del borde es negro cambia el texto del marcador a blanco
scoreboard_data_->color = IS_BLACK ? Color::index(Color::Cpc::WHITE) : BORDER_COLOR;
}
// Comprueba si ha finalizado el juego
auto Game::checkEndGame() -> bool {
const bool IS_ON_THE_ROOM = room_->getName() == "THE JAIL"; // Estar en la habitación que toca
const bool HAVE_THE_ITEMS = scoreboard_data_->items >= int(total_items_ * 0.9F) || Options::cheats.jail_is_open == Options::Cheat::State::ENABLED; // Con mas del 90% de los items recogidos
const bool IS_ON_THE_DOOR = player_->getRect().x <= 128; // Y en la ubicación que toca (En la puerta)
scoreboard_data_->jail_is_open = HAVE_THE_ITEMS;
if (HAVE_THE_ITEMS && IS_ON_THE_ROOM && IS_ON_THE_DOOR) {
// Iniciar transición de fade en vez de cambio inmediato de escena
transitionToState(State::FADE_TO_ENDING);
Audio::get()->fadeOutMusic(Defaults::Music::FADE_DURATION_MS); // Fade out de música
return true;
}
return false;
}
// Obtiene la cantidad total de items que hay en el mapeado del juego
auto Game::getTotalItems() -> int {
int items = 0;
auto rooms = Resource::Cache::get()->getRooms();
for (const auto& room : rooms) {
items += room.room->items.size();
}
return items;
}
// Pone el juego en pausa
void Game::togglePause() {
paused_ = !paused_;
player_->setPaused(paused_);
room_->setPaused(paused_);
scoreboard_->setPaused(paused_);
}
// Da vidas al jugador cuando está en la Jail
void Game::checkRestoringJail(float delta_time) {
if (room_->getName() != "THE JAIL" || scoreboard_data_->lives == 9) {
jail_restore_time_ = 0.0F; // Reset timer cuando no está en la Jail
return;
}
if (!paused_) {
jail_restore_time_ += delta_time;
}
// Incrementa el numero de vidas
if (jail_restore_time_ >= JAIL_RESTORE_INTERVAL) {
jail_restore_time_ -= JAIL_RESTORE_INTERVAL; // Mantiene el excedente para precisión
scoreboard_data_->lives++;
Audio::get()->playSound(Defaults::Sound::HIT, Audio::Group::GAME);
}
}
// Crea la textura con el nombre de la habitación
void Game::fillRoomNameTexture() {
// Pone la textura como destino de renderizado
auto previuos_renderer = Screen::get()->getRendererSurface();
Screen::get()->setRendererSurface(room_name_surface_);
// Rellena la textura de color
room_name_surface_->clear(stringToColor("white"));
// Escribe el texto en la textura
auto text = Resource::Cache::get()->getText("smb2");
text->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, text->getCharacterSize() / 2, room_->getName(), 1, room_->getBGColor());
// Deja el renderizador por defecto
Screen::get()->setRendererSurface(previuos_renderer);
}
// Inicializa al jugador
void Game::initPlayer(const Player::SpawnData& spawn_point, std::shared_ptr<Room> room) {
std::string player_animations = Options::cheats.alternate_skin == Options::Cheat::State::ENABLED ? "player2.yaml" : "player.yaml";
const Player::Data PLAYER{.spawn_data = spawn_point, .animations_path = player_animations, .room = std::move(room)};
player_ = std::make_shared<Player>(PLAYER);
}
// Crea la textura para poner el nombre de la habitación
void Game::createRoomNameTexture() {
auto text = Resource::Cache::get()->getText("smb2");
room_name_surface_ = std::make_shared<Surface>(Options::game.width, text->getCharacterSize() * 2);
// Establece el destino de la textura
room_name_rect_ = {.x = 0.0F, .y = PlayArea::HEIGHT, .w = Options::game.width, .h = text->getCharacterSize() * 2.0F};
}
// Hace sonar la música
void Game::keepMusicPlaying() {
const std::string MUSIC_PATH = mode_ == Mode::GAME ? Defaults::Music::GAME_TRACK : Defaults::Music::TITLE_TRACK;
// Si la música no está sonando
if (Audio::get()->getMusicState() == Audio::MusicState::STOPPED) {
Audio::get()->playMusic(MUSIC_PATH);
}
}
// DEMO MODE: Inicializa las variables para el modo demo
void Game::demoInit() {
if (mode_ == Mode::DEMO) {
demo_ = DemoData(0.0F, 0, {"04.yaml", "54.yaml", "20.yaml", "09.yaml", "05.yaml", "11.yaml", "31.yaml", "44.yaml"});
current_room_ = demo_.rooms.front();
}
}
// DEMO MODE: Comprueba si se ha de cambiar de habitación
void Game::demoCheckRoomChange(float delta_time) {
if (mode_ == Mode::DEMO) {
demo_.time_accumulator += delta_time;
if (demo_.time_accumulator >= DEMO_ROOM_DURATION) {
demo_.time_accumulator = 0.0F;
demo_.room_index++;
if (demo_.room_index == (int)demo_.rooms.size()) {
SceneManager::current = SceneManager::Scene::LOGO;
SceneManager::options = SceneManager::Options::NONE;
} else {
changeRoom(demo_.rooms[demo_.room_index]);
}
}
}
}

132
source/game/scenes/game.hpp Normal file
View File

@@ -0,0 +1,132 @@
#pragma once
#include <SDL3/SDL.h>
#include <initializer_list> // Para initializer_list
#include <memory> // Para shared_ptr
#include <string> // Para string
#include <vector> // Para vector
#include "game/entities/player.hpp" // Para PlayerSpawn
#include "utils/delta_timer.hpp" // Para DeltaTimer
class Room; // lines 12-12
class RoomTracker; // lines 13-13
class Scoreboard; // lines 14-14
class Surface;
class Game {
public:
// --- Estructuras ---
enum class Mode {
DEMO,
GAME
};
enum class State {
PLAYING, // Normal gameplay
BLACK_SCREEN, // Black screen after death (0.30s)
GAME_OVER, // Intermediate state before changing scene
FADE_TO_ENDING, // Fade out transition
POST_FADE_ENDING, // Black screen delay before ending
};
// --- Constructor y Destructor ---
explicit Game(Mode mode);
~Game();
// --- Bucle para el juego ---
void run();
private:
// --- Constantes de tiempo ---
static constexpr float BLACK_SCREEN_DURATION = 0.30F; // Duración de la pantalla negra en segundos (20 frames a 66.67fps)
static constexpr float GAME_OVER_THRESHOLD = 0.255F; // Tiempo antes del game over en segundos (17 frames a 66.67fps)
static constexpr float DEMO_ROOM_DURATION = 6.0F; // Duración de cada habitación en modo demo en segundos (400 frames)
static constexpr float JAIL_RESTORE_INTERVAL = 1.5F; // Intervalo de restauración de vidas en la Jail en segundos (100 frames)
static constexpr float FADE_STEP_INTERVAL = 0.05F; // Intervalo entre pasos de fade en segundos
static constexpr float POST_FADE_DELAY = 2.0F; // Duración de la pantalla negra después del fade
// --- Estructuras ---
struct DemoData {
float time_accumulator{0.0F}; // Acumulador de tiempo para el modo demo
int room_index{0}; // Índice para el vector de habitaciones
std::vector<std::string> rooms; // Listado con los mapas de la demo
};
// --- Métodos ---
void update(); // Actualiza el juego, las variables, comprueba la entrada, etc.
void render(); // Pinta los objetos en pantalla
void handleEvents(); // Comprueba los eventos de la cola
void renderRoomName(); // Escribe el nombre de la pantalla
void transitionToState(State new_state); // Cambia al estado especificado y resetea los timers
void updatePlaying(float delta_time); // Actualiza el juego en estado PLAYING
void updateBlackScreen(float delta_time); // Actualiza el juego en estado BLACK_SCREEN
void updateGameOver(float delta_time); // Actualiza el juego en estado GAME_OVER
void updateFadeToEnding(float delta_time); // Actualiza el juego en estado FADE_TO_ENDING
void updatePostFadeEnding(float delta_time); // Actualiza el juego en estado POST_FADE_ENDING
void renderPlaying(); // Renderiza el juego en estado PLAYING (directo a pantalla)
void renderBlackScreen(); // Renderiza el juego en estado BLACK_SCREEN (pantalla negra)
void renderGameOver(); // Renderiza el juego en estado GAME_OVER (pantalla negra)
void renderFadeToEnding(); // Renderiza el juego en estado FADE_TO_ENDING (via backbuffer)
void renderPostFadeEnding(); // Renderiza el juego en estado POST_FADE_ENDING (pantalla negra)
auto changeRoom(const std::string& room_path) -> bool; // Cambia de habitación
void handleInput(); // Comprueba el teclado
void checkPlayerIsOnBorder(); // Comprueba si el jugador esta en el borde de la pantalla y actua
auto checkPlayerAndEnemies() -> bool; // Comprueba las colisiones del jugador con los enemigos
void checkPlayerAndItems(); // Comprueba las colisiones del jugador con los objetos
void checkIfPlayerIsAlive(); // Comprueba si el jugador esta vivo
void killPlayer(); // Mata al jugador
void setScoreBoardColor(); // Pone el color del marcador en función del color del borde de la habitación
auto checkEndGame() -> bool; // Comprueba si ha finalizado el juego
static auto getTotalItems() -> int; // Obtiene la cantidad total de items que hay en el mapeado del juego
void togglePause(); // Pone el juego en pausa
void checkRestoringJail(float delta_time); // Da vidas al jugador cuando está en la Jail
void fillRoomNameTexture(); // Pone el nombre de la habitación en la textura
void checkSomeCheevos(); // Comprueba algunos logros
void checkEndGameCheevos(); // Comprueba los logros de completar el juego
void initPlayer(const Player::SpawnData& spawn_point, std::shared_ptr<Room> room); // Inicializa al jugador
void createRoomNameTexture(); // Crea la textura para poner el nombre de la habitación
void keepMusicPlaying(); // Hace sonar la música
void demoInit(); // DEMO MODE: Inicializa las variables para el modo demo
void demoCheckRoomChange(float delta_time); // DEMO MODE: Comprueba si se ha de cambiar de habitación
#ifdef _DEBUG
void updateDebugInfo(); // Pone la información de debug en pantalla
static void renderDebugInfo(); // Pone la información de debug en pantalla
void handleDebugEvents(const SDL_Event& event); // Comprueba los eventos
void handleDebugMouseDrag(float delta_time); // Maneja el arrastre del jugador con el ratón (debug)
#endif
// --- Variables miembro ---
// Objetos y punteros a recursos
std::shared_ptr<Scoreboard::Data> scoreboard_data_; // Estructura con los datos del marcador
std::shared_ptr<Scoreboard> scoreboard_; // Objeto encargado de gestionar el marcador
std::shared_ptr<RoomTracker> room_tracker_; // Lleva el control de las habitaciones visitadas
std::shared_ptr<Room> room_; // Objeto encargado de gestionar cada habitación del juego
std::shared_ptr<Player> player_; // Objeto con el jugador
std::shared_ptr<Surface> room_name_surface_; // Textura para escribir el nombre de la habitación
std::shared_ptr<Surface> game_backbuffer_surface_; // Backbuffer para efectos de fade
// Variables de estado del juego
Mode mode_; // Modo del juego
State state_{State::PLAYING}; // Estado actual de la escena
DeltaTimer delta_timer_; // Timer para calcular delta time
std::string current_room_; // Fichero de la habitación actual
Player::SpawnData spawn_data_; // Lugar de la habitación donde aparece el jugador
int total_items_; // Cantidad total de items que hay en el mapeado del juego
bool paused_{false}; // Indica si el juego se encuentra en pausa
float state_time_{0.0F}; // Tiempo acumulado en el estado actual
float fade_accumulator_{0.0F}; // Acumulador de tiempo para el fade
// Variables de demo mode
DemoData demo_; // Variables para el modo demo
// Variables de efectos visuales
SDL_FRect room_name_rect_; // Rectangulo donde pintar la textura con el nombre de la habitación
float jail_restore_time_{0.0F}; // Tiempo acumulado para restauración de vidas en la Jail
#ifdef _DEBUG
// Variables de debug para arrastre con ratón
bool debug_dragging_player_{false}; // Indica si estamos arrastrando al jugador con el ratón
float debug_drag_speed_{0.0F}; // Velocidad actual del arrastre (ease-in)
#endif
};

296
source/game/scenes/logo.cpp Normal file
View File

@@ -0,0 +1,296 @@
#include "game/scenes/logo.hpp"
#include <SDL3/SDL.h>
#include <algorithm> // Para std::clamp
#include <array> // Para std::array
#include <random> // Para generador aleatorio
#include "core/audio/audio.hpp" // Para Audio
#include "core/input/global_inputs.hpp" // Para check
#include "core/input/input.hpp" // Para Input
#include "core/rendering/screen.hpp" // Para Screen
#include "core/rendering/surface.hpp" // Para Surface
#include "core/rendering/surface_sprite.hpp" // Para SSprite
#include "core/resources/resource_cache.hpp" // Para Resource
#include "core/system/global_events.hpp" // Para check
#include "game/options.hpp" // Para Options, SectionState, options, Section
#include "game/scene_manager.hpp" // Para SceneManager
#include "utils/defines.hpp" // Para GameCanvas
#include "utils/delta_timer.hpp" // Para DeltaTimer
#include "utils/easing_functions.hpp" // Para funciones de suavizado
#include "utils/color.hpp" // Para Color
// Constructor
Logo::Logo()
: jailgames_surface_(Resource::Cache::get()->getSurface("jailgames.gif")),
since_1998_surface_(Resource::Cache::get()->getSurface("since_1998.gif")),
delta_timer_(std::make_unique<DeltaTimer>()) {
// Calcula posiciones dinámicas basadas en el tamaño del canvas y las texturas
jailgames_dest_x_ = GameCanvas::CENTER_X - (jailgames_surface_->getWidth() / 2);
base_y_ = GameCanvas::CENTER_Y - (jailgames_surface_->getHeight() / 2);
// Crea el sprite "Since 1998" centrado horizontalmente, debajo del logo JAILGAMES
const int SINCE_1998_X = GameCanvas::CENTER_X - (since_1998_surface_->getWidth() / 2);
const int SINCE_1998_Y = base_y_ + jailgames_surface_->getHeight() + 5;
since_1998_sprite_ = std::make_shared<SurfaceSprite>(
since_1998_surface_,
SINCE_1998_X,
SINCE_1998_Y,
since_1998_surface_->getWidth(),
since_1998_surface_->getHeight());
// Configura variables
since_1998_sprite_->setClip(0, 0, since_1998_surface_->getWidth(), since_1998_surface_->getHeight());
since_1998_color_ = Color::index(Color::Cpc::BLACK);
jailgames_color_ = Color::index(Color::Cpc::BRIGHT_WHITE);
// Inicializa variables
SceneManager::current = SceneManager::Scene::LOGO;
initSprites(); // Crea los sprites de cada linea
initColors(); // Inicializa el vector de colores
// Seleccionar función de easing aleatoria para la animación del logo
// Usamos lambdas para funciones con parámetros opcionales
static const std::array<EasingFunction, 4> EASING_OPTIONS = {
[](float t) { return Easing::backOut(t); }, // Overshoot retro
[](float t) { return Easing::elasticOut(t); }, // Rebote múltiple con oscilación
Easing::bounceOut, // Rebote físico decreciente
Easing::cubicOut // Suavizado sin overshoot (para variedad)
};
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<size_t> dist(0, EASING_OPTIONS.size() - 1);
easing_function_ = EASING_OPTIONS[dist(gen)];
// Cambia el color del borde
Screen::get()->setBorderColor(Color::index(Color::Cpc::BLACK));
}
// Comprueba el manejador de eventos
void Logo::handleEvents() {
SDL_Event event;
while (SDL_PollEvent(&event)) {
GlobalEvents::handle(event);
}
}
// Comprueba las entradas
void Logo::handleInput() {
Input::get()->update();
GlobalInputs::handle();
}
// Gestiona el logo de JAILGAME
void Logo::updateJAILGAMES(float delta_time) {
// Solo actualizar durante el estado JAILGAMES_SLIDE_IN
if (state_ != State::JAILGAMES_SLIDE_IN) {
return;
}
// Calcular el progreso de la animación (0.0 a 1.0)
const float PROGRESS = std::clamp(state_time_ / JAILGAMES_SLIDE_DURATION, 0.0F, 1.0F);
// Aplicar función de suavizado seleccionada aleatoriamente (permite overshoot para efecto de rebote)
// La posición final exacta se garantiza en updateState() antes de transicionar
const float EASED_PROGRESS = easing_function_(PROGRESS);
// Actualizar cada línea del sprite JAILGAMES interpolando con easing
for (size_t i = 0; i < jailgames_sprite_.size(); ++i) {
// Interpolar entre posición inicial y destino usando el progreso suavizado
const auto INITIAL_X = static_cast<float>(jailgames_initial_x_[i]);
const auto DEST_X = static_cast<float>(jailgames_dest_x_);
const float NEW_X = INITIAL_X + ((DEST_X - INITIAL_X) * EASED_PROGRESS);
jailgames_sprite_[i]->setX(NEW_X);
}
}
// Calcula el índice de color según el progreso (0.0-1.0)
auto Logo::getColorIndex(float progress) const -> int {
// Asegurar que progress esté en el rango [0.0, 1.0]
progress = std::clamp(progress, 0.0F, 1.0F);
// Mapear el progreso al índice de color (0-7)
const int MAX_INDEX = static_cast<int>(color_.size()) - 1;
const int INDEX = static_cast<int>(progress * MAX_INDEX);
return INDEX;
}
// Gestiona el color de las texturas
void Logo::updateTextureColors() {
switch (state_) {
case State::SINCE_1998_FADE_IN: {
// Fade-in de "Since 1998" de negro a blanco
const float PROGRESS = state_time_ / SINCE_1998_FADE_DURATION;
since_1998_color_ = color_[getColorIndex(PROGRESS)];
break;
}
case State::DISPLAY: {
// Asegurar que ambos logos estén en blanco durante el display
jailgames_color_ = color_.back(); // BRIGHT_WHITE
since_1998_color_ = color_.back(); // BRIGHT_WHITE
break;
}
case State::FADE_OUT: {
// Fade-out de ambos logos de blanco a negro
const float PROGRESS = 1.0F - (state_time_ / FADE_OUT_DURATION);
const int COLOR_INDEX = getColorIndex(PROGRESS);
jailgames_color_ = color_[COLOR_INDEX];
since_1998_color_ = color_[COLOR_INDEX];
break;
}
default:
// En otros estados, mantener los colores actuales
break;
}
}
// Transiciona a un nuevo estado
void Logo::transitionToState(State new_state) {
state_ = new_state;
state_time_ = 0.0F;
}
// Actualiza el estado actual
void Logo::updateState(float delta_time) {
state_time_ += delta_time;
// Gestionar transiciones entre estados basándose en el tiempo
switch (state_) {
case State::INITIAL:
if (state_time_ >= INITIAL_DELAY) {
transitionToState(State::JAILGAMES_SLIDE_IN);
}
break;
case State::JAILGAMES_SLIDE_IN:
if (state_time_ >= JAILGAMES_SLIDE_DURATION) {
// Garantizar que todas las líneas estén exactamente en la posición final
// antes de transicionar (previene race condition con updateJAILGAMES)
for (auto& sprite : jailgames_sprite_) {
sprite->setX(jailgames_dest_x_);
}
transitionToState(State::SINCE_1998_FADE_IN);
}
break;
case State::SINCE_1998_FADE_IN:
if (state_time_ >= SINCE_1998_FADE_DURATION) {
transitionToState(State::DISPLAY);
}
break;
case State::DISPLAY:
if (state_time_ >= DISPLAY_DURATION) {
transitionToState(State::FADE_OUT);
}
break;
case State::FADE_OUT:
if (state_time_ >= FADE_OUT_DURATION) {
transitionToState(State::END);
endSection();
}
break;
case State::END:
// Estado final, no hacer nada
break;
}
}
// Actualiza las variables
void Logo::update() {
const float DELTA_TIME = delta_timer_->tick();
handleEvents(); // Comprueba los eventos
handleInput(); // Comprueba las entradas
updateState(DELTA_TIME); // Actualiza el estado y gestiona transiciones
updateJAILGAMES(DELTA_TIME); // Gestiona el logo de JAILGAME
updateTextureColors(); // Gestiona el color de las texturas
Audio::update(); // Actualiza el objeto Audio
Screen::get()->update(DELTA_TIME); // Actualiza el objeto Screen
}
// Dibuja en pantalla
void Logo::render() {
// Prepara para empezar a dibujar en la textura de juego
Screen::get()->start();
Screen::get()->clearSurface(Color::index(Color::Cpc::BLACK));
// Dibuja los objetos
for (const auto& sprite : jailgames_sprite_) {
sprite->render(27, jailgames_color_);
}
since_1998_sprite_->render(27, since_1998_color_);
// Vuelca el contenido del renderizador en pantalla
Screen::get()->render();
}
// Bucle para el logo del juego
void Logo::run() {
while (SceneManager::current == SceneManager::Scene::LOGO) {
update();
render();
}
}
// Termina la sección
void Logo::endSection() {
switch (SceneManager::options) {
case SceneManager::Options::NONE:
SceneManager::current = SceneManager::Scene::TITLE;
break;
default:
// Ninguna acción por defecto
break;
}
}
// Inicializa el vector de colores
void Logo::initColors() {
// Inicializa el vector de colores
const std::vector<Uint8> COLORS = {
Color::index(Color::Cpc::BLACK),
Color::index(Color::Cpc::BLUE),
Color::index(Color::Cpc::RED),
Color::index(Color::Cpc::MAGENTA),
Color::index(Color::Cpc::GREEN),
Color::index(Color::Cpc::CYAN),
Color::index(Color::Cpc::YELLOW),
Color::index(Color::Cpc::BRIGHT_WHITE)};
for (const auto& color : COLORS) {
color_.push_back(color);
}
}
// Crea los sprites de cada linea
void Logo::initSprites() {
const int TEXTURE_WIDTH = jailgames_surface_->getWidth();
jailgames_initial_x_.reserve(jailgames_surface_->getHeight());
for (int i = 0; i < jailgames_surface_->getHeight(); ++i) {
jailgames_sprite_.push_back(std::make_shared<SurfaceSprite>(jailgames_surface_, 0, i, TEXTURE_WIDTH, 1));
jailgames_sprite_.back()->setClip(0, i, TEXTURE_WIDTH, 1);
// Calcular posición inicial (alternando entre derecha e izquierda, fuera del canvas)
constexpr int LINE_OFFSET = 6;
const int INITIAL_X = (i % 2 == 0)
? (GameCanvas::WIDTH + (i * LINE_OFFSET))
: (-TEXTURE_WIDTH - (i * LINE_OFFSET));
jailgames_initial_x_.push_back(INITIAL_X);
jailgames_sprite_.at(i)->setX(INITIAL_X);
jailgames_sprite_.at(i)->setY(base_y_ + i);
}
}

View File

@@ -0,0 +1,81 @@
#pragma once
#include <SDL3/SDL.h>
#include <functional> // Para std::function
#include <memory> // Para shared_ptr
#include <vector> // Para vector
#include "utils/delta_timer.hpp" // Para DeltaTimer
class SurfaceSprite; // Forward declaration
class Surface; // Forward declaration
class Logo {
public:
// --- Tipos ---
using EasingFunction = std::function<float(float)>; // Función de easing (permite lambdas)
// --- Enumeraciones ---
enum class State {
INITIAL, // Espera inicial
JAILGAMES_SLIDE_IN, // Las líneas de JAILGAMES se deslizan hacia el centro
SINCE_1998_FADE_IN, // Aparición gradual del texto "Since 1998"
DISPLAY, // Logo completo visible
FADE_OUT, // Desaparición gradual
END // Fin de la secuencia
};
// --- Constructor y Destructor ---
Logo();
~Logo() = default;
// --- Bucle principal ---
void run();
private:
// --- Constantes de tiempo (en segundos) ---
static constexpr float INITIAL_DELAY = 0.5F; // Tiempo antes de que empiece la animación
static constexpr float SINCE_1998_FADE_DURATION = 0.5F; // Duración del fade-in de "Since 1998"
static constexpr float DISPLAY_DURATION = 3.5F; // Tiempo que el logo permanece visible
static constexpr float FADE_OUT_DURATION = 0.5F; // Duración del fade-out final
// --- Constantes de animación ---
static constexpr float JAILGAMES_SLIDE_DURATION = 0.8F; // Duración de la animación de slide-in (segundos)
// --- Métodos ---
void update(); // Actualiza las variables
void render(); // Dibuja en pantalla
static void handleEvents(); // Comprueba el manejador de eventos
static void handleInput(); // Comprueba las entradas
void updateJAILGAMES(float delta_time); // Gestiona el logo de JAILGAME (time-based)
void updateTextureColors(); // Gestiona el color de las texturas
void updateState(float delta_time); // Actualiza el estado actual
void transitionToState(State new_state); // Transiciona a un nuevo estado
[[nodiscard]] auto getColorIndex(float progress) const -> int; // Calcula el índice de color según el progreso (0.0-1.0)
static void endSection(); // Termina la sección
void initColors(); // Inicializa el vector de colores
void initSprites(); // Crea los sprites de cada linea
// --- Variables miembro ---
// Objetos y punteros a recursos
std::shared_ptr<Surface> jailgames_surface_; // Textura con los graficos "JAILGAMES"
std::shared_ptr<Surface> since_1998_surface_; // Textura con los graficos "Since 1998"
std::vector<std::shared_ptr<SurfaceSprite>> jailgames_sprite_; // Vector con los sprites de cada linea que forman el bitmap JAILGAMES
std::vector<int> jailgames_initial_x_; // Posiciones X iniciales de cada línea (para interpolación con easing)
std::shared_ptr<SurfaceSprite> since_1998_sprite_; // SSprite para manejar la textura2
std::unique_ptr<DeltaTimer> delta_timer_; // Timer para delta time
// Posiciones calculadas dinámicamente (centrado independiente del tamaño del canvas)
int jailgames_dest_x_{0}; // Posición X de destino para JAILGAMES (centrado horizontal)
int base_y_{0}; // Posición Y base para los sprites (centrado vertical)
// Variables de estado de colores
std::vector<Uint8> color_; // Vector con los colores para el fade
Uint8 jailgames_color_{0}; // Color para el sprite de "JAILGAMES"
Uint8 since_1998_color_{0}; // Color para el sprite de "Since 1998"
// Variables de estado de la secuencia
State state_{State::INITIAL}; // Estado actual de la secuencia
float state_time_{0.0F}; // Tiempo acumulado en el estado actual
EasingFunction easing_function_; // Función de easing para la animación del logo
};

View File

@@ -0,0 +1,612 @@
#include "game/scenes/title.hpp"
#include <SDL3/SDL.h>
#include <algorithm> // Para clamp
#include "core/audio/audio.hpp" // Para Audio
#include "core/input/global_inputs.hpp" // Para check
#include "core/input/input.hpp" // Para Input, InputAction, Input::DO_NOT_ALLOW_REPEAT, REP...
#include "core/rendering/screen.hpp" // Para Screen
#include "core/rendering/surface.hpp" // Para Surface
#include "core/rendering/surface_sprite.hpp" // Para SSprite
#include "core/rendering/text.hpp" // Para Text, Text::CENTER_FLAG, Text::COLOR_FLAG
#include "core/resources/resource_cache.hpp" // Para Resource
#include "core/resources/resource_list.hpp" // Para Asset
#include "core/system/global_events.hpp" // Para check
#include "game/defaults.hpp" // Para Defaults::Music
#include "game/options.hpp" // Para Options, options, SectionState, Section
#include "game/scene_manager.hpp" // Para SceneManager
#include "utils/defines.hpp" // Para GameCanvas::CENTER_X, GameCanvas::WIDTH
#include "utils/color.hpp" // Para Color
#include "utils/utils.hpp" // Para stringToColor
// Constructor
Title::Title()
: game_logo_surface_(Resource::Cache::get()->getSurface("title_logo.gif")),
game_logo_sprite_(std::make_unique<SurfaceSprite>(
game_logo_surface_,
(GameCanvas::WIDTH - game_logo_surface_->getWidth()) / 2, // Centrado horizontal dinámico
static_cast<int>(GameCanvas::HEIGHT * 0.05F), // Posición Y proporcional (~5% desde arriba)
game_logo_surface_->getWidth(),
game_logo_surface_->getHeight())),
title_surface_(std::make_shared<Surface>(Options::game.width, Options::game.height)),
delta_timer_(std::make_unique<DeltaTimer>()),
menu_text_(Resource::Cache::get()->getText("gauntlet")) {
// Inicializa arrays con valores por defecto
temp_keys_.fill(SDL_SCANCODE_UNKNOWN);
temp_buttons_.fill(-1);
// Determina el estado inicial
state_ = State::MAIN_MENU;
// Establece SceneManager
SceneManager::current = SceneManager::Scene::TITLE;
SceneManager::options = SceneManager::Options::NONE;
// Acciones iniciales
Screen::get()->setBorderColor(Color::index(Color::Cpc::BLACK)); // Cambia el color del borde
Audio::get()->playMusic(Defaults::Music::TITLE_TRACK); // Inicia la musica
}
// Comprueba el manejador de eventos
void Title::handleEvents() {
SDL_Event event;
while (SDL_PollEvent(&event)) {
GlobalEvents::handle(event);
// Manejo especial para captura de botones de gamepad
if (is_remapping_joystick_ && !remap_completed_ &&
(event.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN || event.type == SDL_EVENT_GAMEPAD_AXIS_MOTION)) {
handleJoystickRemap(event);
continue; // No procesar más este evento
}
if (event.type == SDL_EVENT_KEY_DOWN) {
// Si estamos en modo remap de teclado, capturar tecla
if (is_remapping_keyboard_ && !remap_completed_) {
handleKeyboardRemap(event);
}
// Si estamos en el menú principal normal
else if (state_ == State::MAIN_MENU && !is_remapping_keyboard_ && !is_remapping_joystick_) {
handleMainMenuKeyPress(event.key.key);
}
}
}
}
// Maneja las teclas del menu principal
void Title::handleMainMenuKeyPress(SDL_Keycode key) {
switch (key) {
case SDLK_1:
// PLAY
exit_scene_ = SceneManager::Scene::GAME;
transitionToState(State::FADE_MENU);
Audio::get()->fadeOutMusic(Defaults::Music::FADE_DURATION_MS);
break;
case SDLK_2:
// REDEFINE KEYBOARD
is_remapping_keyboard_ = true;
is_remapping_joystick_ = false;
remap_step_ = 0;
remap_completed_ = false;
remap_error_message_.clear();
state_time_ = 0.0F;
break;
case SDLK_3:
// REDEFINE JOYSTICK (siempre visible, pero solo funciona si hay gamepad)
if (Input::get()->gameControllerFound()) {
is_remapping_keyboard_ = false;
is_remapping_joystick_ = true;
remap_step_ = 0;
remap_completed_ = false;
remap_error_message_.clear();
axis_cooldown_ = 0.0F;
state_time_ = 0.0F;
}
// Si no hay gamepad, simplemente no hacer nada
break;
default:
break;
}
}
// Comprueba las entradas
void Title::handleInput(float delta_time) {
Input::get()->update();
// Permitir cancelar remap con ESC/CANCEL
if ((is_remapping_keyboard_ || is_remapping_joystick_) && !remap_completed_) {
if (Input::get()->checkAction(InputAction::CANCEL, Input::DO_NOT_ALLOW_REPEAT)) {
is_remapping_keyboard_ = false;
is_remapping_joystick_ = false;
remap_step_ = 0;
remap_completed_ = false;
remap_error_message_.clear();
}
// Durante el remap, no procesar otras entradas
GlobalInputs::handle();
return;
}
GlobalInputs::handle();
}
// Actualiza las variables
void Title::update() {
const float DELTA_TIME = delta_timer_->tick();
handleEvents(); // Comprueba los eventos
handleInput(DELTA_TIME); // Comprueba las entradas
updateState(DELTA_TIME); // Actualiza el estado actual
Audio::update(); // Actualiza el objeto Audio
Screen::get()->update(DELTA_TIME); // Actualiza el objeto Screen
}
// Actualiza el estado actual
void Title::updateState(float delta_time) {
switch (state_) {
case State::MAIN_MENU:
updateMainMenu(delta_time);
break;
case State::FADE_MENU:
updateFadeMenu(delta_time);
break;
case State::POST_FADE_MENU:
updatePostFadeMenu(delta_time);
break;
default:
break;
}
}
// Transiciona a un nuevo estado
void Title::transitionToState(State new_state) {
state_ = new_state;
state_time_ = 0.0F;
fade_accumulator_ = 0.0F;
}
// Actualiza el estado MAIN_MENU
void Title::updateMainMenu(float delta_time) {
// Si estamos en modo remap, manejar la lógica específica
if (is_remapping_keyboard_ || is_remapping_joystick_) {
// Decrementar cooldown de ejes si estamos capturando botones de joystick
if (is_remapping_joystick_ && axis_cooldown_ > 0.0F) {
axis_cooldown_ -= delta_time;
axis_cooldown_ = std::max(axis_cooldown_, 0.0F);
}
// Si el remap está completado, esperar antes de guardar
if (remap_completed_) {
state_time_ += delta_time;
if (state_time_ >= KEYBOARD_REMAP_DISPLAY_DELAY) {
if (is_remapping_keyboard_) {
applyKeyboardRemap();
} else if (is_remapping_joystick_) {
applyJoystickRemap();
}
// Resetear estado de remap
is_remapping_keyboard_ = false;
is_remapping_joystick_ = false;
remap_completed_ = false;
state_time_ = 0.0F;
}
}
} else {
// Incrementa el temporizador solo en el menú principal normal
state_time_ += delta_time;
// Si el tiempo alcanza el timeout, vuelve al logo
if (state_time_ >= MAIN_MENU_IDLE_TIMEOUT) {
exit_scene_ = SceneManager::Scene::LOGO;
transitionToState(State::FADE_MENU);
}
}
}
// Actualiza el estado FADE_MENU
void Title::updateFadeMenu(float delta_time) {
fade_accumulator_ += delta_time;
if (fade_accumulator_ >= FADE_STEP_INTERVAL) {
fade_accumulator_ = 0.0F;
if (title_surface_->fadeSubPalette()) {
transitionToState(State::POST_FADE_MENU);
}
}
}
// Actualiza el estado POST_FADE_MENU
void Title::updatePostFadeMenu(float delta_time) {
state_time_ += delta_time;
if (state_time_ >= POST_FADE_DELAY) {
SceneManager::current = exit_scene_;
SceneManager::options = SceneManager::Options::NONE;
}
}
// Dibuja en pantalla
void Title::render() {
// Rellena la surface
fillTitleSurface();
// Prepara para empezar a dibujar en la textura de juego
Screen::get()->start();
Screen::get()->clearSurface(Color::index(Color::Cpc::BLACK));
// Dibuja en pantalla la surface con la composicion
title_surface_->render();
// Vuelca el contenido del renderizador en pantalla
Screen::get()->render();
}
// Bucle para el logo del juego
void Title::run() {
while (SceneManager::current == SceneManager::Scene::TITLE) {
update();
render();
}
}
// Dibuja el logo con el titulo del juego
void Title::renderGameLogo() {
game_logo_sprite_->render();
}
// Dibuja el menu principal
void Title::renderMainMenu() {
// Si estamos en modo remap, mostrar la pantalla correspondiente
if (is_remapping_keyboard_) {
renderKeyboardRemap();
return;
}
if (is_remapping_joystick_) {
renderJoystickRemap();
return;
}
// Zona dinámica del menú (proporcional al canvas)
// El logo ocupa la parte superior, el menú se centra en el espacio restante
const int LOGO_BOTTOM = static_cast<int>(GameCanvas::HEIGHT * 0.25F); // Espacio reservado para logo
const int MENU_ZONE_HEIGHT = GameCanvas::HEIGHT - LOGO_BOTTOM; // Espacio disponible para menú
// Menú principal normal con 3 opciones centradas verticalmente en la zona
const Uint8 COLOR = stringToColor("green");
const int TEXT_SIZE = menu_text_->getCharacterSize();
const int MENU_CENTER_Y = LOGO_BOTTOM + (MENU_ZONE_HEIGHT / 2);
const int SPACING = 2 * TEXT_SIZE; // Espaciado entre opciones
// Calcula posiciones centradas verticalmente (3 items con espaciado)
const int TOTAL_HEIGHT = 2 * SPACING; // 2 espacios entre 3 items
const int START_Y = MENU_CENTER_Y - (TOTAL_HEIGHT / 2);
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, START_Y, "1. PLAY", 1, COLOR);
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, START_Y + SPACING, "2. REDEFINE KEYBOARD", 1, COLOR);
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, START_Y + (2 * SPACING), "3. REDEFINE JOYSTICK", 1, COLOR);
}
// Dibuja los elementos en la surface
void Title::fillTitleSurface() {
// Renderiza sobre la textura
auto previuos_renderer = Screen::get()->getRendererSurface();
Screen::get()->setRendererSurface(title_surface_);
// Rellena la textura de color
title_surface_->clear(Color::index(Color::Cpc::BLACK));
switch (state_) {
case State::MAIN_MENU:
case State::FADE_MENU:
renderGameLogo();
renderMainMenu();
break;
default:
break;
}
// Deja el renderizador como estaba
Screen::get()->setRendererSurface(previuos_renderer);
}
// Maneja la captura de teclas para redefinir el teclado
void Title::handleKeyboardRemap(const SDL_Event& event) {
SDL_Scancode scancode = event.key.scancode;
// Valida la tecla
if (!isKeyValid(scancode)) {
remap_error_message_ = "INVALID KEY! TRY ANOTHER";
return;
}
// Verifica duplicados
if (isKeyDuplicate(scancode, remap_step_)) {
remap_error_message_ = "KEY ALREADY USED! TRY ANOTHER";
return;
}
// Tecla valida, guardar
temp_keys_[remap_step_] = scancode;
remap_error_message_.clear();
remap_step_++;
// Si completamos los 3 pasos, mostrar resultado y esperar
if (remap_step_ >= 3) {
remap_completed_ = true;
state_time_ = 0.0F; // Resetear el timer para el delay de 1 segundo
}
}
// Valida si una tecla es permitida
auto Title::isKeyValid(SDL_Scancode scancode) -> bool {
// Prohibir ESC (reservado para cancelar)
if (scancode == SDL_SCANCODE_ESCAPE) {
return false;
}
// Prohibir teclas F1-F12 (reservadas para funciones del sistema)
if (scancode >= SDL_SCANCODE_F1 && scancode <= SDL_SCANCODE_F12) {
return false;
}
// Prohibir Enter/Return (reservado para confirmaciones)
if (scancode == SDL_SCANCODE_RETURN || scancode == SDL_SCANCODE_RETURN2) {
return false;
}
return true;
}
// Verifica si una tecla ya fue usada en pasos anteriores
auto Title::isKeyDuplicate(SDL_Scancode scancode, int current_step) -> bool {
for (int i = 0; i < current_step; i++) {
if (temp_keys_[i] == scancode) {
return true;
}
}
return false;
}
// Retorna el nombre de la accion para el paso actual
auto Title::getActionName(int step) -> std::string {
switch (step) {
case 0:
return "LEFT";
case 1:
return "RIGHT";
case 2:
return "JUMP";
default:
return "UNKNOWN";
}
}
// Aplica y guarda las teclas redefinidas
void Title::applyKeyboardRemap() {
// Guardar las nuevas teclas en Options::controls
Options::keyboard_controls.key_left = temp_keys_[0];
Options::keyboard_controls.key_right = temp_keys_[1];
Options::keyboard_controls.key_jump = temp_keys_[2];
// Aplicar los bindings al sistema de Input
Input::get()->applyKeyboardBindingsFromOptions();
// Guardar a archivo de configuracion
Options::saveToFile();
}
// Dibuja la pantalla de redefinir teclado
void Title::renderKeyboardRemap() {
// Zona dinámica del menú (proporcional al canvas)
const int LOGO_BOTTOM = static_cast<int>(GameCanvas::HEIGHT * 0.25F);
const int MENU_ZONE_HEIGHT = GameCanvas::HEIGHT - LOGO_BOTTOM;
const Uint8 COLOR = stringToColor("green");
const Uint8 ERROR_COLOR = stringToColor("red");
const int TEXT_SIZE = menu_text_->getCharacterSize();
const int MENU_CENTER_Y = LOGO_BOTTOM + (MENU_ZONE_HEIGHT / 2);
// Calcula posiciones centradas verticalmente
// Layout: Mensaje principal, espacio, 3 teclas (LEFT/RIGHT/JUMP), espacio, mensaje de error
const int LINE_SPACING = TEXT_SIZE;
const int START_Y = MENU_CENTER_Y - (2 * TEXT_SIZE); // Centrado aproximado
// Mensaje principal: "PRESS KEY FOR [ACTION]" o "KEYS DEFINED" si completado
if (remap_step_ >= 3) {
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, START_Y, "KEYS DEFINED", 1, COLOR);
} else {
const std::string ACTION = getActionName(remap_step_);
const std::string MESSAGE = "PRESS KEY FOR " + ACTION;
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, START_Y, MESSAGE, 1, COLOR);
}
// Mostrar teclas ya capturadas (con espaciado de 2 líneas desde el mensaje principal)
const int KEYS_START_Y = START_Y + (2 * LINE_SPACING);
if (remap_step_ > 0) {
const std::string LEFT_KEY = SDL_GetScancodeName(temp_keys_[0]);
const std::string LEFT_MSG = "LEFT: " + LEFT_KEY;
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, KEYS_START_Y, LEFT_MSG, 1, COLOR);
}
if (remap_step_ > 1) {
const std::string RIGHT_KEY = SDL_GetScancodeName(temp_keys_[1]);
const std::string RIGHT_MSG = "RIGHT: " + RIGHT_KEY;
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, KEYS_START_Y + LINE_SPACING, RIGHT_MSG, 1, COLOR);
}
if (remap_step_ >= 3) {
const std::string JUMP_KEY = SDL_GetScancodeName(temp_keys_[2]);
const std::string JUMP_MSG = "JUMP: " + JUMP_KEY;
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, KEYS_START_Y + (2 * LINE_SPACING), JUMP_MSG, 1, COLOR);
}
// Mensaje de error si existe (4 líneas después del inicio de las teclas)
if (!remap_error_message_.empty()) {
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, KEYS_START_Y + (4 * LINE_SPACING), remap_error_message_, 1, ERROR_COLOR);
}
}
// Dibuja la pantalla de redefinir joystick
void Title::renderJoystickRemap() {
// Zona dinámica del menú (proporcional al canvas)
const int LOGO_BOTTOM = static_cast<int>(GameCanvas::HEIGHT * 0.25F);
const int MENU_ZONE_HEIGHT = GameCanvas::HEIGHT - LOGO_BOTTOM;
const Uint8 COLOR = stringToColor("green");
const Uint8 ERROR_COLOR = stringToColor("red");
const int TEXT_SIZE = menu_text_->getCharacterSize();
const int MENU_CENTER_Y = LOGO_BOTTOM + (MENU_ZONE_HEIGHT / 2);
// Calcula posiciones centradas verticalmente
// Layout: Mensaje principal, espacio, 3 botones (LEFT/RIGHT/JUMP), espacio, mensaje de error
const int LINE_SPACING = TEXT_SIZE;
const int START_Y = MENU_CENTER_Y - (2 * TEXT_SIZE); // Centrado aproximado
// Mensaje principal: "PRESS BUTTON FOR [ACTION]" o "BUTTONS DEFINED" si completado
if (remap_step_ >= 3) {
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, START_Y, "BUTTONS DEFINED", 1, COLOR);
} else {
const std::string ACTION = getActionName(remap_step_);
const std::string MESSAGE = "PRESS BUTTON FOR " + ACTION;
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, START_Y, MESSAGE, 1, COLOR);
}
// Mostrar botones ya capturados (con espaciado de 2 líneas desde el mensaje principal)
const int BUTTONS_START_Y = START_Y + (2 * LINE_SPACING);
if (remap_step_ > 0) {
const std::string LEFT_BTN = getButtonName(temp_buttons_[0]);
const std::string LEFT_MSG = "LEFT: " + LEFT_BTN;
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, BUTTONS_START_Y, LEFT_MSG, 1, COLOR);
}
if (remap_step_ > 1) {
const std::string RIGHT_BTN = getButtonName(temp_buttons_[1]);
const std::string RIGHT_MSG = "RIGHT: " + RIGHT_BTN;
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, BUTTONS_START_Y + LINE_SPACING, RIGHT_MSG, 1, COLOR);
}
if (remap_step_ >= 3) {
const std::string JUMP_BTN = getButtonName(temp_buttons_[2]);
const std::string JUMP_MSG = "JUMP: " + JUMP_BTN;
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, BUTTONS_START_Y + (2 * LINE_SPACING), JUMP_MSG, 1, COLOR);
}
// Mensaje de error si existe (4 líneas después del inicio de los botones)
if (!remap_error_message_.empty()) {
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, BUTTONS_START_Y + (4 * LINE_SPACING), remap_error_message_, 1, ERROR_COLOR);
}
}
// Maneja la captura de botones del gamepad para redefinir
void Title::handleJoystickRemap(const SDL_Event& event) {
int captured_button = -1;
// Capturar botones del gamepad
if (event.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN) {
captured_button = static_cast<int>(event.gbutton.button);
}
// Capturar triggers y ejes analógicos
else if (event.type == SDL_EVENT_GAMEPAD_AXIS_MOTION) {
// Si el cooldown está activo, ignorar eventos de ejes (evita múltiples capturas)
if (axis_cooldown_ > 0.0F) {
return;
}
constexpr Sint16 TRIGGER_THRESHOLD = 20000;
constexpr Sint16 AXIS_THRESHOLD = 20000;
// Capturar triggers como botones (usando valores especiales 100/101)
if (event.gaxis.axis == SDL_GAMEPAD_AXIS_LEFT_TRIGGER && event.gaxis.value > TRIGGER_THRESHOLD) {
captured_button = Input::TRIGGER_L2_AS_BUTTON; // 100
axis_cooldown_ = 0.5F; // Cooldown de medio segundo
} else if (event.gaxis.axis == SDL_GAMEPAD_AXIS_RIGHT_TRIGGER && event.gaxis.value > TRIGGER_THRESHOLD) {
captured_button = Input::TRIGGER_R2_AS_BUTTON; // 101
axis_cooldown_ = 0.5F;
}
// Capturar ejes del stick analógico (usando valores especiales 200+)
else if (event.gaxis.axis == SDL_GAMEPAD_AXIS_LEFTX) {
if (event.gaxis.value < -AXIS_THRESHOLD) {
captured_button = 200; // Left stick izquierda
axis_cooldown_ = 0.5F;
} else if (event.gaxis.value > AXIS_THRESHOLD) {
captured_button = 201; // Left stick derecha
axis_cooldown_ = 0.5F;
}
}
}
// Si no se capturó ningún input válido, salir
if (captured_button == -1) {
return;
}
// Verifica duplicados
if (isButtonDuplicate(captured_button, remap_step_)) {
remap_error_message_ = "BUTTON ALREADY USED! TRY ANOTHER";
return;
}
// Botón válido, guardar
temp_buttons_[remap_step_] = captured_button;
remap_error_message_.clear();
remap_step_++;
// Si completamos los 3 pasos, mostrar resultado y esperar
if (remap_step_ >= 3) {
remap_completed_ = true;
state_time_ = 0.0F; // Resetear el timer para el delay
}
}
// Valida si un botón está duplicado
auto Title::isButtonDuplicate(int button, int current_step) -> bool {
for (int i = 0; i < current_step; ++i) {
if (temp_buttons_[i] == button) {
return true;
}
}
return false;
}
// Aplica y guarda los botones del gamepad redefinidos
void Title::applyJoystickRemap() {
// Guardar los nuevos botones en Options::gamepad_controls
Options::gamepad_controls.button_left = temp_buttons_[0];
Options::gamepad_controls.button_right = temp_buttons_[1];
Options::gamepad_controls.button_jump = temp_buttons_[2];
// Aplicar los bindings al sistema de Input
Input::get()->applyGamepadBindingsFromOptions();
// Guardar a archivo de configuracion
Options::saveToFile();
}
// Retorna el nombre amigable del botón del gamepad
auto Title::getButtonName(int button) -> std::string {
// Triggers especiales
if (button == Input::TRIGGER_L2_AS_BUTTON) {
return "L2";
}
if (button == Input::TRIGGER_R2_AS_BUTTON) {
return "R2";
}
// Ejes del stick analógico
if (button == 200) {
return "LEFT STICK LEFT";
}
if (button == 201) {
return "LEFT STICK RIGHT";
}
// Botones estándar SDL
const auto GAMEPAD_BUTTON = static_cast<SDL_GamepadButton>(button);
const char* button_name = SDL_GetGamepadStringForButton(GAMEPAD_BUTTON);
return (button_name != nullptr) ? std::string(button_name) : "UNKNOWN";
}

View File

@@ -0,0 +1,87 @@
#pragma once
#include <SDL3/SDL.h>
#include <array> // Para std::array
#include <memory> // Para shared_ptr
#include <string> // Para string
#include "game/scene_manager.hpp" // Para SceneManager::Scene
#include "utils/delta_timer.hpp" // Para DeltaTimer
class SurfaceSprite; // Forward declaration
class Surface; // Forward declaration
class Text; // Forward declaration
class Title {
public:
// --- Constructor y Destructor ---
Title();
~Title() = default;
// --- Bucle principal ---
void run();
private:
// --- Enumeraciones ---
enum class State {
MAIN_MENU,
FADE_MENU,
POST_FADE_MENU,
};
// --- Constantes de tiempo (en segundos) ---
static constexpr float FADE_STEP_INTERVAL = 0.05F; // Intervalo entre pasos de fade (antes cada 4 frames)
static constexpr float POST_FADE_DELAY = 1.0F; // Delay después del fade (pantalla en negro)
static constexpr float MAIN_MENU_IDLE_TIMEOUT = 20.0F; // Timeout para ir a créditos (antes 2200 frames)
static constexpr float KEYBOARD_REMAP_DISPLAY_DELAY = 2.0F; // Tiempo mostrando teclas definidas antes de guardar
// --- Métodos ---
void update(); // Actualiza las variables
void render(); // Dibuja en pantalla
void handleEvents(); // Comprueba el manejador de eventos
void handleMainMenuKeyPress(SDL_Keycode key); // Maneja las teclas del menu principal
void handleInput(float delta_time); // Comprueba las entradas
void updateState(float delta_time); // Actualiza el estado actual
void transitionToState(State new_state); // Transiciona a un nuevo estado
void updateMainMenu(float delta_time); // Actualiza MAIN_MENU
void updateFadeMenu(float delta_time); // Actualiza FADE_MENU
void updatePostFadeMenu(float delta_time); // Actualiza POST_FADE_MENU
void renderGameLogo(); // Dibuja el logo con el titulo del juego
void renderMainMenu(); // Dibuja el menu principal
void renderKeyboardRemap(); // Dibuja la pantalla de redefinir teclado
void renderJoystickRemap(); // Dibuja la pantalla de redefinir joystick
void handleKeyboardRemap(const SDL_Event& event); // Maneja la captura de teclas
void handleJoystickRemap(const SDL_Event& event); // Maneja la captura de botones del gamepad
static auto isKeyValid(SDL_Scancode scancode) -> bool; // Valida si una tecla es permitida
auto isKeyDuplicate(SDL_Scancode scancode, int current_step) -> bool; // Valida si una tecla esta duplicada
auto isButtonDuplicate(int button, int current_step) -> bool; // Valida si un boton esta duplicado
void applyKeyboardRemap(); // Aplica y guarda las teclas redefinidas
void applyJoystickRemap(); // Aplica y guarda los botones del gamepad redefinidos
static auto getActionName(int step) -> std::string; // Retorna el nombre de la accion (LEFT/RIGHT/JUMP)
static auto getButtonName(int button) -> std::string; // Retorna el nombre amigable del boton del gamepad
void fillTitleSurface(); // Dibuja los elementos en la surface
// --- Variables miembro ---
// Objetos y punteros
std::shared_ptr<Surface> game_logo_surface_; // Textura con los graficos
std::unique_ptr<SurfaceSprite> game_logo_sprite_; // SSprite para manejar la surface
std::shared_ptr<Surface> title_surface_; // Surface donde se dibuja toda la clase
std::unique_ptr<DeltaTimer> delta_timer_; // Timer para delta time
std::shared_ptr<Text> menu_text_; // Texto para los menus
// Variables de estado general
State state_; // Estado en el que se encuentra el bucle principal
float state_time_{0.0F}; // Tiempo acumulado en el estado actual
float fade_accumulator_{0.0F}; // Acumulador para controlar el fade por tiempo
SceneManager::Scene exit_scene_{SceneManager::Scene::GAME}; // Escena de destino al salir del título
// Variables para redefinir controles
bool is_remapping_keyboard_{false}; // True si estamos redefiniendo teclado
bool is_remapping_joystick_{false}; // True si estamos redefiniendo joystick
int remap_step_{0}; // Paso actual en la redefinicion (0=LEFT, 1=RIGHT, 2=JUMP)
std::array<SDL_Scancode, 3> temp_keys_; // Almacenamiento temporal de teclas capturadas
std::array<int, 3> temp_buttons_; // Almacenamiento temporal de botones de gamepad capturados
std::string remap_error_message_; // Mensaje de error si la tecla/boton es invalido
float axis_cooldown_{0.0F}; // Cooldown para evitar múltiples capturas de ejes
bool remap_completed_{false}; // True cuando se completa el remap (mostrar antes de guardar)
};