359 lines
11 KiB
C++
359 lines
11 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 = 0;
|
|
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);
|
|
|
|
// Rellena la textura segun la escena
|
|
fillTexture();
|
|
}
|
|
|
|
// 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;
|
|
|
|
case SDL_SCANCODE_RIGHT:
|
|
++scene %= 5;
|
|
fillTexture();
|
|
break;
|
|
|
|
case SDL_SCANCODE_LEFT:
|
|
--scene %= 5;
|
|
fillTexture();
|
|
break;
|
|
|
|
default:
|
|
// section.name = SECTION_PROG_TITLE;
|
|
// section.subsection = 0;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Inicializa los textos
|
|
void Ending::iniTexts()
|
|
{
|
|
// Reinicia el vector
|
|
texts.clear();
|
|
|
|
// Escena #0
|
|
texts.push_back("HE FINALLY MANAGED");
|
|
texts.push_back("TO GET TO THE JAIL");
|
|
texts.push_back("WITH ALL HIS PROJECTS");
|
|
texts.push_back("READY TO BE RELEASED");
|
|
|
|
// Escena #1
|
|
texts.push_back("ALL THE JAILERS WERE THERE");
|
|
texts.push_back("WAITING FOR THE JAILGAMES");
|
|
texts.push_back("TO BE RELEASED");
|
|
|
|
texts.push_back("THERE WERE EVEN BARRULLS AND");
|
|
texts.push_back("BEGINNERS AMONG THE CROWD");
|
|
|
|
texts.push_back("BRY WAS CRYING...");
|
|
|
|
// Escena #2
|
|
texts.push_back("BUT SUDDENLY SOMETHING");
|
|
texts.push_back("CAUGHT HIS ATTENTION");
|
|
|
|
// Escena #3
|
|
texts.push_back("A PILE OF JUNK!");
|
|
texts.push_back("FULL OF NON WORKING THINGS!!");
|
|
|
|
// Escena #4
|
|
texts.push_back("AND THEN,");
|
|
texts.push_back("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);
|
|
|
|
const int size = text->getCharacterSize();
|
|
|
|
if (scene == 0)
|
|
{ // Compone la escena #0
|
|
|
|
// Dibuja el sprite
|
|
texture = resource->getTexture("ending1.png");
|
|
sprite->setTexture(texture);
|
|
sprite->setSpriteClip({0, 0, texture->getWidth(), texture->getHeight()});
|
|
const int x = (PLAY_AREA_WIDTH - texture->getWidth()) / 2;
|
|
sprite->setRect({x, 48, texture->getWidth(), texture->getHeight()});
|
|
sprite->render();
|
|
|
|
// Dos lineas de texto
|
|
text->writeDX(TXT_CENTER | TXT_STROKE, PLAY_AREA_CENTER_X, 34, texts.at(0), 1, c, 2, c);
|
|
text->writeDX(TXT_CENTER | TXT_STROKE, PLAY_AREA_CENTER_X, 45, texts.at(1), 1, c, 2, c);
|
|
|
|
// Dos lineas más de texto
|
|
text->writeDX(TXT_CENTER | TXT_STROKE, PLAY_AREA_CENTER_X, 143, texts.at(2), 1, c, 2, c);
|
|
text->writeDX(TXT_CENTER | TXT_STROKE, PLAY_AREA_CENTER_X, 154, texts.at(3), 1, c, 2, c);
|
|
}
|
|
|
|
if (scene == 1)
|
|
{ // Compone la escena #1
|
|
|
|
// Dibuja el sprite
|
|
texture = resource->getTexture("ending2.png");
|
|
sprite->setTexture(texture);
|
|
sprite->setSpriteClip({0, 0, texture->getWidth(), texture->getHeight()});
|
|
const int x = (GAMECANVAS_WIDTH - texture->getWidth()) / 2;
|
|
const int y = (GAMECANVAS_HEIGHT - texture->getHeight()) / 2;
|
|
sprite->setRect({x, y, texture->getWidth(), texture->getHeight()});
|
|
sprite->render();
|
|
|
|
// Tres lineas de texto
|
|
text->writeDX(TXT_CENTER | TXT_STROKE, PLAY_AREA_CENTER_X, 3, texts.at(4), 1, c, 2, c);
|
|
text->writeDX(TXT_CENTER | TXT_STROKE, PLAY_AREA_CENTER_X, 13, texts.at(5), 1, c, 2, c);
|
|
text->writeDX(TXT_CENTER | TXT_STROKE, PLAY_AREA_CENTER_X, 23, texts.at(6), 1, c, 2, c);
|
|
|
|
// Dos lineas más de texto
|
|
text->writeDX(TXT_CENTER | TXT_STROKE, PLAY_AREA_CENTER_X, 163, texts.at(7), 1, c, 2, c);
|
|
text->writeDX(TXT_CENTER | TXT_STROKE, PLAY_AREA_CENTER_X, 173, texts.at(8), 1, c, 2, c);
|
|
|
|
// Una linea mas
|
|
text->writeDX(TXT_CENTER | TXT_STROKE, PLAY_AREA_CENTER_X, 183, texts.at(9), 1, c, 2, c);
|
|
}
|
|
|
|
if (scene == 2)
|
|
{ // Compone la escena #2
|
|
|
|
// Dibuja el sprite
|
|
texture = resource->getTexture("ending3.png");
|
|
sprite->setTexture(texture);
|
|
sprite->setSpriteClip({0, 0, texture->getWidth(), texture->getHeight()});
|
|
const int x = (GAMECANVAS_WIDTH - texture->getWidth()) / 2;
|
|
sprite->setRect({x, 29, texture->getWidth(), texture->getHeight()});
|
|
sprite->render();
|
|
|
|
// Dos lineas de texto
|
|
text->writeDX(TXT_CENTER | TXT_STROKE, PLAY_AREA_CENTER_X, 21, texts.at(10), 1, c, 2, c);
|
|
text->writeDX(TXT_CENTER | TXT_STROKE, PLAY_AREA_CENTER_X, 33, texts.at(11), 1, c, 2, c);
|
|
}
|
|
|
|
if (scene == 3)
|
|
{ // Compone la escena #3
|
|
|
|
// Dibuja el sprite
|
|
texture = resource->getTexture("ending4.png");
|
|
sprite->setTexture(texture);
|
|
sprite->setSpriteClip({0, 0, texture->getWidth(), texture->getHeight()});
|
|
const int x = (GAMECANVAS_WIDTH - texture->getWidth()) / 2;
|
|
sprite->setRect({x, 63, texture->getWidth(), texture->getHeight()});
|
|
sprite->render();
|
|
|
|
// Dos lineas de texto
|
|
text->writeDX(TXT_CENTER | TXT_STROKE, PLAY_AREA_CENTER_X, 38, texts.at(12), 1, c, 2, c);
|
|
text->writeDX(TXT_CENTER | TXT_STROKE, PLAY_AREA_CENTER_X, 51, texts.at(13), 1, c, 2, c);
|
|
}
|
|
|
|
if (scene == 4)
|
|
{ // Compone la escena #4
|
|
|
|
// Dibuja el sprite
|
|
texture = resource->getTexture("ending5.png");
|
|
sprite->setTexture(texture);
|
|
sprite->setSpriteClip({0, 0, texture->getWidth(), texture->getHeight()});
|
|
const int x = (GAMECANVAS_WIDTH - texture->getWidth()) / 2;
|
|
sprite->setRect({x, 53, texture->getWidth(), texture->getHeight()});
|
|
sprite->render();
|
|
|
|
// Dos lineas de texto
|
|
text->writeDX(TXT_CENTER | TXT_STROKE, PLAY_AREA_CENTER_X, 28, texts.at(14), 1, c, 2, c);
|
|
text->writeDX(TXT_CENTER | TXT_STROKE, PLAY_AREA_CENTER_X, 38, texts.at(15), 1, c, 2, c);
|
|
|
|
// Una linea mas
|
|
text->writeDX(TXT_CENTER | TXT_STROKE, PLAY_AREA_CENTER_X, 160, texts.at(16), 1, c, 2, c);
|
|
}
|
|
|
|
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;
|
|
} |