Files
jail_engine/units/text.cpp

300 lines
6.6 KiB
C++

#include "text.h"
#include <iostream>
#include <fstream>
// Llena una estructuta textFile_t desde un fichero
textFile_t LoadTextFile(string file, bool verbose)
{
textFile_t tf;
// Inicializa a cero el vector con las coordenadas
for (int i = 0; i < 128; ++i)
{
tf.offset[i].x = 0;
tf.offset[i].y = 0;
tf.offset[i].w = 0;
}
// Abre el fichero para leer los valores
const string filename = file.substr(file.find_last_of("\\/") + 1).c_str();
ifstream rfile(file);
if (rfile.is_open() && rfile.good())
{
string buffer;
// Lee los dos primeros valores del fichero
getline(rfile, buffer);
getline(rfile, buffer);
tf.boxWidth = stoi(buffer);
getline(rfile, buffer);
getline(rfile, buffer);
tf.boxHeight = stoi(buffer);
// lee el resto de datos del fichero
int index = 32;
int line_read = 0;
while (getline(rfile, buffer))
{
// Almacena solo las lineas impares
if (line_read % 2 == 1)
{
tf.offset[index++].w = stoi(buffer);
}
// Limpia el buffer
buffer.clear();
line_read++;
};
// Cierra el fichero
if (verbose)
{
cout << "Text loaded: " << filename.c_str() << endl;
}
rfile.close();
}
// El fichero no se puede abrir
else
{
if (verbose)
{
cout << "Warning: Unable to open " << filename.c_str() << " file" << endl;
}
}
// Establece las coordenadas para cada caracter ascii de la cadena y su ancho
for (int i = 32; i < 128; ++i)
{
tf.offset[i].x = ((i - 32) % 15) * tf.boxWidth;
tf.offset[i].y = ((i - 32) / 15) * tf.boxHeight;
}
return tf;
}
// Constructor
Text::Text(string textFile, string bitmapFile, SDL_Renderer *renderer)
{
//std::cout << "Construido Text" << std::endl;
// Carga los offsets desde el fichero
textFile_t tf = LoadTextFile(textFile);
// Inicializa variables desde la estructura
boxHeight = tf.boxHeight;
boxWidth = tf.boxWidth;
for (int i = 0; i < 128; ++i)
{
offset[i].x = tf.offset[i].x;
offset[i].y = tf.offset[i].y;
offset[i].w = tf.offset[i].w;
}
// Crea los objetos
texture = new Texture(renderer, bitmapFile);
sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture, renderer);
// Inicializa variables
fixedWidth = false;
zoom = 1;
}
// Constructor
Text::Text(string textFile, Texture *texture, SDL_Renderer *renderer)
{
//std::cout << "Construido Text" << std::endl;
// Carga los offsets desde el fichero
textFile_t tf = LoadTextFile(textFile);
// Inicializa variables desde la estructura
boxHeight = tf.boxHeight;
boxWidth = tf.boxWidth;
for (int i = 0; i < 128; ++i)
{
offset[i].x = tf.offset[i].x;
offset[i].y = tf.offset[i].y;
offset[i].w = tf.offset[i].w;
}
// Crea los objetos
sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture, renderer);
// Inicializa variables
fixedWidth = false;
zoom = 1;
}
// Constructor
Text::Text(textFile_t *textFile, Texture *texture, SDL_Renderer *renderer)
{
//std::cout << "Construido Text" << std::endl;
// Inicializa variables desde la estructura
boxHeight = textFile->boxHeight;
boxWidth = textFile->boxWidth;
for (int i = 0; i < 128; ++i)
{
offset[i].x = textFile->offset[i].x;
offset[i].y = textFile->offset[i].y;
offset[i].w = textFile->offset[i].w;
}
// Crea los objetos
sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture, renderer);
// Inicializa variables
fixedWidth = false;
zoom = 1;
}
// Destructor
Text::~Text()
{
//std::cout << "Destruido Text" << std::endl;
if (sprite != nullptr)
{
delete sprite;
}
if (texture != nullptr)
{
delete texture;
}
}
// Escribe texto en pantalla
void Text::write(int x, int y, string text, int kerning, int lenght)
{
int shift = 0;
if (lenght == -1)
lenght = text.length();
for (int i = 0; i < lenght; ++i)
{
sprite->setSpriteClip(offset[int(text[i])].x, offset[int(text[i])].y, sprite->getWidth(), sprite->getHeight());
sprite->setPosX(x + shift);
sprite->setPosY(y);
sprite->render();
// shift += (offset[int(text[i])].w + kerning);
shift += fixedWidth ? boxWidth * zoom : (offset[int(text[i])].w + kerning) * zoom;
}
}
// Escribe el texto con colores
void Text::writeColored(int x, int y, string text, color_t color, int kerning, int lenght)
{
sprite->getTexture()->setColor(color.r, color.g, color.b);
write(x, y, text, kerning, lenght);
sprite->getTexture()->setColor(255, 255, 255);
}
// Escribe el texto con sombra
void Text::writeShadowed(int x, int y, string text, color_t color, Uint8 shadowDistance, int kerning, int lenght)
{
sprite->getTexture()->setColor(color.r, color.g, color.b);
write(x + shadowDistance, y + shadowDistance, text, kerning, lenght);
sprite->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, 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, 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);
const bool stroked = ((flags & TXT_STROKE) == TXT_STROKE);
if (centered)
{
x -= (Text::lenght(text, kerning) / 2);
}
if (shadowed)
{
writeColored(x + shadowDistance, y + shadowDistance, text, shadowColor, kerning, lenght);
}
if (stroked)
{
for (int dist = 1; dist <= shadowDistance; ++dist)
{
for (int dy = -dist; dy <= dist; ++dy)
{
for (int dx = -dist; dx <= dist; ++dx)
{
writeColored(x + dx, y + dy, 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
int Text::lenght(string text, int kerning)
{
int shift = 0;
for (int i = 0; i < (int)text.length(); ++i)
shift += (offset[int(text[i])].w + kerning) * zoom;
// Descuenta el kerning del último caracter
return shift - kerning;
}
// Devuelve el valor de la variable
int Text::getCharacterSize()
{
return boxWidth;
}
// Recarga la textura
void Text::reLoadTexture()
{
sprite->getTexture()->reLoad();
}
// Establece si se usa un tamaño fijo de letra
void Text::setFixedWidth(bool value)
{
fixedWidth = value;
}
// Establece el tamaño del zoom
void Text::setZoom(int value)
{
if (value < 1)
{
value = 1;
}
zoom = value;
sprite->setZoomW(zoom);
sprite->setZoomH(zoom);
}
// Quita el zoom
void Text::disableZoom()
{
zoom = 1;
sprite->setZoomW(zoom);
sprite->setZoomH(zoom);
}