c45e524109
Pase automático de clang-tidy --fix sobre el conjunto de checks que son puro transform de sintaxis y no rompen API. Invocado con --format-style=none para que clang-tidy NO arrastre clang-format sobre las líneas tocadas (evita la regla NamespaceIndentation: All del .clang-format reformateando solo trozos del archivo). Checks aplicados: - modernize-use-trailing-return-type (193 hits): 'int foo()' → 'auto foo() -> int'. Estilo coherente con la convención del proyecto. - modernize-use-default-member-init (36 hits): inicialización de miembros pasa de la lista del constructor a la declaración. Reduce duplicación cuando hay varios constructores con los mismos defaults. - modernize-use-auto (6 hits): tipos largos sustituidos por auto donde el tipo es evidente del contexto (new T, dynamic_cast, etc). - modernize-use-starts-ends-with (2 hits): s.rfind(x) == 0 → s.starts_with(x), aprovechando C++20. - performance-enum-size (10 hits): enums pequeños declaran tipo subyacente (uint8_t / similar) para reducir tamaño y precisar layout. NO aplicado en este pase (riesgo de cambios semánticos o de API): - readability-identifier-naming (renames pueden romper callsites parciales) - readability-convert-member-functions-to-static (cambia firma) - readability-use-anyofallof (reescribe loops, side effects) - readability-function-cognitive-complexity (requiere refactor manual) - bugs reales (bugprone-*, clang-diagnostic-*) → uno a uno Cambios manuales asociados: - SDLManager::clear() ahora devuelve bool: propaga el resultado de beginFrame al caller para que Director::runFrameLoop salte draw+present cuando la swapchain no esté disponible (ventana minimizada). Antes la función ignoraba el [[nodiscard]] del beginFrame y los vértices se acumulaban en el batch sin nadie que los consumiera. - vector_text.cpp: borrada la línea suelta "// Test pre-commit hook" que quedó como cruft. clang-tidy crashea en LLVM 19.1 con performance-noexcept-move-constructor (recursión infinita en ExceptionSpecAnalyzer al procesar std::set); check deshabilitado en .clang-tidy con comentario explicativo. Build limpio, smoke test OK. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
87 lines
2.7 KiB
C++
87 lines
2.7 KiB
C++
// shape_loader.cpp - Implementació del carregador con caché
|
|
// © 2026 JailDesigner
|
|
|
|
#include "core/graphics/shape_loader.hpp"
|
|
|
|
#include <iostream>
|
|
|
|
#include "core/resources/resource_helper.hpp"
|
|
|
|
namespace Graphics {
|
|
|
|
// Inicialización de variables estàtiques
|
|
std::unordered_map<std::string, std::shared_ptr<Shape>> ShapeLoader::cache_;
|
|
std::string ShapeLoader::base_path_ = "data/shapes/";
|
|
|
|
auto ShapeLoader::load(const std::string& filename) -> std::shared_ptr<Shape> {
|
|
// Check cache first
|
|
auto it = cache_.find(filename);
|
|
if (it != cache_.end()) {
|
|
std::cout << "[ShapeLoader] Cache hit: " << filename << '\n';
|
|
return it->second; // Cache hit
|
|
}
|
|
|
|
// Normalize path: "ship.shp" → "shapes/ship.shp"
|
|
// "logo/letra_j.shp" → "shapes/logo/letra_j.shp"
|
|
std::string normalized = filename;
|
|
if (!normalized.starts_with("shapes/")) {
|
|
// Doesn't start with "shapes/", so add it
|
|
normalized = "shapes/" + normalized;
|
|
}
|
|
|
|
// Load from resource system
|
|
std::vector<uint8_t> data = Resource::Helper::loadFile(normalized);
|
|
if (data.empty()) {
|
|
std::cerr << "[ShapeLoader] Error: no s'ha pogut load " << normalized
|
|
<< '\n';
|
|
return nullptr;
|
|
}
|
|
|
|
// Convert bytes to string and parse
|
|
std::string file_content(data.begin(), data.end());
|
|
auto shape = std::make_shared<Shape>();
|
|
if (!shape->parseFile(file_content)) {
|
|
std::cerr << "[ShapeLoader] Error: no s'ha pogut parsejar " << normalized
|
|
<< '\n';
|
|
return nullptr;
|
|
}
|
|
|
|
// Verify shape is valid
|
|
if (!shape->isValid()) {
|
|
std::cerr << "[ShapeLoader] Error: shape invàlida " << normalized << '\n';
|
|
return nullptr;
|
|
}
|
|
|
|
// Cache and return
|
|
std::cout << "[ShapeLoader] Carregat: " << normalized << " (" << shape->getName()
|
|
<< ", " << shape->getNumPrimitives() << " primitives)" << '\n';
|
|
|
|
cache_[filename] = shape;
|
|
return shape;
|
|
}
|
|
|
|
void ShapeLoader::clear_cache() {
|
|
std::cout << "[ShapeLoader] Netejant caché (" << cache_.size() << " formes)"
|
|
<< '\n';
|
|
cache_.clear();
|
|
}
|
|
|
|
auto ShapeLoader::get_cache_size() -> size_t { return cache_.size(); }
|
|
|
|
auto ShapeLoader::resolve_path(const std::string& filename) -> std::string {
|
|
// Si es un path absolut (comença con '/'), usar-lo directament
|
|
if (!filename.empty() && filename[0] == '/') {
|
|
return filename;
|
|
}
|
|
|
|
// Si ya conté el prefix base_path, usar-lo directament
|
|
if (filename.starts_with(base_path_)) {
|
|
return filename;
|
|
}
|
|
|
|
// Altrament, añadir base_path (ara suporta subdirectoris)
|
|
return base_path_ + filename;
|
|
}
|
|
|
|
} // namespace Graphics
|