fix(resources): resoldre rutes de data/ respecte a SDL_GetBasePath

Abans el binari només arrencava si la CWD era la que contenia data/
(típicament build/). Executat des de qualsevol altra carpeta no trobava
keys.yaml, paletes, etc. i es moria al carregar el primer recurs.

Patró seguit de ../aee: Resource::Helper guarda un data root absolut
i resolve(rel) el prefixa davant de rutes no absolutes. Director fa
setDataRoot(SDL_GetBasePath()) com a primera acció del ctor, així tots
els loaders posteriors hi passen sense canvis al call site.

Migrats a Resource::Helper::resolve els loaders que obrien std::ifstream
directament amb el path rebut: Locale, KeyConfig, Player, Map, Tilemap
i EntityLoader. Els que ja usaven Resource::Helper::loadFile
(Resource::Cache, PaletteLoader) hereten el fix automàticament.

Els fitxers de system_folder_ (config.yaml, debug.yaml, postfx.yaml,
crtpi.yaml) es passen com a rutes absolutes i resolve() les deixa
intactes, així no es veuen afectats.

Test targets (test_entities_smoke, test_scene_flow) enllacen ara
resource_helper.cpp perquè el seu codi passa per Helper::resolve. Els
tests corren amb CWD = repo root, on les rutes "data/..." funcionen
amb data root buit sense canviar cap assert.

Verificat executant el binari des de build/, /tmp i / — tots tres
troben els recursos i arrenquen correctament, tant en debug com en
release (release sempre arranca a LOGO).
This commit is contained in:
JailDesigner committed 2026-04-22 11:29:51 +02:00
1 parent 9c9b6028e7
commit 6800786194
10 files changed
+88 -27

No files matched your search

+7 -5
View File
@@ -4,6 +4,7 @@
#include <stdexcept>
#include <unordered_map>
#include "core/resources/resource_helper.hpp"
#include "game/entities/entity.hpp"
#include "game/entities/entity_factory.hpp"
@@ -30,10 +31,10 @@ namespace EntityLoader {
auto it = cache.find(name);
if (it != cache.end()) { return it->second; }
const std::string PATH = templatePath(name);
std::ifstream file(PATH);
const std::string FULL = Resource::Helper::resolve(templatePath(name));
std::ifstream file(FULL);
if (!file.is_open()) {
throw std::runtime_error("EntityLoader: no s'ha pogut obrir " + PATH);
throw std::runtime_error("EntityLoader: no s'ha pogut obrir " + FULL);
}
fkyaml::node node = fkyaml::node::deserialize(file);
cache.emplace(name, node);
@@ -41,9 +42,10 @@ namespace EntityLoader {
}
auto readSpawns(const std::string& level_yaml_path) -> std::vector<fkyaml::node> {
std::ifstream file(level_yaml_path);
const auto FULL = Resource::Helper::resolve(level_yaml_path);
std::ifstream file(FULL);
if (!file.is_open()) {
throw std::runtime_error("EntityLoader: no s'ha pogut obrir " + level_yaml_path);
throw std::runtime_error("EntityLoader: no s'ha pogut obrir " + FULL);
}
fkyaml::node root = fkyaml::node::deserialize(file);
std::vector<fkyaml::node> spawns;
+5 -3
View File
@@ -9,6 +9,7 @@
#include <utility>
#include "core/resources/resource_cache.hpp"
#include "core/resources/resource_helper.hpp"
#include "external/fkyaml_node.hpp"
#include "game/entities/event_bus.hpp"
#include "utils/defaults.hpp"
@@ -21,9 +22,10 @@ namespace {
} // namespace
void Player::loadFrom(const std::string& yaml_path) {
std::ifstream file(yaml_path);
const auto FULL = Resource::Helper::resolve(yaml_path);
std::ifstream file(FULL);
if (!file.is_open()) {
throw std::runtime_error("Player: no s'ha pogut obrir " + yaml_path);
throw std::runtime_error("Player: no s'ha pogut obrir " + FULL);
}
auto yaml = fkyaml::node::deserialize(file);
@@ -31,7 +33,7 @@ void Player::loadFrom(const std::string& yaml_path) {
sprite_h = yaml["frameHeight"].get_value<int>();
if (!yaml.contains("collision")) {
throw std::runtime_error("Player: manca bloc `collision:` a " + yaml_path);
throw std::runtime_error("Player: manca bloc `collision:` a " + FULL);
}
const auto& c = yaml["collision"];
collision_size = c["size"].get_value<int>();
+6 -4
View File
@@ -4,6 +4,7 @@
#include <iostream>
#include <stdexcept>
#include "core/resources/resource_helper.hpp"
#include "external/fkyaml_node.hpp"
namespace {
@@ -21,13 +22,14 @@ namespace {
namespace Map {
void loadFrom(const std::string& yaml_path) {
std::ifstream file(yaml_path);
const auto FULL = Resource::Helper::resolve(yaml_path);
std::ifstream file(FULL);
if (!file.is_open()) {
throw std::runtime_error("Map: no s'ha pogut obrir " + yaml_path);
throw std::runtime_error("Map: no s'ha pogut obrir " + FULL);
}
auto yaml = fkyaml::node::deserialize(file);
if (!yaml.contains("geometry")) {
throw std::runtime_error("Map: manca bloc `geometry:` a " + yaml_path);
throw std::runtime_error("Map: manca bloc `geometry:` a " + FULL);
}
const auto& g = yaml["geometry"];
@@ -76,7 +78,7 @@ namespace Map {
std::cout << "Map: geometria " << grid_cols << "x" << grid_rows
<< " sarcs " << sarc_w << "x" << sarc_h
<< " pitch " << pitch_x << "x" << pitch_y
<< " des de " << yaml_path << '\n';
<< " des de " << FULL << '\n';
}
auto getSarcRect(int col, int row) -> SDL_FRect {
+5 -3
View File
@@ -12,6 +12,7 @@
#include "core/rendering/screen.hpp"
#include "core/rendering/surface.hpp"
#include "core/resources/resource_cache.hpp"
#include "core/resources/resource_helper.hpp"
#include "external/fkyaml_node.hpp"
namespace {
@@ -67,9 +68,10 @@ namespace {
} // namespace
Tilemap::Tilemap(const std::string& yaml_path) {
std::ifstream file(yaml_path);
const auto FULL = Resource::Helper::resolve(yaml_path);
std::ifstream file(FULL);
if (!file.is_open()) {
throw std::runtime_error("Tilemap: no s'ha pogut obrir " + yaml_path);
throw std::runtime_error("Tilemap: no s'ha pogut obrir " + FULL);
}
auto yaml = fkyaml::node::deserialize(file);
@@ -111,7 +113,7 @@ Tilemap::Tilemap(const std::string& yaml_path) {
}
std::cout << "Tilemap: " << COLS << "x" << ROWS << " tiles ("
<< width_px_ << "x" << height_px_ << " px) des de " << yaml_path << '\n';
<< width_px_ << "x" << height_px_ << " px) des de " << FULL << '\n';
}
void Tilemap::render() const {