generic cache system

This commit is contained in:
arthur.jamet
2021-06-01 17:00:00 +02:00
parent 812a4da727
commit 382b830b27
12 changed files with 115 additions and 74 deletions

View File

@@ -12,7 +12,8 @@
namespace RAY::Drawables::Drawables3D {
std::unordered_map<std::string, std::shared_ptr<::Model>> Model::_modelsCache;
RAY::Cache<::Model> Model::_modelsCache(LoadModel, UnloadModel);
Model::Model(const std::string &filename,
std::optional<std::pair<MaterialType, std::string>> texture,
const RAY::Vector3 &position,
@@ -20,7 +21,7 @@ namespace RAY::Drawables::Drawables3D {
float rotationAngle,
const RAY::Vector3 &scale)
: ADrawable3D(position, WHITE),
_model(fetchModelInCache(filename)),
_model(_modelsCache.fetch(filename)),
_rotationAxis(rotationAxis),
_rotationAngle(rotationAngle),
_scale(scale)
@@ -105,15 +106,4 @@ namespace RAY::Drawables::Drawables3D {
{
DrawModelEx(*this->_model, this->_position, this->_rotationAxis, this->_rotationAngle, this->_scale, this->_color);
}
std::shared_ptr<::Model> Model::fetchModelInCache(const std::string &path)
{
if (Model::_modelsCache.find(path) == Model::_modelsCache.end())
Model::_modelsCache.emplace(path, std::shared_ptr<::Model>(
new ::Model(LoadModel(path.c_str())), [](::Model *p) {
UnloadModel(*p);
delete p;
}));
return _modelsCache[path];
}
}