cache system add lonely functionnality

This commit is contained in:
arthur.jamet
2021-06-09 11:14:53 +02:00
parent aab9477571
commit 70524fa935
13 changed files with 46 additions and 27 deletions
+2 -2
View File
@@ -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))
{
}
+2 -1
View File
@@ -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;
+2 -2
View File
@@ -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))
{
}
+2 -1
View File
@@ -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;
+2 -2
View File
@@ -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);
}
+2 -1
View File
@@ -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;
+2 -2
View File
@@ -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)
{
}
+2 -1
View File
@@ -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;
+2 -2
View File
@@ -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))
{
}
+2 -1
View File
@@ -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;
+2 -2
View File
@@ -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)
+2 -1
View File
@@ -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<std::pair<MaterialType, std::string>> 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
+22 -9
View File
@@ -10,6 +10,9 @@
#include <unordered_map>
#include <functional>
#include <raylib.h>
#include <vector>
#include <algorithm>
#include <string>
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_ptr<T>fetch(const std::string &path)
std::shared_ptr<T>fetch(const std::string &path, bool lonely = false)
{
if (this->_cache.find(path) == this->_cache.end())
this->_cache.emplace(path, std::shared_ptr<T>(
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::shared_ptr<T>>());
std::vector<std::shared_ptr<T>> &matchingDataVector = this->_cache.at(path);
for (std::shared_ptr<T> &i: matchingDataVector) {
if (!lonely)
return i;
if (lonely && i.use_count() == 1)
return i;
}
matchingDataVector.emplace_back(std::shared_ptr<T>(
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<void(T)> _dataUnloader;
//! @brief map storing shared ptr of caches
std::unordered_map<std::string, std::shared_ptr<T>> _cache;
std::unordered_map<std::string, std::vector<std::shared_ptr<T>>> _cache;
};
template<>