368 lines
11 KiB
C++
368 lines
11 KiB
C++
#include "credits.h"
|
|
#include <SDL2/SDL_blendmode.h> // Para SDL_BLENDMODE_BLEND
|
|
#include <SDL2/SDL_error.h> // Para SDL_GetError
|
|
#include <SDL2/SDL_pixels.h> // Para SDL_PIXELFORMAT_RGBA8888
|
|
#include <SDL2/SDL_rect.h> // Para SDL_Rect
|
|
#include <SDL2/SDL_timer.h> // Para SDL_GetTicks
|
|
#include <algorithm> // Para min
|
|
#include <iostream> // Para char_traits, basic_ostream, operator<<
|
|
#include "animatedsprite.h" // Para AnimatedSprite
|
|
#include "const.h" // Para GAMECANVAS_HEIGHT, GAMECANVAS_WIDTH
|
|
#include "input.h" // Para Input, REPEAT_FALSE, inputs_e
|
|
#include "resource.h" // Para Resource
|
|
#include "screen.h" // Para Screen
|
|
#include "text.h" // Para Text, TXT_CENTER, TXT_COLOR
|
|
#include "asset.h"
|
|
class Asset;
|
|
|
|
// Constructor
|
|
Credits::Credits(Resource *resource, options_t *options, section_t *section)
|
|
: screen_(Screen::get()),
|
|
renderer_(Screen::get()->getRenderer()),
|
|
resource_(resource),
|
|
asset_(Asset::get()),
|
|
input_(Input::get()),
|
|
options_(options),
|
|
section_(section)
|
|
|
|
{
|
|
// Reserva memoria para los punteros
|
|
event_handler_ = 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
|
|
section->name = SECTION_CREDITS;
|
|
section->subsection = 0;
|
|
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
|
|
text_texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
|
|
if (text_texture_ == nullptr)
|
|
{
|
|
if (options->console)
|
|
{
|
|
std::cout << "Error: textTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
|
|
}
|
|
}
|
|
SDL_SetTextureBlendMode(text_texture_, SDL_BLENDMODE_BLEND);
|
|
|
|
// Crea la textura para cubrir el rexto
|
|
cover_texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
|
|
if (cover_texture_ == nullptr)
|
|
{
|
|
if (options->console)
|
|
{
|
|
std::cout << "Error: textTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
|
|
}
|
|
}
|
|
SDL_SetTextureBlendMode(cover_texture_, SDL_BLENDMODE_BLEND);
|
|
|
|
// Escribe el texto en la textura
|
|
fillTexture();
|
|
}
|
|
|
|
// Destructor
|
|
Credits::~Credits()
|
|
{
|
|
delete event_handler_;
|
|
delete text_;
|
|
delete sprite_;
|
|
SDL_DestroyTexture(text_texture_);
|
|
SDL_DestroyTexture(cover_texture_);
|
|
}
|
|
|
|
// Comprueba el manejador de eventos
|
|
void Credits::checkEvents()
|
|
{
|
|
// Comprueba los eventos que hay en la cola
|
|
while (SDL_PollEvent(event_handler_) != 0)
|
|
{
|
|
// Evento de salida de la aplicación
|
|
if (event_handler_->type == SDL_QUIT)
|
|
{
|
|
section_->name = SECTION_QUIT;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Comprueba las entradas
|
|
void Credits::checkInput()
|
|
{
|
|
|
|
if (input_->checkInput(input_exit, REPEAT_FALSE))
|
|
{
|
|
section_->name = SECTION_QUIT;
|
|
}
|
|
|
|
else if (input_->checkInput(input_toggle_border, REPEAT_FALSE))
|
|
{
|
|
screen_->toggleBorder();
|
|
}
|
|
|
|
else if (input_->checkInput(input_toggle_videomode, REPEAT_FALSE))
|
|
{
|
|
screen_->toggleVideoMode();
|
|
}
|
|
|
|
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_toggle_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_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_, text_texture_);
|
|
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_, cover_texture_);
|
|
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 (counter_enabled_)
|
|
{
|
|
counter_++;
|
|
if (counter_ == 224 || counter_ == 544 || counter_ == 672)
|
|
{
|
|
counter_enabled_ = false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
sub_counter_++;
|
|
if (sub_counter_ == 100)
|
|
{
|
|
counter_enabled_ = true;
|
|
sub_counter_ = 0;
|
|
}
|
|
}
|
|
|
|
// Comprueba si ha terminado la sección
|
|
if (counter_ > 1200)
|
|
{
|
|
section_->name = SECTION_DEMO;
|
|
}
|
|
}
|
|
|
|
// Actualiza las variables
|
|
void Credits::update()
|
|
{
|
|
// Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
|
|
if (SDL_GetTicks() - ticks_ > ticks_speed_)
|
|
{
|
|
// 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_, text_texture_, 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_, cover_texture_, &srcRect, &dstRect);
|
|
|
|
// Dibuja el sprite con el brillo
|
|
sprite_->render();
|
|
}
|
|
|
|
// Vuelca el contenido del renderizador en pantalla
|
|
screen_->render();
|
|
}
|
|
|
|
// Bucle para el logo del juego
|
|
void Credits::run()
|
|
{
|
|
while (section_->name == SECTION_CREDITS)
|
|
{
|
|
update();
|
|
checkEvents();
|
|
render();
|
|
}
|
|
}
|
|
|
|
// Cambia la paleta
|
|
void Credits::switchPalette()
|
|
{
|
|
options_->palette = options_->palette == p_zxspectrum ? p_zxarne : p_zxspectrum;
|
|
fillTexture();
|
|
} |