diff --git a/lib/Ray/sources/Audio/Music.cpp b/lib/Ray/sources/Audio/Music.cpp index db3dae78..670ba2f8 100644 --- a/lib/Ray/sources/Audio/Music.cpp +++ b/lib/Ray/sources/Audio/Music.cpp @@ -10,8 +10,8 @@ RAY::Cache<::Music> RAY::Audio::Music::_musicsCache(LoadMusicStream, UnloadMusicStream); -RAY::Audio::Music::Music(const std::string &path): - _music(this->_musicsCache.fetch(path.c_str())) +RAY::Audio::Music::Music(const std::string &path, bool lonely): + _music(this->_musicsCache.fetch(path, lonely)) { } diff --git a/lib/Ray/sources/Audio/Music.hpp b/lib/Ray/sources/Audio/Music.hpp index c28e8089..2c35bfcd 100644 --- a/lib/Ray/sources/Audio/Music.hpp +++ b/lib/Ray/sources/Audio/Music.hpp @@ -19,7 +19,8 @@ namespace RAY::Audio public: //! @brief Load Music stream from file - Music(const std::string &path); + //! @param lonely: should be set to true if the entity's loaded data must be independant from others + Music(const std::string &path, bool lonely = false); //! @brief Default destructor ~Music() = default; diff --git a/lib/Ray/sources/Audio/Sound.cpp b/lib/Ray/sources/Audio/Sound.cpp index da8df7ba..de1002bc 100644 --- a/lib/Ray/sources/Audio/Sound.cpp +++ b/lib/Ray/sources/Audio/Sound.cpp @@ -9,8 +9,8 @@ RAY::Cache<::Sound> RAY::Audio::Sound::_soundsCache(LoadSound, UnloadSound); -RAY::Audio::Sound::Sound(const std::string &path): - _sound(_soundsCache.fetch(path.c_str())) +RAY::Audio::Sound::Sound(const std::string &path, bool lonely): + _sound(_soundsCache.fetch(path, lonely)) { } diff --git a/lib/Ray/sources/Audio/Sound.hpp b/lib/Ray/sources/Audio/Sound.hpp index 00ba3ddc..0f7a7ff5 100644 --- a/lib/Ray/sources/Audio/Sound.hpp +++ b/lib/Ray/sources/Audio/Sound.hpp @@ -20,7 +20,8 @@ namespace RAY::Audio public: //! @brief Load Sound stream from file - Sound(const std::string &path); + //! @param lonely: should be set to true if the entity's loaded data must be independant from others + Sound(const std::string &path, bool lonely = false); //! @brief Default destructor ~Sound() = default; diff --git a/lib/Ray/sources/Drawables/Image.cpp b/lib/Ray/sources/Drawables/Image.cpp index 9676bc95..685aa376 100644 --- a/lib/Ray/sources/Drawables/Image.cpp +++ b/lib/Ray/sources/Drawables/Image.cpp @@ -12,9 +12,9 @@ namespace RAY { Cache<::Image> Image::_imagesCache(LoadImage, UnloadImage); - Image::Image(const std::string &filename): + Image::Image(const std::string &filename, bool lonely): Rectangle(Vector2(0, 0), Vector2(0, 0), WHITE), - _image(_imagesCache.fetch(filename)) + _image(_imagesCache.fetch(filename, lonely)) { this->_dimensions = Vector2(this->_image->width, this->_image->height); } diff --git a/lib/Ray/sources/Drawables/Image.hpp b/lib/Ray/sources/Drawables/Image.hpp index 840be518..911f1a96 100644 --- a/lib/Ray/sources/Drawables/Image.hpp +++ b/lib/Ray/sources/Drawables/Image.hpp @@ -21,7 +21,8 @@ namespace RAY public: //! @brief Create an image, loading a file //! @param filename: path to file to load - Image(const std::string &filename); + //! @param lonely: should be set to true if the entity's loaded data must be independant from others + Image(const std::string &filename, bool lonely = false); //! @brief A default copy constructor Image(const Image &image) = default; diff --git a/lib/Ray/sources/Drawables/Texture.cpp b/lib/Ray/sources/Drawables/Texture.cpp index befe8579..821a743d 100644 --- a/lib/Ray/sources/Drawables/Texture.cpp +++ b/lib/Ray/sources/Drawables/Texture.cpp @@ -11,8 +11,8 @@ namespace RAY { Cache<::Texture> Texture::_texturesCache(LoadTexture, UnloadTexture); - Texture::Texture(const std::string &filename): - _texture(_texturesCache.fetch(filename)), + Texture::Texture(const std::string &filename, bool lonely): + _texture(_texturesCache.fetch(filename, lonely)), _resourcePath(filename) { } diff --git a/lib/Ray/sources/Drawables/Texture.hpp b/lib/Ray/sources/Drawables/Texture.hpp index 6fb2be66..2952b58d 100644 --- a/lib/Ray/sources/Drawables/Texture.hpp +++ b/lib/Ray/sources/Drawables/Texture.hpp @@ -19,7 +19,8 @@ namespace RAY public: //! @brief Create an texture, loading a file //! @param filename: path to file to load - Texture(const std::string &filename); + //! @param lonely: should be set to true if the entity's loaded data must be independant from others + Texture(const std::string &filename, bool lonely = false); //! @brief A texture is copy constructable Texture(const Texture &) = default; diff --git a/lib/Ray/sources/Font.cpp b/lib/Ray/sources/Font.cpp index c4288463..89bf9517 100644 --- a/lib/Ray/sources/Font.cpp +++ b/lib/Ray/sources/Font.cpp @@ -9,8 +9,8 @@ RAY::Cache<::Font> RAY::Font::_fontsCache(LoadFont, UnloadFont); -RAY::Font::Font(const std::string &filename): - _font(_fontsCache.fetch(filename)) +RAY::Font::Font(const std::string &filename, bool lonely): + _font(_fontsCache.fetch(filename, lonely)) { } diff --git a/lib/Ray/sources/Font.hpp b/lib/Ray/sources/Font.hpp index a403dcb3..1aaeb173 100644 --- a/lib/Ray/sources/Font.hpp +++ b/lib/Ray/sources/Font.hpp @@ -19,7 +19,8 @@ namespace RAY public: //! @brief Create an font, loading a file //! @param filename: path to file to load - Font(const std::string &filename); + //! @param lonely: should be set to true if the entity's loaded data must be independant from others + Font(const std::string &filename, bool lonely = false); //! @brief A default copy constructor Font(const Font &) = default; diff --git a/lib/Ray/sources/Model/Model.cpp b/lib/Ray/sources/Model/Model.cpp index aab5fdc6..77951483 100644 --- a/lib/Ray/sources/Model/Model.cpp +++ b/lib/Ray/sources/Model/Model.cpp @@ -19,9 +19,9 @@ namespace RAY::Drawables::Drawables3D { const RAY::Vector3 &scale, const RAY::Vector3 &position, const RAY::Vector3 &rotationAxis, - float rotationAngle) + float rotationAngle, bool lonely) : ADrawable3D(position, WHITE), - _model(_modelsCache.fetch(filename)), + _model(_modelsCache.fetch(filename, lonely)), _rotationAxis(rotationAxis), _rotationAngle(rotationAngle), _scale(scale) diff --git a/lib/Ray/sources/Model/Model.hpp b/lib/Ray/sources/Model/Model.hpp index 7a7e3fdf..40c9a9dd 100644 --- a/lib/Ray/sources/Model/Model.hpp +++ b/lib/Ray/sources/Model/Model.hpp @@ -25,12 +25,13 @@ namespace RAY::Drawables::Drawables3D { //! @brief Create an model, loading a file //! @param filePath: path to file to load + //! @param lonely: should be set to true if the entity's loaded data must be independant from others Model(const std::string &filePath, std::optional> texture = std::nullopt, const RAY::Vector3 &scale = RAY::Vector3(1, 1, 1), const RAY::Vector3 &position = {0, 0, 0}, const RAY::Vector3 &rotationAxis = RAY::Vector3(0, 1, 0), - float rotationAngle = 0); + float rotationAngle = 0, bool lonely = false); //! @brief Create an model, loading a file //! @param mesh: mesh to load diff --git a/lib/Ray/sources/Utils/Cache.hpp b/lib/Ray/sources/Utils/Cache.hpp index 653933f5..c0e370d1 100644 --- a/lib/Ray/sources/Utils/Cache.hpp +++ b/lib/Ray/sources/Utils/Cache.hpp @@ -10,6 +10,9 @@ #include #include #include +#include +#include +#include namespace RAY { //! @brief A templated class used to cache ressources, indexed with a string @@ -31,16 +34,26 @@ namespace RAY { Cache &operator=(const Cache &) = default; //! @param path path of the file + //! @param lonely: should be set to true if the loaded data must be held by no other active entites //! @return a newly loaded ressource if it hasn't be previously loaded, or one from cache - std::shared_ptrfetch(const std::string &path) + std::shared_ptrfetch(const std::string &path, bool lonely = false) { - if (this->_cache.find(path) == this->_cache.end()) - this->_cache.emplace(path, std::shared_ptr( - new T(this->_dataLoader(path.c_str())), [this](T *p) { - this->_dataUnloader(*p); - delete p; - })); - return _cache[path]; + if (!this->_cache.contains(path)) + this->_cache.emplace(path, std::vector>()); + std::vector> &matchingDataVector = this->_cache.at(path); + + for (std::shared_ptr &i: matchingDataVector) { + if (!lonely) + return i; + if (lonely && i.use_count() == 1) + return i; + } + matchingDataVector.emplace_back(std::shared_ptr( + new T(this->_dataLoader(path.c_str())), [this](T *p) { + this->_dataUnloader(*p); + delete p; + })); + return matchingDataVector.back(); }; private: //! @brief function to call to load data @@ -50,7 +63,7 @@ namespace RAY { std::function _dataUnloader; //! @brief map storing shared ptr of caches - std::unordered_map> _cache; + std::unordered_map>> _cache; }; template<>