Menudo puto lio de renamar coses, a vore si tot va quedant al lloc que els structs i els enums estan revolant i duplicats per tots llocs

This commit is contained in:
2024-10-11 20:12:50 +02:00
parent a9ca23138d
commit 3a6950f3a4
28 changed files with 445 additions and 454 deletions

View File

@@ -5,10 +5,10 @@
#include "texture.h" // for Texture
#include "utils.h" // for Color
// Llena una estructuta textFile_t desde un fichero
textFile_t LoadTextFile(std::string file)
// Llena una estructuta TextFile desde un fichero
TextFile LoadTextFile(std::string file_path)
{
textFile_t tf;
TextFile tf;
// Inicializa a cero el vector con las coordenadas
for (int i = 0; i < 128; ++i)
@@ -19,26 +19,26 @@ textFile_t LoadTextFile(std::string file)
}
// Abre el fichero para leer los valores
const std::string filename = file.substr(file.find_last_of("\\/") + 1).c_str();
std::ifstream rfile(file);
const std::string file_name = file_path.substr(file_path.find_last_of("\\/") + 1).c_str();
std::ifstream file(file_path);
if (rfile.is_open() && rfile.good())
if (file.is_open() && file.good())
{
std::string buffer;
// Lee los dos primeros valores del fichero
std::getline(rfile, buffer);
std::getline(rfile, buffer);
tf.boxWidth = std::stoi(buffer);
std::getline(file, buffer);
std::getline(file, buffer);
tf.box_width = std::stoi(buffer);
std::getline(rfile, buffer);
std::getline(rfile, buffer);
tf.boxHeight = std::stoi(buffer);
std::getline(file, buffer);
std::getline(file, buffer);
tf.box_height = std::stoi(buffer);
// lee el resto de datos del fichero
int index = 32;
int line_read = 0;
while (std::getline(rfile, buffer))
auto index = 32;
auto line_read = 0;
while (std::getline(file, buffer))
{
// Almacena solo las lineas impares
if (line_read % 2 == 1)
@@ -53,133 +53,133 @@ textFile_t LoadTextFile(std::string file)
// Cierra el fichero
#ifdef VERBOSE
std::cout << "Text loaded: " << filename.c_str() << std::endl;
std::cout << "Text loaded: " << file_name.c_str() << std::endl;
#endif
rfile.close();
file.close();
}
// El fichero no se puede abrir
else
{
#ifdef VERBOSE
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl;
std::cout << "Warning: Unable to open " << file_name.c_str() << " file" << std::endl;
#endif
}
// 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;
tf.offset[i].x = ((i - 32) % 15) * tf.box_width;
tf.offset[i].y = ((i - 32) / 15) * tf.box_height;
}
return tf;
}
// Constructor
Text::Text(std::string bitmapFile, std::string textFile, SDL_Renderer *renderer)
Text::Text(std::string bitmap_file, std::string text_file, SDL_Renderer *renderer)
{
// Carga los offsets desde el fichero
textFile_t tf = LoadTextFile(textFile);
auto tf = LoadTextFile(text_file);
// Inicializa variables desde la estructura
boxHeight = tf.boxHeight;
boxWidth = tf.boxWidth;
box_height_ = tf.box_height;
box_width_ = tf.box_width;
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;
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 = std::make_shared<Texture>(renderer, bitmapFile);
sprite = std::make_unique<Sprite>((SDL_Rect){0, 0, boxWidth, boxHeight}, texture);
texture_ = std::make_shared<Texture>(renderer, bitmap_file);
sprite_ = std::make_unique<Sprite>((SDL_Rect){0, 0, box_width_, box_height_}, texture_);
// Inicializa variables
fixedWidth = false;
fixed_width_ = false;
}
// Constructor
Text::Text(std::string textFile, std::shared_ptr<Texture> texture)
Text::Text(std::string text_file, std::shared_ptr<Texture> texture)
{
// Carga los offsets desde el fichero
textFile_t tf = LoadTextFile(textFile);
auto tf = LoadTextFile(text_file);
// Inicializa variables desde la estructura
boxHeight = tf.boxHeight;
boxWidth = tf.boxWidth;
box_height_ = tf.box_height;
box_width_ = tf.box_width;
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;
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 = std::make_unique<Sprite>((SDL_Rect){0, 0, boxWidth, boxHeight}, texture);
sprite_ = std::make_unique<Sprite>((SDL_Rect){0, 0, box_width_, box_height_}, texture);
// Inicializa variables
fixedWidth = false;
fixed_width_ = false;
}
// Constructor
Text::Text(textFile_t *textFile, std::shared_ptr<Texture> texture)
Text::Text(TextFile *text_file, std::shared_ptr<Texture> texture)
{
// Inicializa variables desde la estructura
boxHeight = textFile->boxHeight;
boxWidth = textFile->boxWidth;
box_height_ = text_file->box_height;
box_width_ = text_file->box_width;
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;
offset_[i].x = text_file->offset[i].x;
offset_[i].y = text_file->offset[i].y;
offset_[i].w = text_file->offset[i].w;
}
// Crea los objetos
sprite = std::make_unique<Sprite>((SDL_Rect){0, 0, boxWidth, boxHeight}, texture);
sprite_ = std::make_unique<Sprite>((SDL_Rect){0, 0, box_width_, box_height_}, texture);
// Inicializa variables
fixedWidth = false;
fixed_width_ = false;
}
// Escribe texto en pantalla
void Text::write(int x, int y, std::string text, int kerning, int lenght)
{
int shift = 0;
auto shift = 0;
if (lenght == -1)
{
lenght = text.length();
}
sprite->setPosY(y);
const int width = sprite->getWidth();
const int height = sprite->getHeight();
sprite_->setPosY(y);
const auto width = sprite_->getWidth();
const auto height = sprite_->getHeight();
for (int i = 0; i < lenght; ++i)
{
const int index = text[i];
sprite->setSpriteClip(offset[index].x, offset[index].y, width, height);
sprite->setPosX(x + shift);
sprite->render();
shift += fixedWidth ? boxWidth : (offset[int(text[i])].w + kerning);
const auto index = static_cast<int>(text[i]);
sprite_->setSpriteClip(offset_[index].x, offset_[index].y, width, height);
sprite_->setPosX(x + shift);
sprite_->render();
shift += fixed_width_ ? box_width_ : (offset_[int(text[i])].w + kerning);
}
}
// Escribe el texto con colores
void Text::writeColored(int x, int y, std::string text, Color color, int kerning, int lenght)
{
sprite->getTexture()->setColor(color.r, color.g, color.b);
sprite_->getTexture()->setColor(color.r, color.g, color.b);
write(x, y, text, kerning, lenght);
sprite->getTexture()->setColor(255, 255, 255);
sprite_->getTexture()->setColor(255, 255, 255);
}
// Escribe el texto con sombra
void Text::writeShadowed(int x, int y, std::string text, Color color, Uint8 shadowDistance, int kerning, int lenght)
void Text::writeShadowed(int x, int y, std::string text, Color color, Uint8 shadow_distance, 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);
sprite_->getTexture()->setColor(color.r, color.g, color.b);
write(x + shadow_distance, y + shadow_distance, text, kerning, lenght);
sprite_->getTexture()->setColor(255, 255, 255);
write(x, y, text, kerning, lenght);
}
@@ -191,12 +191,12 @@ void Text::writeCentered(int x, int y, std::string text, int kerning, int lenght
}
// Escribe texto con extras
void Text::writeDX(Uint8 flags, int x, int y, std::string text, int kerning, Color textColor, Uint8 shadowDistance, Color shadowColor, int lenght)
void Text::writeDX(Uint8 flags, int x, int y, std::string text, int kerning, Color textColor, Uint8 shadow_distance, Color shadow_color, 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);
const auto centered = ((flags & TEXT_CENTER) == TEXT_CENTER);
const auto shadowed = ((flags & TEXT_SHADOW) == TEXT_SHADOW);
const auto colored = ((flags & TEXT_COLOR) == TEXT_COLOR);
const auto stroked = ((flags & TEXT_STROKE) == TEXT_STROKE);
if (centered)
{
@@ -205,18 +205,18 @@ void Text::writeDX(Uint8 flags, int x, int y, std::string text, int kerning, Col
if (shadowed)
{
writeColored(x + shadowDistance, y + shadowDistance, text, shadowColor, kerning, lenght);
writeColored(x + shadow_distance, y + shadow_distance, text, shadow_color, kerning, lenght);
}
if (stroked)
{
for (int dist = 1; dist <= shadowDistance; ++dist)
for (int dist = 1; dist <= shadow_distance; ++dist)
{
for (int dy = -dist; dy <= dist; ++dy)
{
for (int dx = -dist; dx <= dist; ++dx)
{
writeColored(x + dx, y + dy, text, shadowColor, kerning, lenght);
writeColored(x + dx, y + dy, text, shadow_color, kerning, lenght);
}
}
}
@@ -233,12 +233,14 @@ void Text::writeDX(Uint8 flags, int x, int y, std::string text, int kerning, Col
}
// Obtiene la longitud en pixels de una cadena
int Text::lenght(std::string text, int kerning)
int Text::lenght(std::string text, int kerning) const
{
int shift = 0;
auto shift = 0;
for (int i = 0; i < (int)text.length(); ++i)
shift += (offset[int(text[i])].w + kerning);
{
shift += (offset_[int(text[i])].w + kerning);
}
// Descuenta el kerning del último caracter
return shift - kerning;
@@ -247,29 +249,29 @@ int Text::lenght(std::string text, int kerning)
// Devuelve el valor de la variable
int Text::getCharacterSize() const
{
return boxWidth;
return box_width_;
}
// Recarga la textura
void Text::reLoadTexture()
{
sprite->getTexture()->reLoad();
sprite_->getTexture()->reLoad();
}
// Establece si se usa un tamaño fijo de letra
void Text::setFixedWidth(bool value)
{
fixedWidth = value;
fixed_width_ = value;
}
// Carga una paleta de colores para el texto
void Text::addPalette(std::string path)
{
texture->addPalette(path);
texture_->addPalette(path);
}
// Establece una paleta de colores para el texto
void Text::setPalette(int index)
{
texture->setPalette(index);
texture_->setPalette(index);
}