diff --git a/source/common/text.cpp b/source/common/text.cpp index 63f858f..e420e43 100644 --- a/source/common/text.cpp +++ b/source/common/text.cpp @@ -94,6 +94,9 @@ Text::Text(std::string textFile, Texture *texture, SDL_Renderer *renderer) // Crea los objetos sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture, renderer); + + // Inicializa variables + fixedWidth = false; } // Constructor @@ -111,6 +114,9 @@ Text::Text(textFile_t *textFile, Texture *texture, SDL_Renderer *renderer) // Crea los objetos sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture, renderer); + + // Inicializa variables + fixedWidth = false; } // Destructor @@ -133,7 +139,8 @@ void Text::write(int x, int y, std::string text, int kerning, int lenght) sprite->setPosX(x + shift); sprite->setPosY(y); 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); } } @@ -225,4 +232,10 @@ int Text::getCharacterSize() void Text::reLoadTexture() { sprite->getTexture()->reLoad(); +} + +// Establece si se usa un tamaño fijo de letra +void Text::setFixedWidth(bool value) +{ + fixedWidth = value; } \ No newline at end of file diff --git a/source/common/text.h b/source/common/text.h index b6703b2..f1ae260 100644 --- a/source/common/text.h +++ b/source/common/text.h @@ -33,11 +33,12 @@ class Text { private: // Objetos y punteros - Sprite *sprite; // Objeto con los graficos para el texto + Sprite *sprite; // Objeto con los graficos para el texto // Variables int boxWidth; // Anchura 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 public: @@ -71,6 +72,9 @@ public: // Recarga la textura void reLoadTexture(); + + // Establece si se usa un tamaño fijo de letra + void setFixedWidth(bool value); }; #endif diff --git a/source/credits.cpp b/source/credits.cpp index 69a4d4f..8a4e543 100644 --- a/source/credits.cpp +++ b/source/credits.cpp @@ -318,14 +318,6 @@ section_t Credits::run() // Cambia la paleta void Credits::switchPalette() { - if (options->palette == p_zxspectrum) - { - options->palette = p_zxarne; - } - else - { - options->palette = p_zxspectrum; - } - - fillTexture(); + options->palette = options->palette == p_zxspectrum ? p_zxarne : p_zxspectrum; + fillTexture(); } \ No newline at end of file diff --git a/source/enter_id.cpp b/source/enter_id.cpp index ddc0e04..6042e2d 100644 --- a/source/enter_id.cpp +++ b/source/enter_id.cpp @@ -1,6 +1,7 @@ -#include "enter_id.h" -#include "const.h" #include "common/jail_audio.h" +#include "const.h" +#include "enter_id.h" +#include // Constructor 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")); 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 counter = 0; section.name = SECTION_PROG_ENTER_ID; ticks = 0; 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 @@ -62,33 +83,77 @@ void EnterID::checkEventHandler() // Comprueba las teclas que se han pulsado 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; break; + } - case SDL_SCANCODE_F1: + else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_F1) + { screen->setWindowSize(1); break; + } - case SDL_SCANCODE_F2: + else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_F2) + { screen->setWindowSize(2); break; + } - case SDL_SCANCODE_F3: + else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_F3) + { screen->setWindowSize(3); break; + } - case SDL_SCANCODE_F4: + else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_F4) + { screen->setWindowSize(4); break; + } - case SDL_SCANCODE_F5: - // switchPalette(); - break; - - default: + else if (eventHandler->key.keysym.scancode == SDL_SCANCODE_F5) + { + switchPalette(); break; } } @@ -110,11 +175,8 @@ void EnterID::update() // Actualiza el contador counter++; - // Comprueba el contador - if (counter > 200) - { - section.name = SECTION_PROG_INTRO; - } + // Actualiza el cursor + cursor = (counter % 20 >= 10) ? " " : "_"; } } @@ -127,9 +189,77 @@ void EnterID::render() // Limpia la pantalla screen->clean(); - // Escribe texto - text->writeCentered(GAMECANVAS_CENTER_X, GAMECANVAS_CENTER_Y, "SECCION ENTER_ID"); + // 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->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 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(); } \ No newline at end of file diff --git a/source/enter_id.h b/source/enter_id.h index 0eada85..ec6018f 100644 --- a/source/enter_id.h +++ b/source/enter_id.h @@ -14,20 +14,33 @@ class EnterID { private: + struct captions_t + { + std::string label; // Texto a escribir + color_t color; // Color del texto + }; + // Punteros y objetos - Asset *asset; // Objeto con los ficheros de recursos - options_t *options; // Puntero a las opciones del juego - Screen *screen; // Objeto encargado de dibujar en pantalla - SDL_Event *eventHandler; // Manejador de eventos - SDL_Renderer *renderer; // El renderizador de la ventana - Text *text; // Objeto para escribir texto en pantalla - Texture *texture; // Textura para la fuente para el texto + Asset *asset; // Objeto con los ficheros de recursos + options_t *options; // Puntero a las opciones del juego + Screen *screen; // Objeto encargado de dibujar en pantalla + SDL_Event *eventHandler; // Manejador de eventos + SDL_Renderer *renderer; // El renderizador de la ventana + SDL_Texture *textTexture; // Textura para dibujar el texto + Text *text; // Objeto para escribir texto en pantalla + Texture *texture; // Textura para la fuente para el texto // Variables - int counter; // Contador - 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 ticksSpeed; // Velocidad a la que se repiten los bucles del programa + int counter; // Contador + 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 ticksSpeed; // Velocidad a la que se repiten los bucles del programa + std::vector 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 void update(); @@ -38,6 +51,15 @@ private: // Comprueba el manejador de eventos void checkEventHandler(); + // Inicializa los textos + void iniTexts(); + + // Escribe el texto en la textura + void fillTexture(); + + // Cambia la paleta + void switchPalette(); + public: // Constructor EnterID(SDL_Renderer *renderer, Screen *screen, Asset *asset, options_t *options);