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:
2025-10-23 09:16:18 +02:00
parent 41c76316ef
commit 2fa1684f01
9 changed files with 264 additions and 64 deletions

View File

@@ -12,38 +12,7 @@
#include <string> // Para operator<<, string
#include "stb_image.h" // Para stbi_failure_reason, stbi_image_free
#include "../resource_pack.h" // Sistema de empaquetado de recursos
// Instancia global de ResourcePack (se inicializa al primer uso)
static ResourcePack* g_resourcePack = nullptr;
// Inicializar el sistema de recursos (llamar desde main antes de cargar texturas)
void Texture::initResourceSystem(const std::string& packFilePath) {
if (g_resourcePack == nullptr) {
g_resourcePack = new ResourcePack();
if (!g_resourcePack->loadPack(packFilePath)) {
// Si falla, borrar instancia (usará fallback a disco)
delete g_resourcePack;
g_resourcePack = nullptr;
std::cout << "resources.pack no encontrado - usando carpeta data/" << std::endl;
} else {
std::cout << "resources.pack cargado (" << g_resourcePack->getResourceCount() << " recursos)" << std::endl;
}
}
}
// Obtener lista de recursos disponibles en el pack
std::vector<std::string> Texture::getPackResourceList() {
if (g_resourcePack != nullptr) {
return g_resourcePack->getResourceList();
}
return std::vector<std::string>(); // Vacío si no hay pack
}
// Verificar si el pack está cargado
bool Texture::isPackLoaded() {
return g_resourcePack != nullptr;
}
#include "../resource_manager.h" // Sistema de empaquetado de recursos centralizado
Texture::Texture(SDL_Renderer *renderer)
: renderer_(renderer),
@@ -70,30 +39,29 @@ bool Texture::loadFromFile(const std::string &file_path) {
int width, height, orig_format;
unsigned char *data = nullptr;
// 1. Intentar cargar desde pack (si está inicializado)
if (g_resourcePack != nullptr) {
ResourcePack::ResourceData packData = g_resourcePack->loadResource(file_path);
if (packData.data != nullptr) {
// Descodificar imagen desde memoria usando stb_image
data = stbi_load_from_memory(packData.data, static_cast<int>(packData.size),
&width, &height, &orig_format, req_format);
delete[] packData.data; // Liberar buffer temporal del pack
// 1. Intentar cargar desde ResourceManager (pack o disco)
unsigned char* resourceData = nullptr;
size_t resourceSize = 0;
if (data != nullptr) {
if (ResourceManager::loadResource(file_path, resourceData, resourceSize)) {
// Descodificar imagen desde memoria usando stb_image
data = stbi_load_from_memory(resourceData, static_cast<int>(resourceSize),
&width, &height, &orig_format, req_format);
delete[] resourceData; // Liberar buffer temporal
if (data != nullptr) {
if (ResourceManager::isPackLoaded()) {
std::cout << "Imagen cargada desde pack: " << filename.c_str() << std::endl;
} else {
std::cout << "Imagen cargada desde disco: " << filename.c_str() << std::endl;
}
}
}
// 2. Fallback: cargar desde disco (modo desarrollo)
// 2. Si todo falla, error
if (data == nullptr) {
data = stbi_load(file_path.c_str(), &width, &height, &orig_format, req_format);
if (data == nullptr) {
SDL_Log("Error al cargar la imagen: %s", stbi_failure_reason());
exit(1);
} else {
std::cout << "Imagen cargada desde disco: " << filename.c_str() << std::endl;
}
SDL_Log("Error al cargar la imagen: %s", stbi_failure_reason());
exit(1);
}
int pitch;