#include "title.h" #include // for SDLK_1, SDLK_2, SDLK_3 #include // for SDL_Renderer #include // for SDL_GetTicks #include // for allocator, basic_string, char_traits #include // for vector #include "asset.h" // for Asset #include "define_buttons.h" // for DefineButtons #include "fade.h" // for Fade, FADE_RANDOM_SQUARE #include "game_logo.h" // for GameLogo #include "global_inputs.h" // for checkGlobalInputs #include "input.h" // for Input, inputs_e, INPUT_DO_NOT_ALLOW_RE... #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_e, options, name_e, name #include "sprite.h" // for Sprite #include "text.h" // for Text, TXT_CENTER, TXT_SHADOW #include "texture.h" // for Texture #include "tiled_bg.h" // for Tiledbg, TILED_MODE_RANDOM struct JA_Music_t; // Constructor Title::Title(JA_Music_t *music) { // Copia las direcciones de los punteros y objetos this->music = music; input = Input::get(); asset = Asset::get(); screen = Screen::get(); SDL_Renderer *renderer = screen->getRenderer(); // Reserva memoria y crea los objetos eventHandler = new SDL_Event(); fade = new Fade(renderer); text1 = new Text(asset->get("smb2.gif"), asset->get("smb2.txt"), renderer); text1->addPalette(asset->get("smb2_pal1.gif")); text1->setPalette(1); text2 = new Text(asset->get("8bithud.png"), asset->get("8bithud.txt"), renderer); miniLogoTexture = new Texture(renderer, asset->get("logo_jailgames_mini.png")); miniLogoSprite = new Sprite(param.game.gameArea.centerX - miniLogoTexture->getWidth() / 2, 0, miniLogoTexture->getWidth(), miniLogoTexture->getHeight(), miniLogoTexture); tiledbg = new Tiledbg(asset->get("title_bg_tile.png"), {0, 0, param.game.width, param.game.height}, TILED_MODE_RANDOM); gameLogo = new GameLogo(param.game.gameArea.centerX, param.title.titleCCPosition); gameLogo->enable(); defineButtons = new DefineButtons(text2); // Inicializa los valores init(); } // Destructor Title::~Title() { // Destruye los objetos y libera la memoria delete eventHandler; delete fade; delete text1; delete text2; delete miniLogoTexture; delete miniLogoSprite; delete tiledbg; delete gameLogo; delete defineButtons; } // Inicializa los valores de las variables void Title::init() { // Inicializa variables section::options = section::OPTIONS_TITLE_1; counter = 0; nextSection.name = section::NAME_GAME; postFade = 0; ticks = 0; ticksSpeed = 15; fade->setColor(fadeColor.r, fadeColor.g, fadeColor.b); fade->setType(FADE_RANDOM_SQUARE); fade->setPost(param.fade.postDuration); demo = true; numControllers = input->getNumControllers(); } // Actualiza las variables del objeto void Title::update() { // Calcula la lógica de los objetos if (SDL_GetTicks() - ticks > ticksSpeed) { // Actualiza el contador de ticks ticks = SDL_GetTicks(); // Actualiza el objeto screen screen->update(); // Actualiza el objeto 'defineButtons' defineButtons->update(); // Comprueba el fade y si se ha acabado fade->update(); if (fade->hasEnded()) { if (postFade == -1) { section::name = section::NAME_GAME_DEMO; } else { section::name = section::NAME_GAME; section::options = postFade == 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) { gameLogo->update(); if (gameLogo->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 if (!defineButtons->isEnabled()) { counter++; } else { counter = 0; } // 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 gameLogo->update(); // Actualiza el mosaico de fondo tiledbg->update(); if (counter == param.title.titleDuration) { fade->activate(); postFade = -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(bgColor); // Dibuja el mosacico de fondo tiledbg->render(); // Dibuja el logo con el título del juego gameLogo->render(); if (section::options == section::OPTIONS_TITLE_2) { const color_t shadow = {0x14, 0x87, 0xc4}; // 'PULSA 1P o 2P PARA JUGAR' if (counter % 50 > 14 && !defineButtons->isEnabled()) { text1->writeDX(TXT_CENTER | TXT_SHADOW, param.game.gameArea.centerX, param.title.pressStartPosition, lang::getText(23), 1, noColor, 1, shadow); } // Mini logo const int pos1 = (param.game.height / 5 * 4) + BLOCK; const int pos2 = pos1 + miniLogoSprite->getHeight() + 3; miniLogoSprite->setPosY(pos1); miniLogoSprite->render(); // Texto con el copyright text1->writeDX(TXT_CENTER | TXT_SHADOW, param.game.gameArea.centerX, pos2, TEXT_COPYRIGHT, 1, noColor, 1, shadow); } // Define Buttons defineButtons->render(); // Fade fade->render(); // Vuelca el contenido del renderizador en pantalla screen->blit(); } // Comprueba los eventos void Title::checkEvents() { // Si defineButtons está habilitado, es él quien gestiona los eventos if (!defineButtons->isEnabled()) { // Comprueba los eventos que hay en la cola while (SDL_PollEvent(eventHandler) != 0) { // Evento de salida de la aplicación if (eventHandler->type == SDL_QUIT) { section::name = section::NAME_QUIT; break; } // Recarga las texturas else if (eventHandler->type == SDL_RENDER_DEVICE_RESET || eventHandler->type == SDL_RENDER_TARGETS_RESET) { reLoadTextures(); } else if (eventHandler->type == SDL_KEYDOWN && eventHandler->key.repeat == 0) { switch (eventHandler->key.keysym.sym) { case SDLK_1: if (defineButtons->enable(0)) resetCounter(); break; case SDLK_2: if (defineButtons->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 (!defineButtons->isEnabled()) { ////////////////////////////////////////////////// // TECLADO // ////////////////////////////////////////////////// // Comprueba el teclado para empezar a jugar if (input->checkInput(input_start, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD)) { if (section::options == section::OPTIONS_TITLE_2 || ALLOW_TITLE_ANIMATION_SKIP) { fade->activate(); postFade = options.controller[0].playerId; } } ////////////////////////////////////////////////// // 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(input_service, input_swap_controllers, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) { swapControllers(); return; } // Comprueba si algun mando quiere ser configurado if (input->checkModInput(input_service, input_config, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) { defineButtons->enable(i); return; } // Comprueba el botón de START de los mandos if (input->checkInput(input_start, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) { // Si no está el botón de servicio activo if (!input->checkInput(input_service, INPUT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) { if (section::options == section::OPTIONS_TITLE_2 || ALLOW_TITLE_ANIMATION_SKIP) { fade->activate(); postFade = options.controller[i].playerId; return; } } } } } // Comprueba el input para el resto de objetos screen->checkInput(); defineButtons->checkInput(); // Comprueba los inputs que se pueden introducir en cualquier sección del juego checkGlobalInputs(); } // 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() { gameLogo->reLoad(); tiledbg->reLoad(); } // Reinicia el contador interno void Title::resetCounter() { counter = 0; } // Intercambia la asignación de mandos a los jugadores void Title::swapControllers() { const int numControllers = input->getNumControllers(); if (numControllers == 0) { return; } defineButtons->swapControllers(); // Crea cadenas de texto vacias para un numero máximo de mandos const int MAX_CONTROLLERS = 2; std::string text[MAX_CONTROLLERS]; int playerControllerIndex[MAX_CONTROLLERS]; for (int i = 0; i < MAX_CONTROLLERS; ++i) { text[i] = ""; playerControllerIndex[i] = -1; } // Obtiene para cada jugador el índice del mando correspondiente for (int i = 0; i < MAX_CONTROLLERS; ++i) { playerControllerIndex[options.controller[i].playerId - 1] = i; } // Genera el texto correspondiente for (int i = 0; i < MAX_CONTROLLERS; ++i) { const int index = playerControllerIndex[i]; if (options.controller[index].name != "NO NAME") { text[i] = "Jugador " + std::to_string(i + 1) + ": " + options.controller[index].name; } } screen->showNotification(text[0], text[1]); resetCounter(); }