#include "core/resources/resource_cache.hpp" #include #include #include #include "core/resources/resource_helper.hpp" #include "utils/utils.hpp" namespace Resource { auto Cache::get() -> Cache& { static Cache instance_; return instance_; } void Cache::loadSurface(const std::string& name, const std::string& file_path) { auto surface = std::make_shared(file_path); surfaces_.emplace_back(SurfaceResource{.name = name, .surface = std::move(surface)}); } void Cache::loadAnimation(const std::string& name, const std::string& file_path) { auto bytes = Helper::loadFile(file_path); if (bytes.empty()) { throw std::runtime_error("Cache: cannot load animation " + file_path); } animations_.emplace_back(AnimationResource{.name = name, .yaml_data = std::move(bytes)}); printWithDots("Animation : ", getFileName(file_path), "[ LOADED ]"); } auto Cache::getSurface(const std::string& name) -> std::shared_ptr { auto it = std::ranges::find_if(surfaces_, [&name](const auto& s) { return s.name == name; }); if (it == surfaces_.end()) { std::cerr << "Cache: surface not found: " << name << '\n'; throw std::runtime_error("Surface not found: " + name); } return it->surface; } auto Cache::getAnimationData(const std::string& name) -> const AnimationResource& { auto it = std::ranges::find_if(animations_, [&name](const auto& a) { return a.name == name; }); if (it == animations_.end()) { std::cerr << "Cache: animation not found: " << name << '\n'; throw std::runtime_error("Animation not found: " + name); } return *it; } } // namespace Resource