Browse Source

Add singleton to shader and texture manager

master
Riyyi 4 years ago
parent
commit
57a34b8be1
  1. 17
      inferno/src/inferno/application.cpp
  2. 16
      inferno/src/inferno/render/shader.cpp
  3. 7
      inferno/src/inferno/render/shader.h
  4. 16
      inferno/src/inferno/render/texture.cpp
  5. 7
      inferno/src/inferno/render/texture.h

17
inferno/src/inferno/application.cpp

@ -37,19 +37,28 @@ namespace Inferno {
Input::initialize(); Input::initialize();
ShaderManager* shaderManager = new ShaderManager();
shaderManager->initialize();
TextureManager* textureManager = new TextureManager();
textureManager->initialize();
m_cameraO = std::make_shared<OrthographicCamera>(); m_cameraO = std::make_shared<OrthographicCamera>();
m_cameraP = std::make_shared<PerspectiveCamera>(); m_cameraP = std::make_shared<PerspectiveCamera>();
TextureManager textureManager;
m_texture = textureManager.load("assets/gfx/test.png");
m_texture2 = textureManager.load("assets/gfx/test-inverted.png");
Renderer2D::initialize(); Renderer2D::initialize();
// Load assets
m_texture = TextureManager::the().load("assets/gfx/test.png");
m_texture2 = TextureManager::the().load("assets/gfx/test-inverted.png");
} }
Application::~Application() Application::~Application()
{ {
Renderer2D::destroy(); Renderer2D::destroy();
TextureManager::the().destroy();
ShaderManager::the().destroy();
// Input::destroy(); // Input::destroy();
Settings::destroy(); Settings::destroy();
} }

16
inferno/src/inferno/render/shader.cpp

@ -191,6 +191,22 @@ namespace Inferno {
// ----------------------------------------- // -----------------------------------------
ShaderManager* ShaderManager::s_instance = nullptr;
void ShaderManager::initialize()
{
ASSERT(!s_instance, "ShaderManager already exists!");
s_instance = this;
dbg(Log::Info) << "ShaderManager initialized";
}
void ShaderManager::destroy()
{
delete s_instance;
s_instance = nullptr;
}
void ShaderManager::add(const std::string& name, const std::shared_ptr<Shader>& shader) void ShaderManager::add(const std::string& name, const std::shared_ptr<Shader>& shader)
{ {
// Construct (key, value) pair and insert it into the unordered_map // Construct (key, value) pair and insert it into the unordered_map

7
inferno/src/inferno/render/shader.h

@ -47,6 +47,9 @@ namespace Inferno {
class ShaderManager { class ShaderManager {
public: public:
void initialize();
void destroy();
void add(const std::string& name, const std::shared_ptr<Shader>& shader); void add(const std::string& name, const std::shared_ptr<Shader>& shader);
std::shared_ptr<Shader> load(const std::string& name); std::shared_ptr<Shader> load(const std::string& name);
std::shared_ptr<Shader> load(const std::string& vertexSource, std::shared_ptr<Shader> load(const std::string& vertexSource,
@ -57,12 +60,16 @@ namespace Inferno {
void remove(const std::string& name); void remove(const std::string& name);
void remove(const std::shared_ptr<Shader>& shader); void remove(const std::shared_ptr<Shader>& shader);
static inline ShaderManager& the() { return *s_instance; }
protected: protected:
std::string computeName(const std::string& vertexSource, std::string computeName(const std::string& vertexSource,
const std::string& fragmentSource); const std::string& fragmentSource);
private: private:
std::unordered_map<std::string, std::shared_ptr<Shader>> m_shaderList; std::unordered_map<std::string, std::shared_ptr<Shader>> m_shaderList;
static ShaderManager* s_instance;
}; };
} }

16
inferno/src/inferno/render/texture.cpp

@ -101,6 +101,22 @@ namespace Inferno {
// ----------------------------------------- // -----------------------------------------
TextureManager* TextureManager::s_instance = nullptr;
void TextureManager::initialize()
{
ASSERT(!s_instance, "TextureManager already exists!");
s_instance = this;
dbg(Log::Info) << "TextureManager initialized";
}
void TextureManager::destroy()
{
delete s_instance;
s_instance = nullptr;
}
void TextureManager::add(const std::string& path, const std::shared_ptr<Texture>& texture) void TextureManager::add(const std::string& path, const std::shared_ptr<Texture>& texture)
{ {
// Construct (key, value) pair and insert it into the unordered_map // Construct (key, value) pair and insert it into the unordered_map

7
inferno/src/inferno/render/texture.h

@ -39,6 +39,9 @@ namespace Inferno {
class TextureManager { class TextureManager {
public: public:
void initialize();
void destroy();
void add(const std::string& path, const std::shared_ptr<Texture>& texture); void add(const std::string& path, const std::shared_ptr<Texture>& texture);
std::shared_ptr<Texture> load(const std::string& path); std::shared_ptr<Texture> load(const std::string& path);
std::shared_ptr<Texture> get(const std::string& path); std::shared_ptr<Texture> get(const std::string& path);
@ -47,8 +50,12 @@ namespace Inferno {
void remove(const std::string& path); void remove(const std::string& path);
void remove(const std::shared_ptr<Texture>& texture); void remove(const std::shared_ptr<Texture>& texture);
static inline TextureManager& the() { return *s_instance; }
private: private:
std::unordered_map<std::string, std::shared_ptr<Texture>> m_textureList; std::unordered_map<std::string, std::shared_ptr<Texture>> m_textureList;
static TextureManager* s_instance;
}; };
} }

Loading…
Cancel
Save