forked from jaildesigner-jailgames/coffee_crisis
release v1.3 beta 1
This commit is contained in:
244
source/text.cpp
Normal file
244
source/text.cpp
Normal file
@@ -0,0 +1,244 @@
|
||||
#include "const.h"
|
||||
#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 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->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 = " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-/().:#!";
|
||||
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();
|
||||
if (type == TEXT_FIXED)
|
||||
{
|
||||
mOffset[int(text[i])].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('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 el texto en pantalla
|
||||
void Text::write(int x, int y, std::string text)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// 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, int kerning, Uint8 lenght)
|
||||
{
|
||||
Uint16 shift = 0;
|
||||
for (Uint8 i = 0; i < lenght; ++i)
|
||||
{
|
||||
if (text[i] == '*')
|
||||
{
|
||||
shift = 0;
|
||||
y += mSize;
|
||||
}
|
||||
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 mSize;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void Text::setSize(Uint8 size)
|
||||
{
|
||||
mSize = size;
|
||||
}
|
||||
Reference in New Issue
Block a user