integrat Asset amb ResourceHelper
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#include <stdexcept> // Para runtime_error
|
||||
|
||||
#include "utils.h" // Para getFileName
|
||||
#include "resource_helper.h" // Para ResourceHelper
|
||||
|
||||
// Singleton
|
||||
Asset *Asset::instance = nullptr;
|
||||
@@ -139,6 +140,17 @@ auto Asset::get(const std::string &filename) const -> std::string {
|
||||
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
|
||||
auto Asset::exists(const std::string &filename) const -> bool {
|
||||
return file_list_.find(filename) != file_list_.end();
|
||||
@@ -194,9 +206,16 @@ auto Asset::check() const -> bool {
|
||||
|
||||
// Comprueba que existe un fichero
|
||||
auto Asset::checkFile(const std::string &path) -> bool {
|
||||
// Intentar primero con ResourceHelper
|
||||
auto data = ResourceHelper::loadFile(path);
|
||||
bool success = !data.empty();
|
||||
|
||||
// Si no se encuentra en el pack, intentar con filesystem directo
|
||||
if (!success) {
|
||||
std::ifstream file(path);
|
||||
bool success = file.good();
|
||||
success = file.good();
|
||||
file.close();
|
||||
}
|
||||
|
||||
if (!success) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <unordered_map> // Para unordered_map
|
||||
#include <utility> // Para move
|
||||
#include <vector> // Para vector
|
||||
#include <cstdint> // Para uint8_t
|
||||
|
||||
// --- Clase Asset: gestor optimizado de recursos (singleton) ---
|
||||
class Asset {
|
||||
@@ -33,6 +34,7 @@ class Asset {
|
||||
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
|
||||
[[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 getListByType(Type type) const -> std::vector<std::string>;
|
||||
[[nodiscard]] auto exists(const std::string &filename) const -> bool; // Nueva función para verificar existencia
|
||||
|
||||
@@ -224,8 +224,10 @@ void Screen::renderInfo() {
|
||||
void Screen::loadShaders() {
|
||||
if (shader_source_.empty()) {
|
||||
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());
|
||||
shader_source_ = std::string((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
|
||||
auto data = Asset::get()->loadData(GLSL_FILE);
|
||||
if (!data.empty()) {
|
||||
shader_source_ = std::string(data.begin(), data.end());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user