#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}); gameLogo = new GameLogo(renderer, screen, asset, param, GAMECANVAS_CENTER_X, GAMECANVAS_FIRST_QUARTER_Y + 20); gameLogo->enable(); // 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; } // 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(); // Comprueba el fade y si se ha acabado fade->update(); if (fade->hasEnded()) { switch (postFade) { case 0: // 1 PLAYER section->name = SECTION_PROG_GAME; section->subsection = SUBSECTION_GAME_PLAY_1P; JA_StopMusic(); break; case 1: // 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) { if (counter < param->titleCounter) { 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(); } else if (counter >= param->titleCounter) { section->name = SECTION_PROG_GAME_DEMO; } } } } // 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(); // Dinuja 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) { 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); } // Fade fade->render(); // Vuelca el contenido del renderizador en pantalla screen->blit(); } // Comprueba los eventos void Title::checkEvents() { // 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; } else if (eventHandler->type == SDL_RENDER_DEVICE_RESET || eventHandler->type == SDL_RENDER_TARGETS_RESET) { reLoadTextures(); } } } // Comprueba las entradas void Title::checkInput() { // Comprueba todos los controladores for (int i = 0; i < numControllers; ++i) { if (input->checkInput(input_exit, REPEAT_FALSE, options->game.input[i].deviceType, options->game.input[i].id)) { section->name = SECTION_PROG_QUIT; } else if (input->checkInput(input_accept, REPEAT_FALSE, options->game.input[i].deviceType, options->game.input[i].id)) { fade->activate(); postFade = i; } } // Comprueba el teclado if (input->checkInput(input_exit, REPEAT_FALSE)) { section->name = SECTION_PROG_QUIT; } else if (input->checkInput(input_accept, REPEAT_FALSE)) { fade->activate(); postFade = 0; } else if (input->checkInput(input_window_fullscreen, REPEAT_FALSE)) { screen->switchVideoMode(); } else if (input->checkInput(input_window_dec_size, REPEAT_FALSE)) { screen->decWindowSize(); } else if (input->checkInput(input_window_inc_size, REPEAT_FALSE)) { screen->incWindowSize(); } else if (input->checkInput(input_video_shaders, REPEAT_FALSE)) { screen->switchShaders(); } } // Bucle para el titulo del juego void Title::run() { while (section->name == SECTION_PROG_TITLE) { checkInput(); update(); checkEvents(); render(); } } // Recarga las texturas void Title::reLoadTextures() { gameLogo->reLoad(); tiledbg->reLoad(); }