From f8ca87cd9dc94b4bc98840d04841bff49f228682 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Mon, 31 May 2021 18:22:24 +0200 Subject: [PATCH] model now use shared ptr + cache system to spare memmory --- lib/Ray/sources/Model/Model.cpp | 38 +++++++++++++++++++++------------ lib/Ray/sources/Model/Model.hpp | 15 ++++++++----- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/lib/Ray/sources/Model/Model.cpp b/lib/Ray/sources/Model/Model.cpp index cb4cd024..f3e396d6 100644 --- a/lib/Ray/sources/Model/Model.cpp +++ b/lib/Ray/sources/Model/Model.cpp @@ -7,9 +7,12 @@ #include "Model/Model.hpp" #include "Exceptions/RayError.hpp" +#include + namespace RAY::Drawables::Drawables3D { + std::unordered_map> Model::_modelsCache; Model::Model(const std::string &filename, std::optional> texture, const RAY::Vector3 &position, @@ -17,7 +20,7 @@ namespace RAY::Drawables::Drawables3D { float rotationAngle, const RAY::Vector3 &scale) : ADrawable3D(position, WHITE), - _model(LoadModel(filename.c_str())), + _model(fetchModelInCache(filename)), _rotationAxis(rotationAxis), _rotationAngle(rotationAngle), _scale(scale) @@ -27,33 +30,29 @@ namespace RAY::Drawables::Drawables3D { } Model::Model(const Mesh &mesh) - : ADrawable3D({0, 0, 0}, WHITE), _model(LoadModelFromMesh(mesh)) + : ADrawable3D({0, 0, 0}, WHITE), + _model(std::make_shared<::Model>(LoadModelFromMesh(mesh))) { } - Model::~Model() - { - UnloadModel(this->_model); - } - bool Model::unloadKeepMeshes() { - UnloadModelKeepMeshes(_model); + UnloadModelKeepMeshes(*this->_model); return true; } bool Model::setAnimation(const RAY::ModelAnimation &animation) { - if (!IsModelAnimationValid(this->_model, animation)) + if (!IsModelAnimationValid(*this->_model, animation)) throw RAY::Exception::NotCompatibleError("The animation is not compatible with the model"); - UpdateModelAnimation(this->_model, animation, animation.getFrameCounter()); + UpdateModelAnimation(*this->_model, animation, animation.getFrameCounter()); return true; } bool Model::setTextureToMaterial(Model::MaterialType materialType, const std::string &texturePath) { this->_textureList.emplace(materialType, texturePath); - SetMaterialTexture(&this->_model.materials[materialType], + SetMaterialTexture(&this->_model->materials[materialType], materialType, this->_textureList.at(materialType)); return true; @@ -61,12 +60,12 @@ namespace RAY::Drawables::Drawables3D { Model::operator ::Model() const { - return this->_model; + return *this->_model; } int Model::getBoneCount() const { - return this->_model.boneCount; + return this->_model->boneCount; } Model &Model::setRotationAngle(float rotationAngle) @@ -104,6 +103,17 @@ namespace RAY::Drawables::Drawables3D { void Model::drawOn(RAY::Window &) { - DrawModelEx(this->_model, this->_position, this->_rotationAxis, this->_rotationAngle, this->_scale, this->_color); + 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())), [](auto p) { + UnloadModel(*p); + delete p; + })); + return _modelsCache[path]; } } \ No newline at end of file diff --git a/lib/Ray/sources/Model/Model.hpp b/lib/Ray/sources/Model/Model.hpp index 039304ae..01bb5c13 100644 --- a/lib/Ray/sources/Model/Model.hpp +++ b/lib/Ray/sources/Model/Model.hpp @@ -14,7 +14,8 @@ #include #include #include -#include +#include +#include namespace RAY::Drawables::Drawables3D { //! @brief Basic 3D Model type @@ -42,8 +43,8 @@ namespace RAY::Drawables::Drawables3D { //! @brief A model is assignable Model& operator=(const Model &model) = default; - //! @brief Model destructor, unloads all related data - ~Model(); + //! @brief Model destructor, model's data will be deleted if it's the last entity alive + ~Model() = default; //! @brief Unload model (excluding meshes) from memory (RAM and/or VRAM) bool unloadKeepMeshes(); @@ -81,15 +82,19 @@ namespace RAY::Drawables::Drawables3D { private: //! @brief Raw data from raylib - ::Model _model; + std::shared_ptr<::Model> _model; //! @brief The list of textures that can be applied to this model. - std::map _textureList; + std::unordered_map _textureList; //! @brief Rotation property RAY::Vector3 _rotationAxis; //! @brief Rotation property float _rotationAngle; //! @brief Scale of the shape RAY::Vector3 _scale; + //! @brief, look through cache to see if a model using same file + std::shared_ptr<::Model>fetchModelInCache(const std::string &path); + + static std::unordered_map> _modelsCache; INTERNAL: //! @brief A RAY Model is cast-able in libray's model