text: afegit TextStyle

This commit is contained in:
2025-07-21 14:23:07 +02:00
parent 734c348996
commit 087643c71b
2 changed files with 28 additions and 2 deletions

View File

@@ -7,10 +7,11 @@
#include <iostream> // Para cerr
#include <stdexcept> // Para runtime_error
#include "color.h" // Para Color, getFileName, printWithDots
#include "screen.h" // Para Screen
#include "sprite.h" // Para Sprite
#include "texture.h" // Para Texture
#include "utils.h" // Para Color, getFileName, printWithDots
#include "utils.h"
// Llena una estructuta TextFile desde un fichero
auto loadTextFile(const std::string &file_path) -> std::shared_ptr<TextFile> {
@@ -231,6 +232,12 @@ void Text::writeDX(Uint8 flags, int x, int y, const std::string &text, int kerni
}
}
// Escribe texto a partir de un TextStyle
void Text::writeDX(Uint8 flags, int x, int y, const std::string& text, const TextStyle& style, int length) {
writeDX(flags, x, y, text, style.kerning, style.text_color, style.shadow_distance, style.shadow_color);
}
// Obtiene la longitud en pixels de una cadena
auto Text::lenght(const std::string &text, int kerning) const -> int {
int shift = 0;

View File

@@ -6,8 +6,9 @@
#include <memory> // Para unique_ptr, shared_ptr
#include <string> // Para string
#include "color.h" // Para Color
#include "sprite.h" // Para Sprite
#include "utils.h" // Para Color
#include "utils.h"
class Texture;
@@ -28,6 +29,23 @@ struct TextFile {
std::array<TextOffset, 128> offset = {}; // Vector con las posiciones y ancho de cada letra
};
struct TextStyle {
Color text_color;
Color shadow_color;
Uint8 shadow_distance;
int kerning;
// Constructor con argumentos por defecto
TextStyle(Color text = Color(),
Color shadow = Color(),
Uint8 distance = 1,
int kern = 1)
: text_color(text),
shadow_color(shadow),
shadow_distance(distance),
kerning(kern) {}
};
// Llena una estructura TextFile desde un fichero
auto loadTextFile(const std::string &file_path) -> std::shared_ptr<TextFile>;
@@ -52,6 +70,7 @@ class Text {
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(), Uint8 shadow_distance = 1, Color shadow_color = Color(), int lenght = -1); // Escribe texto con extras
void writeDX(Uint8 flags, int x, int y, const std::string &text, const TextStyle &style, int length = -1); // Escribe texto a partir de un TextStyle
// --- Utilidades ---
[[nodiscard]] auto lenght(const std::string &text, int kerning = 1) const -> int; // Obtiene la longitud en pixels de una cadena