afegit resources.pack y prefixe a les rutes de recursos

This commit is contained in:
2025-12-08 21:48:52 +01:00
parent 4b7cbd88bb
commit a41e696b69
21 changed files with 1066 additions and 36 deletions

View File

@@ -32,6 +32,9 @@ class Shape {
// Carregar forma des de fitxer .shp
bool carregar(const std::string& filepath);
// Parsejar forma des de buffer de memòria (per al sistema de recursos)
bool parsejar_fitxer(const std::string& contingut);
// Getters
const std::vector<ShapePrimitive>& get_primitives() const {
return primitives_;
@@ -50,9 +53,6 @@ class Shape {
float escala_defecte_; // Escala per defecte (normalment 1.0)
std::string nom_; // Nom de la forma (per depuració)
// Parsejador del fitxer
bool parsejar_fitxer(const std::string& contingut);
// Helpers privats per parsejar
std::string trim(const std::string& str) const;
bool starts_with(const std::string& str, const std::string& prefix) const;

View File

@@ -3,6 +3,8 @@
#include "core/graphics/shape_loader.hpp"
#include "core/resources/resource_helper.hpp"
#include <iostream>
namespace Graphics {
@@ -19,28 +21,40 @@ std::shared_ptr<Shape> ShapeLoader::load(const std::string& filename) {
return it->second; // Cache hit
}
// Resolve full path
std::string fullpath = resolve_path(filename);
// Normalize path: "ship.shp" → "shapes/ship.shp"
// "logo/letra_j.shp" → "shapes/logo/letra_j.shp"
std::string normalized = filename;
if (normalized.find("shapes/") != 0) {
// Doesn't start with "shapes/", so add it
normalized = "shapes/" + normalized;
}
// Create and load shape
// Load from resource system
std::vector<uint8_t> data = Resource::Helper::loadFile(normalized);
if (data.empty()) {
std::cerr << "[ShapeLoader] Error: no s'ha pogut carregar " << normalized
<< std::endl;
return nullptr;
}
// Convert bytes to string and parse
std::string file_content(data.begin(), data.end());
auto shape = std::make_shared<Shape>();
if (!shape->carregar(fullpath)) {
std::cerr << "[ShapeLoader] Error: no s'ha pogut carregar " << filename
if (!shape->parsejar_fitxer(file_content)) {
std::cerr << "[ShapeLoader] Error: no s'ha pogut parsejar " << normalized
<< std::endl;
return nullptr;
}
// Verify shape is valid
if (!shape->es_valida()) {
std::cerr << "[ShapeLoader] Error: forma invàlida " << filename
<< std::endl;
std::cerr << "[ShapeLoader] Error: forma invàlida " << normalized << std::endl;
return nullptr;
}
// Cache and return
std::cout << "[ShapeLoader] Carregat: " << filename << " ("
<< shape->get_nom() << ", " << shape->get_num_primitives()
<< " primitives)" << std::endl;
std::cout << "[ShapeLoader] Carregat: " << normalized << " (" << shape->get_nom()
<< ", " << shape->get_num_primitives() << " primitives)" << std::endl;
cache_[filename] = shape;
return shape;

View File

@@ -8,6 +8,7 @@
#include <iostream>
#include "core/defaults.hpp"
#include "core/graphics/shape_loader.hpp"
#include "core/rendering/shape_renderer.hpp"
namespace Graphics {
@@ -21,11 +22,11 @@ Starfield::Starfield(SDL_Renderer* renderer,
punt_fuga_(punt_fuga),
area_(area),
densitat_(densitat) {
// Carregar forma d'estrella
shape_estrella_ = std::make_shared<Shape>("data/shapes/star.shp");
// Carregar forma d'estrella amb ShapeLoader
shape_estrella_ = ShapeLoader::load("star.shp");
if (!shape_estrella_->es_valida()) {
std::cerr << "ERROR: No s'ha pogut carregar data/shapes/star.shp" << std::endl;
if (!shape_estrella_ || !shape_estrella_->es_valida()) {
std::cerr << "ERROR: No s'ha pogut carregar star.shp" << std::endl;
return;
}