working on new font engine

This commit is contained in:
2021-08-26 13:59:25 +02:00
parent 5768c1f7a2
commit e93aba2522
11 changed files with 160 additions and 176 deletions

View File

@@ -1,12 +1,16 @@
#include "const.h"
#include "text.h"
#include <iostream>
#include <fstream>
//#include <cstdlib>
// Constructor
Text::Text(LTexture *texture, SDL_Renderer *renderer)
Text::Text(std::string file, LTexture *texture, SDL_Renderer *renderer)
{
mSprite = new Sprite();
mSprite->setTexture(texture);
mSprite->setRenderer(renderer);
mFile = file;
}
// Destructor
@@ -21,24 +25,9 @@ Text::~Text()
// y el listado de anchos de cada una
// el init ya no necesita type ni size
// Inicializador
void Text::init(Uint8 type, Uint8 size)
void Text::init()
{
// 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++)
{
@@ -47,106 +36,21 @@ void Text::init(Uint8 type, Uint8 size)
mOffset[i].w = 0;
}
// Carga los offsets desde el fichero
initOffsetFromFile();
// Inicia los valores del sprite que dibuja las letras
mSprite->setWidth(mBoxWidth);
mSprite->setHeight(mBoxHeight);
mSprite->setPosX(0);
mSprite->setPosY(0);
mSprite->setSpriteClip(0, 0, mSprite->getWidth(), mSprite->getHeight());
// Establece las coordenadas para cada caracter ascii de la cadena y su ancho
for (int i = 0; i < text.length(); ++i)
for (int i = 32; i < 128; 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;
mOffset[i].x = ((i - 32) % 16) * mBoxWidth;
mOffset[i].y = ((i - 32) / 16) * mBoxHeight;
}
}
@@ -222,26 +126,41 @@ Uint16 Text::lenght(std::string text, int kerning)
return shift;
}
// Obtiene el valor de la variable
Uint8 Text::getType()
// Inicializa el vector de offsets desde un fichero
void Text::initOffsetFromFile()
{
return mType;
std::ifstream rfile(mFile);
if (rfile.is_open() && rfile.good())
{
std::string buffer;
// Lee los dos primeros valores del fichero
std::getline(rfile, buffer);
std::getline(rfile, buffer);
mBoxWidth = std::stoi(buffer);
std::getline(rfile, buffer);
std::getline(rfile, buffer);
mBoxHeight = std::stoi(buffer);
// lee el resto de datos del fichero
int index = 32;
int line_read = 0;
while (std::getline(rfile, buffer))
{
// Almacena solo las lineas impares
if (line_read % 2 == 1)
mOffset[index++].w = std::stoi(buffer);
//Then clear the buffer once you're done with it.
buffer.clear();
line_read++;
};
}
}
// Establece el valor de la variable
void Text::setType(Uint8 type)
// Devuelve el valor de la variable
Uint8 Text::getCharacterWidth()
{
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;
return mBoxWidth;
}