9f0dfc4e24
afegida gestió de ratolí
73 lines
2.1 KiB
C++
73 lines
2.1 KiB
C++
// shape_loader.cpp - Implementació del carregador amb caché
|
|
// © 2025 Port a C++20 amb SDL3
|
|
|
|
#include "core/graphics/shape_loader.hpp"
|
|
|
|
#include <iostream>
|
|
|
|
namespace Graphics {
|
|
|
|
// Inicialització de variables estàtiques
|
|
std::unordered_map<std::string, std::shared_ptr<Shape>> ShapeLoader::cache_;
|
|
std::string ShapeLoader::base_path_ = "data/shapes/";
|
|
|
|
std::shared_ptr<Shape> ShapeLoader::load(const std::string& filename) {
|
|
// Check cache first
|
|
auto it = cache_.find(filename);
|
|
if (it != cache_.end()) {
|
|
std::cout << "[ShapeLoader] Cache hit: " << filename << std::endl;
|
|
return it->second; // Cache hit
|
|
}
|
|
|
|
// Resolve full path
|
|
std::string fullpath = resolve_path(filename);
|
|
|
|
// Create and load shape
|
|
auto shape = std::make_shared<Shape>();
|
|
if (!shape->carregar(fullpath)) {
|
|
std::cerr << "[ShapeLoader] Error: no s'ha pogut carregar " << filename
|
|
<< std::endl;
|
|
return nullptr;
|
|
}
|
|
|
|
// Verify shape is valid
|
|
if (!shape->es_valida()) {
|
|
std::cerr << "[ShapeLoader] Error: forma invàlida " << filename
|
|
<< std::endl;
|
|
return nullptr;
|
|
}
|
|
|
|
// Cache and return
|
|
std::cout << "[ShapeLoader] Carregat: " << filename << " ("
|
|
<< shape->get_nom() << ", " << shape->get_num_primitives()
|
|
<< " primitives)" << std::endl;
|
|
|
|
cache_[filename] = shape;
|
|
return shape;
|
|
}
|
|
|
|
void ShapeLoader::clear_cache() {
|
|
std::cout << "[ShapeLoader] Netejant caché (" << cache_.size() << " formes)"
|
|
<< std::endl;
|
|
cache_.clear();
|
|
}
|
|
|
|
size_t ShapeLoader::get_cache_size() { return cache_.size(); }
|
|
|
|
std::string ShapeLoader::resolve_path(const std::string& filename) {
|
|
// Si és un path absolut (comença amb '/'), usar-lo directament
|
|
if (!filename.empty() && filename[0] == '/') {
|
|
return filename;
|
|
}
|
|
|
|
// Si ja conté el prefix base_path, usar-lo directament
|
|
if (filename.find(base_path_) == 0) {
|
|
return filename;
|
|
}
|
|
|
|
// Altrament, afegir base_path (ara suporta subdirectoris)
|
|
return base_path_ + filename;
|
|
}
|
|
|
|
} // namespace Graphics
|