#include "title.h" #include // for SDL_PollEvent, SDL_Event, SDL_KEYDOWN #include // for SDLK_1, SDLK_2, SDLK_3 #include // for SDL_Rect #include // for SDL_Renderer #include // for SDL_GetTicks #include // for char_traits, operator+, to_string, bas... #include // for move #include // for vector #include "asset.h" // for Asset #include "global_inputs.h" // for check #include "input.h" // for Input, InputType, INPUT_DO_NOT_ALLOW_R... #include "jail_audio.h" // for JA_GetMusicState, JA_Music_state, JA_P... #include "lang.h" // for getText #include "notifier.h" // for Notifier #include "options.h" // for options #include "param.h" // for param #include "resource.h" // for Resource #include "screen.h" // for Screen #include "section.h" // for Options, options, Name, name #include "texture.h" // for Texture #include "utils.h" // for Param, OptionsController, Color, Param... struct JA_Music_t; // lines 17-17 // Constructor Title::Title() { // Reserva memoria y crea los objetos fade_ = std::make_unique(); text1_ = std::make_unique(Resource::get()->getTexture("smb2.gif"), Resource::get()->getTextFile("smb2.txt")); text1_->addPalette(Asset::get()->get("smb2_pal1.gif")); text1_->setPalette(1); text2_ = std::make_unique(Resource::get()->getTexture("8bithud.png"), Resource::get()->getTextFile("8bithud.txt")); mini_logo_texture_ = Resource::get()->getTexture("logo_jailgames_mini.png"); mini_logo_sprite_ = std::make_unique(mini_logo_texture_, param.game.game_area.center_x - mini_logo_texture_->getWidth() / 2, 0, mini_logo_texture_->getWidth(), mini_logo_texture_->getHeight()); tiled_bg_ = std::make_unique((SDL_Rect){0, 0, param.game.width, param.game.height}, TiledBGMode::RANDOM); game_logo_ = std::make_unique(param.game.game_area.center_x, param.title.title_c_c_position); game_logo_->enable(); define_buttons_ = std::make_unique(std::move(text2_)); // Inicializa los valores init(); } // Inicializa los valores de las variables void Title::init() { // Inicializa variables section::options = section::Options::TITLE_1; counter_ = 0; next_section_ = section::Name::GAME; post_fade_ = 0; ticks_ = 0; ticks_speed_ = 15; fade_->setColor(fade_color.r, fade_color.g, fade_color.b); fade_->setType(FadeType::RANDOM_SQUARE); fade_->setPost(param.fade.post_duration); demo_ = true; num_controllers_ = Input::get()->getNumControllers(); } // Actualiza las variables del objeto void Title::update() { // Calcula la lógica de los objetos if (SDL_GetTicks() - ticks_ > ticks_speed_) { // Actualiza el contador de ticks_ ticks_ = SDL_GetTicks(); // Actualiza el objeto screen Screen::get()->update(); // Comprueba el fade_ y si se ha acabado fade_->update(); if (fade_->hasEnded()) { if (post_fade_ == -1) { section::name = section::Name::GAME_DEMO; } 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 = {0x14, 0x87, 0xc4}; // 'PRESS TO PLAY' if (counter_ % 50 > 14 && !define_buttons_->isEnabled()) { text1_->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 text1_->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() { // Si define_buttons_ está habilitado, es él quien gestiona los eventos if (!define_buttons_->isEnabled()) { SDL_Event event; // Comprueba los eventos que hay en la cola 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: { if (define_buttons_->enable(0)) resetCounter(); break; } case SDLK_2: { if (define_buttons_->enable(1)) resetCounter(); break; } case SDLK_3: { swapControllers(); 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 el teclado para empezar a jugar if (Input::get()->checkInput(InputType::START, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD)) { if (section::options == section::Options::TITLE_2 || ALLOW_TITLE_ANIMATION_SKIP) { fade_->activate(); post_fade_ = options.controller[0].player_id; } } // Comprueba los mandos for (int i = 0; i < Input::get()->getNumControllers(); ++i) { // Comprueba si se va a intercambiar la asignación de mandos a jugadores if (Input::get()->checkModInput(InputType::SERVICE, InputType::SWAP_CONTROLLERS, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) { swapControllers(); return; } // Comprueba si algun mando quiere ser configurado if (Input::get()->checkModInput(InputType::SERVICE, InputType::CONFIG, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) { define_buttons_->enable(i); return; } // Comprueba el botón de START de los mandos if (Input::get()->checkInput(InputType::START, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) { // Si no está el botón de servicio activo if (!Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) { if (section::options == section::Options::TITLE_2 || ALLOW_TITLE_ANIMATION_SKIP) { fade_->activate(); post_fade_ = options.controller[i].player_id; return; } } } } } // Comprueba el input para el resto de objetos Screen::get()->checkInput(); define_buttons_->checkInput(); // 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() { const auto num_controllers = Input::get()->getNumControllers(); if (num_controllers == 0) { return; } define_buttons_->swapControllers(); // Crea cadenas de texto vacias para un numero máximo de mandos constexpr int MAX_CONTROLLERS = 2; std::string text[MAX_CONTROLLERS]; int playerControllerIndex[MAX_CONTROLLERS]; for (int i = 0; i < MAX_CONTROLLERS; ++i) { text[i].clear(); playerControllerIndex[i] = -1; } // Obtiene para cada jugador el índice del mando correspondiente for (int i = 0; i < MAX_CONTROLLERS; ++i) { playerControllerIndex[options.controller[i].player_id - 1] = i; } // Genera el texto correspondiente for (int i = 0; i < MAX_CONTROLLERS; ++i) { const int index = playerControllerIndex[i]; if (options.controller[index].plugged) { text[i] = lang::getText(101) + std::to_string(i + 1) + ": " + options.controller[index].name; } } Notifier::get()->showText(text[0], text[1]); resetCounter(); }