working on class sections
This commit is contained in:
126
source/text.cpp
126
source/text.cpp
@@ -2,10 +2,11 @@
|
||||
#include "text.h"
|
||||
|
||||
// Constructor
|
||||
Text::Text()
|
||||
Text::Text(LTexture *texture, SDL_Renderer *renderer)
|
||||
{
|
||||
mSprite = new Sprite();
|
||||
init(nullptr, nullptr, 0, 0);
|
||||
mSprite->setTexture(texture);
|
||||
mSprite->setRenderer(renderer);
|
||||
}
|
||||
|
||||
// Destructor
|
||||
@@ -15,7 +16,7 @@ Text::~Text()
|
||||
}
|
||||
|
||||
// Inicializador
|
||||
void Text::init(LTexture *texture, SDL_Renderer *renderer, Uint8 type, Uint8 size)
|
||||
void Text::init(Uint8 type, Uint8 size)
|
||||
{
|
||||
// Inicializa variables
|
||||
mType = type;
|
||||
@@ -26,15 +27,13 @@ void Text::init(LTexture *texture, SDL_Renderer *renderer, Uint8 type, Uint8 siz
|
||||
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 = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ{\\[]]^_`abcdefghijklmnopqrstuvwxyz";
|
||||
const std::string text = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ{\\[]]^_`abcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
// Inicializa a cero el vector con las coordenadas
|
||||
for (Uint8 i = 0; i < 255; i++)
|
||||
for (int i = 0; i < 255; i++)
|
||||
{
|
||||
mOffset[i].x = 0;
|
||||
mOffset[i].y = 0;
|
||||
@@ -42,14 +41,12 @@ void Text::init(LTexture *texture, SDL_Renderer *renderer, Uint8 type, Uint8 siz
|
||||
}
|
||||
|
||||
// Establece las coordenadas para cada caracter ascii de la cadena y su ancho
|
||||
for (Uint8 i = 0; i < text.length(); ++i)
|
||||
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 el ancho de cada caracter
|
||||
@@ -133,25 +130,15 @@ void Text::init(LTexture *texture, SDL_Renderer *renderer, Uint8 type, Uint8 siz
|
||||
}
|
||||
}
|
||||
|
||||
// Escribe el texto en pantalla
|
||||
void Text::write(int x, int y, std::string text)
|
||||
// Escribe 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 < 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)
|
||||
if (lenght == 0)
|
||||
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);
|
||||
@@ -161,88 +148,57 @@ void Text::write(int x, int y, std::string text, int 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)
|
||||
void Text::writeColored(int x, int y, std::string text, color_t color, int kerning, Uint8 lenght)
|
||||
{
|
||||
mSprite->getTexture()->setColor(R, G, B);
|
||||
write(x, y, text);
|
||||
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, Uint8 R, Uint8 G, Uint8 B)
|
||||
void Text::writeShadowed(int x, int y, std::string text, color_t color, Uint8 shadowDistance, int kerning, Uint8 lenght)
|
||||
{
|
||||
mSprite->getTexture()->setColor(R, G, B);
|
||||
write(x+1, y+1, text);
|
||||
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);
|
||||
write(x, y, text, kerning, lenght);
|
||||
}
|
||||
|
||||
// Escribe el texto centrado en un punto x y con kerning
|
||||
void Text::writeCentered(int x, int y, std::string text, int kerning)
|
||||
// Escribe el texto centrado en un punto x
|
||||
void Text::writeCentered(int x, int y, std::string text, int kerning, Uint8 lenght)
|
||||
{
|
||||
// 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);
|
||||
}
|
||||
x -= (Text::lenght(text, kerning) / 2);
|
||||
write(x, y, text, kerning, lenght);
|
||||
}
|
||||
|
||||
// Escribe el texto centrado en un punto x y con kerning
|
||||
void Text::writeCenteredAndColored(int x, int y, std::string text, int kerning, Uint8 R, Uint8 G, Uint8 B)
|
||||
// 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, Uint8 lenght)
|
||||
{
|
||||
mSprite->getTexture()->setColor(R, G, B);
|
||||
const bool centered = ((flags & TXT_CENTER) == TXT_CENTER);
|
||||
const bool shadowed = ((flags & TXT_SHADOW) == TXT_SHADOW);
|
||||
const bool colored = ((flags & TXT_COLOR) == TXT_COLOR);
|
||||
|
||||
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);
|
||||
}
|
||||
if (centered)
|
||||
x -= (Text::lenght(text, kerning) / 2);
|
||||
|
||||
mSprite->getTexture()->setColor(255, 255, 255);
|
||||
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 (Uint8 i = 0; i < text.length(); ++i)
|
||||
{
|
||||
|
||||
for (int i = 0; i < text.length(); ++i)
|
||||
shift += (mOffset[int(text[i])].w + kerning);
|
||||
}
|
||||
|
||||
return shift;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user