Files
jdd_opendingux/source/intro.cpp

310 lines
7.0 KiB
C++

#include "intro.h"
// Constructor
Intro::Intro(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, options_t *options)
{
// Copia la dirección de los objetos
this->resource = resource;
this->renderer = renderer;
this->screen = screen;
this->asset = asset;
this->options = options;
// Reserva memoria para los punteros
eventHandler = new SDL_Event();
if (options->palette == p_zxspectrum)
{
loadingScreenTexture1 = resource->getTexture("loading_screen_bn.png");
loadingScreenTexture2 = resource->getTexture("loading_screen_color.png");
}
else if (options->palette == p_zxarne)
{
loadingScreenTexture1 = resource->getTexture("loading_screen_bn_zxarne.png");
loadingScreenTexture2 = resource->getTexture("loading_screen_color_zxarne.png");
}
sprite1 = new Sprite(0, 0, loadingScreenTexture1->getWidth(), loadingScreenTexture1->getHeight(), loadingScreenTexture1, renderer);
sprite2 = new Sprite(0, 0, loadingScreenTexture2->getWidth(), loadingScreenTexture2->getHeight(), loadingScreenTexture2, renderer);
loadingSound1 = JA_LoadMusic(asset->get("loading_sound1.ogg").c_str());
loadingSound2 = JA_LoadMusic(asset->get("loading_sound2.ogg").c_str());
loadingSound3 = JA_LoadMusic(asset->get("loading_sound3.ogg").c_str());
// Inicializa variables
preCounter = 0;
counter = 0;
section.name = SECTION_PROG_INTRO;
section.subsection = 0;
ticks = 0;
ticksSpeed = 15;
loadCounter = 0;
lineCounter = 0;
loadingFirstPart = true;
// Ls lineas que tapan la primera textura
for (int i = 0; i < 192; ++i)
{
lines[i].x1 = 0;
lines[i].x2 = 257;
lines[i].y = i;
}
// Establece el orden de las lineas para imitar el direccionamiento de memoria del spectrum
for (int i = 0; i < 192; ++i)
{
if (i < 64)
{ // Primer bloque de 2K
lineIndex[i] = ((i % 8) * 8) + (i / 8);
}
else if (i >= 64 && i < 128)
{ // Segundo bloque de 2K
lineIndex[i] = 64 + ((i % 8) * 8) + ((i - 64) / 8);
}
else if (i >= 128 && i < 192)
{ // tercer bloque de 2K
lineIndex[i] = 128 + ((i % 8) * 8) + ((i - 128) / 8);
}
}
// Cambia el color del borde
screen->setBorderColor(stringToColor(options->palette, "black"));
}
// Destructor
Intro::~Intro()
{
delete sprite1;
delete sprite2;
delete eventHandler;
JA_DeleteMusic(loadingSound1);
JA_DeleteMusic(loadingSound2);
JA_DeleteMusic(loadingSound3);
}
// Comprueba el manejador de eventos
void Intro::checkEventHandler()
{
// 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;
}
// Comprueba las teclas que se han pulsado
if ((eventHandler->type == SDL_KEYDOWN && eventHandler->key.repeat == 0) || (eventHandler->type == SDL_JOYBUTTONDOWN))
{
switch (eventHandler->key.keysym.scancode)
{
case SDL_SCANCODE_ESCAPE:
section.name = SECTION_PROG_QUIT;
break;
case SDL_SCANCODE_B:
screen->switchBorder();
resource->reLoadTextures();
break;
case SDL_SCANCODE_F:
screen->switchVideoMode();
resource->reLoadTextures();
break;
case SDL_SCANCODE_F1:
screen->setWindowSize(1);
resource->reLoadTextures();
break;
case SDL_SCANCODE_F2:
screen->setWindowSize(2);
resource->reLoadTextures();
break;
case SDL_SCANCODE_F3:
screen->setWindowSize(3);
resource->reLoadTextures();
break;
case SDL_SCANCODE_F4:
screen->setWindowSize(4);
resource->reLoadTextures();
break;
case SDL_SCANCODE_F5:
switchPalette();
break;
default:
section.name = SECTION_PROG_TITLE;
section.subsection = 0;
break;
}
}
}
}
// Gestiona el contador de carga
void Intro::updateLoad()
{
// Primera parte de la carga, la parte en blanco ynegro
if (loadingFirstPart)
{
// Cada 5 pasos el loadCounter se incrementa en uno
const int numSteps = 5;
loadCounter = counter / numSteps;
if (loadCounter < 192)
{
const int step = 256 / numSteps;
lines[lineIndex[loadCounter]].x1 = step * (counter % numSteps);
// Elimina por completo la linea anterior
if (loadCounter > 0)
{
lines[lineIndex[loadCounter - 1]].x1 = 256;
}
}
// Una vez actualizadas las 192 lineas, pasa a la segunda fase de la carga
else if (loadCounter == 192)
{
loadingFirstPart = false;
loadCounter = 0;
JA_PlayMusic(loadingSound3);
}
}
// Segunda parte de la carga, la parte de los bloques en color
else
{
loadCounter += 2;
loadCounter = std::min(loadCounter, 768);
}
}
// Gestiona el contador interno
void Intro::updateCounter()
{
if (preCounter >= 50)
{ // Si el contador previo ha llegado a 50, empieza a contar el contador normal
if (counter == 0)
{
JA_PlayMusic(loadingSound2);
}
counter++;
}
else
{ // Actualiza el precontador
preCounter++;
}
}
// Dibuja la pantalla de carga
void Intro::renderLoad()
{
// Carga 1 - Blanco y negro
if (loadingFirstPart)
{
// Dibuja la textura de pantalla de carga en blanco y negro en pantalla
sprite1->render();
// Dibuja las 192 lineas negras sobre la textura
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
for (int i = 0; i < 192; ++i)
{
SDL_RenderDrawLine(renderer, lines[i].x1, lines[i].y, lines[i].x2, lines[i].y);
}
}
else
// Carga 2 - Color
{
sprite1->render();
SDL_Rect rect = {0, 0, 8, 8};
for (int i = 0; i < loadCounter; ++i)
{
rect.x = (i * 8) % 256;
rect.y = (i / 32) * 8;
sprite2->setRect(rect);
sprite2->setSpriteClip(rect);
sprite2->render();
}
}
}
// Actualiza las variables
void Intro::update()
{
// Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
if (SDL_GetTicks() - ticks > ticksSpeed)
{
// Actualiza el contador de ticks
ticks = SDL_GetTicks();
// Comprueba el manejador de eventos
checkEventHandler();
// Gestiona el contador interno
updateCounter();
// Gestiona el contador de carga
updateLoad();
// Comprueba si ha terminado la intro
if (loadCounter >= 768)
{
section.name = SECTION_PROG_TITLE;
section.subsection = 0;
JA_StopMusic();
}
}
}
// Dibuja en pantalla
void Intro::render()
{
// Prepara para empezar a dibujar en la textura de juego
screen->start();
// Limpia la pantalla
screen->clean();
// Dibuja la pantalla de carga
renderLoad();
// Vuelca el contenido del renderizador en pantalla
screen->blit();
}
// Bucle para el logo del juego
section_t Intro::run()
{
// Inicia el sonido de carga
JA_PlayMusic(loadingSound1);
while (section.name == SECTION_PROG_INTRO)
{
update();
render();
}
return section;
}
// Cambia la paleta
void Intro::switchPalette()
{
if (options->palette == p_zxspectrum)
{
options->palette = p_zxarne;
sprite1->setTexture(resource->getTexture("loading_screen_bn_zxarne.png"));
sprite2->setTexture(resource->getTexture("loading_screen_color_zxarne.png"));
}
else
{
options->palette = p_zxspectrum;
sprite1->setTexture(resource->getTexture("loading_screen_bn.png"));
sprite2->setTexture(resource->getTexture("loading_screen_color.png"));
}
}