#include "title.h" // Constructor Title::Title(SDL_Renderer *renderer, Screen *screen, Input *input, Asset *asset, options_t *options, Lang *lang, param_t *param, section_t *section, JA_Music_t *music) { // Copia las direcciones de los punteros y objetos this->renderer = renderer; this->screen = screen; this->input = input; this->asset = asset; this->options = options; this->lang = lang; this->param = param; this->section = section; this->music = music; // Reserva memoria y crea los objetos eventHandler = new SDL_Event(); fade = new Fade(renderer, param); text1 = new Text(asset->get("smb2.png"), asset->get("smb2.txt"), renderer); 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(GAMECANVAS_CENTER_X - miniLogoTexture->getWidth() / 2, 0, miniLogoTexture->getWidth(), miniLogoTexture->getHeight(), miniLogoTexture, renderer); background = new Background(renderer, screen, asset, param); background->setSrcDest(windowArea); background->setDstDest(windowArea); background->setCloudsSpeed(-0.5f); background->setGradientNumber(1); background->setTransition(0.8f); tiledbg = new Tiledbg(renderer, screen, asset, {0, 0, param->gameWidth, param->gameHeight}, TILED_MODE_RANDOM); gameLogo = new GameLogo(renderer, screen, asset, param, GAMECANVAS_CENTER_X, GAMECANVAS_FIRST_QUARTER_Y + 20); gameLogo->enable(); defineButtons = new DefineButtons(renderer, 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; miniLogoTexture->unload(); delete miniLogoTexture; delete miniLogoSprite; delete background; delete tiledbg; delete gameLogo; delete defineButtons; } // Inicializa los valores de las variables void Title::init() { // Inicializa variables section->subsection = SUBSECTION_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->fadePostDuration); 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 'background' background->update(); // Actualiza el objeto 'defineButtons' defineButtons->update(); // Comprueba el fade y si se ha acabado fade->update(); if (fade->hasEnded()) { switch (postFade) { case 1: // 1 PLAYER section->name = SECTION_PROG_GAME; section->subsection = SUBSECTION_GAME_PLAY_1P; JA_StopMusic(); break; case 2: // 2 PLAYER section->name = SECTION_PROG_GAME; section->subsection = SUBSECTION_GAME_PLAY_2P; JA_StopMusic(); break; case 3: // TIME OUT section->name = SECTION_PROG_GAME_DEMO; break; default: break; } } // Sección 1 - Titulo animandose if (section->subsection == SUBSECTION_TITLE_1) { gameLogo->update(); if (gameLogo->hasFinished()) { section->subsection = SUBSECTION_TITLE_2; } } // Sección 2 - La pantalla con el titulo, el fondo animado y la música else if (section->subsection == SUBSECTION_TITLE_2) { // El contador solo sube si no estamos definiendo botones if (!defineButtons->isEnabled()) { counter++; } // 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->titleCounter) { fade->activate(); postFade = 3; } } } } // 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(); // background->render(); // Dibuja el logo con el título del juego gameLogo->render(); if (section->subsection == SUBSECTION_TITLE_2) { // 'PULSA 1P o 2P PARA JUGAR' if (counter % 50 > 14 && !defineButtons->isEnabled()) { text1->writeDX(TXT_CENTER | TXT_SHADOW, GAMECANVAS_CENTER_X, param->pressStart, lang->getText(23), 1, noColor, 1, shdwTxtColor); } // Mini logo const int pos1 = (param->gameHeight / 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, GAMECANVAS_CENTER_X, pos2, TEXT_COPYRIGHT, 1, noColor, 1, shdwTxtColor); } 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(); } // Comprueba en el primer mando el botón de salir del programa else if (eventHandler->type == SDL_CONTROLLERBUTTONDOWN) { if ((SDL_GameControllerButton)eventHandler->cbutton.which == 0) if ((SDL_GameControllerButton)eventHandler->cbutton.button == input->getControllerBinding(0, input_exit)) { section->name = SECTION_PROG_QUIT; break; } } } } } // Comprueba las entradas void Title::checkInput() { // Comprueba los controladores solo si no se estan definiendo los botones if (!defineButtons->isEnabled()) { // Comprueba si se ha pulsado algún botón para empezar a jugar const int index = input->checkAnyButtonPressed(); if (index) { fade->activate(); postFade = index; } // Comprueba el teclado para salir if (input->checkInput(input_exit, DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD)) { section->name = SECTION_PROG_QUIT; } // Comprueba si se ha pulsado la tecla 1 o 2 para definir los controladores const Uint8 *keyStates = SDL_GetKeyboardState(nullptr); if (keyStates[SDL_SCANCODE_1] != 0) { defineButtons->enable(0); } else if (keyStates[SDL_SCANCODE_2] != 0) { defineButtons->enable(1); } } // 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(); }