neteja text: elimina constructors morts i amaga TextFile/loaders al cpp
This commit is contained in:
@@ -9,8 +9,17 @@
|
||||
#include "core/resources/resource_helper.h" // for loadFile (pack + filesystem fallback)
|
||||
#include "utils/utils.h" // for Color
|
||||
|
||||
// Parser compartido: rellena un TextFile desde cualquier istream
|
||||
static void parseTextFileStream(std::istream &rfile, TextFile &tf) {
|
||||
namespace {
|
||||
|
||||
// Estructura intermedia para serializar/parsear el bitmap font.
|
||||
// No se expone fuera del TU: solo la usan los constructores de Text.
|
||||
struct TextFile {
|
||||
int box_width; // Anchura de la caja de cada caracter en el png
|
||||
int box_height; // Altura de la caja de cada caracter en el png
|
||||
Text::Offset offset[128]; // Vector con las posiciones y ancho de cada letra
|
||||
};
|
||||
|
||||
void parseTextFileStream(std::istream &rfile, TextFile &tf) {
|
||||
std::string buffer;
|
||||
std::getline(rfile, buffer);
|
||||
std::getline(rfile, buffer);
|
||||
@@ -31,15 +40,37 @@ static void parseTextFileStream(std::istream &rfile, TextFile &tf) {
|
||||
}
|
||||
}
|
||||
|
||||
static void computeTextFileOffsets(TextFile &tf) {
|
||||
void computeTextFileOffsets(TextFile &tf) {
|
||||
for (int i = 32; i < 128; ++i) {
|
||||
tf.offset[i].x = ((i - 32) % 15) * tf.box_width;
|
||||
tf.offset[i].y = ((i - 32) / 15) * tf.box_height;
|
||||
}
|
||||
}
|
||||
|
||||
// Llena una estructuta TextFile desde un fichero (vía ResourceHelper: pack o filesystem)
|
||||
auto loadTextFile(const std::string &file, bool verbose) -> TextFile {
|
||||
// Llena un TextFile desde bytes en memoria
|
||||
auto loadTextFileFromMemory(const std::vector<uint8_t> &bytes, bool verbose) -> TextFile {
|
||||
TextFile tf;
|
||||
tf.box_width = 0;
|
||||
tf.box_height = 0;
|
||||
for (auto &i : tf.offset) {
|
||||
i.x = 0;
|
||||
i.y = 0;
|
||||
i.w = 0;
|
||||
}
|
||||
if (!bytes.empty()) {
|
||||
std::string content(reinterpret_cast<const char *>(bytes.data()), bytes.size());
|
||||
std::stringstream ss(content);
|
||||
parseTextFileStream(ss, tf);
|
||||
if (verbose) {
|
||||
std::cout << "Text loaded from memory" << '\n';
|
||||
}
|
||||
}
|
||||
computeTextFileOffsets(tf);
|
||||
return tf;
|
||||
}
|
||||
|
||||
// Llena un TextFile desde un fichero (vía ResourceHelper: pack o filesystem)
|
||||
auto loadTextFile(const std::string &file, bool verbose = false) -> TextFile {
|
||||
const std::string FILE_NAME = file.substr(file.find_last_of("\\/") + 1);
|
||||
auto bytes = ResourceHelper::loadFile(file);
|
||||
if (bytes.empty()) {
|
||||
@@ -63,27 +94,7 @@ auto loadTextFile(const std::string &file, bool verbose) -> TextFile {
|
||||
return loadTextFileFromMemory(bytes, verbose);
|
||||
}
|
||||
|
||||
// Llena una estructura TextFile desde bytes en memoria
|
||||
auto loadTextFileFromMemory(const std::vector<uint8_t> &bytes, bool verbose) -> TextFile {
|
||||
TextFile tf;
|
||||
tf.box_width = 0;
|
||||
tf.box_height = 0;
|
||||
for (auto &i : tf.offset) {
|
||||
i.x = 0;
|
||||
i.y = 0;
|
||||
i.w = 0;
|
||||
}
|
||||
if (!bytes.empty()) {
|
||||
std::string content(reinterpret_cast<const char *>(bytes.data()), bytes.size());
|
||||
std::stringstream ss(content);
|
||||
parseTextFileStream(ss, tf);
|
||||
if (verbose) {
|
||||
std::cout << "Text loaded from memory" << '\n';
|
||||
}
|
||||
}
|
||||
computeTextFileOffsets(tf);
|
||||
return tf;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
// Constructor
|
||||
Text::Text(const std::string &bitmap_file, const std::string &text_file, SDL_Renderer *renderer) {
|
||||
@@ -107,50 +118,9 @@ Text::Text(const std::string &bitmap_file, const std::string &text_file, SDL_Ren
|
||||
fixed_width_ = false;
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Text::Text(const std::string &text_file, Texture *texture, SDL_Renderer *renderer) {
|
||||
// Carga los offsets desde el fichero
|
||||
TextFile tf = loadTextFile(text_file);
|
||||
|
||||
// Inicializa variables desde la estructura
|
||||
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;
|
||||
}
|
||||
|
||||
// Crea los objetos
|
||||
this->texture_ = nullptr;
|
||||
sprite_ = new Sprite({0, 0, box_width_, box_height_}, texture, renderer);
|
||||
|
||||
// Inicializa variables
|
||||
fixed_width_ = false;
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Text::Text(TextFile *text_file, Texture *texture, SDL_Renderer *renderer) {
|
||||
// Inicializa variables desde la estructura
|
||||
box_height_ = text_file->box_height;
|
||||
box_width_ = text_file->box_width;
|
||||
for (int i = 0; i < 128; ++i) {
|
||||
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
|
||||
this->texture_ = nullptr;
|
||||
sprite_ = new Sprite({0, 0, box_width_, box_height_}, texture, renderer);
|
||||
|
||||
// Inicializa variables
|
||||
fixed_width_ = false;
|
||||
}
|
||||
|
||||
// Constructor desde bytes
|
||||
Text::Text(const std::vector<uint8_t> &png_bytes, const std::vector<uint8_t> &txt_bytes, SDL_Renderer *renderer) {
|
||||
TextFile tf = loadTextFileFromMemory(txt_bytes);
|
||||
TextFile tf = loadTextFileFromMemory(txt_bytes, false);
|
||||
box_height_ = tf.box_height;
|
||||
box_width_ = tf.box_width;
|
||||
for (int i = 0; i < 128; ++i) {
|
||||
@@ -271,4 +241,4 @@ void Text::reLoadTexture() {
|
||||
// Establece si se usa un tamaño fijo de letra
|
||||
void Text::setFixedWidth(bool value) {
|
||||
fixed_width_ = value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,31 +15,17 @@ constexpr int TXT_SHADOW = 2;
|
||||
constexpr int TXT_CENTER = 4;
|
||||
constexpr int TXT_STROKE = 8;
|
||||
|
||||
struct Offset {
|
||||
int x;
|
||||
int y;
|
||||
int w;
|
||||
};
|
||||
|
||||
struct TextFile {
|
||||
int box_width; // Anchura de la caja de cada caracter en el png
|
||||
int box_height; // Altura de la caja de cada caracter en el png
|
||||
Offset offset[128]; // Vector con las posiciones y ancho de cada letra
|
||||
};
|
||||
|
||||
// Llena una estructuta TextFile desde un fichero
|
||||
auto loadTextFile(const std::string &file, bool verbose = false) -> TextFile;
|
||||
|
||||
// Llena una estructura TextFile desde bytes en memoria
|
||||
auto loadTextFileFromMemory(const std::vector<uint8_t> &bytes, bool verbose = false) -> TextFile;
|
||||
|
||||
// Clase texto. Pinta texto en pantalla a partir de un bitmap
|
||||
class Text {
|
||||
public:
|
||||
Text(const std::string &bitmap_file, const std::string &text_file, SDL_Renderer *renderer); // Constructor
|
||||
Text(const std::string &text_file, Texture *texture, SDL_Renderer *renderer);
|
||||
Text(TextFile *text_file, Texture *texture, SDL_Renderer *renderer);
|
||||
Text(const std::vector<uint8_t> &png_bytes, const std::vector<uint8_t> &txt_bytes, SDL_Renderer *renderer); // Constructor desde bytes en memoria: comparte ownership del texture (no lo libera)
|
||||
struct Offset {
|
||||
int x; // Posición X dentro del bitmap
|
||||
int y; // Posición Y dentro del bitmap
|
||||
int w; // Anchura del glifo
|
||||
};
|
||||
|
||||
Text(const std::string &bitmap_file, const std::string &text_file, SDL_Renderer *renderer); // Constructor desde paths
|
||||
Text(const std::vector<uint8_t> &png_bytes, const std::vector<uint8_t> &txt_bytes, SDL_Renderer *renderer); // Constructor desde bytes en memoria
|
||||
|
||||
~Text(); // Destructor
|
||||
|
||||
@@ -47,11 +33,11 @@ class Text {
|
||||
Text(const Text &) = delete;
|
||||
auto operator=(const Text &) -> Text & = delete;
|
||||
|
||||
void write(int x, int y, const std::string &text, int kerning = 1, int lenght = -1); // Escribe el texto en pantalla
|
||||
void writeColored(int x, int y, const std::string &text, Color color, int kerning = 1, int lenght = -1); // Escribe el texto con colores
|
||||
void writeShadowed(int x, int y, const std::string &text, Color color, Uint8 shadow_distance = 1, int kerning = 1, int lenght = -1); // Escribe el texto con sombra
|
||||
void writeCentered(int x, int y, const std::string &text, int kerning = 1, int lenght = -1); // Escribe el texto centrado en un punto x
|
||||
void writeDX(Uint8 flags, int x, int y, const std::string &text, int kerning = 1, Color text_color = Color(255, 255, 255), Uint8 shadow_distance = 1, Color shadow_color = Color(0, 0, 0), int lenght = -1); // Escribe texto con extras
|
||||
void write(int x, int y, const std::string &text, int kerning = 1, int lenght = -1); // Escribe el texto en pantalla
|
||||
void writeColored(int x, int y, const std::string &text, Color color, int kerning = 1, int lenght = -1); // Escribe el texto con colores
|
||||
void writeShadowed(int x, int y, const std::string &text, Color color, Uint8 shadow_distance = 1, int kerning = 1, int lenght = -1); // Escribe el texto con sombra
|
||||
void writeCentered(int x, int y, const std::string &text, int kerning = 1, int lenght = -1); // Escribe el texto centrado en un punto x
|
||||
void writeDX(Uint8 flags, int x, int y, const std::string &text, int kerning = 1, Color text_color = Color(255, 255, 255), Uint8 shadow_distance = 1, Color shadow_color = Color(0, 0, 0), int lenght = -1); // Escribe texto con extras
|
||||
|
||||
auto lenght(const std::string &text, int kerning = 1) -> int; // Obtiene la longitud en pixels de una cadena
|
||||
|
||||
|
||||
Reference in New Issue
Block a user