175 lines
4.0 KiB
C++
175 lines
4.0 KiB
C++
#include "credits.h"
|
|
|
|
// Constructor
|
|
Credits::Credits(SDL_Renderer *renderer, Screen *screen, Asset *asset)
|
|
{
|
|
// Copia la dirección de los objetos
|
|
this->renderer = renderer;
|
|
this->screen = screen;
|
|
this->asset = asset;
|
|
|
|
// Reserva memoria para los punteros
|
|
eventHandler = new SDL_Event();
|
|
text = new Text(asset->get("smb2.png"), asset->get("smb2.txt"), renderer);
|
|
|
|
// Inicializa variables
|
|
counter = 0;
|
|
section.name = SECTION_PROG_CREDITS;
|
|
section.subsection = 0;
|
|
ticks = 0;
|
|
ticksSpeed = 15;
|
|
|
|
// Cambia el color del borde
|
|
screen->setBorderColor(stringToColor("black"));
|
|
|
|
// Inicializa los textos
|
|
texts.push_back("");
|
|
texts.push_back("INSTRUCTIONS:");
|
|
texts.push_back("");
|
|
texts.push_back("HELP JAILDOC TO GET BACK ALL HIS");
|
|
texts.push_back("PROJECTS AND GO TO THE JAIL TO");
|
|
texts.push_back("FINISH THEM");
|
|
texts.push_back("");
|
|
texts.push_back("");
|
|
texts.push_back("KEYS:");
|
|
texts.push_back("");
|
|
texts.push_back("USE CURSORS TO MOVE AND JUMP");
|
|
texts.push_back("F1-F4 TO CHANGE WINDOWS SIZE");
|
|
texts.push_back("F TO SWITCH TO FULLSCREEN");
|
|
texts.push_back("B TO DE/ACTIVATE THE BORDER SC.");
|
|
texts.push_back("M TO TURN ON/OFF THE MUSIC");
|
|
texts.push_back("ESC TO EXIT GAME");
|
|
texts.push_back("");
|
|
texts.push_back("");
|
|
texts.push_back("A GAME BY JAILDESIGNER");
|
|
texts.push_back("MADE ON SUMMER/FALL 2022");
|
|
texts.push_back("");
|
|
texts.push_back("");
|
|
texts.push_back("LOVE JAILGAMES!");
|
|
texts.push_back("");
|
|
|
|
// 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)
|
|
{
|
|
printf("Error: textTexture could not be created!\nSDL Error: %s\n", SDL_GetError());
|
|
}
|
|
SDL_SetTextureBlendMode(textTexture, SDL_BLENDMODE_BLEND);
|
|
|
|
// Escribe el texto en la textura
|
|
fillTexture();
|
|
}
|
|
|
|
// Destructor
|
|
Credits::~Credits()
|
|
{
|
|
delete eventHandler;
|
|
delete text;
|
|
}
|
|
|
|
// 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 si se ha pulsado alguna tecla
|
|
else if ((eventHandler->type == SDL_KEYDOWN) and (eventHandler->key.repeat == 0))
|
|
{
|
|
switch (eventHandler->key.keysym.scancode)
|
|
{
|
|
case SDL_SCANCODE_ESCAPE:
|
|
section.name = SECTION_PROG_QUIT;
|
|
break;
|
|
|
|
case SDL_SCANCODE_RETURN:
|
|
section.name = SECTION_PROG_TITLE;
|
|
section.subsection = 0;
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Escribe el texto en la textura
|
|
void Credits::fillTexture()
|
|
{
|
|
SDL_SetRenderTarget(renderer, textTexture);
|
|
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
|
|
SDL_RenderClear(renderer);
|
|
|
|
// Escribe el texto en la textura
|
|
const int size = text->getCharacterSize();
|
|
int i = 0;
|
|
|
|
for (auto t:texts)
|
|
{
|
|
text->write(0, i * size, t);
|
|
++i;
|
|
}
|
|
|
|
SDL_SetRenderTarget(renderer, nullptr);
|
|
}
|
|
|
|
// Actualiza las variables
|
|
void Credits::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();
|
|
|
|
// Incrementa el contador
|
|
++counter;
|
|
|
|
// Comprueba si ha terminado la sección
|
|
if (counter > 1000)
|
|
{
|
|
section.name = SECTION_PROG_LOGO;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Dibuja en pantalla
|
|
void Credits::render()
|
|
{
|
|
// Prepara para empezar a dibujar en la textura de juego
|
|
screen->start();
|
|
|
|
// Limpia la pantalla
|
|
screen->clean();
|
|
|
|
SDL_Rect rect = {0, 0, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT};
|
|
|
|
// Dibuja la textura con el mapa en pantalla
|
|
SDL_RenderCopy(renderer, textTexture, &rect, nullptr);
|
|
|
|
// Vuelca el contenido del renderizador en pantalla
|
|
screen->blit();
|
|
}
|
|
|
|
// Bucle para el logo del juego
|
|
section_t Credits::run()
|
|
{
|
|
while (section.name == SECTION_PROG_CREDITS)
|
|
{
|
|
update();
|
|
render();
|
|
}
|
|
|
|
return section;
|
|
} |