Files
coffee_crisis_arcade_edition/source/title.cpp

485 lines
12 KiB
C++

#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)
{
// 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;
// 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);
backgroundObj = new Background(renderer, screen, asset, param);
backgroundObj->setSrcDest(windowArea);
backgroundObj->setDstDest(windowArea);
backgroundObj->setCloudsSpeed(-0.5f);
backgroundObj->setGradientNumber(1);
backgroundObj->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();
// 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;
delete text1;
delete text2;
miniLogoTexture->unload();
delete miniLogoTexture;
delete miniLogoSprite;
delete backgroundObj;
delete tiledbg;
delete gameLogo;
JA_DeleteMusic(titleMusic);
}
// Inicializa los valores de las variables
void Title::init()
{
// Inicializa variables
section->subsection = SUBSECTION_TITLE_1;
counter = TITLE_COUNTER;
nextSection.name = SECTION_PROG_GAME;
postFade = 0;
ticks = 0;
ticksSpeed = 15;
fade->setColor(fadeColor.r, fadeColor.g, fadeColor.b);
fade->setType(FADE_RANDOM_SQUARE);
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;
}
}
// 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();
// 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 > 0)
{
counter--;
// Reproduce la música
if ((JA_GetMusicState() == JA_MUSIC_INVALID) || (JA_GetMusicState() == JA_MUSIC_STOPPED))
{
JA_PlayMusic(titleMusic);
}
// Actualiza el logo con el título del juego
gameLogo->update();
// Actualiza el mosaico de fondo
tiledbg->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;
}
}
}
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;
}
}
}
}
// 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();
//backgroundObj->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->gameHeight / 5 * 3, 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()
{
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->activate();
postFade = 0;
}
}
// 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, param, 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, param, 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, param, 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;
}
}
// 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()
{
gameLogo->reLoad();
tiledbg->reLoad();
}