Ya permite establecer el online ID desde el propio juego

This commit is contained in:
2022-11-23 13:29:35 +01:00
parent c71adfbac4
commit 54d428ed96
5 changed files with 204 additions and 43 deletions

View File

@@ -94,6 +94,9 @@ Text::Text(std::string textFile, Texture *texture, SDL_Renderer *renderer)
// Crea los objetos // Crea los objetos
sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture, renderer); sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture, renderer);
// Inicializa variables
fixedWidth = false;
} }
// Constructor // Constructor
@@ -111,6 +114,9 @@ Text::Text(textFile_t *textFile, Texture *texture, SDL_Renderer *renderer)
// Crea los objetos // Crea los objetos
sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture, renderer); sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture, renderer);
// Inicializa variables
fixedWidth = false;
} }
// Destructor // Destructor
@@ -133,7 +139,8 @@ void Text::write(int x, int y, std::string text, int kerning, int lenght)
sprite->setPosX(x + shift); sprite->setPosX(x + shift);
sprite->setPosY(y); sprite->setPosY(y);
sprite->render(); sprite->render();
shift += (offset[int(text[i])].w + kerning); // shift += (offset[int(text[i])].w + kerning);
shift += fixedWidth ? boxWidth : (offset[int(text[i])].w + kerning);
} }
} }
@@ -226,3 +233,9 @@ void Text::reLoadTexture()
{ {
sprite->getTexture()->reLoad(); sprite->getTexture()->reLoad();
} }
// Establece si se usa un tamaño fijo de letra
void Text::setFixedWidth(bool value)
{
fixedWidth = value;
}

View File

@@ -38,6 +38,7 @@ private:
// Variables // Variables
int boxWidth; // Anchura de la caja de cada caracter en el png int boxWidth; // Anchura de la caja de cada caracter en el png
int boxHeight; // Altura de la caja de cada caracter en el png int boxHeight; // Altura de la caja de cada caracter en el png
bool fixedWidth; // Indica si el texto se ha de escribir con longitud fija en todas las letras
offset_t offset[128]; // Vector con las posiciones y ancho de cada letra offset_t offset[128]; // Vector con las posiciones y ancho de cada letra
public: public:
@@ -71,6 +72,9 @@ public:
// Recarga la textura // Recarga la textura
void reLoadTexture(); void reLoadTexture();
// Establece si se usa un tamaño fijo de letra
void setFixedWidth(bool value);
}; };
#endif #endif

View File

@@ -318,14 +318,6 @@ section_t Credits::run()
// Cambia la paleta // Cambia la paleta
void Credits::switchPalette() void Credits::switchPalette()
{ {
if (options->palette == p_zxspectrum) options->palette = options->palette == p_zxspectrum ? p_zxarne : p_zxspectrum;
{
options->palette = p_zxarne;
}
else
{
options->palette = p_zxspectrum;
}
fillTexture(); fillTexture();
} }

View File

@@ -1,6 +1,7 @@
#include "enter_id.h"
#include "const.h"
#include "common/jail_audio.h" #include "common/jail_audio.h"
#include "const.h"
#include "enter_id.h"
#include <iostream>
// Constructor // Constructor
EnterID::EnterID(SDL_Renderer *renderer, Screen *screen, Asset *asset, options_t *options) EnterID::EnterID(SDL_Renderer *renderer, Screen *screen, Asset *asset, options_t *options)
@@ -16,11 +17,31 @@ EnterID::EnterID(SDL_Renderer *renderer, Screen *screen, Asset *asset, options_t
texture = new Texture(renderer, asset->get("smb2.png")); texture = new Texture(renderer, asset->get("smb2.png"));
text = new Text(asset->get("smb2.txt"), texture, renderer); 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 // Inicializa variables
counter = 0; counter = 0;
section.name = SECTION_PROG_ENTER_ID; section.name = SECTION_PROG_ENTER_ID;
ticks = 0; ticks = 0;
ticksSpeed = 15; ticksSpeed = 15;
pos = 0;
name[pos] = 0;
maxLenght = 15;
// Escribe el texto en la textura
fillTexture();
// Deja el texto a tamaño fijo
// text->setFixedWidth(true);
} }
// Destructor // Destructor
@@ -62,33 +83,77 @@ void EnterID::checkEventHandler()
// Comprueba las teclas que se han pulsado // Comprueba las teclas que se han pulsado
if ((eventHandler->type == SDL_KEYDOWN && eventHandler->key.repeat == 0) || (eventHandler->type == SDL_JOYBUTTONDOWN)) if ((eventHandler->type == SDL_KEYDOWN && eventHandler->key.repeat == 0) || (eventHandler->type == SDL_JOYBUTTONDOWN))
{ {
switch (eventHandler->key.keysym.scancode)
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_RETURN)
{
options->online.jailerID = (std::string)name;
section.name = SECTION_PROG_INTRO;
break;
}
else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_ESCAPE)
{ {
case SDL_SCANCODE_ESCAPE:
section.name = SECTION_PROG_QUIT; section.name = SECTION_PROG_QUIT;
break; break;
}
case SDL_SCANCODE_F1: else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_F1)
{
screen->setWindowSize(1); screen->setWindowSize(1);
break; break;
}
case SDL_SCANCODE_F2: else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_F2)
{
screen->setWindowSize(2); screen->setWindowSize(2);
break; break;
}
case SDL_SCANCODE_F3: else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_F3)
{
screen->setWindowSize(3); screen->setWindowSize(3);
break; break;
}
case SDL_SCANCODE_F4: else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_F4)
{
screen->setWindowSize(4); screen->setWindowSize(4);
break; break;
}
case SDL_SCANCODE_F5: else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_F5)
// switchPalette(); {
break; switchPalette();
default:
break; break;
} }
} }
@@ -110,11 +175,8 @@ void EnterID::update()
// Actualiza el contador // Actualiza el contador
counter++; counter++;
// Comprueba el contador // Actualiza el cursor
if (counter > 200) cursor = (counter % 20 >= 10) ? " " : "_";
{
section.name = SECTION_PROG_INTRO;
}
} }
} }
@@ -127,9 +189,77 @@ void EnterID::render()
// Limpia la pantalla // Limpia la pantalla
screen->clean(); screen->clean();
// Escribe texto // Dibuja la textura con el texto en pantalla
text->writeCentered(GAMECANVAS_CENTER_X, GAMECANVAS_CENTER_Y, "SECCION ENTER_ID"); 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->writeCentered(GAMECANVAS_CENTER_X, 97, jailerID);
text->writeDX(TXT_CENTER | TXT_COLOR, GAMECANVAS_CENTER_X, 97, jailerID,1,color);
// Vuelca el contenido del renderizador en pantalla // Vuelca el contenido del renderizador en pantalla
screen->blit(); 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({"ONLINE CONFIGURATION:", stringToColor(options->palette, "red")});
texts.push_back({"", stringToColor(options->palette, "white")});
texts.push_back({"YOU HAVE NOT SPECIFIED ANY ID", stringToColor(options->palette, "white")});
texts.push_back({"FOR THE ONLINE SERVICE", stringToColor(options->palette, "white")});
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({"JAILER_ID:", stringToColor(options->palette, "red")});
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({"", 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")});
}
// 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);
}
// Cambia la paleta
void EnterID::switchPalette()
{
options->palette = options->palette == p_zxspectrum ? p_zxarne : p_zxspectrum;
fillTexture();
}

View File

@@ -14,12 +14,19 @@
class EnterID class EnterID
{ {
private: private:
struct captions_t
{
std::string label; // Texto a escribir
color_t color; // Color del texto
};
// Punteros y objetos // Punteros y objetos
Asset *asset; // Objeto con los ficheros de recursos Asset *asset; // Objeto con los ficheros de recursos
options_t *options; // Puntero a las opciones del juego options_t *options; // Puntero a las opciones del juego
Screen *screen; // Objeto encargado de dibujar en pantalla Screen *screen; // Objeto encargado de dibujar en pantalla
SDL_Event *eventHandler; // Manejador de eventos SDL_Event *eventHandler; // Manejador de eventos
SDL_Renderer *renderer; // El renderizador de la ventana SDL_Renderer *renderer; // El renderizador de la ventana
SDL_Texture *textTexture; // Textura para dibujar el texto
Text *text; // Objeto para escribir texto en pantalla Text *text; // Objeto para escribir texto en pantalla
Texture *texture; // Textura para la fuente para el texto Texture *texture; // Textura para la fuente para el texto
@@ -28,6 +35,12 @@ private:
section_t section; // Estado del bucle principal para saber si continua o se sale section_t section; // Estado del bucle principal para saber si continua o se sale
Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa
Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa
std::vector<captions_t> texts; // Vector con los textos
std::string cursor; // Contiene el caracter que se muestra como cursor
char name[15];
int pos;
int maxLenght; // Tamaño máximo del jailerID
// Actualiza las variables // Actualiza las variables
void update(); void update();
@@ -38,6 +51,15 @@ private:
// Comprueba el manejador de eventos // Comprueba el manejador de eventos
void checkEventHandler(); void checkEventHandler();
// Inicializa los textos
void iniTexts();
// Escribe el texto en la textura
void fillTexture();
// Cambia la paleta
void switchPalette();
public: public:
// Constructor // Constructor
EnterID(SDL_Renderer *renderer, Screen *screen, Asset *asset, options_t *options); EnterID(SDL_Renderer *renderer, Screen *screen, Asset *asset, options_t *options);