Browse Source

Implement singleton class in all singletons

master
Riyyi 4 years ago
parent
commit
8f7665b8a3
  1. 39
      inferno/src/inferno/application.cpp
  2. 11
      inferno/src/inferno/render/font.cpp
  3. 11
      inferno/src/inferno/render/font.h
  4. 11
      inferno/src/inferno/render/gltf.h
  5. 24
      inferno/src/inferno/render/renderer.cpp
  6. 49
      inferno/src/inferno/render/renderer.h
  7. 11
      inferno/src/inferno/render/shader.cpp
  8. 14
      inferno/src/inferno/render/shader.h
  9. 11
      inferno/src/inferno/render/texture.cpp
  10. 16
      inferno/src/inferno/render/texture.h
  11. 20
      inferno/src/inferno/scene/scene.cpp
  12. 17
      inferno/src/inferno/system/camerasystem.cpp
  13. 12
      inferno/src/inferno/system/camerasystem.h
  14. 17
      inferno/src/inferno/system/rendersystem.cpp
  15. 12
      inferno/src/inferno/system/rendersystem.h
  16. 12
      inferno/src/inferno/system/scriptsystem.cpp
  17. 13
      inferno/src/inferno/system/scriptsystem.h
  18. 17
      inferno/src/inferno/system/transformsystem.cpp
  19. 12
      inferno/src/inferno/system/transformsystem.h

39
inferno/src/inferno/application.cpp

@ -34,27 +34,16 @@ namespace Inferno {
m_window->setEventCallback(NF_BIND_EVENT(Application::onEvent));
Input::initialize();
ShaderManager* shaderManager = new ShaderManager();
shaderManager->initialize();
TextureManager* textureManager = new TextureManager();
textureManager->initialize();
ShaderManager::initialize();
TextureManager::initialize();
RenderCommand::initialize();
Renderer2D::initialize();
RendererCharacter::initialize();
FontManager::initialize();
m_scene = std::make_shared<Scene>();
m_scene->initialize();
RenderCommand::initialize();
Renderer2D* renderer2D = new Renderer2D();
renderer2D->initialize();
RendererCharacter* rendererCharacter = new RendererCharacter();
rendererCharacter->initialize();
FontManager* fontManager = new FontManager();
fontManager->initialize();
// Load assets
m_font = FontManager::the().load("assets/fnt/dejavu-sans");
@ -62,14 +51,18 @@ namespace Inferno {
Application::~Application()
{
FontManager::the().destroy();
RendererCharacter::the().destroy();
Renderer2D::the().destroy();
RenderCommand::destroy();
m_scene->destroy();
TextureManager::the().destroy();
ShaderManager::the().destroy();
FontManager::destroy();
RendererCharacter::destroy();
Renderer2D::destroy();
RenderCommand::destroy();
TextureManager::destroy();
ShaderManager::destroy();
// Input::destroy();
m_window->destroy();
Settings::destroy();
}

11
inferno/src/inferno/render/font.cpp

@ -103,20 +103,13 @@ namespace Inferno {
// -----------------------------------------
FontManager* FontManager::s_instance = nullptr;
void FontManager::initialize()
FontManager::FontManager(s)
{
ASSERT(!s_instance, "FontManager already exists!");
s_instance = this;
info() << "FontManager initialized";
}
void FontManager::destroy()
FontManager::~FontManager()
{
delete s_instance;
s_instance = nullptr;
}
void FontManager::add(const std::string& name, const std::shared_ptr<Font>& font)

11
inferno/src/inferno/render/font.h

@ -11,6 +11,7 @@
#include "glm/ext/vector_int2.hpp" // glm::ivec2
#include "inferno/io/log.h"
#include "inferno/singleton.h"
namespace Inferno {
@ -51,10 +52,10 @@ class Texture;
// -----------------------------------------
class FontManager {
class FontManager final : public Singleton<FontManager> {
public:
void initialize();
void destroy();
FontManager(s);
virtual ~FontManager();
void add(const std::string& name, const std::shared_ptr<Font>& font);
std::shared_ptr<Font> load(const std::string& name);
@ -64,12 +65,8 @@ class Texture;
void remove(const std::string& name);
void remove(const std::shared_ptr<Font>& font);
static inline FontManager& the() { return *s_instance; }
private:
std::unordered_map<std::string, std::shared_ptr<Font>> m_fontList;
static FontManager* s_instance;
};

11
inferno/src/inferno/render/gltf.h

@ -7,6 +7,7 @@
#include <unordered_map> // std::unordered_map
#include <vector> // std::vector
#include "inferno/singleton.h"
#include "inferno/util/json.h"
namespace Inferno {
@ -128,10 +129,10 @@ namespace Inferno {
// -----------------------------------------
class GltfManager {
class GltfManager final : Singleton<GltfManager> {
public:
void initialize();
void destroy();
GltfManager(s) {}
virtual ~GltfManager() {}
void add(const std::string& path, const std::shared_ptr<Gltf>& gltf);
std::shared_ptr<Gltf> load(const std::string& path);
@ -141,12 +142,8 @@ namespace Inferno {
void remove(const std::string& path);
void remove(const std::shared_ptr<Gltf>& gltf);
static inline GltfManager& the() { return *s_instance; }
private:
std::unordered_map<std::string, std::shared_ptr<Gltf>> m_gltfList;
static GltfManager* s_instance;
};
} // namespace Inferno

24
inferno/src/inferno/render/renderer.cpp

@ -151,13 +151,8 @@ namespace Inferno {
// -----------------------------------------
Renderer2D* Renderer2D::s_instance = nullptr;
void Renderer2D::initialize()
Renderer2D::Renderer2D(s)
{
ASSERT(!s_instance, "RendererCharacter already exists!");
s_instance = this;
Renderer::initialize();
// CPU
@ -210,10 +205,9 @@ namespace Inferno {
info() << "Renderer2D initialized";
}
void Renderer2D::destroy()
Renderer2D::~Renderer2D()
{
delete s_instance;
s_instance = nullptr;
Renderer::destroy();
}
void Renderer2D::beginScene(glm::mat4 cameraProjectionView)
@ -308,13 +302,8 @@ namespace Inferno {
// -----------------------------------------
RendererCharacter* RendererCharacter::s_instance = nullptr;
void RendererCharacter::initialize()
RendererCharacter::RendererCharacter(s)
{
ASSERT(!s_instance, "RendererCharacter already exists!");
s_instance = this;
Renderer::initialize();
// CPU
@ -367,10 +356,9 @@ namespace Inferno {
info() << "RendererCharacter initialized";
}
void RendererCharacter::destroy()
RendererCharacter::~RendererCharacter()
{
delete s_instance;
s_instance = nullptr;
Renderer::destroy();
}
void RendererCharacter::beginScene()

49
inferno/src/inferno/render/renderer.h

@ -1,7 +1,7 @@
#ifndef RENDERER_H
#define RENDERER_H
#include <cstdint> // std::int32_t, std::uint32_t
#include <cstdint> // int32_t, uint32_t
#include <memory> // std::shared_ptr, std::unique_ptr, std::make_shared, std::make_unique
#include "glm/ext/matrix_float4x4.hpp" // glm::mat4
@ -10,6 +10,7 @@
#include "glm/ext/vector_float4.hpp" // glm::vec4
#include "inferno/component/transformcomponent.h"
#include "inferno/singleton.h"
namespace Inferno {
@ -29,12 +30,14 @@ namespace Inferno {
struct CharacterVertex {
QuadVertex quad;
// Font
float width = 0.44f;
float edge = 0.15f;
// Outline
float borderWidth = 0.7f;
float borderEdge = 0.1f;
glm::vec4 borderColor { 1.0f, 1.0f, 1.0f, 1.0f };
// Dropshadow
float offset = 0.0f;
};
@ -65,8 +68,10 @@ namespace Inferno {
static const uint32_t textureUnitPerBatch = 32;
protected:
virtual void initialize() = 0;
virtual void destroy() = 0;
Renderer() {}
void initialize();
void destroy();
uint32_t addTextureUnit(std::shared_ptr<Texture> texture);
@ -91,15 +96,20 @@ namespace Inferno {
// -----------------------------------------
class Renderer2D final : public Renderer {
class Renderer2D final
: public Renderer
, public Singleton<Renderer2D> {
public:
Renderer2D(s);
virtual ~Renderer2D();
using Singleton<Renderer2D>::initialize;
using Singleton<Renderer2D>::destroy;
static const uint32_t quadCount = 1000;
static const uint32_t vertexCount = quadCount * vertexPerQuad;
static const uint32_t indexCount = quadCount * indexPerQuad;
void initialize() override;
void destroy() override;
void beginScene(glm::mat4 cameraProjectionView);
void endScene();
@ -107,9 +117,6 @@ namespace Inferno {
void drawQuad(const TransformComponent& transform, glm::mat4 color);
void drawQuad(const TransformComponent& transform, glm::vec4 color, std::shared_ptr<Texture> texture);
void drawQuad(const TransformComponent& transform, glm::mat4 color, std::shared_ptr<Texture> texture);
static inline Renderer2D& the() { return *s_instance; }
private:
void loadShader() override;
void flush() override;
@ -122,28 +129,28 @@ namespace Inferno {
// Default quad vertex positions
glm::vec4 m_vertexPositions[vertexPerQuad];
static Renderer2D* s_instance;
};
// -----------------------------------------
class RendererCharacter final : public Renderer {
class RendererCharacter final
: public Renderer
, public Singleton<RendererCharacter> {
public:
RendererCharacter(s);
virtual ~RendererCharacter();
using Singleton<RendererCharacter>::initialize;
using Singleton<RendererCharacter>::destroy;
static const uint32_t quadCount = 1000;
static const uint32_t vertexCount = quadCount * vertexPerQuad;
static const uint32_t indexCount = quadCount * indexPerQuad;
void initialize() override;
void destroy() override;
void beginScene();
void endScene();
void drawCharacter(std::array<CharacterVertex, vertexPerQuad>& characterQuad, std::shared_ptr<Texture> texture);
static inline RendererCharacter& the() { return *s_instance; }
private:
void loadShader() override;
void flush() override;
@ -153,8 +160,6 @@ namespace Inferno {
// CPU quad vertices
std::unique_ptr<CharacterVertex[]> m_vertexBufferBase = nullptr;
CharacterVertex* m_vertexBufferPtr = nullptr;
static RendererCharacter* s_instance;
};
}

11
inferno/src/inferno/render/shader.cpp

@ -193,20 +193,13 @@ namespace Inferno {
// -----------------------------------------
ShaderManager* ShaderManager::s_instance = nullptr;
void ShaderManager::initialize()
ShaderManager::ShaderManager(s)
{
ASSERT(!s_instance, "ShaderManager already exists!");
s_instance = this;
info() << "ShaderManager initialized";
}
void ShaderManager::destroy()
ShaderManager::~ShaderManager()
{
delete s_instance;
s_instance = nullptr;
}
void ShaderManager::add(const std::string& name, const std::shared_ptr<Shader>& shader)

14
inferno/src/inferno/render/shader.h

@ -8,12 +8,14 @@
#include "glm/glm.hpp"
#include "inferno/singleton.h"
namespace Inferno {
class Shader {
public:
Shader(const std::string& name);
~Shader();
virtual ~Shader();
int32_t findUniform(const std::string& name) const;
@ -45,10 +47,10 @@ namespace Inferno {
// -----------------------------------------
class ShaderManager {
class ShaderManager final : public Singleton<ShaderManager> {
public:
void initialize();
void destroy();
ShaderManager(s);
virtual ~ShaderManager();
void add(const std::string& name, const std::shared_ptr<Shader>& shader);
std::shared_ptr<Shader> load(const std::string& name);
@ -60,16 +62,12 @@ namespace Inferno {
void remove(const std::string& name);
void remove(const std::shared_ptr<Shader>& shader);
static inline ShaderManager& the() { return *s_instance; }
protected:
std::string computeName(const std::string& vertexSource,
const std::string& fragmentSource);
private:
std::unordered_map<std::string, std::shared_ptr<Shader>> m_shaderList;
static ShaderManager* s_instance;
};
}

11
inferno/src/inferno/render/texture.cpp

@ -103,20 +103,13 @@ namespace Inferno {
// -----------------------------------------
TextureManager* TextureManager::s_instance = nullptr;
void TextureManager::initialize()
TextureManager::TextureManager(s)
{
ASSERT(!s_instance, "TextureManager already exists!");
s_instance = this;
info() << "TextureManager initialized";
}
void TextureManager::destroy()
TextureManager::~TextureManager()
{
delete s_instance;
s_instance = nullptr;
}
void TextureManager::add(const std::string& path, const std::shared_ptr<Texture>& texture)

16
inferno/src/inferno/render/texture.h

@ -1,11 +1,13 @@
#ifndef TEXTURE_H
#define TEXTURE_H
#include <cstdint> // std::uint32_t
#include <cstdint> // uint32_t
#include <memory> // std::shared_ptr
#include <string> // std::string
#include <string> // std::string
#include <unordered_map> // std::unordered_map
#include "inferno/singleton.h"
namespace Inferno {
class Texture {
@ -37,10 +39,10 @@ namespace Inferno {
// -----------------------------------------
class TextureManager {
class TextureManager final : public Singleton<TextureManager> {
public:
void initialize();
void destroy();
TextureManager(s);
virtual ~TextureManager();
void add(const std::string& path, const std::shared_ptr<Texture>& texture);
std::shared_ptr<Texture> load(const std::string& path);
@ -50,12 +52,8 @@ namespace Inferno {
void remove(const std::string& path);
void remove(const std::shared_ptr<Texture>& texture);
static inline TextureManager& the() { return *s_instance; }
private:
std::unordered_map<std::string, std::shared_ptr<Texture>> m_textureList;
static TextureManager* s_instance;
};
}

20
inferno/src/inferno/scene/scene.cpp

@ -21,20 +21,16 @@ namespace Inferno {
m_registry = std::make_shared<entt::registry>();
TransformSystem* transformSystem = new TransformSystem();
transformSystem->initialize();
TransformSystem::initialize();
TransformSystem::the().setRegistry(m_registry);
CameraSystem* cameraSystem = new CameraSystem();
cameraSystem->initialize();
CameraSystem::initialize();
CameraSystem::the().setRegistry(m_registry);
RenderSystem* renderSystem = new RenderSystem();
renderSystem->initialize();
RenderSystem::initialize();
RenderSystem::the().setRegistry(m_registry);
ScriptSystem* scriptSystem = new ScriptSystem();
scriptSystem->initialize();
ScriptSystem::initialize();
ScriptSystem::the().setScene(this);
// Load assets
@ -85,10 +81,10 @@ namespace Inferno {
void Scene::destroy()
{
ScriptSystem::the().destroy();
RenderSystem::the().destroy();
CameraSystem::the().destroy();
TransformSystem::the().destroy();
ScriptSystem::destroy();
RenderSystem::destroy();
CameraSystem::destroy();
TransformSystem::destroy();
}
uint32_t Scene::createEntity(const std::string& name)

17
inferno/src/inferno/system/camerasystem.cpp

@ -10,16 +10,15 @@
namespace Inferno {
CameraSystem* CameraSystem::s_instance = nullptr;
void CameraSystem::initialize()
CameraSystem::CameraSystem(s)
{
ASSERT(!s_instance, "CameraSystem already exists!");
s_instance = this;
info() << "CameraSystem initialized";
}
CameraSystem::~CameraSystem()
{
}
void CameraSystem::update()
{
auto view = m_registry->view<TransformComponent, CameraComponent>();
@ -35,12 +34,6 @@ namespace Inferno {
}
}
void CameraSystem::destroy()
{
delete s_instance;
s_instance = nullptr;
}
glm::mat4 CameraSystem::projectionView()
{
auto view = m_registry->view<TransformComponent, CameraComponent>();

12
inferno/src/inferno/system/camerasystem.h

@ -7,31 +7,29 @@
#include "inferno/component/cameracomponent.h"
#include "inferno/component/transformcomponent.h"
#include "inferno/singleton.h"
#define NEAR_PLANE 0.1f
#define FAR_PLANE 100.0f
namespace Inferno {
class CameraSystem {
class CameraSystem final : public Singleton<CameraSystem> {
public:
void initialize();
CameraSystem(s);
virtual ~CameraSystem();
void update();
void destroy();
glm::mat4 projectionView();
void setRegistry(const std::shared_ptr<entt::registry>& registry) { m_registry = registry; };
static inline CameraSystem& the() { return *s_instance; }
private:
void updateOrthographic(TransformComponent& transform, CameraComponent& camera);
void updatePerspective(TransformComponent& transform, CameraComponent& camera);
std::shared_ptr<entt::registry> m_registry;
static CameraSystem* s_instance;
};
}

17
inferno/src/inferno/system/rendersystem.cpp

@ -9,16 +9,15 @@
namespace Inferno {
RenderSystem* RenderSystem::s_instance = nullptr;
void RenderSystem::initialize()
RenderSystem::RenderSystem(s)
{
ASSERT(!s_instance, "RenderSystem already exists!");
s_instance = this;
info() << "RenderSystem initialized";
}
RenderSystem::~RenderSystem()
{
}
void RenderSystem::render()
{
auto group = m_registry->group<TransformComponent, SpriteComponent>();
@ -28,10 +27,4 @@ namespace Inferno {
}
}
void RenderSystem::destroy()
{
delete s_instance;
s_instance = nullptr;
}
}

12
inferno/src/inferno/system/rendersystem.h

@ -6,23 +6,21 @@
#include "entt/entity/registry.hpp" // entt::entity, entt::registry
#include "inferno/render/renderer.h"
#include "inferno/singleton.h"
namespace Inferno {
class RenderSystem {
class RenderSystem final : public Singleton<RenderSystem> {
public:
void initialize();
RenderSystem(s);
virtual ~RenderSystem();
void render();
void destroy();
void setRegistry(const std::shared_ptr<entt::registry>& registry) { m_registry = registry; };
static inline RenderSystem& the() { return *s_instance; }
private:
std::shared_ptr<entt::registry> m_registry;
static RenderSystem* s_instance;
};
}

12
inferno/src/inferno/system/scriptsystem.cpp

@ -12,17 +12,12 @@
namespace Inferno {
ScriptSystem* ScriptSystem::s_instance = nullptr;
void ScriptSystem::initialize()
ScriptSystem::ScriptSystem(s)
{
ASSERT(!s_instance, "ScriptSystem already exists!");
s_instance = this;
info() << "ScriptSystem initialized";
}
void ScriptSystem::destroy()
ScriptSystem::~ScriptSystem()
{
auto nativeScriptView = m_scene->registry()->view<NativeScriptComponent>();
@ -35,9 +30,6 @@ namespace Inferno {
for (auto entity : luaScriptView) {
cleanup(luaScriptView.get<LuaScriptComponent>(entity));
}
delete s_instance;
s_instance = nullptr;
}
void ScriptSystem::update(float deltaTime)

13
inferno/src/inferno/system/scriptsystem.h

@ -3,6 +3,8 @@
#include <cstdint> // uint32_t
#include "inferno/singleton.h"
namespace Inferno {
struct NativeScriptComponent;
@ -10,10 +12,11 @@ namespace Inferno {
class Scene;
class ScriptSystem {
class ScriptSystem final : public Singleton<ScriptSystem> {
public:
void initialize();
void destroy();
ScriptSystem(s);
virtual ~ScriptSystem();
void update(float deltaTime);
void cleanup(uint32_t entity);
@ -22,12 +25,8 @@ namespace Inferno {
void setScene(Scene* scene) { m_scene = scene; }
static inline ScriptSystem& the() { return *s_instance; }
private:
Scene* m_scene;
static ScriptSystem* s_instance;
};
}

17
inferno/src/inferno/system/transformsystem.cpp

@ -7,16 +7,15 @@
namespace Inferno {
TransformSystem* TransformSystem::s_instance = nullptr;
void TransformSystem::initialize()
TransformSystem::TransformSystem(s)
{
ASSERT(!s_instance, "TransformSystem already exists!");
s_instance = this;
info() << "TransformSystem initialized";
}
TransformSystem::~TransformSystem()
{
}
void TransformSystem::update()
{
auto view = m_registry->view<TransformComponent>();
@ -41,10 +40,4 @@ namespace Inferno {
}
}
void TransformSystem::destroy()
{
delete s_instance;
s_instance = nullptr;
}
}

12
inferno/src/inferno/system/transformsystem.h

@ -4,23 +4,21 @@
#include <memory> //std::shared_ptr
#include "entt/entity/registry.hpp" // entt::entity, entt::registry
#include "inferno/singleton.h"
namespace Inferno {
class TransformSystem {
class TransformSystem final : public Singleton<TransformSystem> {
public:
void initialize();
TransformSystem(s);
virtual ~TransformSystem();
void update();
void destroy();
void setRegistry(const std::shared_ptr<entt::registry>& registry) { m_registry = registry; };
static inline TransformSystem& the() { return *s_instance; }
private:
std::shared_ptr<entt::registry> m_registry;
static TransformSystem* s_instance;
};
}

Loading…
Cancel
Save