#include "title.h" // Constructor Title::Title(Screen *screen, Asset *asset, Input *input, Lang *lang, options_t *options, param_t *param, section_t *section, JA_Music_t *music) { // Copia las direcciones de los punteros y objetos this->screen = screen; this->input = input; this->asset = asset; this->options = options; this->lang = lang; this->param = param; this->section = section; this->music = music; SDL_Renderer *renderer = screen->getRenderer(); // Reserva memoria y crea los objetos eventHandler = new SDL_Event(); fade = new Fade(renderer, param); 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(renderer, asset, {0, 0, param->game.width, param->game.height}, TILED_MODE_RANDOM); gameLogo = new GameLogo(renderer, screen, asset, param, param->game.gameArea.centerX, param->title.titleCCPosition); gameLogo->enable(); defineButtons = new DefineButtons(input, text2, param, options, section); // 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_PROG_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_PROG_GAME_DEMO; } else { section->name = SECTION_PROG_GAME; section->options = postFade; 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_PROG_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 si se sale con el teclado if (input->checkInput(input_exit, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD)) { section->name = SECTION_PROG_QUIT; section->options = SECTION_OPTIONS_QUIT_NORMAL; return; } // 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 resetear el juego if (input->checkModInput(input_service, input_reset, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) { section->name = SECTION_PROG_LOGO; screen->showNotification("Reset"); return; } // Comprueba si se va a activar o desactivar el audio if (input->checkModInput(input_service, input_mute, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) { options->audio.sound.enabled = options->audio.music.enabled = !options->audio.music.enabled; JA_EnableMusic(options->audio.music.enabled); JA_EnableSound(options->audio.sound.enabled); screen->showNotification("Audio " + boolToOnOff(options->audio.music.enabled)); return; } // 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)) { 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(); } // Bucle para el titulo del juego void Title::run() { while (section->name == SECTION_PROG_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(); }