From 087643c71b4583fecfabf29bbe526731cf6f9031 Mon Sep 17 00:00:00 2001 From: Sergio Date: Mon, 21 Jul 2025 14:23:07 +0200 Subject: [PATCH] text: afegit TextStyle --- source/text.cpp | 9 ++++++++- source/text.h | 21 ++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/source/text.cpp b/source/text.cpp index 2dcd6f8..112a93c 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -7,10 +7,11 @@ #include // Para cerr #include // 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 { @@ -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; diff --git a/source/text.h b/source/text.h index 16901a0..49362e4 100644 --- a/source/text.h +++ b/source/text.h @@ -6,8 +6,9 @@ #include // Para unique_ptr, shared_ptr #include // 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 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; @@ -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