#include "title.h" #include // Para SDL_PollEvent, SDL_Event, SDL_KEYDOWN #include // Para SDLK_1, SDLK_2, SDLK_3, SDLK_4, SDLK_5 #include // Para SDL_Rect #include // Para SDL_GetTicks #include // Para size_t #include // Para char_traits, operator+, basic_string #include // 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 #include "input.h" // Para Input, InputType, INPUT_DO_NOT_ALLOW_R... #include "jail_audio.h" // Para JA_GetMusicState, JA_Music_state, JA_P... #include "lang.h" // Para getText #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, options, Name, name, AttractMode #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 // Constructor Title::Title() : text_(Resource::get()->getText("smb2")), fade_(std::make_unique()), tiled_bg_(std::make_unique(param.game.game_area.rect, TiledBGMode::RANDOM)), game_logo_(std::make_unique(param.game.game_area.center_x, param.title.title_c_c_position)), mini_logo_sprite_(std::make_unique(Resource::get()->getTexture("logo_jailgames_mini.png"))), define_buttons_(std::make_unique()), num_controllers_(Input::get()->getNumControllers()) { // Configura objetos game_logo_->enable(); 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); 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); JA_StopChannel(-1); } // Actualiza las variables del objeto void Title::update() { constexpr int TICKS_SPEED = 15; if (SDL_GetTicks() - ticks_ > TICKS_SPEED) { // Actualiza el contador de ticks_ ticks_ = SDL_GetTicks(); // Actualiza el objeto screen Screen::get()->update(); // Actualiza las variables de globalInputs globalInputs::update(); // Comprueba el fundido y si se ha acabado fade_->update(); JA_SetMusicVolume(100 - fade_->getValue()); if (fade_->hasEnded()) { if (post_fade_ == -1) { section::name = next_section_; } else { section::name = section::Name::GAME; section::options = post_fade_ == 1 ? section::Options::GAME_PLAY_1P : section::Options::GAME_PLAY_2P; JA_StopMusic(); } } // Sección 1 - Titulo animandose if (section::options == section::Options::TITLE_1) { game_logo_->update(); if (game_logo_->hasFinished()) { section::options = section::Options::TITLE_2; } } // Sección 2 - La pantalla con el titulo, el fondo animado y la música else if (section::options == section::Options::TITLE_2) { // El contador solo sube si no estamos definiendo botones counter_ = define_buttons_->isEnabled() ? 0 : counter_ + 1; // Reproduce la música if ((JA_GetMusicState() == JA_MUSIC_INVALID) || (JA_GetMusicState() == JA_MUSIC_STOPPED)) { JA_PlayMusic(Resource::get()->getMusic("title.ogg")); } // 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) { fade_->activate(); post_fade_ = -1; } } } } // 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(bg_color); // Dibuja el mosacico de fondo tiled_bg_->render(); // Dibuja el logo con el título del juego game_logo_->render(); if (section::options == section::Options::TITLE_2) { constexpr Color shadow = Color(0x14, 0x87, 0xc4); // '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(23), 1, no_color, 1, shadow); } // 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_color, 1, shadow); } // Define Buttons define_buttons_->render(); // Fade fade_->render(); // Vuelca el contenido del renderizador en pantalla Screen::get()->blit(); } // Comprueba los eventos void Title::checkEvents() { // Comprueba el input para el resto de objetos define_buttons_->checkEvents(); // Si define_buttons_ está habilitado, es él quien gestiona los eventos if (!define_buttons_->isEnabled()) { SDL_Event event; while (SDL_PollEvent(&event)) { // Evento de salida de la aplicación if (event.type == SDL_QUIT) { section::name = section::Name::QUIT; section::options = section::Options::QUIT_FROM_EVENT; break; } // Recarga las texturas else if (event.type == SDL_RENDER_DEVICE_RESET || event.type == SDL_RENDER_TARGETS_RESET) { reLoadTextures(); } else if (event.type == SDL_KEYDOWN && event.key.repeat == 0) { switch (event.key.keysym.sym) { case SDLK_1: // Redefine los botones del mando #0 { if (define_buttons_->enable(0)) resetCounter(); break; } case SDLK_2: // Redefine los botones del mando #1 { if (define_buttons_->enable(1)) resetCounter(); break; } case SDLK_3: // Intercambia los mandos entre los dos jugadores { swapControllers(); resetCounter(); break; } case SDLK_4: // Intercambia la asignación del teclado { swapKeyboard(); resetCounter(); break; } case SDLK_5: // Muestra la asignacion de mandos y teclado { showControllers(); resetCounter(); break; } default: break; } } } } } // Comprueba las entradas void Title::checkInput() { // Comprueba los controladores solo si no se estan definiendo los botones if (!define_buttons_->isEnabled()) { // Comprueba los métodos de control for (const auto &controller : options.controllers) { if (Input::get()->checkInput(InputType::START, INPUT_DO_NOT_ALLOW_REPEAT, controller.type, controller.index) && !Input::get()->checkInput(InputType::SERVICE, INPUT_DO_NOT_ALLOW_REPEAT, controller.type, controller.index)) { if (section::options == section::Options::TITLE_2 || ALLOW_TITLE_ANIMATION_SKIP) { fade_->activate(); post_fade_ = controller.player_id; return; } } // Comprueba si se va a intercambiar la asignación de mandos a jugadores if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, controller.type, controller.index) && Input::get()->checkInput(InputType::SWAP_CONTROLLERS, INPUT_DO_NOT_ALLOW_REPEAT, controller.type, controller.index)) { swapControllers(); return; } // Comprueba si algun mando quiere ser configurado if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, controller.type, controller.index) && Input::get()->checkInput(InputType::CONFIG, INPUT_DO_NOT_ALLOW_REPEAT, controller.type, controller.index)) { define_buttons_->enable(controller.index); return; } } } // Comprueba los inputs que se pueden introducir en cualquier sección del juego 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(); } } // Recarga las texturas void Title::reLoadTextures() { game_logo_->reLoad(); tiled_bg_->reLoad(); } // 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; swapOptionsControllers(); showControllers(); } // Intercambia el teclado de jugador void Title::swapKeyboard() { swapOptionsKeyboard(); std::string text = lang::getText(100) + std::to_string(getPlayerWhoUsesKeyboard()) + ": " + lang::getText(69); Notifier::get()->showText({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 text(NUM_CONTROLLERS); std::vector playerControllerIndex(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 playerControllerIndex.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 = playerControllerIndex.at(i); if (options.controllers.at(index).plugged) { text.at(i) = lang::getText(100) + std::to_string(i + 1) + ": " + options.controllers.at(index).name; } } // std::string spaces(lang::getText(100).length() + 3, ' '); // std::string kb_text = spaces + lang::getText(69); Notifier::get()->showText({text.at(0), text.at(1)}); }