integrades mes clases amb ResourceHelper
mogudes les dades de la demo a resource.pack
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
#include "difficulty.h" // Para Difficulty
|
||||
#include "external/json.hpp" // Para basic_json, iteration_proxy_value, oper...
|
||||
#include "options.h" // Para SettingsOpt...
|
||||
#include "resource_helper.h" // Para ResourceHelper
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
@@ -27,14 +28,24 @@ std::vector<Language> languages = {
|
||||
auto loadFromFile(const std::string &file_path) -> bool {
|
||||
texts.clear();
|
||||
|
||||
std::ifstream rfile(file_path);
|
||||
if (!rfile.is_open()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Intentar cargar desde ResourceHelper primero
|
||||
auto resource_data = ResourceHelper::loadFile(file_path);
|
||||
|
||||
try {
|
||||
json j;
|
||||
rfile >> j;
|
||||
|
||||
if (!resource_data.empty()) {
|
||||
// Cargar desde datos del pack
|
||||
std::string content(resource_data.begin(), resource_data.end());
|
||||
j = json::parse(content);
|
||||
} else {
|
||||
// Fallback a filesystem directo
|
||||
std::ifstream rfile(file_path);
|
||||
if (!rfile.is_open()) {
|
||||
return false;
|
||||
}
|
||||
rfile >> j;
|
||||
}
|
||||
|
||||
for (const auto &el : j.items()) {
|
||||
texts[el.key()] = el.value();
|
||||
|
||||
@@ -225,22 +225,30 @@ auto Texture::loadSurface(const std::string &file_path) -> std::shared_ptr<Surfa
|
||||
// Libera la superficie actual
|
||||
unloadSurface();
|
||||
|
||||
// Abrir el archivo usando std::ifstream para manejo automático del recurso
|
||||
std::ifstream file(file_path, std::ios::binary | std::ios::ate);
|
||||
if (!file) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Fichero no encontrado %s", file_path.c_str());
|
||||
throw std::runtime_error("Fichero no encontrado: " + file_path);
|
||||
}
|
||||
std::vector<Uint8> buffer;
|
||||
|
||||
// Intentar cargar desde ResourceHelper primero
|
||||
auto resource_data = ResourceHelper::loadFile(file_path);
|
||||
if (!resource_data.empty()) {
|
||||
buffer = resource_data;
|
||||
} else {
|
||||
// Fallback a filesystem directo
|
||||
std::ifstream file(file_path, std::ios::binary | std::ios::ate);
|
||||
if (!file) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Fichero no encontrado %s", file_path.c_str());
|
||||
throw std::runtime_error("Fichero no encontrado: " + file_path);
|
||||
}
|
||||
|
||||
// Obtener el tamaño del archivo
|
||||
std::streamsize size = file.tellg();
|
||||
file.seekg(0, std::ios::beg);
|
||||
// Obtener el tamaño del archivo
|
||||
std::streamsize size = file.tellg();
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
// Leer el contenido del archivo en un buffer
|
||||
std::vector<Uint8> buffer(size);
|
||||
if (!file.read(reinterpret_cast<char *>(buffer.data()), size)) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error al leer el fichero %s", file_path.c_str());
|
||||
throw std::runtime_error("Error al leer el fichero: " + file_path);
|
||||
// Leer el contenido del archivo en un buffer
|
||||
buffer.resize(size);
|
||||
if (!file.read(reinterpret_cast<char *>(buffer.data()), size)) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error al leer el fichero %s", file_path.c_str());
|
||||
throw std::runtime_error("Error al leer el fichero: " + file_path);
|
||||
}
|
||||
}
|
||||
|
||||
// Crear un objeto Gif y llamar a la función loadGif
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <string> // Para basic_string, allocator, string, operator==, operator+, char_traits
|
||||
|
||||
#include "lang.h" // Para getText
|
||||
#include "resource_helper.h" // Para ResourceHelper
|
||||
|
||||
// Variables
|
||||
Overrides overrides = Overrides();
|
||||
@@ -323,8 +324,17 @@ void printWithDots(const std::string &text1, const std::string &text2, const std
|
||||
auto loadDemoDataFromFile(const std::string &file_path) -> DemoData {
|
||||
DemoData dd;
|
||||
|
||||
// Indicador de éxito en la carga
|
||||
auto *file = SDL_IOFromFile(file_path.c_str(), "r+b");
|
||||
SDL_IOStream *file = nullptr;
|
||||
|
||||
// Intentar cargar desde ResourceHelper primero
|
||||
auto resource_data = ResourceHelper::loadFile(file_path);
|
||||
if (!resource_data.empty()) {
|
||||
file = SDL_IOFromConstMem(resource_data.data(), resource_data.size());
|
||||
} else {
|
||||
// Fallback a filesystem directo
|
||||
file = SDL_IOFromFile(file_path.c_str(), "r+b");
|
||||
}
|
||||
|
||||
if (file == nullptr) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Fichero no encontrado %s", file_path.c_str());
|
||||
throw std::runtime_error("Fichero no encontrado: " + file_path);
|
||||
|
||||
Reference in New Issue
Block a user