362 lines
11 KiB
C++
362 lines
11 KiB
C++
#include "credits.h"
|
|
#include <iostream>
|
|
|
|
// Constructor
|
|
Credits::Credits(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Input *input, options_t *options, section_t *section)
|
|
{
|
|
// Copia la dirección de los objetos
|
|
this->renderer = renderer;
|
|
this->screen = screen;
|
|
this->resource = resource;
|
|
this->asset = asset;
|
|
this->input = input;
|
|
this->options = options;
|
|
this->section = section;
|
|
|
|
// Reserva memoria para los punteros
|
|
eventHandler = new SDL_Event();
|
|
text = new Text(resource->getOffset("smb2.txt"), resource->getTexture("smb2.png"), renderer);
|
|
sprite = new AnimatedSprite(renderer, resource->getAnimation("shine.ani"));
|
|
|
|
// Inicializa variables
|
|
counter = 0;
|
|
counterEnabled = true;
|
|
subCounter = 0;
|
|
section->name = SECTION_PROG_CREDITS;
|
|
section->subsection = 0;
|
|
ticks = 0;
|
|
ticksSpeed = 15;
|
|
sprite->setRect({194, 174, 8, 8});
|
|
|
|
// Cambia el color del borde
|
|
screen->setBorderColor(stringToColor(options->palette, "black"));
|
|
|
|
// Crea la textura para el texto que se escribe en pantalla
|
|
textTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
|
|
if (textTexture == nullptr)
|
|
{
|
|
if (options->console)
|
|
{
|
|
std::cout << "Error: textTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
|
|
}
|
|
}
|
|
SDL_SetTextureBlendMode(textTexture, 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: textTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
|
|
}
|
|
}
|
|
SDL_SetTextureBlendMode(coverTexture, SDL_BLENDMODE_BLEND);
|
|
|
|
// Escribe el texto en la textura
|
|
fillTexture();
|
|
}
|
|
|
|
// Destructor
|
|
Credits::~Credits()
|
|
{
|
|
delete eventHandler;
|
|
delete text;
|
|
delete sprite;
|
|
SDL_DestroyTexture(textTexture);
|
|
SDL_DestroyTexture(coverTexture);
|
|
}
|
|
|
|
// Comprueba el manejador de eventos
|
|
void Credits::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 entradas
|
|
void Credits::checkInput()
|
|
{
|
|
|
|
if (input->checkInput(input_exit, REPEAT_FALSE))
|
|
{
|
|
section->name = SECTION_PROG_QUIT;
|
|
}
|
|
|
|
else if (input->checkInput(input_toggle_border, REPEAT_FALSE))
|
|
{
|
|
screen->switchBorder();
|
|
}
|
|
|
|
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_swap_palette, REPEAT_FALSE))
|
|
{
|
|
switchPalette();
|
|
}
|
|
|
|
else if (input->checkInput(input_pause, REPEAT_FALSE) || input->checkInput(input_accept, REPEAT_FALSE) || input->checkInput(input_jump, REPEAT_FALSE))
|
|
{
|
|
section->name = SECTION_PROG_TITLE;
|
|
section->subsection = 0;
|
|
}
|
|
}
|
|
|
|
// Inicializa los textos
|
|
void Credits::iniTexts()
|
|
{
|
|
std::string keys = "";
|
|
if (options->keys == ctrl_cursor)
|
|
{
|
|
keys = "CURSORS";
|
|
}
|
|
else if (options->keys == ctrl_opqa)
|
|
{
|
|
keys = "O,P AND Q";
|
|
}
|
|
else
|
|
{
|
|
keys = "A,D AND W";
|
|
}
|
|
|
|
#ifndef GAME_CONSOLE
|
|
texts.clear();
|
|
texts.push_back({"", stringToColor(options->palette, "white")});
|
|
texts.push_back({"INSTRUCTIONS:", stringToColor(options->palette, "yellow")});
|
|
texts.push_back({"", stringToColor(options->palette, "white")});
|
|
texts.push_back({"HELP JAILDOC TO GET BACK ALL", stringToColor(options->palette, "white")});
|
|
texts.push_back({"HIS PROJECTS AND GO TO THE", stringToColor(options->palette, "white")});
|
|
texts.push_back({"JAIL TO FINISH THEM", stringToColor(options->palette, "white")});
|
|
texts.push_back({"", stringToColor(options->palette, "white")});
|
|
texts.push_back({"", stringToColor(options->palette, "white")});
|
|
|
|
texts.push_back({"KEYS:", stringToColor(options->palette, "yellow")});
|
|
texts.push_back({"", stringToColor(options->palette, "white")});
|
|
texts.push_back({keys + " TO MOVE AND JUMP", stringToColor(options->palette, "white")});
|
|
texts.push_back({"M TO SWITCH THE MUSIC", stringToColor(options->palette, "white")});
|
|
texts.push_back({"H TO PAUSE THE GAME", stringToColor(options->palette, "white")});
|
|
texts.push_back({"F1-F2 TO CHANGE WINDOWS SIZE", stringToColor(options->palette, "white")});
|
|
texts.push_back({"F3 TO SWITCH TO FULLSCREEN", stringToColor(options->palette, "white")});
|
|
texts.push_back({"B TO TOOGLE THE BORDER SCREEN", stringToColor(options->palette, "white")});
|
|
texts.push_back({"", stringToColor(options->palette, "white")});
|
|
texts.push_back({"", stringToColor(options->palette, "white")});
|
|
|
|
texts.push_back({"A GAME BY JAILDESIGNER", stringToColor(options->palette, "yellow")});
|
|
texts.push_back({"MADE ON SUMMER/FALL 2022", stringToColor(options->palette, "yellow")});
|
|
texts.push_back({"", stringToColor(options->palette, "white")});
|
|
texts.push_back({"", stringToColor(options->palette, "white")});
|
|
|
|
texts.push_back({"I LOVE JAILGAMES! ", stringToColor(options->palette, "white")});
|
|
texts.push_back({"", stringToColor(options->palette, "white")});
|
|
#else
|
|
texts.clear();
|
|
texts.push_back({"", stringToColor(options->palette, "white")});
|
|
texts.push_back({"INSTRUCTIONS:", stringToColor(options->palette, "yellow")});
|
|
texts.push_back({"", stringToColor(options->palette, "white")});
|
|
texts.push_back({"HELP JAILDOC TO GET BACK ALL", stringToColor(options->palette, "white")});
|
|
texts.push_back({"HIS PROJECTS AND GO TO THE", stringToColor(options->palette, "white")});
|
|
texts.push_back({"JAIL TO FINISH THEM", stringToColor(options->palette, "white")});
|
|
texts.push_back({"", stringToColor(options->palette, "white")});
|
|
texts.push_back({"", stringToColor(options->palette, "white")});
|
|
|
|
texts.push_back({"KEYS:", stringToColor(options->palette, "yellow")});
|
|
texts.push_back({"", stringToColor(options->palette, "white")});
|
|
texts.push_back({"B TO JUMP", stringToColor(options->palette, "white")});
|
|
texts.push_back({"R TO SWITCH THE MUSIC", stringToColor(options->palette, "white")});
|
|
texts.push_back({"L TO SWAP THE COLOR PALETTE", stringToColor(options->palette, "white")});
|
|
texts.push_back({"START TO PAUSE", stringToColor(options->palette, "white")});
|
|
texts.push_back({"SELECT TO EXIT", stringToColor(options->palette, "white")});
|
|
texts.push_back({"", stringToColor(options->palette, "white")});
|
|
texts.push_back({"", stringToColor(options->palette, "white")});
|
|
texts.push_back({"", stringToColor(options->palette, "white")});
|
|
|
|
texts.push_back({"A GAME BY JAILDESIGNER", stringToColor(options->palette, "yellow")});
|
|
texts.push_back({"MADE ON SUMMER/FALL 2022", stringToColor(options->palette, "yellow")});
|
|
texts.push_back({"", stringToColor(options->palette, "white")});
|
|
texts.push_back({"", stringToColor(options->palette, "white")});
|
|
|
|
texts.push_back({"I LOVE JAILGAMES! ", stringToColor(options->palette, "white")});
|
|
texts.push_back({"", stringToColor(options->palette, "white")});
|
|
#endif
|
|
}
|
|
|
|
// Escribe el texto en la textura
|
|
void Credits::fillTexture()
|
|
{
|
|
// Inicializa los textos
|
|
iniTexts();
|
|
|
|
// Rellena la textura de texto
|
|
SDL_SetRenderTarget(renderer, textTexture);
|
|
color_t c = stringToColor(options->palette, "black");
|
|
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, 0xFF);
|
|
SDL_RenderClear(renderer);
|
|
|
|
// Escribe el texto en la textura
|
|
const int size = text->getCharacterSize();
|
|
int i = 0;
|
|
|
|
for (auto t : texts)
|
|
{
|
|
text->writeDX(TXT_CENTER | TXT_COLOR, PLAY_AREA_CENTER_X, i * size, t.label, 1, t.color);
|
|
i++;
|
|
}
|
|
|
|
// Escribe el corazón
|
|
const int textLenght = text->lenght(texts[22].label, 1) - text->lenght(" ", 1); // Se resta el ultimo caracter que es un espacio
|
|
const int posX = ((PLAY_AREA_WIDTH - textLenght) / 2) + textLenght;
|
|
text->writeColored(posX, 176, "}", stringToColor(options->palette, "bright_red"));
|
|
|
|
// Recoloca el sprite del brillo
|
|
sprite->setPosX(posX + 2);
|
|
|
|
SDL_SetRenderTarget(renderer, nullptr);
|
|
|
|
// Rellena la textura que cubre el texto con color transparente
|
|
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);
|
|
}
|
|
|
|
// Actualiza el contador
|
|
void Credits::updateCounter()
|
|
{
|
|
// Incrementa el contador
|
|
if (counterEnabled)
|
|
{
|
|
counter++;
|
|
if (counter == 224 || counter == 544 || counter == 672)
|
|
{
|
|
counterEnabled = false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
subCounter++;
|
|
if (subCounter == 100)
|
|
{
|
|
counterEnabled = true;
|
|
subCounter = 0;
|
|
}
|
|
}
|
|
|
|
// Comprueba si ha terminado la sección
|
|
if (counter > 1200)
|
|
{
|
|
section->name = SECTION_PROG_DEMO;
|
|
}
|
|
}
|
|
|
|
// Actualiza las variables
|
|
void Credits::update()
|
|
{
|
|
// Comprueba el manejador de eventos
|
|
checkEventHandler();
|
|
|
|
// 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 las entradas
|
|
checkInput();
|
|
|
|
// Actualiza el contador
|
|
updateCounter();
|
|
|
|
// Actualiza las notificaciones
|
|
screen->updateNotifier();
|
|
|
|
// Actualiza el sprite con el brillo
|
|
if (counter > 770)
|
|
{
|
|
sprite->update();
|
|
}
|
|
}
|
|
}
|
|
|
|
// Dibuja en pantalla
|
|
void Credits::render()
|
|
{
|
|
// Prepara para empezar a dibujar en la textura de juego
|
|
screen->start();
|
|
|
|
// Limpia la pantalla
|
|
screen->clean();
|
|
|
|
if (counter < 1150)
|
|
{
|
|
// Dibuja la textura con el texto en pantalla
|
|
SDL_RenderCopy(renderer, textTexture, nullptr, nullptr);
|
|
|
|
// Dibuja la textura que cubre el texto
|
|
const int offset = std::min(counter / 8, 192 / 2);
|
|
SDL_Rect srcRect = {0, 0, 256, 192 - (offset * 2)};
|
|
SDL_Rect dstRect = {0, offset * 2, 256, 192 - (offset * 2)};
|
|
SDL_RenderCopy(renderer, coverTexture, &srcRect, &dstRect);
|
|
|
|
// Dibuja el sprite con el brillo
|
|
sprite->render();
|
|
}
|
|
|
|
// Vuelca el contenido del renderizador en pantalla
|
|
screen->blit();
|
|
}
|
|
|
|
// Bucle para el logo del juego
|
|
void Credits::run()
|
|
{
|
|
while (section->name == SECTION_PROG_CREDITS)
|
|
{
|
|
update();
|
|
render();
|
|
}
|
|
}
|
|
|
|
// Cambia la paleta
|
|
void Credits::switchPalette()
|
|
{
|
|
options->palette = options->palette == p_zxspectrum ? p_zxarne : p_zxspectrum;
|
|
fillTexture();
|
|
} |