Files
aee-arcade/source/core/resources/resource_cache.cpp
T

66 lines
2.5 KiB
C++

#include "core/resources/resource_cache.hpp"
#include <algorithm>
#include <iostream>
#include <stdexcept>
#include "core/resources/palette_loader.hpp"
#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 ]");
}
void Cache::loadPalette(const std::string& name, const std::string& file_path) {
auto palette = PaletteLoader::loadFromFile(file_path);
palettes_.emplace_back(PaletteResource{.name = name, .palette = palette});
printWithDots("Palette : ", 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;
}
auto Cache::getPalette(const std::string& name) -> const Palette& {
auto it = std::ranges::find_if(palettes_, [&name](const auto& p) { return p.name == name; });
if (it == palettes_.end()) {
std::cerr << "Cache: palette not found: " << name << '\n';
throw std::runtime_error("Palette not found: " + name);
}
return it->palette;
}
} // namespace Resource