From 812a4da727c40685c16958b66e19078bbfbf165e Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Tue, 1 Jun 2021 14:44:04 +0200 Subject: [PATCH] cache system for textures --- lib/Ray/sources/Drawables/Texture.cpp | 39 +++++++++++---------------- lib/Ray/sources/Drawables/Texture.hpp | 18 +++++++++---- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/lib/Ray/sources/Drawables/Texture.cpp b/lib/Ray/sources/Drawables/Texture.cpp index b2691a23..93b3d36b 100644 --- a/lib/Ray/sources/Drawables/Texture.cpp +++ b/lib/Ray/sources/Drawables/Texture.cpp @@ -9,40 +9,33 @@ namespace RAY { + std::unordered_map> Texture::_textureCache; + Texture::Texture(const std::string &filename): - _texture(LoadTexture(filename.c_str())), + _texture(fetchTextureInCache(filename)), _resourcePath(filename) { } - Texture::Texture(const Texture &texture): - _texture(LoadTexture(texture._resourcePath.c_str())), - _resourcePath(texture._resourcePath) - { - } - Texture::Texture(const Image &image): - _texture(LoadTextureFromImage(image)), + _texture(std::make_shared<::Texture>(LoadTextureFromImage(image))), _resourcePath() { } - - Texture &Texture::operator=(const Texture &other) - { - UnloadTexture(this->_texture); - this->_resourcePath = other._resourcePath; - this->_texture = LoadTexture(this->_resourcePath.c_str()); - return *this; - } - - Texture::~Texture() - { - UnloadTexture(this->_texture); - } - Texture::operator ::Texture() const { - return this->_texture; + return *this->_texture; + } + + std::shared_ptr<::Texture> Texture::fetchTextureInCache(const std::string &path) + { + if (Texture::_textureCache.find(path) == Texture::_textureCache.end()) + Texture::_textureCache.emplace(path, std::shared_ptr<::Texture>( + new ::Texture(LoadTexture(path.c_str())), [](::Texture *p) { + UnloadTexture(*p); + delete p; + })); + return _textureCache[path]; } } diff --git a/lib/Ray/sources/Drawables/Texture.hpp b/lib/Ray/sources/Drawables/Texture.hpp index e087275d..265a63c2 100644 --- a/lib/Ray/sources/Drawables/Texture.hpp +++ b/lib/Ray/sources/Drawables/Texture.hpp @@ -10,6 +10,8 @@ #include #include +#include +#include namespace RAY { @@ -21,24 +23,30 @@ namespace RAY Texture(const std::string &filename); //! @brief A texture is copy constructable - Texture(const Texture &); + Texture(const Texture &) = default; //! @brief A textrue can be loaded from an image Texture(const Image &); //! @brief An texture is assignable - Texture &operator=(const Texture &); + Texture &operator=(const Texture &) = default; - //! @brief Texture destructor, will unload ressources - ~Texture(); + //! @brief Texture destructor, will not unload ressources + ~Texture() = default; protected: private: //! @brief Texture, really, that's just it... - ::Texture _texture; + std::shared_ptr<::Texture> _texture; + //! @brief path to the file the texture is loaded from std::string _resourcePath; + //! @param path path of the file to load + //! @return a newly loaded texture if it hasn't be previously loaded, or one from cache + std::shared_ptr<::Texture> fetchTextureInCache(const std::string &path); + static std::unordered_map> _textureCache; + INTERNAL: //! @return libray Texture struct operator ::Texture() const;