diff --git a/lib/Ray/CMakeLists.txt b/lib/Ray/CMakeLists.txt index d713eb1e..9a55f6be 100644 --- a/lib/Ray/CMakeLists.txt +++ b/lib/Ray/CMakeLists.txt @@ -97,7 +97,7 @@ set(SRC sources/Model/ModelAnimations.cpp sources/Vector/Vector2.cpp sources/Vector/Vector3.cpp -) + sources/Shaders/Shaders.cpp sources/Shaders/Shaders.hpp) find_package(raylib QUIET) if (NOT raylib_FOUND) diff --git a/lib/Ray/sources/Exceptions/RayError.cpp b/lib/Ray/sources/Exceptions/RayError.cpp index bc2ba888..a4de1200 100644 --- a/lib/Ray/sources/Exceptions/RayError.cpp +++ b/lib/Ray/sources/Exceptions/RayError.cpp @@ -21,3 +21,8 @@ RAY::Exception::NotCompatibleError::NotCompatibleError(const std::string &expect RayError(expectionMessage) { } + +RAY::Exception::WrongInputError::WrongInputError(const std::string &what): + RayError(what) +{ +} diff --git a/lib/Ray/sources/Exceptions/RayError.hpp b/lib/Ray/sources/Exceptions/RayError.hpp index f700ac17..158640d6 100644 --- a/lib/Ray/sources/Exceptions/RayError.hpp +++ b/lib/Ray/sources/Exceptions/RayError.hpp @@ -59,6 +59,23 @@ namespace RAY::Exception { //! @brief A default assignment operator NotSupportedError &operator=(const NotSupportedError &) = default; }; + + + //! @brief exception used when an non-supported operation is done + class WrongInputError: public RayError { + public: + //! @brief Create a new exception instance + explicit WrongInputError(const std::string &what = "The input can't be proceed"); + + //! @brief A default destructor + ~WrongInputError() override = default; + + //! @brief An exception is copy constructable + WrongInputError(const WrongInputError &) = default; + + //! @brief A default assignment operator + WrongInputError &operator=(const WrongInputError &) = default; + }; } #endif /* !RAYERROR_HPP_ */ diff --git a/lib/Ray/sources/Shaders/Shaders.cpp b/lib/Ray/sources/Shaders/Shaders.cpp new file mode 100644 index 00000000..435b4f72 --- /dev/null +++ b/lib/Ray/sources/Shaders/Shaders.cpp @@ -0,0 +1,49 @@ +// +// Created by cbihan on 04/06/2021. +// + +#include "Shaders.hpp" + +#include +#include "Exceptions/RayError.hpp" + +namespace RAY +{ + Cache<::Shader> Shader::_shadersCache(LoadShader, UnloadShader); + + + Shader::Shader(const std::string &vertexFile, const std::string &fragmentFile) + : _vertexFile(vertexFile), + _fragmentFile(fragmentFile), + _rayLibShader(_shadersCache.fetch(vertexFile, fragmentFile)) + { + } + + const std::shared_ptr<::Shader> &Shader::getShaderPtr() const + { + return this->_rayLibShader; + } + + void Shader::setShaderUniformVar(const std::string &varName, float value) + { + if (this->_shaderIndexVars.find(varName) == this->_shaderIndexVars.end()) { + int varShaderIndex = GetShaderLocation(*this->_rayLibShader, varName.c_str()); + + if (varShaderIndex < 0) { + throw Exception::WrongInputError("The loaded shader doesn't have a variable called: " + varName); + } + this->_shaderIndexVars[varName] = varShaderIndex; + } + SetShaderValue(*this->_rayLibShader, this->_shaderIndexVars[varName], &value, SHADER_UNIFORM_FLOAT); + } + + void Shader::BeginUsingCustomShader(Shader &shader) + { + BeginShaderMode(*shader.getShaderPtr()); + } + + void Shader::EndUsingCustomShader() + { + EndShaderMode(); + } +} \ No newline at end of file diff --git a/lib/Ray/sources/Shaders/Shaders.hpp b/lib/Ray/sources/Shaders/Shaders.hpp new file mode 100644 index 00000000..529eea64 --- /dev/null +++ b/lib/Ray/sources/Shaders/Shaders.hpp @@ -0,0 +1,55 @@ +// +// Created by cbihan on 04/06/2021. +// + +#pragma once + +#include +#include +#include +#include +#include "Utils/Cache.hpp" + +namespace RAY +{ + class Shader + { + private: + //! @brief The vertex file of the shader + std::string _vertexFile; + //! @brief The fragment file of the shader + std::string _fragmentFile; + + //! @brief Raylib shader struct + std::shared_ptr<::Shader> _rayLibShader; + + //! @brief Fetch and cache the index of the shader variable on the first set of the var + std::map _shaderIndexVars = {}; + + static Cache<::Shader> _shadersCache; + public: + //INTERNAL: + const std::shared_ptr<::Shader> &getShaderPtr() const; + + public: + + //! @brief start using a custom shader when drawing + static void BeginUsingCustomShader(RAY::Shader &shader); + //! @brief stop using the custom shader + static void EndUsingCustomShader(); + + //! @brief The set var for float values + //! @note Throw if the var is not found + void setShaderUniformVar(const std::string &varName, float value); + + //! @brief ctor if no vertexfile in needed set it to nullptr + Shader(const std::string &vertexFile, const std::string &fragmentFile); + //! @brief Default copy ctor + Shader(const Shader &) = default; + //! @brief dtor + ~Shader() = default; + //! @brief Assignment operator + Shader &operator=(const Shader &) = default; + }; + +} \ No newline at end of file diff --git a/lib/Ray/sources/Utils/Cache.hpp b/lib/Ray/sources/Utils/Cache.hpp index 653933f5..8ecbde29 100644 --- a/lib/Ray/sources/Utils/Cache.hpp +++ b/lib/Ray/sources/Utils/Cache.hpp @@ -9,7 +9,9 @@ #include #include #include +#include #include +#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::functiondataUnloader): - _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> _cache; }; + + template<> + class Cache<::Shader> + { + public: + Cache(std::function<::Shader(const char *, const char *)> dataLoader, + std::function 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 _dataUnloader; + + //! @brief map storing shared ptr of caches + std::unordered_map> _cache; + }; }