integrades mes clases amb ResourceHelper

mogudes les dades de la demo a resource.pack
This commit is contained in:
2025-08-19 13:08:37 +02:00
parent 8cfe28922c
commit 6bf8490776
9 changed files with 116 additions and 30 deletions

View File

@@ -16,10 +16,12 @@ DATA|${PREFIX}/config/param_320x256.txt
DATA|${PREFIX}/config/param_red.txt
DATA|${PREFIX}/config/pools.txt
DATA|${PREFIX}/config/stages.txt
DEMODATA|${PREFIX}/config/demo1.bin
DEMODATA|${PREFIX}/config/demo2.bin
# Música (archivos empaquetados)
# Archivos con los datos de la demo
DEMODATA|${PREFIX}/data/demo/demo1.bin
DEMODATA|${PREFIX}/data/demo/demo2.bin
# Música
MUSIC|${PREFIX}/data/music/credits.ogg
MUSIC|${PREFIX}/data/music/intro.ogg
MUSIC|${PREFIX}/data/music/playing.ogg

View File

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View File

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

View File

@@ -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();

View File

@@ -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

View File

@@ -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);

BIN
tools/pack_resources Executable file

Binary file not shown.

View File

@@ -2,22 +2,77 @@
#include <iostream>
#include <filesystem>
void showHelp() {
std::cout << "Coffee Crisis Arcade Edition - Resource Packer" << std::endl;
std::cout << "===============================================" << std::endl;
std::cout << "Usage: pack_resources [options] [input_dir] [output_file]" << std::endl;
std::cout << std::endl;
std::cout << "Options:" << std::endl;
std::cout << " --help Show this help message" << std::endl;
std::cout << " --list List contents of an existing pack file" << std::endl;
std::cout << std::endl;
std::cout << "Arguments:" << std::endl;
std::cout << " input_dir Directory to pack (default: data)" << std::endl;
std::cout << " output_file Pack file name (default: resources.pack)" << std::endl;
std::cout << std::endl;
std::cout << "Examples:" << std::endl;
std::cout << " pack_resources # Pack 'data' to 'resources.pack'" << std::endl;
std::cout << " pack_resources mydata # Pack 'mydata' to 'resources.pack'" << std::endl;
std::cout << " pack_resources data my.pack # Pack 'data' to 'my.pack'" << std::endl;
std::cout << " pack_resources --list my.pack # List contents of 'my.pack'" << std::endl;
}
void listPackContents(const std::string& packFile) {
ResourcePack pack;
if (!pack.loadPack(packFile)) {
std::cerr << "Error: Cannot open pack file: " << packFile << std::endl;
return;
}
auto resources = pack.getResourceList();
std::cout << "Pack file: " << packFile << std::endl;
std::cout << "Resources: " << resources.size() << std::endl;
std::cout << "Contents:" << std::endl;
for (const auto& resource : resources) {
std::cout << " " << resource << std::endl;
}
}
int main(int argc, char* argv[]) {
std::string dataDir = "data";
std::string outputFile = "resources.pack";
bool listMode = false;
if (argc > 1) {
dataDir = argv[1];
// Parse arguments
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
if (arg == "--help" || arg == "-h") {
showHelp();
return 0;
} else if (arg == "--list") {
listMode = true;
if (i + 1 < argc) {
outputFile = argv[++i]; // Next argument is pack file to list
}
} else if (!arg.empty() && arg[0] != '-') {
if (dataDir == "data") {
dataDir = arg;
} else {
outputFile = arg;
}
}
}
if (argc > 2) {
outputFile = argv[2];
if (listMode) {
listPackContents(outputFile);
return 0;
}
std::cout << "Coffee Crisis Arcade Edition - Resource Packer" << std::endl;
std::cout << "===============================================" << std::endl;
std::cout << "Input directory: " << dataDir << std::endl;
std::cout << "Output file: " << outputFile << std::endl;
std::cout << "Note: config/ directory is excluded from packing" << std::endl;
std::cout << std::endl;
if (!std::filesystem::exists(dataDir)) {