#include "title.h" // Constructor Title::Title(SDL_Renderer *renderer, Screen *screen, Input *input, Asset *asset, options_t *options, Lang *lang, section_t *section) { // 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->section = section; // Reserva memoria y crea los objetos eventHandler = new SDL_Event(); fade = new Fade(renderer); dustTexture = new Texture(renderer, asset->get("title_dust.png")); coffeeTexture = new Texture(renderer, asset->get("title_coffee.png")); crisisTexture = new Texture(renderer, asset->get("title_crisis.png")); coffeeBitmap = new SmartSprite(coffeeTexture, renderer); crisisBitmap = new SmartSprite(crisisTexture, renderer); dustBitmapL = new AnimatedSprite(dustTexture, renderer, asset->get("title_dust.ani")); dustBitmapR = new AnimatedSprite(dustTexture, renderer, asset->get("title_dust.ani")); text1 = new Text(asset->get("smb2.png"), asset->get("smb2.txt"), renderer); text2 = new Text(asset->get("8bithud.png"), asset->get("8bithud.txt"), renderer); backgroundObj = new Background(renderer, screen, asset); backgroundObj->setSrcDest(windowArea); backgroundObj->setDstDest(windowArea); backgroundObj->setCloudsSpeed(-0.5f); backgroundObj->setGradientNumber(1); backgroundObj->setTransition(0.8f); // Sonidos crashSound = JA_LoadSound(asset->get("title.wav").c_str()); // Musicas titleMusic = JA_LoadMusic(asset->get("title.ogg").c_str()); // Inicializa los valores init(); } // Destructor Title::~Title() { // Destruye los objetos y libera la memoria delete eventHandler; delete fade; dustTexture->unload(); delete dustTexture; coffeeTexture->unload(); delete coffeeTexture; crisisTexture->unload(); delete crisisTexture; delete coffeeBitmap; delete crisisBitmap; delete dustBitmapL; delete dustBitmapR; delete text1; delete text2; delete backgroundObj; JA_DeleteSound(crashSound); JA_DeleteMusic(titleMusic); SDL_DestroyTexture(background); } // Inicializa los valores de las variables void Title::init() { // Inicializa variables section->subsection = SUBSECTION_TITLE_1; counter = TITLE_COUNTER; backgroundCounter = 0; backgroundMode = rand() % 2; menuVisible = false; nextSection.name = SECTION_PROG_GAME; postFade = 0; ticks = 0; ticksSpeed = 15; fade->init(0x17, 0x17, 0x26); demo = true; // Pone valores por defecto a las opciones de control options->input.clear(); input_t i; i.id = 0; i.name = "KEYBOARD"; i.deviceType = INPUT_USE_KEYBOARD; options->input.push_back(i); i.id = 0; i.name = "GAME CONTROLLER"; i.deviceType = INPUT_USE_GAMECONTROLLER; options->input.push_back(i); // Comprueba si hay mandos conectados checkInputDevices(); // Pone valores por defecto. El primer jugador el teclado. El segundo jugador el primer mando deviceIndex.clear(); deviceIndex.push_back(availableInputDevices.size() - 1); // El último dispositivo encontrado es el teclado deviceIndex.push_back(0); // El primer mando encontrado. Si no ha encontrado ninguno es el teclado // Si ha encontrado un mando se lo asigna al segundo jugador if (input->gameControllerFound()) { options->input[1].id = availableInputDevices[deviceIndex[1]].id; options->input[1].name = availableInputDevices[deviceIndex[1]].name; options->input[1].deviceType = availableInputDevices[deviceIndex[1]].deviceType; } // Inicializa el bitmap de 'Coffee' coffeeBitmap->init(); coffeeBitmap->setPosX(45); coffeeBitmap->setPosY(11 - 200); coffeeBitmap->setWidth(167); coffeeBitmap->setHeight(46); coffeeBitmap->setVelX(0.0f); coffeeBitmap->setVelY(2.5f); coffeeBitmap->setAccelX(0.0f); coffeeBitmap->setAccelY(0.1f); coffeeBitmap->setSpriteClip(0, 0, 167, 46); coffeeBitmap->setEnabled(true); coffeeBitmap->setEnabledCounter(0); coffeeBitmap->setDestX(45); coffeeBitmap->setDestY(11); // Inicializa el bitmap de 'Crisis' crisisBitmap->init(); crisisBitmap->setPosX(60); crisisBitmap->setPosY(57 + 200); crisisBitmap->setWidth(137); crisisBitmap->setHeight(46); crisisBitmap->setVelX(0.0f); crisisBitmap->setVelY(-2.5f); crisisBitmap->setAccelX(0.0f); crisisBitmap->setAccelY(-0.1f); crisisBitmap->setSpriteClip(0, 0, 137, 46); crisisBitmap->setEnabled(true); crisisBitmap->setEnabledCounter(0); crisisBitmap->setDestX(60); crisisBitmap->setDestY(57); // Inicializa el bitmap de 'DustRight' dustBitmapR->resetAnimation(); dustBitmapR->setPosX(218); dustBitmapR->setPosY(47); dustBitmapR->setWidth(16); dustBitmapR->setHeight(16); dustBitmapR->setFlip(SDL_FLIP_HORIZONTAL); // Inicializa el bitmap de 'DustLeft' dustBitmapL->resetAnimation(); dustBitmapL->setPosX(33); dustBitmapL->setPosY(47); dustBitmapL->setWidth(16); dustBitmapL->setHeight(16); // Crea el mosaico de fondo del titulo createTiledBackground(); // Coloca la ventana que recorre el mosaico de fondo de manera que coincida // con el mosaico que hay pintado en el titulo al iniciar backgroundWindow.x = 128; backgroundWindow.y = 96; backgroundWindow.w = GAMECANVAS_WIDTH; backgroundWindow.h = GAMECANVAS_HEIGHT; // Inicializa los valores del vector con los valores del seno for (int i = 0; i < 360; ++i) { sin[i] = SDL_sinf((float)i * 3.14f / 180.0f); } } // 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' backgroundObj->update(); // Se realizan diferentes cosas en funcion de la subsección actual switch (section->subsection) { // Sección 1 - Titulo desplazandose case SUBSECTION_TITLE_1: { // Actualiza los objetos coffeeBitmap->update(); crisisBitmap->update(); // Si los objetos han llegado a su destino, cambiamos de Sección if (coffeeBitmap->hasFinished() && crisisBitmap->hasFinished()) { section->subsection = SUBSECTION_TITLE_2; // Pantallazo blanco screen->setFlash({0xFF, 0xFF, 0xFF}, 5); // Reproduce el efecto sonoro JA_PlaySound(crashSound); } } break; // Sección 2 - Titulo vibrando case SUBSECTION_TITLE_2: { // Agita el logo static const int v[] = {-1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, 0}; static const int a = coffeeBitmap->getPosX(); static const int b = crisisBitmap->getPosX(); static int step = 0; coffeeBitmap->setPosX(a + v[step / 3]); crisisBitmap->setPosX(b + v[step / 3]); dustBitmapR->update(); dustBitmapL->update(); step++; if (step == 33) { section->subsection = SUBSECTION_TITLE_3; step = 0; } } break; // Sección 3 - La pantalla de titulo con el menú y la música case SUBSECTION_TITLE_3: { if (counter > 0) { counter--; // Reproduce la música if ((JA_GetMusicState() == JA_MUSIC_INVALID) || (JA_GetMusicState() == JA_MUSIC_STOPPED)) { JA_PlayMusic(titleMusic); } // Actualiza los objetos dustBitmapR->update(); dustBitmapL->update(); // Actualiza la lógica del titulo 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 PLAYERS section->name = SECTION_PROG_GAME; section->subsection = SUBSECTION_GAME_PLAY_2P; JA_StopMusic(); break; case 2: // QUIT section->name = SECTION_PROG_QUIT; JA_StopMusic(); break; case 3: // TIME OUT counter = TITLE_COUNTER; if (demo) { runDemoGame(); if (section->name != SECTION_PROG_QUIT) { runInstructions(m_auto); } if (section->name != SECTION_PROG_QUIT) { runHiScoreTable(mhst_auto); } } else section->name = SECTION_PROG_LOGO; break; default: break; } } // Actualiza el tileado de fondo updateBG(); } else if (counter == 0) { if (demo) { if (section->name != SECTION_PROG_QUIT) { runHiScoreTable(mhst_auto); } runDemoGame(); if (section->name != SECTION_PROG_QUIT) { runInstructions(m_auto); } init(); demo = false; counter = TITLE_COUNTER; } else { section->name = SECTION_PROG_LOGO; } } // Sección Instrucciones if (section->subsection == SUBSECTION_TITLE_INSTRUCTIONS) { runInstructions(m_auto); counter = TITLE_COUNTER; demo = true; } } break; default: break; } } } // 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 tileado de fondo SDL_RenderCopy(renderer, background, &backgroundWindow, nullptr); // backgroundObj->render(); // Dibuja el logo coffeeBitmap->render(); crisisBitmap->render(); // Dibuja el polvillo del logo dustBitmapR->render(); dustBitmapL->render(); if (section->subsection == SUBSECTION_TITLE_3) { // PRESS ANY KEY! if (counter % 50 > 14) { text1->writeDX(TXT_CENTER | TXT_SHADOW, GAMECANVAS_CENTER_X, GAMECANVAS_THIRD_QUARTER_Y + BLOCK, lang->getText(23), 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() { if (input->checkInput(input_exit, REPEAT_FALSE)) { section->name = SECTION_PROG_QUIT; } 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_accept, REPEAT_FALSE)) { fade->activateFade(); postFade = 0; } } // Actualiza el tileado de fondo void Title::updateBG() { if (backgroundMode == 0) { // El tileado de fondo se desplaza en diagonal ++backgroundWindow.x %= 64; ++backgroundWindow.y %= 64; } else { // El tileado de fondo se desplaza en circulo ++backgroundCounter %= 360; backgroundWindow.x = 128 + (int(sin[(backgroundCounter + 270) % 360] * 128)); backgroundWindow.y = 96 + (int(sin[(360 - backgroundCounter) % 360] * 96)); } } // Cambia el valor de la variable de modo de pantalla completa void Title::switchFullScreenModeVar() { switch (options->video.mode) { case 0: options->video.mode = SDL_WINDOW_FULLSCREEN; break; case SDL_WINDOW_FULLSCREEN: options->video.mode = SDL_WINDOW_FULLSCREEN_DESKTOP; break; case SDL_WINDOW_FULLSCREEN_DESKTOP: options->video.mode = 0; break; default: options->video.mode = 0; break; } } // Bucle para el titulo del juego void Title::run() { while (section->name == SECTION_PROG_TITLE) { checkInput(); update(); checkEvents(); render(); } } // Ejecuta la parte donde se muestran las instrucciones void Title::runInstructions(mode_e mode) { instructions = new Instructions(renderer, screen, asset, input, lang, section); instructions->run(mode); delete instructions; } // Ejecuta la parte donde se muestra la tabla de puntuaciones void Title::runHiScoreTable(mode_hiScoreTable_e mode) { hiScoreTable = new HiScoreTable(renderer, screen, asset, input, lang, options, section); hiScoreTable->run(mode); delete hiScoreTable; } // Ejecuta el juego en modo demo void Title::runDemoGame() { demoGame = new Game(1, 0, renderer, screen, asset, lang, input, true, options, section); demoGame->run(); delete demoGame; } // Modifica las opciones para los controles de los jugadores bool Title::updatePlayerInputs(int numPlayer) { const int numDevices = availableInputDevices.size(); if (!input->gameControllerFound()) { // Si no hay mandos se deja todo de manera prefijada deviceIndex[0] = 0; deviceIndex[1] = 0; options->input[0].id = -1; options->input[0].name = "KEYBOARD"; options->input[0].deviceType = INPUT_USE_KEYBOARD; options->input[1].id = 0; options->input[1].name = "GAME CONTROLLER"; options->input[1].deviceType = INPUT_USE_GAMECONTROLLER; return true; } else { // Si hay mas de un dispositivo, se recorre el vector if (options->console) { std::cout << "numplayer:" << numPlayer << std::endl; std::cout << "deviceindex:" << deviceIndex[numPlayer] << std::endl; } // Incrementa el indice if (deviceIndex[numPlayer] < numDevices - 1) { deviceIndex[numPlayer]++; } else { deviceIndex[numPlayer] = 0; } if (options->console) { std::cout << "deviceindex:" << deviceIndex[numPlayer] << std::endl; } // Si coincide con el del otro jugador, se lo intercambian if (deviceIndex[0] == deviceIndex[1]) { const int theOtherPlayer = (numPlayer + 1) % 2; deviceIndex[theOtherPlayer]--; if (deviceIndex[theOtherPlayer] < 0) { deviceIndex[theOtherPlayer] = numDevices - 1; } } // Copia el dispositivo marcado por el indice a la variable de opciones de cada jugador options->input[0] = availableInputDevices[deviceIndex[0]]; options->input[1] = availableInputDevices[deviceIndex[1]]; return true; } } // Crea el mosaico de fondo del titulo void Title::createTiledBackground() { // Crea la textura para el mosaico de fondo background = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH * 2, GAMECANVAS_HEIGHT * 2); if (background == nullptr) { if (options->console) { std::cout << "TitleSurface could not be created!\nSDL Error: " << SDL_GetError() << std::endl; } } // Crea los objetos para pintar en la textura de fondo Texture *bgTileTexture = new Texture(renderer, asset->get("title_bg_tile.png")); Sprite *tile = new Sprite({0, 0, 64, 64}, bgTileTexture, renderer); // Prepara para dibujar sobre la textura SDL_SetRenderTarget(renderer, background); SDL_SetRenderDrawColor(renderer, 0x43, 0x43, 0x4F, 0xFF); SDL_RenderClear(renderer); // Rellena la textura con el tile tile->setSpriteClip(0, 0, 64, 64); for (int i = 0; i < 8; ++i) { for (int j = 0; j < 6; ++j) { tile->setPosX(i * 64); tile->setPosY(j * 64); tile->render(); } } // Vuelve a colocar el renderizador apuntando a la pantalla SDL_SetRenderTarget(renderer, nullptr); // Libera la memoria utilizada por los objetos bgTileTexture->unload(); delete bgTileTexture; delete tile; } // Comprueba cuantos mandos hay conectados para gestionar el menu de opciones void Title::checkInputDevices() { if (options->console) { std::cout << "Filling devices for options menu..." << std::endl; } input->discoverGameController(); const int numControllers = input->getNumControllers(); availableInputDevices.clear(); input_t temp; // Añade todos los mandos if (numControllers > 0) for (int i = 0; i < numControllers; ++i) { temp.id = i; temp.name = input->getControllerName(i); temp.deviceType = INPUT_USE_GAMECONTROLLER; availableInputDevices.push_back(temp); if (options->console) { std::cout << "Device " << (int)availableInputDevices.size() << " - " << temp.name.c_str() << std::endl; } } // Añade el teclado al final temp.id = -1; temp.name = "KEYBOARD"; temp.deviceType = INPUT_USE_KEYBOARD; availableInputDevices.push_back(temp); if (options->console) { std::cout << "Device " << (int)availableInputDevices.size() << " - " << temp.name.c_str() << std::endl; std::cout << std::endl; } } // Recarga las texturas void Title::reLoadTextures() { dustTexture->reLoad(); coffeeTexture->reLoad(); crisisTexture->reLoad(); createTiledBackground(); }