forked from jaildesigner-jailgames/jaildoctors_dilemma
327 lines
9.5 KiB
C++
327 lines
9.5 KiB
C++
#include "jail_engine/jail_audio.h"
|
|
#include "jail_engine/jscore.h"
|
|
#include "const.h"
|
|
#include "enter_id.h"
|
|
#include <iostream>
|
|
|
|
// Constructor
|
|
EnterID::EnterID(SDL_Renderer *renderer, Screen *screen, Asset *asset, options_t *options, section_t *section)
|
|
{
|
|
// Copia la dirección de los objetos
|
|
this->renderer = renderer;
|
|
this->screen = screen;
|
|
this->asset = asset;
|
|
this->options = options;
|
|
this->section = section;
|
|
|
|
// Reserva memoria para los punteros
|
|
eventHandler = new SDL_Event();
|
|
texture = new Texture(renderer, asset->get("smb2.png"));
|
|
text = new Text(asset->get("smb2.txt"), texture, renderer);
|
|
|
|
// 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);
|
|
|
|
// Inicializa variables
|
|
oldJailerID = options->online.jailerID;
|
|
loopRunning = true;
|
|
counter = 0;
|
|
ticks = 0;
|
|
ticksSpeed = 15;
|
|
jailerIDPos = 0;
|
|
initName();
|
|
|
|
// Escribe el texto en la textura
|
|
fillTexture();
|
|
}
|
|
|
|
// Destructor
|
|
EnterID::~EnterID()
|
|
{
|
|
delete eventHandler;
|
|
delete text;
|
|
delete texture;
|
|
SDL_DestroyTexture(textTexture);
|
|
}
|
|
|
|
// Bucle para el logo del juego
|
|
void EnterID::run()
|
|
{
|
|
while (loopRunning)
|
|
{
|
|
update();
|
|
checkEvents();
|
|
render();
|
|
}
|
|
}
|
|
|
|
// Comprueba el manejador de eventos
|
|
void EnterID::checkEvents()
|
|
{
|
|
// 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_QUIT;
|
|
loopRunning = false;
|
|
break;
|
|
}
|
|
|
|
// Comprueba las teclas que se han pulsado
|
|
if ((eventHandler->type == SDL_KEYDOWN && eventHandler->key.repeat == 0) || (eventHandler->type == SDL_JOYBUTTONDOWN))
|
|
{
|
|
if (eventHandler->key.keysym.scancode == SDL_SCANCODE_RETURN)
|
|
{
|
|
options->online.jailerID = toLower((std::string)name);
|
|
endSection();
|
|
break;
|
|
}
|
|
|
|
if (eventHandler->key.keysym.scancode >= SDL_SCANCODE_A && eventHandler->key.keysym.scancode <= SDL_SCANCODE_Z)
|
|
{ // Si pulsa una letra
|
|
if (pos < maxLenght)
|
|
{
|
|
name[pos++] = eventHandler->key.keysym.scancode + 61;
|
|
name[pos] = 0;
|
|
}
|
|
}
|
|
|
|
else if (eventHandler->key.keysym.scancode >= SDL_SCANCODE_1 && eventHandler->key.keysym.scancode <= SDL_SCANCODE_9)
|
|
{ // Si pulsa un número
|
|
if (pos < maxLenght)
|
|
{ // En ascii el '0' va antes del '1', pero en scancode el '0' va despues de '9'
|
|
name[pos++] = eventHandler->key.keysym.scancode + 19;
|
|
name[pos] = 0;
|
|
}
|
|
}
|
|
|
|
else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_0)
|
|
{
|
|
if (pos < maxLenght)
|
|
{
|
|
name[pos++] = 48;
|
|
name[pos] = 0;
|
|
}
|
|
}
|
|
|
|
else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_BACKSPACE)
|
|
{
|
|
if (pos > 0)
|
|
{
|
|
name[--pos] = 0;
|
|
}
|
|
}
|
|
|
|
else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_ESCAPE)
|
|
{
|
|
section->name = SECTION_QUIT;
|
|
loopRunning = false;
|
|
break;
|
|
}
|
|
|
|
else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_F1)
|
|
{
|
|
screen->setWindowSize(1);
|
|
break;
|
|
}
|
|
|
|
else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_F2)
|
|
{
|
|
screen->setWindowSize(2);
|
|
break;
|
|
}
|
|
|
|
else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_F3)
|
|
{
|
|
screen->setWindowSize(3);
|
|
break;
|
|
}
|
|
|
|
else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_F4)
|
|
{
|
|
screen->setWindowSize(4);
|
|
break;
|
|
}
|
|
|
|
else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_F5)
|
|
{
|
|
switchPalette();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Actualiza las variables
|
|
void EnterID::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();
|
|
|
|
// Actualiza el contador
|
|
counter++;
|
|
|
|
// Actualiza el cursor
|
|
cursor = (counter % 20 >= 10) ? " " : "_";
|
|
|
|
// Actualiza las notificaciones
|
|
screen->updateNotifier();
|
|
}
|
|
}
|
|
|
|
// Dibuja en pantalla
|
|
void EnterID::render()
|
|
{
|
|
// Prepara para empezar a dibujar en la textura de juego
|
|
screen->start();
|
|
|
|
// Dibuja la textura con el texto en pantalla
|
|
SDL_RenderCopy(renderer, textTexture, nullptr, nullptr);
|
|
|
|
// Escribe el jailerID
|
|
const std::string jailerID = (std::string)name + cursor;
|
|
const color_t color = stringToColor(options->palette, "white");
|
|
text->writeDX(TXT_CENTER | TXT_COLOR, GAMECANVAS_CENTER_X, jailerIDPos, jailerID, 1, color);
|
|
|
|
// Vuelca el contenido del renderizador en pantalla
|
|
screen->blit();
|
|
}
|
|
|
|
// Inicializa los textos
|
|
void EnterID::iniTexts()
|
|
{
|
|
texts.clear();
|
|
texts.push_back({"", 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({"", stringToColor(options->palette, "white")});
|
|
texts.push_back({"", stringToColor(options->palette, "white")});
|
|
texts.push_back({"ONLINE CONFIGURATION:", stringToColor(options->palette, "green")});
|
|
texts.push_back({"", stringToColor(options->palette, "white")});
|
|
texts.push_back({"PLEASE ENTER AN ID OR", stringToColor(options->palette, "white")});
|
|
texts.push_back({"LEAVE BLANK FOR OFFLINE MODE", 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({"JAILER_ID:", stringToColor(options->palette, "green")});
|
|
|
|
jailerIDPos = ((int)texts.size() + 1) * text->getCharacterSize();
|
|
}
|
|
|
|
// Escribe el texto en la textura
|
|
void EnterID::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++;
|
|
}
|
|
|
|
SDL_SetRenderTarget(renderer, nullptr);
|
|
}
|
|
|
|
// Inicializa los servicios online
|
|
void EnterID::initOnline()
|
|
{
|
|
// Si ya ha iniciado la sesión y no ha cambiado el jailerID, que no continue
|
|
if (options->online.sessionEnabled)
|
|
{
|
|
if (oldJailerID == options->online.jailerID)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (options->online.jailerID == "")
|
|
{ // Jailer ID no definido
|
|
options->online.enabled = false;
|
|
options->online.sessionEnabled = false;
|
|
}
|
|
else
|
|
{ // Jailer ID iniciado
|
|
options->online.enabled = options->online.sessionEnabled = true;
|
|
// Establece el servidor y el puerto
|
|
jscore::init(options->online.server, options->online.port);
|
|
#ifdef DEBUG
|
|
const std::string caption = "IS LOGGED IN (DEBUG)";
|
|
#else
|
|
const std::string caption = "IS LOGGED IN";
|
|
#endif
|
|
screen->showNotification(options->online.jailerID, caption, 12);
|
|
if (options->console)
|
|
{
|
|
std::cout << caption << std::endl;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Termina la sección
|
|
void EnterID::endSection()
|
|
{
|
|
loopRunning = false;
|
|
initOnline();
|
|
}
|
|
|
|
// Inicializa el vector utilizado para almacenar el texto que se escribe en pantalla
|
|
void EnterID::initName()
|
|
{
|
|
// Calcula el tamaño del vector
|
|
name[0] = 0;
|
|
maxLenght = sizeof(name) / sizeof(name[pos]);
|
|
|
|
// Inicializa el vector con ceros
|
|
for (int i = 0; i < maxLenght; ++i)
|
|
{
|
|
name[i] = 0;
|
|
}
|
|
|
|
// Si no hay definido ningun JailerID, coloca el cursor en primera posición
|
|
if (options->online.jailerID == "")
|
|
{
|
|
pos = 0;
|
|
}
|
|
else
|
|
{ // En caso contrario, copia el texto al vector y coloca el cursor en posición
|
|
const int len = std::min((int)options->online.jailerID.size(), maxLenght);
|
|
for (int i = 0; i < len; ++i)
|
|
{
|
|
name[i] = (char)options->online.jailerID[i];
|
|
}
|
|
pos = len;
|
|
}
|
|
}
|
|
|
|
// Cambia la paleta
|
|
void EnterID::switchPalette()
|
|
{
|
|
options->palette = options->palette == p_zxspectrum ? p_zxarne : p_zxspectrum;
|
|
fillTexture();
|
|
}
|