Motor de sprites/animacions/surfaces i jugador centrat (v0.2.0)

This commit is contained in:
JailDesigner committed 2026-04-18 21:02:55 +02:00
1 parent 7bb1edd08a
commit de5a670fdb
34 files changed
+16137 -81

No files matched your search

+49
View File
@@ -0,0 +1,49 @@
#include "core/resources/resource_cache.hpp"
#include <algorithm>
#include <iostream>
#include <stdexcept>
#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<Surface>(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<Surface> {
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