Actualizada la libreria text.h a la última versión

This commit is contained in:
2022-08-08 18:37:42 +02:00
parent 2b2b822540
commit 386039336e
5 changed files with 169 additions and 303 deletions

View File

@@ -6,7 +6,6 @@
#include "player.h"
#include "map.h"
#include "text.h"
#include "text2.h"
#include "menu.h"
#include "const.h"
#include "jail_audio.h"

View File

@@ -1,84 +1,65 @@
#include "const.h"
#include "text.h"
#include <iostream>
#include <fstream>
Text::Text()
// Constructor
Text::Text(std::string file, LTexture *texture, SDL_Renderer *renderer)
{
mSprite = new Sprite();
init(nullptr, nullptr, 0);
SDL_Rect rect = {0,0,0,0};
mSprite = new Sprite(rect, texture, renderer);
mSprite->setTexture(texture);
mSprite->setRenderer(renderer);
mFile = file;
init();
}
// Destructor
Text::~Text()
{
delete mSprite;
mSprite = nullptr;
}
void Text::init(LTexture *texture, SDL_Renderer *renderer, Uint8 height)
// Inicializador
void Text::init()
{
// Inicializa variables
mHeight = height;
// Inicia los valores del sprite que dibuja las letras
mSprite->setWidth(mHeight);
mSprite->setHeight(mHeight);
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";
// Inicializa a cero el vector con las coordenadas
for (Uint8 i = 0; i < 255; i++)
for (int i = 0; i < 128; i++)
{
mOffset[i].x = 0;
mOffset[i].y = 0;
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 (Uint8 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();
mOffset[int(text[i])].w = mHeight;
mOffset[int(text[i])].h = mHeight;
mOffset[i].x = ((i - 32) % 15) * mBoxWidth;
mOffset[i].y = ((i - 32) / 15) * mBoxHeight;
}
}
void Text::write(int x, int y, std::string text, Sint8 kerning, Uint8 lenght)
// Escribe texto en pantalla
void Text::write(int x, int y, std::string text, int kerning, int lenght)
{
Uint16 shift = 0;
std::string text2;
if (lenght > 0)
text2 = text.substr(0, lenght);
else
text2 = text;
for (Uint8 i = 0; i < text2.length(); ++i)
{
mSprite->setSpriteClip(mOffset[int(text2[i])].x, mOffset[int(text2[i])].y, mSprite->getWidth(), mSprite->getHeight());
mSprite->setPosX(x + shift);
mSprite->setPosY(y);
mSprite->render();
shift += (mOffset[int(text2[i])].w + kerning);
}
}
if (lenght == -1)
lenght = text.length();
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);
}
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)
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);
@@ -88,22 +69,110 @@ void Text::writeCentered(int x, int y, std::string text, int 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);
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)
{
writeColored(x + shadowDistance, y + shadowDistance, text, shadowColor, kerning, lenght);
writeColored(x - shadowDistance, y + shadowDistance, text, shadowColor, kerning, lenght);
writeColored(x + shadowDistance, y - shadowDistance, text, shadowColor, kerning, lenght);
writeColored(x - shadowDistance, y - shadowDistance, text, shadowColor, kerning, lenght);
writeColored(x, y + shadowDistance, text, shadowColor, kerning, lenght);
writeColored(x, y - shadowDistance, text, shadowColor, kerning, lenght);
writeColored(x + shadowDistance, y, text, shadowColor, kerning, lenght);
writeColored(x - shadowDistance, y, 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 < (int)text.length(); ++i)
shift += (mOffset[int(text[i])].w + kerning);
}
return shift;
}
Uint8 Text::getHeight()
// Inicializa el vector de offsets desde un fichero
void Text::initOffsetFromFile()
{
return mHeight;
std::ifstream rfile(mFile);
if (rfile.is_open() && rfile.good())
{
std::string buffer;
//printf("Reading %s file\n", mFile.c_str());
// 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);
// Limpia el buffer
buffer.clear();
line_read++;
};
}
}
void Text::setHeight(Uint8 size)
// Devuelve el valor de la variable
Uint8 Text::getCharacterWidth()
{
mHeight = size;
return mBoxWidth;
}

View File

@@ -1,58 +1,66 @@
#pragma once
#include "sprite.h"
#include "utils.h"
#ifndef TEXT_H
#define TEXT_H
#define TXT_COLOR 1
#define TXT_SHADOW 2
#define TXT_CENTER 4
#define TXT_STROKE 8
// Clase texto. Pinta texto en pantalla a partir de un bitmap
class Text
{
private:
Sprite *mSprite; // Objeto con los graficos para el texto
struct Offset
{
int x;
int y;
Uint8 w;
};
Offset mOffset[128]; // Vector con las posiciones y ancho de cada letra
Uint8 mBoxWidth; // Anchura de la caja de cada caracter en el png
Uint8 mBoxHeight; // Altura de la caja de cada caracter en el png
std::string mFile; // Fichero con los descriptores de la fuente
// Inicializa el vector de offsets desde un fichero
void initOffsetFromFile();
public:
// Constructor
Text();
Text(std::string file, LTexture *texture, SDL_Renderer *renderer);
// Destructor
~Text();
// Inicializador
void init(LTexture *texture, SDL_Renderer *renderer, Uint8 height);
void init();
/**
* \brief Escribe texto en pantalla
*
* \param x Posición en el eje X donde empezar a escribir el texto
* \param y Posición en el eje Y donde empezar a escribir el texto
* \param string Texto para escribir
* \param kerning Espacio de separación entre letras
* \param lenght Longitud de la cadena de texto a escribir
*/
void write(int x, int y, std::string text, Sint8 kerning = 0, Uint8 lenght = 0);
// Escribe el texto en pantalla
void write(int x, int y, std::string text, int kerning = 1, int lenght = -1);
// Escribe el texto con colores
void writeColored(int x, int y, std::string text, Uint8 R, Uint8 G, Uint8 B);
void writeColored(int x, int y, std::string text, color_t color, int kerning = 1, int lenght = -1);
// Escribe el texto con sombra
void writeShadowed(int x, int y, std::string text, color_t color, Uint8 shadowDistance = 1, int kerning = 1, int lenght = -1);
// Escribe el texto centrado en un punto x y con kerning
void writeCentered(int x, int y, std::string text, int kerning);
void writeCentered(int x, int y, std::string text, int kerning = 1, int lenght = -1);
// Escribe texto con extras
void writeDX(Uint8 flags, int x, int y, std::string text, int kerning = 1, color_t textColor = {255, 255, 255}, Uint8 shadowDistance = 1, color_t shadowColor = {0, 0, 0}, int lenght = -1);
// Obtiene la longitud en pixels de una cadena
Uint16 lenght(std::string text, int kerning);
Uint16 lenght(std::string text, int kerning = 1);
// Obtiene el valor de la variable
Uint8 getType();
// Establece el valor de la variable
void setType(Uint8 type);
// Obtiene el valor de la variable
Uint8 getHeight();
// Establece el valor de la variable
void setHeight(Uint8 size);
private:
Sprite *mSprite; // Objeto con los graficos para el texto
SDL_Rect mOffset[255]; // Vector con las posiciones y ancho de cada letra
Uint8 mHeight; // Altura del texto
// Devuelve el valor de la variable
Uint8 getCharacterWidth();
};
#endif

View File

@@ -1,135 +0,0 @@
#include "const.h"
#include "text2.h"
Text2::Text2()
{
init(nullptr, nullptr, 0);
}
Text2::~Text2()
{
init(nullptr, nullptr, 0);
}
void Text2::init(LTexture *texture, SDL_Renderer *renderer, Uint8 height)
{
Text::init(texture, renderer, height);
mPosX = 0;
mPosY = 0;
mKerning = 0;
mCaption = "";
mSpeed = 0;
mCounter = 0;
mIndex = 0;
mLenght = 0;
mCompleted = false;
mEnabled = false;
mEnabledCounter = 0;
mId = -1;
}
void Text2::setPosX(int value)
{
mPosX = value;
}
void Text2::setPosY(int value)
{
mPosY = value;
}
void Text2::setKerning(int value)
{
mKerning = value;
}
void Text2::setCaption(std::string text)
{
mCaption = text;
mLenght = text.length();
}
void Text2::setSpeed(Uint16 value)
{
mSpeed = value;
mCounter = value;
}
void Text2::setEnabled(bool value)
{
mEnabled = value;
}
bool Text2::IsEnabled()
{
return mEnabled;
}
void Text2::setEnabledCounter(Uint16 value)
{
mEnabledCounter = value;
}
Uint16 Text2::getEnabledCounter()
{
return mEnabledCounter;
}
void Text2::update()
{
if (mEnabled)
{
if (mCompleted == false)
{
if (mCounter > 0)
{
--mCounter;
}
if (mCounter == 0)
{
++mIndex;
mCounter = mSpeed;
}
if (mIndex == mLenght)
{
mCompleted = true;
}
}
if (mCompleted)
{
if (mEnabledCounter > 0)
{
--mEnabledCounter;
}
else if (mEnabledCounter == 0)
{
if (mId < 0)
{
mEnabled = false;
}
else
{
//mIntroEvents[mId] = EVENT_COMPLETED;
}
}
}
}
}
void Text2::render()
{
if (mEnabled)
{
Text::write(mPosX, mPosY, mCaption, mKerning, mIndex);
}
}
void Text2::center(int x)
{
setPosX(x - (lenght(mCaption, mKerning) / 2));
}
void Text2::setId(int id)
{
mId = id;
}

View File

@@ -1,75 +0,0 @@
#pragma once
#include "sprite.h"
#include "text.h"
#ifndef TEXT2_H
#define TEXT2_H
// Clase texto. Pinta texto en pantalla a partir de un bitmap
class Text2 : public Text
{
public:
// Constructor
Text2();
// Destructor
~Text2();
// Inicializador
void init(LTexture *texture, SDL_Renderer *renderer, Uint8 height);
// Establece el valor de la variable
void setPosX(int value);
// Establece el valor de la variable
void setPosY(int value);
// Establece el valor de la variable
void setKerning(int value);
// Establece el valor de la variable
void setCaption(std::string text);
// Establece el valor de la variable
void setSpeed(Uint16 value);
// Establece el valor de la variable
void setEnabled(bool value);
// Obtiene el valor de la variable
bool IsEnabled();
// Establece el valor de la variable
void setEnabledCounter(Uint16 value);
// Obtiene el valor de la variable
Uint16 getEnabledCounter();
// Actualiza todas las variables del objeto
void update();
// Dibuja el objeto en pantalla
void render();
// Centra la cadena de texto respecto a un punto X
void center(int x);
// Establece el valor de la variable mId
void setId(int id);
private:
bool mCompleted; // Indica si se ha escrito todo el texto
bool mEnabled; // Indica si el objeto está habilitado
int mId; // Identificador
int mKerning; // Espacio entre caracteres
int mPosX; // Posicion en el eje X donde empezar a escribir el texto
int mPosY; // Posicion en el eje Y donde empezar a escribir el texto
std::string mCaption; // Texto para escribir
Uint16 mCounter; // Temporizador de escritura para cada caracter
Uint16 mEnabledCounter; // Contador para deshabilitar el objeto
Uint16 mIndex; // Indice a la posición dentro de la cadena de texto que se está escribiendo
Uint16 mLenght; // Longitud de la cadena a escribir
Uint16 mSpeed; // Velocidad de escritura
};
#endif