Refactorizar sistema de recursos: crear ResourceManager centralizado
- Crear ResourceManager singleton para gestión centralizada de recursos - Separar lógica de ResourcePack de la clase Texture - Adaptar TextRenderer para cargar fuentes TTF desde pack - Adaptar LogoScaler para cargar imágenes PNG desde pack - Actualizar main.cpp y engine.cpp para usar ResourceManager - Regenerar resources.pack con fuentes y logos incluidos Fixes: - Resuelve error de carga de fuentes desde disco - Resuelve error de carga de logos (can't fopen) - Implementa fallback automático a disco si no existe pack - Todas las clases ahora pueden cargar recursos desde pack 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include "textrenderer.h"
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
#include "../resource_manager.h"
|
||||
|
||||
TextRenderer::TextRenderer() : renderer_(nullptr), font_(nullptr), font_size_(0), use_antialiasing_(true) {
|
||||
}
|
||||
@@ -23,7 +24,30 @@ bool TextRenderer::init(SDL_Renderer* renderer, const char* font_path, int font_
|
||||
}
|
||||
}
|
||||
|
||||
// Cargar la fuente
|
||||
// Intentar cargar la fuente desde ResourceManager (pack o disco)
|
||||
unsigned char* fontData = nullptr;
|
||||
size_t fontDataSize = 0;
|
||||
|
||||
if (ResourceManager::loadResource(font_path, fontData, fontDataSize)) {
|
||||
// Crear SDL_IOStream desde memoria
|
||||
SDL_IOStream* fontIO = SDL_IOFromConstMem(fontData, static_cast<size_t>(fontDataSize));
|
||||
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());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} else {
|
||||
delete[] fontData;
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback final: intentar cargar directamente desde disco (por si falla ResourceManager)
|
||||
font_ = TTF_OpenFont(font_path, font_size);
|
||||
if (font_ == nullptr) {
|
||||
SDL_Log("Error al cargar fuente '%s': %s", font_path, SDL_GetError());
|
||||
@@ -51,7 +75,30 @@ bool TextRenderer::reinitialize(int new_font_size) {
|
||||
font_ = nullptr;
|
||||
}
|
||||
|
||||
// Cargar fuente con nuevo tamaño
|
||||
// Intentar cargar la fuente desde ResourceManager con el nuevo tamaño
|
||||
unsigned char* fontData = nullptr;
|
||||
size_t fontDataSize = 0;
|
||||
|
||||
if (ResourceManager::loadResource(font_path_, fontData, fontDataSize)) {
|
||||
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());
|
||||
return false;
|
||||
}
|
||||
|
||||
font_size_ = new_font_size;
|
||||
return true;
|
||||
} else {
|
||||
delete[] fontData;
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: cargar directamente desde disco
|
||||
font_ = TTF_OpenFont(font_path_.c_str(), new_font_size);
|
||||
if (font_ == nullptr) {
|
||||
SDL_Log("Error al recargar fuente '%s' con tamaño %d: %s",
|
||||
@@ -59,9 +106,7 @@ bool TextRenderer::reinitialize(int new_font_size) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Actualizar tamaño almacenado
|
||||
font_size_ = new_font_size;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user