putting shaders changes on a new branch to avoid merge nightmare

This commit is contained in:
Clément Le Bihan
2021-06-08 12:41:32 +02:00
parent 6ff8a20030
commit 4ee45c9c28
6 changed files with 168 additions and 2 deletions

View File

@@ -9,7 +9,9 @@
#include <memory>
#include <unordered_map>
#include <functional>
#include <utility>
#include <raylib.h>
#include "Exceptions/RayError.hpp"
namespace RAY {
//! @brief A templated class used to cache ressources, indexed with a string
@@ -57,7 +59,7 @@ namespace RAY {
class Cache<::ModelAnimation> {
public:
Cache(std::function<::ModelAnimation *(const char *, int *)> dataLoader, std::function<void(::ModelAnimation *, unsigned int)>dataUnloader):
_dataLoader(dataLoader), _dataUnloader(dataUnloader)
_dataLoader(std::move(dataLoader)), _dataUnloader(std::move(dataUnloader))
{};
std::shared_ptr<::ModelAnimation> fetch(const std::string &path, int *counter)
{
@@ -83,4 +85,42 @@ namespace RAY {
//! @brief map storing shared ptr of caches
std::unordered_map<std::string, std::shared_ptr<::ModelAnimation>> _cache;
};
template<>
class Cache<::Shader>
{
public:
Cache(std::function<::Shader(const char *, const char *)> dataLoader,
std::function<void(::Shader)> dataUnloader) :
_dataLoader(std::move(dataLoader)), _dataUnloader(std::move(dataUnloader))
{};
std::shared_ptr<::Shader> fetch(const std::string &vertexFile, const std::string &fragmentFile)
{
const std::string index = vertexFile + fragmentFile;
if (vertexFile.empty() && fragmentFile.empty()) {
throw RAY::Exception::WrongInputError();
}
if (this->_cache.find(index) != this->_cache.end())
return this->_cache[index];
this->_cache.emplace(index, std::shared_ptr<::Shader>(
new ::Shader(
this->_dataLoader(vertexFile.empty() ? nullptr : vertexFile.c_str(), fragmentFile.c_str())),
[this](::Shader *p) {
this->_dataUnloader(*p);
}));
return this->_cache[index];
};
private:
//! @brief function to call to load data
std::function<::Shader(const char *, const char *)> _dataLoader;
//! @brief function to call when the ray data will be unloaded
std::function<void(::Shader)> _dataUnloader;
//! @brief map storing shared ptr of caches
std::unordered_map<std::string, std::shared_ptr<::Shader>> _cache;
};
}