#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 "options.h" // for options #include "param.h" // for param #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(JA_Music_t *music) : music_(music) { // Copia las direcciones de los punteros y objetos input_ = Input::get(); screen_ = Screen::get(); SDL_Renderer *renderer = screen_->getRenderer(); // Reserva memoria y crea los objetos fade_ = std::make_unique(renderer); text1_ = std::make_unique(Asset::get()->get("smb2.gif"), Asset::get()->get("smb2.txt"), renderer); text1_->addPalette(Asset::get()->get("smb2_pal1.gif")); text1_->setPalette(1); text2_ = std::make_unique(Asset::get()->get("8bithud.png"), Asset::get()->get("8bithud.txt"), renderer); mini_logo_texture_ = std::make_shared(renderer, Asset::get()->get("logo_jailgames_mini.png")); mini_logo_sprite_ = std::make_unique(param.game.game_area.center_x - mini_logo_texture_->getWidth() / 2, 0, mini_logo_texture_->getWidth(), mini_logo_texture_->getHeight(), mini_logo_texture_); tiled_bg_ = std::make_unique(Asset::get()->get("title_bg_tile.png"), (SDL_Rect){0, 0, param.game.width, param.game.height}, TILED_MODE_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_->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_->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(music_); } // 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_->start(); // Limpia la pantalla screen_->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_->setPosY(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_->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; 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_->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_->getNumControllers(); ++i) { // Comprueba si se va a intercambiar la asignación de mandos a jugadores if (input_->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_->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_->checkInput(InputType::START, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) { // Si no está el botón de servicio activo if (!input_->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_->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_->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; } } screen_->showNotification(text[0], text[1]); resetCounter(); }