acabat amb resource.pack

This commit is contained in:
2026-04-16 16:21:44 +02:00
parent b2d5f5af61
commit 4244bcaea3
11 changed files with 55 additions and 167 deletions

View File

@@ -2,7 +2,7 @@
#include <fstream>
#include "core/jail/jfile.hpp"
#include "core/resources/resource_helper.hpp"
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-but-set-variable"
@@ -44,10 +44,10 @@ JD8_Surface JD8_NewSurface() {
}
JD8_Surface JD8_LoadSurface(const char* file) {
auto buffer = file_readfile(file);
auto buffer = ResourceHelper::loadFile(file);
unsigned short w, h;
Uint8* pixels = LoadGif(reinterpret_cast<unsigned char*>(buffer.data()), &w, &h);
Uint8* pixels = LoadGif(buffer.data(), &w, &h);
if (pixels == NULL) {
printf("Unable to load bitmap: %s\n", SDL_GetError());
@@ -62,8 +62,8 @@ JD8_Surface JD8_LoadSurface(const char* file) {
}
JD8_Palette JD8_LoadPalette(const char* file) {
auto buffer = file_readfile(file);
return (JD8_Palette)LoadPalette(reinterpret_cast<unsigned char*>(buffer.data()));
auto buffer = ResourceHelper::loadFile(file);
return (JD8_Palette)LoadPalette(buffer.data());
}
void JD8_SetScreenPalette(JD8_Palette palette) {

View File

@@ -1,15 +1,10 @@
#include "core/jail/jfile.hpp"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
@@ -19,58 +14,14 @@
namespace {
constexpr const char* DEFAULT_FILENAME = "data.jf2";
constexpr const char* DEFAULT_FOLDER = "data/";
struct file_entry {
std::string path;
uint32_t size;
uint32_t offset;
};
struct keyvalue {
std::string key;
std::string value;
};
std::vector<file_entry> toc;
std::vector<keyvalue> config;
std::string resource_filename;
std::string resource_folder;
std::string config_folder;
int file_source = SOURCE_FILE;
bool dictionary_loaded() {
if (resource_filename.empty()) resource_filename = DEFAULT_FILENAME;
std::ifstream fi(resource_filename, std::ios::binary);
if (!fi.is_open()) return false;
char header[4];
fi.read(header, 4);
uint32_t num_files, toc_offset;
fi.read(reinterpret_cast<char*>(&num_files), 4);
fi.read(reinterpret_cast<char*>(&toc_offset), 4);
fi.seekg(toc_offset);
for (uint32_t i = 0; i < num_files; ++i) {
uint32_t file_offset, file_size;
fi.read(reinterpret_cast<char*>(&file_offset), 4);
fi.read(reinterpret_cast<char*>(&file_size), 4);
uint8_t path_size;
fi.read(reinterpret_cast<char*>(&path_size), 1);
char file_name[256];
fi.read(file_name, path_size);
file_name[path_size] = 0;
toc.push_back({std::string(file_name), file_size, file_offset});
}
return true;
}
std::string filename_with_folder(const char* filename) {
return resource_folder + filename;
}
void load_config_values() {
config.clear();
@@ -97,10 +48,6 @@ void save_config_values() {
} // namespace
void file_setresourcefilename(const char* str) {
resource_filename = str;
}
void file_setresourcefolder(const char* str) {
resource_folder = str;
}
@@ -109,58 +56,6 @@ const char* file_getresourcefolder() {
return resource_folder.c_str();
}
void file_setsource(const int src) {
file_source = src % 2;
if (src == SOURCE_FOLDER && resource_folder.empty()) file_setresourcefolder(DEFAULT_FOLDER);
}
FILE* file_getfilepointer(const char* resourcename, int& filesize, const bool binary) {
if (file_source == SOURCE_FILE && toc.empty()) {
if (!dictionary_loaded()) file_setsource(SOURCE_FOLDER);
}
FILE* f = nullptr;
if (file_source == SOURCE_FILE) {
const std::string name(resourcename);
size_t count = 0;
for (; count < toc.size(); ++count) {
if (toc[count].path == name) break;
}
if (count == toc.size()) {
perror("El recurs no s'ha trobat en l'arxiu de recursos");
exit(1);
}
filesize = static_cast<int>(toc[count].size);
f = fopen(resource_filename.c_str(), binary ? "rb" : "r");
if (!f) {
perror("No s'ha pogut obrir l'arxiu de recursos");
exit(1);
}
fseek(f, toc[count].offset, SEEK_SET);
} else {
const std::string full = filename_with_folder(resourcename);
f = fopen(full.c_str(), binary ? "rb" : "r");
if (!f) return nullptr;
fseek(f, 0, SEEK_END);
filesize = static_cast<int>(ftell(f));
fseek(f, 0, SEEK_SET);
}
return f;
}
std::vector<char> file_readfile(const char* resourcename) {
int filesize = 0;
FILE* f = file_getfilepointer(resourcename, filesize, true);
if (!f) return {};
std::vector<char> buffer(filesize);
fread(buffer.data(), filesize, 1, f);
fclose(f);
return buffer;
}
// Crea la carpeta del sistema on guardar les dades.
// Accepta rutes amb subdirectoris (ex: "jailgames/aee") i crea tota la jerarquia.
void file_setconfigfolder(const char* foldername) {

View File

@@ -1,26 +1,10 @@
#pragma once
#include <stdio.h>
#include <vector>
#define SOURCE_FILE 0
#define SOURCE_FOLDER 1
void file_setconfigfolder(const char* foldername);
const char* file_getconfigfolder();
void file_setresourcefilename(const char* str);
void file_setresourcefolder(const char* str);
const char* file_getresourcefolder();
void file_setsource(const int src);
FILE* file_getfilepointer(const char* resourcename, int& filesize, const bool binary = false);
// Llig tot el contingut d'un recurs (fitxer solt o entrada del .jrf).
// Retorna un vector buit si el recurs no existeix. El vector es destrueix
// automàticament en eixir d'àmbit — no fa falta cap free() manual. Mida =
// bytes llegits (el buffer no està null-terminated).
std::vector<char> file_readfile(const char* resourcename);
const char* file_getconfigvalue(const char* key);
void file_setconfigvalue(const char* key, const char* value);

View File

@@ -4,7 +4,7 @@
#include <string>
#include <unordered_map>
#include "core/jail/jfile.hpp"
#include "core/resources/resource_helper.hpp"
#include "external/fkyaml_node.hpp"
namespace Locale {
@@ -27,12 +27,12 @@ namespace Locale {
}
bool load(const char* filename) {
auto buffer = file_readfile(filename);
auto buffer = ResourceHelper::loadFile(filename);
if (buffer.empty()) {
std::cerr << "Locale: unable to load " << filename << '\n';
return false;
}
std::string content(buffer.data(), buffer.size());
std::string content(reinterpret_cast<const char*>(buffer.data()), buffer.size());
try {
auto yaml = fkyaml::node::deserialize(content);

View File

@@ -8,7 +8,7 @@
#include <sstream>
#include <string>
#include "core/jail/jfile.hpp"
#include "core/resources/resource_helper.hpp"
// Forward declarations de gif.h (inclòs des de jdraw8.cpp, no es pot incloure dos vegades)
struct rgb;
@@ -62,13 +62,13 @@ auto Text::nextCodepoint(const char*& ptr) -> uint32_t {
// --- Càrrega de font ---
void Text::loadFont(const char* fnt_file) {
auto buffer = file_readfile(fnt_file);
auto buffer = ResourceHelper::loadFile(fnt_file);
if (buffer.empty()) {
std::cerr << "Text: unable to load font file: " << fnt_file << '\n';
return;
}
std::istringstream stream(std::string(buffer.data(), buffer.size()));
std::istringstream stream(std::string(reinterpret_cast<const char*>(buffer.data()), buffer.size()));
std::string line;
int glyph_index = 0;
@@ -126,14 +126,14 @@ void Text::loadFont(const char* fnt_file) {
}
void Text::loadBitmap(const char* gif_file) {
auto buffer = file_readfile(gif_file);
auto buffer = ResourceHelper::loadFile(gif_file);
if (buffer.empty()) {
std::cerr << "Text: unable to load bitmap: " << gif_file << '\n';
return;
}
// Extrau dimensions del header GIF (bytes 6-7 = width, 8-9 = height, little-endian)
auto* raw = reinterpret_cast<unsigned char*>(buffer.data());
auto* raw = buffer.data();
int w = raw[6] | (raw[7] << 8);
int h = raw[8] | (raw[9] << 8);

View File

@@ -2,9 +2,9 @@
#include "core/jail/jail_audio.hpp"
#include "core/jail/jdraw8.hpp"
#include "core/jail/jfile.hpp"
#include "core/jail/jgame.hpp"
#include "core/jail/jinput.hpp"
#include "core/resources/resource_helper.hpp"
ModuleGame::ModuleGame() {
this->gfx = JD8_LoadSurface(info::ctx.pepe_activat ? "frames2.gif" : "frames.gif");
@@ -49,8 +49,8 @@ void ModuleGame::onEnter() {
const char* current_music = JA_GetMusicFilename();
if ((JA_GetMusicState() != JA_MUSIC_PLAYING) || !current_music ||
strcmp(music, current_music) != 0) {
auto buffer = file_readfile(music);
JA_PlayMusic(JA_LoadMusic(reinterpret_cast<Uint8*>(buffer.data()),
auto buffer = ResourceHelper::loadFile(music);
JA_PlayMusic(JA_LoadMusic(buffer.data(),
static_cast<Uint32>(buffer.size()), music));
}

View File

@@ -19,6 +19,7 @@
#include "core/rendering/menu.hpp"
#include "core/rendering/overlay.hpp"
#include "core/rendering/screen.hpp"
#include "core/resources/resource_helper.hpp"
#include "core/system/director.hpp"
#include "game/options.hpp"
@@ -32,10 +33,27 @@ SDL_AppResult SDL_AppInit(void** /*appstate*/, int /*argc*/, char* /*argv*/[]) {
// SDL_GetBasePath() detecta automàticament si estem dins d'un .app bundle
// (retorna Contents/Resources/) o en un executable normal (carpeta del binari).
const char* base_path = SDL_GetBasePath();
std::string resource_pack_path;
if (base_path) {
const std::string data_path = std::string(base_path) + "data/";
file_setresourcefolder(data_path.c_str());
resource_pack_path = std::string(base_path) + "resource.pack";
} else {
resource_pack_path = "resource.pack";
}
// Sistema de recursos: prova el pack i cau a fitxers solts dins data/.
// Release natiu exigix el pack (sense fallback); Debug i WASM mantenen
// el fallback actiu per a desenvolupament i per al build amb MEMFS.
#if defined(NDEBUG) && !defined(__EMSCRIPTEN__)
const bool enable_fallback = false;
#else
const bool enable_fallback = true;
#endif
if (!ResourceHelper::initializeResourceSystem(resource_pack_path, enable_fallback)) {
return SDL_APP_FAILURE;
}
Options::setConfigFile(std::string(file_getconfigfolder()) + "config.yaml");
Options::loadFromFile();
@@ -102,4 +120,5 @@ void SDL_AppQuit(void* /*appstate*/, SDL_AppResult /*result*/) {
JD8_Quit();
Screen::destroy();
JG_Finalize();
ResourceHelper::shutdownResourceSystem();
}

View File

@@ -3,17 +3,17 @@
#include <SDL3/SDL.h>
#include "core/jail/jail_audio.hpp"
#include "core/jail/jfile.hpp"
#include "core/resources/resource_helper.hpp"
namespace scenes {
void playMusic(const char* filename, int loop) {
if (!filename) return;
auto buffer = file_readfile(filename);
auto buffer = ResourceHelper::loadFile(filename);
if (buffer.empty()) return;
// JA_LoadMusic fa una còpia interna del OGG comprimit (via SDL_malloc)
// per a stb_vorbis. El `buffer` local es destruirà en sortir d'àmbit.
JA_PlayMusic(JA_LoadMusic(reinterpret_cast<Uint8*>(buffer.data()),
JA_PlayMusic(JA_LoadMusic(buffer.data(),
static_cast<Uint32>(buffer.size()), filename),
loop);
}