45 lines
1.2 KiB
C++
45 lines
1.2 KiB
C++
// Copyright (c) 2023 Dominic Masters
|
|
//
|
|
// This software is released under the MIT License.
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
#pragma once
|
|
#include "display/shader/Shader.hpp"
|
|
|
|
namespace Dawn {
|
|
class ShaderManager {
|
|
private:
|
|
std::vector<std::shared_ptr<IShaderBase>> shaders;
|
|
|
|
public:
|
|
/**
|
|
* Retreives an instance of the shader from the shader manager. If the
|
|
* shader does not exist it will be created.
|
|
*
|
|
* @tparam T Type of shader to retreive.
|
|
* @return Shader instance.
|
|
*/
|
|
template<class T>
|
|
std::shared_ptr<T> getShader() {
|
|
auto itShaders = shaders.begin();
|
|
while(itShaders != shaders.end()) {
|
|
// auto shader = itShaders->lock();
|
|
// if(!shader) {
|
|
// itShaders = shaders.erase(itShaders);
|
|
// continue;
|
|
// }
|
|
// std::shared_ptr<T> casted = std::dynamic_pointer_cast<T>(shader);
|
|
|
|
auto shader = *itShaders;
|
|
std::shared_ptr<T> casted = std::dynamic_pointer_cast<T>(shader);
|
|
if(casted) return casted;
|
|
itShaders++;
|
|
}
|
|
|
|
auto newShader = std::make_shared<T>();
|
|
shaders.push_back(newShader);
|
|
newShader->init();
|
|
return newShader;
|
|
}
|
|
};
|
|
} |