From 2a8fbbb0957b45fdff50dc992586ed537f1c206e Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Sat, 18 Apr 2026 13:44:07 +0200 Subject: [PATCH] =?UTF-8?q?refactor:=20fase=203=20=E2=80=94=20Text::bitmap?= =?UTF-8?q?=5F=20a=20std::vector?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - bitmap_: Uint8* (owning, free'd al destructor) → std::vector - loadBitmap copia des del buffer de LoadGif i fa free(pixels) de l'intermedi (gif.h usa malloc internament) - ~Text() eliminat: regla 0 aplicada (vector es destrueix sol) - Les 4 comprovacions !bitmap_ → bitmap_.empty() Co-Authored-By: Claude Opus 4.7 (1M context) --- source/core/rendering/text.cpp | 15 ++++++--------- source/core/rendering/text.hpp | 4 ++-- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/source/core/rendering/text.cpp b/source/core/rendering/text.cpp index 0e936fd..5ad2238 100644 --- a/source/core/rendering/text.cpp +++ b/source/core/rendering/text.cpp @@ -19,10 +19,6 @@ Text::Text(const char* fnt_file, const char* gif_file) { loadFont(fnt_file); } -Text::~Text() { - if (bitmap_) free(bitmap_); -} - // --- UTF-8 --- auto Text::nextCodepoint(const char*& ptr) -> uint32_t { @@ -146,7 +142,8 @@ void Text::loadBitmap(const char* gif_file) { bitmap_width_ = w; bitmap_height_ = h; - bitmap_ = pixels; + bitmap_.assign(pixels, pixels + (static_cast(w) * h)); + free(pixels); // LoadGif usa malloc internament std::cout << "Text: bitmap loaded " << w << "x" << h << '\n'; } @@ -154,7 +151,7 @@ void Text::loadBitmap(const char* gif_file) { // --- Renderitzat --- void Text::draw(Uint32* pixel_data, int x, int y, const char* text, Uint32 color) const { - if (!bitmap_ || !pixel_data) return; + if (bitmap_.empty() || !pixel_data) return; const char* ptr = text; int cursor_x = x; @@ -207,7 +204,7 @@ void Text::drawCentered(Uint32* pixel_data, int y, const char* text, Uint32 colo } void Text::drawClipped(Uint32* pixel_data, int x, int y, const char* text, Uint32 color, int clip_x_min, int clip_x_max, int clip_y_min, int clip_y_max) const { - if (!bitmap_ || !pixel_data) return; + if (bitmap_.empty() || !pixel_data) return; // Descart ràpid si el glifo sencer cau fora verticalment if (y + box_height_ <= clip_y_min || y >= clip_y_max) return; @@ -262,7 +259,7 @@ void Text::drawClipped(Uint32* pixel_data, int x, int y, const char* text, Uint3 } void Text::drawMono(Uint32* pixel_data, int x, int y, const char* text, Uint32 color, int cell_w) const { - if (!bitmap_ || !pixel_data) return; + if (bitmap_.empty() || !pixel_data) return; const char* ptr = text; int cursor_x = x; @@ -308,7 +305,7 @@ void Text::drawMono(Uint32* pixel_data, int x, int y, const char* text, Uint32 c } void Text::drawMonoDigits(Uint32* pixel_data, int x, int y, const char* text, Uint32 color, int digit_cell_w) const { - if (!bitmap_ || !pixel_data) return; + if (bitmap_.empty() || !pixel_data) return; const char* ptr = text; int cursor_x = x; diff --git a/source/core/rendering/text.hpp b/source/core/rendering/text.hpp index 75ebc48..b6fc9ba 100644 --- a/source/core/rendering/text.hpp +++ b/source/core/rendering/text.hpp @@ -5,11 +5,11 @@ #include #include #include +#include class Text { public: Text(const char* fnt_file, const char* gif_file); - ~Text(); // Pinta texto sobre un buffer ARGB de 320x200 void draw(Uint32* pixel_data, int x, int y, const char* text, Uint32 color) const; @@ -46,7 +46,7 @@ class Text { int cell_spacing_{0}; int row_spacing_{0}; - Uint8* bitmap_{nullptr}; // píxels 8-bit del GIF de la font + std::vector bitmap_; // píxels 8-bit del GIF de la font int bitmap_width_{0}; int bitmap_height_{0};