integrat Asset amb ResourceHelper

This commit is contained in:
2025-08-19 10:06:52 +02:00
parent ed077c1da5
commit 4b6b89ceb2
3 changed files with 28 additions and 5 deletions

View File

@@ -9,6 +9,7 @@
#include <stdexcept> // Para runtime_error #include <stdexcept> // Para runtime_error
#include "utils.h" // Para getFileName #include "utils.h" // Para getFileName
#include "resource_helper.h" // Para ResourceHelper
// Singleton // Singleton
Asset *Asset::instance = nullptr; Asset *Asset::instance = nullptr;
@@ -139,6 +140,17 @@ auto Asset::get(const std::string &filename) const -> std::string {
return ""; return "";
} }
// Carga datos del archivo usando ResourceHelper
auto Asset::loadData(const std::string &filename) const -> std::vector<uint8_t> {
auto it = file_list_.find(filename);
if (it != file_list_.end()) {
return ResourceHelper::loadFile(it->second.file);
}
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Warning: file %s not found for data loading", filename.c_str());
return {};
}
// Verifica si un recurso existe // Verifica si un recurso existe
auto Asset::exists(const std::string &filename) const -> bool { auto Asset::exists(const std::string &filename) const -> bool {
return file_list_.find(filename) != file_list_.end(); return file_list_.find(filename) != file_list_.end();
@@ -194,9 +206,16 @@ auto Asset::check() const -> bool {
// Comprueba que existe un fichero // Comprueba que existe un fichero
auto Asset::checkFile(const std::string &path) -> bool { auto Asset::checkFile(const std::string &path) -> bool {
std::ifstream file(path); // Intentar primero con ResourceHelper
bool success = file.good(); auto data = ResourceHelper::loadFile(path);
file.close(); bool success = !data.empty();
// Si no se encuentra en el pack, intentar con filesystem directo
if (!success) {
std::ifstream file(path);
success = file.good();
file.close();
}
if (!success) { if (!success) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,

View File

@@ -4,6 +4,7 @@
#include <unordered_map> // Para unordered_map #include <unordered_map> // Para unordered_map
#include <utility> // Para move #include <utility> // Para move
#include <vector> // Para vector #include <vector> // Para vector
#include <cstdint> // Para uint8_t
// --- Clase Asset: gestor optimizado de recursos (singleton) --- // --- Clase Asset: gestor optimizado de recursos (singleton) ---
class Asset { class Asset {
@@ -33,6 +34,7 @@ class Asset {
void add(const std::string &file_path, Type type, bool required = true, bool absolute = false); void add(const std::string &file_path, Type type, bool required = true, bool absolute = false);
void loadFromFile(const std::string &config_file_path, const std::string &prefix = "", const std::string &system_folder = ""); // Con soporte para variables void loadFromFile(const std::string &config_file_path, const std::string &prefix = "", const std::string &system_folder = ""); // Con soporte para variables
[[nodiscard]] auto get(const std::string &filename) const -> std::string; // Mantener nombre original [[nodiscard]] auto get(const std::string &filename) const -> std::string; // Mantener nombre original
[[nodiscard]] auto loadData(const std::string &filename) const -> std::vector<uint8_t>; // Carga datos del archivo
[[nodiscard]] auto check() const -> bool; [[nodiscard]] auto check() const -> bool;
[[nodiscard]] auto getListByType(Type type) const -> std::vector<std::string>; [[nodiscard]] auto getListByType(Type type) const -> std::vector<std::string>;
[[nodiscard]] auto exists(const std::string &filename) const -> bool; // Nueva función para verificar existencia [[nodiscard]] auto exists(const std::string &filename) const -> bool; // Nueva función para verificar existencia

View File

@@ -224,8 +224,10 @@ void Screen::renderInfo() {
void Screen::loadShaders() { void Screen::loadShaders() {
if (shader_source_.empty()) { if (shader_source_.empty()) {
const std::string GLSL_FILE = param.game.game_area.rect.h == 256 ? "crtpi_256.glsl" : "crtpi_240.glsl"; const std::string GLSL_FILE = param.game.game_area.rect.h == 256 ? "crtpi_256.glsl" : "crtpi_240.glsl";
std::ifstream f(Asset::get()->get(GLSL_FILE).c_str()); auto data = Asset::get()->loadData(GLSL_FILE);
shader_source_ = std::string((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>()); if (!data.empty()) {
shader_source_ = std::string(data.begin(), data.end());
}
} }
} }