Fix: Corregir carga de fuentes desde ResourceManager
Problema: - Las fuentes TTF no se renderizaban (error "Text has zero width") - Ocurría tanto al cargar desde resources.pack como desde disco - El buffer de memoria se liberaba inmediatamente después de crear el SDL_IOStream, pero SDL_ttf necesita acceder a esos datos durante toda la vida de la fuente Solución: - Añadido campo font_data_buffer_ para mantener los datos en memoria - Modificado init() y reinitialize() para NO liberar el buffer inmediatamente después de cargar la fuente - Modificado cleanup() para liberar el buffer cuando se cierre la fuente - Añadidos logs de debug para confirmar la carga desde ResourceManager Archivos modificados: - source/text/textrenderer.h: Añadido campo font_data_buffer_ - source/text/textrenderer.cpp: Correcciones en init(), reinitialize() y cleanup() 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
#include "../resource_manager.h"
|
||||
|
||||
TextRenderer::TextRenderer() : renderer_(nullptr), font_(nullptr), font_size_(0), use_antialiasing_(true) {
|
||||
TextRenderer::TextRenderer() : renderer_(nullptr), font_(nullptr), font_size_(0), use_antialiasing_(true), font_data_buffer_(nullptr) {
|
||||
}
|
||||
|
||||
TextRenderer::~TextRenderer() {
|
||||
@@ -34,13 +34,17 @@ bool TextRenderer::init(SDL_Renderer* renderer, const char* font_path, int font_
|
||||
if (fontIO != nullptr) {
|
||||
// Cargar fuente desde IOStream
|
||||
font_ = TTF_OpenFontIO(fontIO, true, font_size); // true = cerrar stream automáticamente
|
||||
delete[] fontData; // Liberar buffer temporal después de crear el stream
|
||||
|
||||
if (font_ == nullptr) {
|
||||
SDL_Log("Error al cargar fuente desde memoria '%s': %s", font_path, SDL_GetError());
|
||||
delete[] fontData; // Liberar solo si falla la carga
|
||||
return false;
|
||||
}
|
||||
|
||||
// CRÍTICO: NO eliminar fontData aquí - SDL_ttf necesita estos datos en memoria
|
||||
// mientras la fuente esté abierta. Se liberará en cleanup()
|
||||
font_data_buffer_ = fontData;
|
||||
SDL_Log("Fuente cargada desde ResourceManager: %s (%zu bytes)", font_path, fontDataSize);
|
||||
return true;
|
||||
} else {
|
||||
delete[] fontData;
|
||||
@@ -69,11 +73,15 @@ bool TextRenderer::reinitialize(int new_font_size) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Cerrar fuente actual
|
||||
// Cerrar fuente actual y liberar buffer previo
|
||||
if (font_ != nullptr) {
|
||||
TTF_CloseFont(font_);
|
||||
font_ = nullptr;
|
||||
}
|
||||
if (font_data_buffer_ != nullptr) {
|
||||
delete[] font_data_buffer_;
|
||||
font_data_buffer_ = nullptr;
|
||||
}
|
||||
|
||||
// Intentar cargar la fuente desde ResourceManager con el nuevo tamaño
|
||||
unsigned char* fontData = nullptr;
|
||||
@@ -83,15 +91,18 @@ bool TextRenderer::reinitialize(int new_font_size) {
|
||||
SDL_IOStream* fontIO = SDL_IOFromConstMem(fontData, static_cast<size_t>(fontDataSize));
|
||||
if (fontIO != nullptr) {
|
||||
font_ = TTF_OpenFontIO(fontIO, true, new_font_size);
|
||||
delete[] fontData;
|
||||
|
||||
if (font_ == nullptr) {
|
||||
SDL_Log("Error al recargar fuente '%s' con tamaño %d: %s",
|
||||
font_path_.c_str(), new_font_size, SDL_GetError());
|
||||
delete[] fontData; // Liberar solo si falla
|
||||
return false;
|
||||
}
|
||||
|
||||
// Mantener buffer en memoria (NO eliminar)
|
||||
font_data_buffer_ = fontData;
|
||||
font_size_ = new_font_size;
|
||||
SDL_Log("Fuente recargada desde ResourceManager: %s (tamaño %d)", font_path_.c_str(), new_font_size);
|
||||
return true;
|
||||
} else {
|
||||
delete[] fontData;
|
||||
@@ -115,6 +126,10 @@ void TextRenderer::cleanup() {
|
||||
TTF_CloseFont(font_);
|
||||
font_ = nullptr;
|
||||
}
|
||||
if (font_data_buffer_ != nullptr) {
|
||||
delete[] font_data_buffer_;
|
||||
font_data_buffer_ = nullptr;
|
||||
}
|
||||
renderer_ = nullptr;
|
||||
}
|
||||
|
||||
|
||||
@@ -50,4 +50,5 @@ private:
|
||||
int font_size_;
|
||||
bool use_antialiasing_;
|
||||
std::string font_path_; // Almacenar ruta para reinitialize()
|
||||
unsigned char* font_data_buffer_; // Buffer de datos de fuente (mantener en memoria mientras esté abierta)
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user