Files
jdd_opendingux/source/ending.cpp
2022-11-03 12:42:11 +01:00

258 lines
7.2 KiB
C++

#include "ending.h"
// Constructor
Ending::Ending(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, options_t *options)
{
// Copia los punteros
this->renderer = renderer;
this->screen = screen;
this->resource = resource;
this->asset = asset;
this->options = options;
// Reserva memoria para los punteros a objetos
eventHandler = new SDL_Event();
text = new Text(resource->getOffset("smb2.txt"), resource->getTexture("smb2.png"), renderer);
texture = resource->getTexture("ending1.png");
sprite = new Sprite({0, 0, texture->getWidth(), texture->getHeight()}, texture, renderer);
// Inicializa variables
counter = 0;
section.name = SECTION_PROG_ENDING;
section.subsection = 0;
ticks = 0;
ticksSpeed = 15;
scene = 1;
sceneLenght.insert(sceneLenght.end(), {0, 100, 100, 100});
// Inicializa los textos
iniTexts();
// Cambia el color del borde
screen->setBorderColor(stringToColor(options->palette, "black"));
// Crea la textura para el texto que se escribe en pantalla
canvasTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
if (canvasTexture == nullptr)
{
if (options->console)
{
std::cout << "Error: canvasTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
}
}
SDL_SetTextureBlendMode(canvasTexture, SDL_BLENDMODE_BLEND);
// Crea la textura para cubrir el rexto
coverTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
if (coverTexture == nullptr)
{
if (options->console)
{
std::cout << "Error: canvasTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
}
}
SDL_SetTextureBlendMode(coverTexture, SDL_BLENDMODE_BLEND);
}
// Destructor
Ending::~Ending()
{
// Libera la memoria de los objetos
delete eventHandler;
delete text;
delete sprite;
SDL_DestroyTexture(canvasTexture);
SDL_DestroyTexture(coverTexture);
}
// Actualiza el objeto
void Ending::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();
}
}
// Dibuja el final en pantalla
void Ending::render()
{
// Prepara para empezar a dibujar en la textura de juego
screen->start();
// Limpia la pantalla
screen->clean();
// Dibuja la textura con el texto en pantalla
SDL_RenderCopy(renderer, canvasTexture, nullptr, nullptr);
// Vuelca el contenido del renderizador en pantalla
screen->blit();
}
// Comprueba el manejador de eventos
void Ending::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;
}
}
}
}
// Inicializa los textos
void Ending::iniTexts()
{
// Reinicia el vector
texts.clear();
// Escena #1
texts.push_back("HE FINALLY MANAGED TO GET");
texts.push_back("TO THE JAIL WITH ALL HIS");
texts.push_back("PROJECTS READY TO BE RELEASED");
// Escena #2
texts.push_back("ALL THE JAILERS WERE THERE");
texts.push_back("WAITING FOR THE JAILGAMES");
texts.push_back("TO BE RELEASED");
// Escena #3
texts.push_back("THERE WERE EVEN BARRULLS AND");
texts.push_back("BEGINNERS AMONG THE CROWD");
texts.push_back("BRY WAS CRYING...");
// Escena #4
texts.push_back("BUT SUDDENLY SOMETHING");
texts.push_back("CAUGHT HIS ATTENTION");
// Escena #5
texts.push_back("A PILE OF JUNK!");
texts.push_back("FULL OF NON WORKING THINGS!!");
texts.push_back("AND THEN, FOURTY NEW PROJECTS");
texts.push_back("WERE BORN...");
}
// Rellena la textura segun la escena
void Ending::fillTexture()
{
// Inicializa los textos
iniTexts();
// Rellena la canvasTexture de color
SDL_SetRenderTarget(renderer, canvasTexture);
const color_t c = stringToColor(options->palette, "black");
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, 0x00);
SDL_RenderClear(renderer);
// Escribe el texto en la textura
const int size = text->getCharacterSize();
int y = 20;
for (int i = 0; i < 3; ++i)
{
text->writeDX(TXT_CENTER | TXT_STROKE, PLAY_AREA_CENTER_X, y, texts.at(i), 1, c);
y += size;
}
// Dibuja el sprite
const int x = (PLAY_AREA_WIDTH - texture->getWidth()) / 2;
sprite->setPos({x, y + 10});
SDL_SetRenderTarget(renderer, nullptr);
// Rellena la textura que cubre el texto
SDL_SetRenderTarget(renderer, coverTexture);
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, 0x00);
SDL_RenderClear(renderer);
// Los primeros 8 pixels crea una malla
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, 0xFF);
for (int i = 0; i < 256; i += 2)
{
SDL_RenderDrawPoint(renderer, i, 0);
SDL_RenderDrawPoint(renderer, i, 2);
SDL_RenderDrawPoint(renderer, i, 4);
SDL_RenderDrawPoint(renderer, i, 6);
SDL_RenderDrawPoint(renderer, i + 1, 5);
SDL_RenderDrawPoint(renderer, i + 1, 7);
}
// El resto se rellena de color sólido
SDL_Rect rect = {0, 8, 256, 192};
SDL_RenderFillRect(renderer, &rect);
SDL_SetRenderTarget(renderer, nullptr);
}
// Bucle principal
section_t Ending::run()
{
while (section.name == SECTION_PROG_ENDING)
{
update();
render();
}
return section;
}