#include "const.h" #include "text.h" // Constructor Text::Text(LTexture *texture, SDL_Renderer *renderer) { mSprite = new Sprite(); mSprite->setTexture(texture); mSprite->setRenderer(renderer); } // Destructor Text::~Text() { delete mSprite; mSprite = nullptr; } // en el constructor se le pasa la ruta del fichero de descripcion // el fichero tendra el alto y ancho del cuadrado donde esta cada letra // y el listado de anchos de cada una // el init ya no necesita type ni size // Inicializador void Text::init(Uint8 type, Uint8 size) { // Inicializa variables mType = type; mSize = size; // Inicia los valores del sprite que dibuja las letras mSprite->setWidth(size); mSprite->setHeight(size); mSprite->setPosX(0); mSprite->setPosY(0); mSprite->setSpriteClip(0, 0, mSprite->getWidth(), mSprite->getHeight()); // Cadena con los caracteres ascii que se van a inicializar const std::string text = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ{\\[]]^_`abcdefghijklmnopqrstuvwxyz"; // Inicializa a cero el vector con las coordenadas for (int i = 0; i < 128; 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 (int 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(); if (type == TEXT_FIXED) mOffset[int(text[i])].w = size; } // Establece las coordenadas de forma manual para la ñ mOffset[int('^')].x = mOffset[int('z')].x + size; mOffset[int('^')].y = mOffset[int('z')].y; mOffset[int('^')].w = size; // Establece las coordenadas de forma manual para la ñç mOffset[int('~')].x = mOffset[int('z')].x + size * 2; mOffset[int('~')].y = mOffset[int('z')].y; mOffset[int('~')].w = size; // Establece el ancho de cada caracter if (type == TEXT_VARIABLE) { mOffset[int(' ')].w = 7; mOffset[int('-')].w = 6; mOffset[int('/')].w = 5; mOffset[int('(')].w = 5; mOffset[int(')')].w = 5; mOffset[int('.')].w = 4; mOffset[int(':')].w = 4; mOffset[int('#')].w = 7; mOffset[int('!')].w = 7; mOffset[int('\'')].w = 3; mOffset[int('0')].w = 7; mOffset[int('1')].w = 5; mOffset[int('2')].w = 7; mOffset[int('3')].w = 7; mOffset[int('4')].w = 7; mOffset[int('5')].w = 7; mOffset[int('6')].w = 7; mOffset[int('7')].w = 7; mOffset[int('8')].w = 7; mOffset[int('9')].w = 7; mOffset[int('A')].w = 7; mOffset[int('B')].w = 7; mOffset[int('C')].w = 7; mOffset[int('D')].w = 7; mOffset[int('E')].w = 7; mOffset[int('F')].w = 7; mOffset[int('G')].w = 7; mOffset[int('H')].w = 7; mOffset[int('I')].w = 4; mOffset[int('J')].w = 6; mOffset[int('K')].w = 8; mOffset[int('L')].w = 6; mOffset[int('M')].w = 9; mOffset[int('N')].w = 8; mOffset[int('O')].w = 8; mOffset[int('P')].w = 7; mOffset[int('Q')].w = 8; mOffset[int('R')].w = 7; mOffset[int('S')].w = 6; mOffset[int('T')].w = 8; mOffset[int('U')].w = 7; mOffset[int('V')].w = 8; mOffset[int('W')].w = 9; mOffset[int('X')].w = 8; mOffset[int('Y')].w = 8; mOffset[int('Z')].w = 7; mOffset[int('^')].w = 7; mOffset[int('~')].w = 7; mOffset[int('a')].w = 7; mOffset[int('b')].w = 7; mOffset[int('c')].w = 6; mOffset[int('d')].w = 7; mOffset[int('e')].w = 7; mOffset[int('f')].w = 5; mOffset[int('g')].w = 7; mOffset[int('h')].w = 7; mOffset[int('i')].w = 4; mOffset[int('j')].w = 5; mOffset[int('k')].w = 7; mOffset[int('l')].w = 4; mOffset[int('m')].w = 10; mOffset[int('n')].w = 7; mOffset[int('o')].w = 7; mOffset[int('p')].w = 7; mOffset[int('q')].w = 7; mOffset[int('r')].w = 6; mOffset[int('s')].w = 6; mOffset[int('t')].w = 5; mOffset[int('u')].w = 7; mOffset[int('v')].w = 7; mOffset[int('w')].w = 9; mOffset[int('x')].w = 7; mOffset[int('y')].w = 7; mOffset[int('z')].w = 7; } } // Escribe texto en pantalla void Text::write(int x, int y, std::string text, int kerning, int lenght) { Uint16 shift = 0; if (lenght == -1) lenght = text.length(); for (int i = 0; i < lenght; ++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 el texto con colores void Text::writeColored(int x, int y, std::string text, color_t color, int kerning, int lenght) { mSprite->getTexture()->setColor(color.r, color.g, color.b); write(x, y, text, kerning, lenght); mSprite->getTexture()->setColor(255, 255, 255); } // Escribe el texto con sombra void Text::writeShadowed(int x, int y, std::string text, color_t color, Uint8 shadowDistance, int kerning, int lenght) { mSprite->getTexture()->setColor(color.r, color.g, color.b); write(x + shadowDistance, y + shadowDistance, text, kerning, lenght); mSprite->getTexture()->setColor(255, 255, 255); write(x, y, text, kerning, lenght); } // Escribe el texto centrado en un punto x void Text::writeCentered(int x, int y, std::string text, int kerning, int lenght) { x -= (Text::lenght(text, kerning) / 2); write(x, y, text, kerning, lenght); } // Escribe texto con extras void Text::writeDX(Uint8 flags, int x, int y, std::string text, int kerning, color_t textColor, Uint8 shadowDistance, color_t shadowColor, int lenght) { const bool centered = ((flags & TXT_CENTER) == TXT_CENTER); const bool shadowed = ((flags & TXT_SHADOW) == TXT_SHADOW); const bool colored = ((flags & TXT_COLOR) == TXT_COLOR); if (centered) x -= (Text::lenght(text, kerning) / 2); if (shadowed) writeColored(x + shadowDistance, y + shadowDistance, text, shadowColor, kerning, lenght); if (colored) writeColored(x, y, text, textColor, kerning, lenght); else write(x, y, text, kerning, lenght); } // Obtiene la longitud en pixels de una cadena Uint16 Text::lenght(std::string text, int kerning) { Uint16 shift = 0; for (int 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 mSize; } // Establece el valor de la variable void Text::setSize(Uint8 size) { mSize = size; }