Modernizar convenciones de código C++ aplicando las siguientes directivas:
## Cambios principales
**1. Renombrar headers (.h → .hpp)**
- 36 archivos renombrados a extensión .hpp (estándar C++)
- Mantenidos como .h: stb_image.h, stb_image_resize2.h (librerías C externas)
**2. Modernizar include guards (#ifndef → #pragma once)**
- resource_manager.hpp: #ifndef RESOURCE_MANAGER_H → #pragma once
- resource_pack.hpp: #ifndef RESOURCE_PACK_H → #pragma once
- spatial_grid.hpp: #ifndef SPATIAL_GRID_H → #pragma once
**3. Sistema de includes desde raíz del proyecto**
- CMakeLists.txt: añadido include_directories(${CMAKE_SOURCE_DIR}/source)
- Eliminadas rutas relativas (../) en todos los includes
- Includes ahora usan rutas absolutas desde source/
**Antes:**
```cpp
#include "../defines.h"
#include "../text/textrenderer.h"
```
**Ahora:**
```cpp
#include "defines.hpp"
#include "text/textrenderer.hpp"
```
## Archivos afectados
- 1 archivo CMakeLists.txt modificado
- 36 archivos renombrados (.h → .hpp)
- 32 archivos .cpp actualizados (includes)
- 36 archivos .hpp actualizados (includes + guards)
- 1 archivo tools/ actualizado
**Total: 70 archivos modificados**
## Verificación
✅ Proyecto compila sin errores
✅ Todas las rutas de includes correctas
✅ Include guards modernizados
✅ Librerías externas C mantienen extensión .h
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
92 lines
2.6 KiB
C++
92 lines
2.6 KiB
C++
#include "resource_manager.hpp"
|
|
#include "resource_pack.hpp"
|
|
|
|
#include <iostream>
|
|
#include <fstream>
|
|
|
|
// Inicializar el puntero estático
|
|
ResourcePack* ResourceManager::resourcePack_ = nullptr;
|
|
|
|
bool ResourceManager::init(const std::string& packFilePath) {
|
|
// Si ya estaba inicializado, liberar primero
|
|
if (resourcePack_ != nullptr) {
|
|
delete resourcePack_;
|
|
resourcePack_ = nullptr;
|
|
}
|
|
|
|
// Intentar cargar el pack
|
|
resourcePack_ = new ResourcePack();
|
|
if (!resourcePack_->loadPack(packFilePath)) {
|
|
// Si falla, borrar instancia (usará fallback a disco)
|
|
delete resourcePack_;
|
|
resourcePack_ = nullptr;
|
|
std::cout << "resources.pack no encontrado - usando carpeta data/" << std::endl;
|
|
return false;
|
|
}
|
|
|
|
std::cout << "resources.pack cargado (" << resourcePack_->getResourceCount() << " recursos)" << std::endl;
|
|
return true;
|
|
}
|
|
|
|
void ResourceManager::shutdown() {
|
|
if (resourcePack_ != nullptr) {
|
|
delete resourcePack_;
|
|
resourcePack_ = nullptr;
|
|
}
|
|
}
|
|
|
|
bool ResourceManager::loadResource(const std::string& resourcePath, unsigned char*& data, size_t& size) {
|
|
data = nullptr;
|
|
size = 0;
|
|
|
|
// 1. Intentar cargar desde pack (si está disponible)
|
|
if (resourcePack_ != nullptr) {
|
|
ResourcePack::ResourceData packData = resourcePack_->loadResource(resourcePath);
|
|
if (packData.data != nullptr) {
|
|
data = packData.data;
|
|
size = packData.size;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
// 2. Fallback: cargar desde disco
|
|
std::ifstream file(resourcePath, std::ios::binary | std::ios::ate);
|
|
if (!file) {
|
|
// Intentar con "data/" como prefijo si no se encontró
|
|
std::string dataPath = "data/" + resourcePath;
|
|
file.open(dataPath, std::ios::binary | std::ios::ate);
|
|
if (!file) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Obtener tamaño del archivo
|
|
size = static_cast<size_t>(file.tellg());
|
|
file.seekg(0, std::ios::beg);
|
|
|
|
// Alocar buffer y leer
|
|
data = new unsigned char[size];
|
|
file.read(reinterpret_cast<char*>(data), size);
|
|
file.close();
|
|
|
|
return true;
|
|
}
|
|
|
|
bool ResourceManager::isPackLoaded() {
|
|
return resourcePack_ != nullptr;
|
|
}
|
|
|
|
std::vector<std::string> ResourceManager::getResourceList() {
|
|
if (resourcePack_ != nullptr) {
|
|
return resourcePack_->getResourceList();
|
|
}
|
|
return std::vector<std::string>(); // Vacío si no hay pack
|
|
}
|
|
|
|
size_t ResourceManager::getResourceCount() {
|
|
if (resourcePack_ != nullptr) {
|
|
return resourcePack_->getResourceCount();
|
|
}
|
|
return 0;
|
|
}
|