#include "text.h" // Constructor Text::Text() { mSprite = new Sprite(); init(nullptr, nullptr, 0, 0); } // Destructor Text::~Text() { delete mSprite; } // Inicializador void Text::init(LTexture *texture, SDL_Renderer *renderer, Uint8 height) { // Inicializa variables mHeight = height; // Inicia los valores del sprite que dibuja las letras mSprite->setWidth(mHeight); mSprite->setHeight(mHeight); mSprite->setPosX(0); mSprite->setPosY(0); mSprite->setTexture(texture); mSprite->setRenderer(renderer); mSprite->setSpriteClip(0, 0, mSprite->getWidth(), mSprite->getHeight()); // Cadena con los caracteres ascii que se van a inicializar std::string text = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ{\\[]]^_`abcdefghijklmnopqrstuvwxyz"; // Inicializa a cero el vector con las coordenadas for (Uint8 i = 0; i < 255; i++) { mOffset[i].x = 0; mOffset[i].y = 0; mOffset[i].w = 0; } // Establece las coordenadas para cada caracter ascii de la cadena y su ancho for (Uint8 i = 0; i < text.length(); ++i) { mOffset[int(text[i])].x = ((int(text[i]) - 32) % 15) * mSprite->getWidth(); mOffset[int(text[i])].y = ((int(text[i]) - 32) / 15) * mSprite->getHeight(); mOffset[int(text[i])].w = mHeight; mOffset[int(text[i])].h = mHeight; } } // Escribe el texto en pantalla con kerning void Text::write(int x, int y, std::string text, int kerning) { Uint16 shift = 0; for (Uint8 i = 0; i < text.length(); ++i) { mSprite->setSpriteClip(mOffset[int(text[i])].x, mOffset[int(text[i])].y, mSprite->getWidth(), mSprite->getHeight()); mSprite->setPosX(x + shift); mSprite->setPosY(y); mSprite->render(); shift += (mOffset[int(text[i])].w + kerning); } } // Escribe una cantidad determinada de caracteres del texto en pantalla void Text::write(int x, int y, std::string text, Sint8 kerning, Uint8 lenght) { Uint16 shift = 0; for (Uint8 i = 0; i < lenght; ++i) { if (text[i] == '*') { shift = 0; y += mHeight; } else { mSprite->setSpriteClip(mOffset[int(text[i])].x, mOffset[int(text[i])].y, mSprite->getWidth(), mSprite->getHeight()); mSprite->setPosX(x + shift); mSprite->setPosY(y); mSprite->render(); shift += (mOffset[int(text[i])].w + kerning); } } } // Escribe el texto con colores void Text::writeColored(int x, int y, std::string text, Uint8 R, Uint8 G, Uint8 B) { mSprite->getTexture()->setColor(R, G, B); write(x, y, text); mSprite->getTexture()->setColor(255, 255, 255); } // Escribe el texto centrado en un punto x y con kerning void Text::writeCentered(int x, int y, std::string text, int kerning) { // Uint16 lenght = Text::lenght(text, kerning); x = x - (Text::lenght(text, kerning) / 2); Uint16 shift = 0; for (Uint8 i = 0; i < text.length(); ++i) { mSprite->setSpriteClip(mOffset[int(text[i])].x, mOffset[int(text[i])].y, mSprite->getWidth(), mSprite->getHeight()); mSprite->setPosX(x + shift); mSprite->setPosY(y); mSprite->render(); shift += (mOffset[int(text[i])].w + kerning); } } // Obtiene la longitud en pixels de una cadena Uint16 Text::lenght(std::string text, int kerning) { Uint16 shift = 0; for (Uint8 i = 0; i < text.length(); ++i) { shift += (mOffset[int(text[i])].w + kerning); } return shift; } // Obtiene el valor de la variable Uint8 Text::getType() { return mType; } // Establece el valor de la variable void Text::setType(Uint8 type) { mType = type; } // Obtiene el valor de la variable Uint8 Text::getSize() { return mHeight; }height Establece el valor de la variable void Text::setSize(Uint8 size) { mHeight = height; }