From 4af037a7fbf384d884b40ab54e5769ee39a82f51 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Wed, 21 Sep 2022 23:29:43 +0200 Subject: [PATCH] Engine: Switch to ruc's singleton --- example/CMakeLists.txt | 3 ++- example/src/game.cpp | 1 - src/inferno/application.cpp | 11 +---------- src/inferno/application.h | 6 +++--- src/inferno/render/font.h | 17 ++++++++--------- src/inferno/render/gltf.h | 5 +++-- src/inferno/render/renderer.h | 20 ++++++++++---------- src/inferno/render/shader.h | 10 ++++------ src/inferno/render/texture.h | 10 +++++----- src/inferno/scene/scene.cpp | 17 ++++------------- src/inferno/system/camerasystem.h | 7 +++---- src/inferno/system/rendersystem.h | 6 +++--- src/inferno/system/scriptsystem.h | 6 +++--- src/inferno/system/textareasystem.h | 5 ++--- src/inferno/system/transformsystem.h | 6 +++--- 15 files changed, 54 insertions(+), 76 deletions(-) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 94e5e73..833da28 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -14,7 +14,8 @@ file(GLOB_RECURSE GAME_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp") add_executable(${GAME} ${GAME_SOURCES}) target_include_directories(${GAME} PRIVATE "src" - "${CMAKE_SOURCE_DIR}/src") + "${CMAKE_SOURCE_DIR}/src" + "${CMAKE_SOURCE_DIR}/vendor/ruc/src") target_link_libraries(${GAME} ${ENGINE}) target_precompile_headers(${GAME} REUSE_FROM ${ENGINE}) diff --git a/example/src/game.cpp b/example/src/game.cpp index 3107b3a..cf008e6 100644 --- a/example/src/game.cpp +++ b/example/src/game.cpp @@ -10,6 +10,5 @@ public: Inferno::Application& Inferno::createApplication() { - Game::initialize(); return Game::the(); } diff --git a/src/inferno/application.cpp b/src/inferno/application.cpp index c9beb2f..3e6a76b 100644 --- a/src/inferno/application.cpp +++ b/src/inferno/application.cpp @@ -27,9 +27,6 @@ namespace Inferno { Application::Application(s) { - // Set singleton instance early - s_instance = this; - // Initialize Settings::initialize(); @@ -38,12 +35,7 @@ namespace Inferno { m_window->setEventCallback(NF_BIND_EVENT(Application::onEvent)); Input::initialize(); - ShaderManager::initialize(); - TextureManager::initialize(); RenderCommand::initialize(); - Renderer2D::initialize(); - RendererCharacter::initialize(); - FontManager::initialize(); m_scene = std::make_shared(); m_scene->initialize(); @@ -162,7 +154,6 @@ namespace Inferno { RendererCharacter::the().endScene(); m_window->render(); - } dbg() << "Application shutdown"; @@ -226,4 +217,4 @@ namespace Inferno { return Input::onMousePosition(e); } -} +} // namespace Inferno diff --git a/src/inferno/application.h b/src/inferno/application.h index 6dfb234..69ae2d7 100644 --- a/src/inferno/application.h +++ b/src/inferno/application.h @@ -3,7 +3,7 @@ #include // std::unique_ptr, std::shared_ptr -#include "inferno/singleton.h" +#include "ruc/singleton.h" namespace Inferno { @@ -16,7 +16,7 @@ namespace Inferno { class WindowCloseEvent; class WindowResizeEvent; - class Application : public Singleton { + class Application : public ruc::Singleton { public: Application(s); virtual ~Application(); @@ -48,7 +48,7 @@ namespace Inferno { // To be defined in the game extern Application& createApplication(); -} +} // namespace Inferno #endif // APPLICATION_H diff --git a/src/inferno/render/font.h b/src/inferno/render/font.h index 068da83..db1b41b 100644 --- a/src/inferno/render/font.h +++ b/src/inferno/render/font.h @@ -7,15 +7,15 @@ #include // std::unordered_map #include // std::vector -#include "glm/ext/vector_uint2.hpp" // glm::uvec2 #include "glm/ext/vector_int2.hpp" // glm::ivec2 +#include "glm/ext/vector_uint2.hpp" // glm::uvec2 +#include "ruc/singleton.h" #include "inferno/io/log.h" -#include "inferno/singleton.h" namespace Inferno { -class Texture; + class Texture; struct Character { glm::uvec2 position; // Position @@ -24,7 +24,7 @@ class Texture; uint32_t advance; // Amount to advance to next glyph }; -// ----------------------------------------- + // ------------------------------------- class Font { public: @@ -50,9 +50,9 @@ class Texture; std::unordered_map> m_characterList; }; -// ----------------------------------------- + // ------------------------------------- - class FontManager final : public Singleton { + class FontManager final : public ruc::Singleton { public: FontManager(s); virtual ~FontManager(); @@ -69,12 +69,11 @@ class Texture; std::unordered_map> m_fontList; }; - -// ----------------------------------------- + // ------------------------------------- const LogStream& operator<<(const LogStream& stream, const glm::ivec2& value); -} +} // namespace Inferno #endif // FONT_H diff --git a/src/inferno/render/gltf.h b/src/inferno/render/gltf.h index fa622ba..7a80ea5 100644 --- a/src/inferno/render/gltf.h +++ b/src/inferno/render/gltf.h @@ -8,7 +8,8 @@ #include // std::unordered_map #include // std::vector -#include "inferno/singleton.h" +#include "ruc/singleton.h" + #include "inferno/util/json.h" #define GLTF_TYPE_SCALAR 1 @@ -149,7 +150,7 @@ namespace Inferno { // ----------------------------------------- - class GltfManager final : Singleton { + class GltfManager final : ruc::Singleton { public: GltfManager(s) {} virtual ~GltfManager() {} diff --git a/src/inferno/render/renderer.h b/src/inferno/render/renderer.h index 69d1b12..39aaa07 100644 --- a/src/inferno/render/renderer.h +++ b/src/inferno/render/renderer.h @@ -8,9 +8,9 @@ #include "glm/ext/vector_float2.hpp" // glm::vec2 #include "glm/ext/vector_float3.hpp" // glm::vec3 #include "glm/ext/vector_float4.hpp" // glm::vec4 +#include "ruc/singleton.h" #include "inferno/component/transformcomponent.h" -#include "inferno/singleton.h" namespace Inferno { @@ -39,7 +39,7 @@ namespace Inferno { float offset = 0.0f; }; -// ----------------------------------------- + // ------------------------------------- class RenderCommand { public: @@ -57,7 +57,7 @@ namespace Inferno { static int32_t textureUnitAmount(); }; -// ----------------------------------------- + // ------------------------------------- class Renderer { public: @@ -92,16 +92,15 @@ namespace Inferno { std::shared_ptr m_vertexArray; }; -// ----------------------------------------- + // ------------------------------------- class Renderer2D final : public Renderer - , public Singleton { + , public ruc::Singleton { public: Renderer2D(s); virtual ~Renderer2D(); - using Singleton::initialize; using Singleton::destroy; static const uint32_t quadCount = 1000; @@ -115,6 +114,7 @@ namespace Inferno { void drawQuad(const TransformComponent& transform, glm::mat4 color); void drawQuad(const TransformComponent& transform, glm::vec4 color, std::shared_ptr texture); void drawQuad(const TransformComponent& transform, glm::mat4 color, std::shared_ptr texture); + private: void loadShader() override; void flush() override; @@ -129,16 +129,15 @@ namespace Inferno { glm::vec4 m_vertexPositions[vertexPerQuad]; }; -// ----------------------------------------- + // ------------------------------------- class RendererCharacter final : public Renderer - , public Singleton { + , public ruc::Singleton { public: RendererCharacter(s); virtual ~RendererCharacter(); - using Singleton::initialize; using Singleton::destroy; static const uint32_t quadCount = 1000; @@ -149,6 +148,7 @@ namespace Inferno { void endScene(); void drawCharacter(std::array& characterQuad, std::shared_ptr texture); + private: void loadShader() override; void flush() override; @@ -160,6 +160,6 @@ namespace Inferno { CharacterVertex* m_vertexBufferPtr { nullptr }; }; -} +} // namespace Inferno #endif // RENDERER_H diff --git a/src/inferno/render/shader.h b/src/inferno/render/shader.h index 4b70a81..e3368ae 100644 --- a/src/inferno/render/shader.h +++ b/src/inferno/render/shader.h @@ -7,8 +7,7 @@ #include // std::unordered_map #include "glm/glm.hpp" - -#include "inferno/singleton.h" +#include "ruc/singleton.h" namespace Inferno { @@ -45,9 +44,9 @@ namespace Inferno { uint32_t m_id; }; -// ----------------------------------------- + // ------------------------------------- - class ShaderManager final : public Singleton { + class ShaderManager final : public ruc::Singleton { public: ShaderManager(s); virtual ~ShaderManager(); @@ -70,7 +69,6 @@ namespace Inferno { std::unordered_map> m_shaderList; }; -} - +} // namespace Inferno #endif // SHADER_H diff --git a/src/inferno/render/texture.h b/src/inferno/render/texture.h index 41aa545..78ff154 100644 --- a/src/inferno/render/texture.h +++ b/src/inferno/render/texture.h @@ -1,12 +1,12 @@ #ifndef TEXTURE_H #define TEXTURE_H -#include // uint32_t +#include // uint32_t #include // std::shared_ptr #include // std::string #include // std::unordered_map -#include "inferno/singleton.h" +#include "ruc/singleton.h" namespace Inferno { @@ -37,9 +37,9 @@ namespace Inferno { uint32_t m_dataFormat; }; -// ----------------------------------------- + // ------------------------------------- - class TextureManager final : public Singleton { + class TextureManager final : public ruc::Singleton { public: TextureManager(s); virtual ~TextureManager(); @@ -56,6 +56,6 @@ namespace Inferno { std::unordered_map> m_textureList; }; -} +} // namespace Inferno #endif // TEXTURE_H diff --git a/src/inferno/scene/scene.cpp b/src/inferno/scene/scene.cpp index 27914ae..c363930 100644 --- a/src/inferno/scene/scene.cpp +++ b/src/inferno/scene/scene.cpp @@ -1,3 +1,4 @@ +#include "inferno/scene/scene.h" #include "inferno/assert.h" #include "inferno/component/cameracomponent.h" #include "inferno/component/luascriptcomponent.h" @@ -5,7 +6,6 @@ #include "inferno/component/spritecomponent.h" #include "inferno/component/tagcomponent.h" #include "inferno/component/textareacomponent.h" -#include "inferno/scene/scene.h" #include "inferno/script/cameracontroller.h" #include "inferno/script/nativescript.h" #include "inferno/system/camerasystem.h" @@ -23,19 +23,10 @@ namespace Inferno { m_registry = std::make_shared(); - TransformSystem::initialize(); TransformSystem::the().setRegistry(m_registry); - - CameraSystem::initialize(); CameraSystem::the().setRegistry(m_registry); - - RenderSystem::initialize(); RenderSystem::the().setRegistry(m_registry); - - ScriptSystem::initialize(); ScriptSystem::the().setScene(this); - - TextAreaSystem::initialize(); TextAreaSystem::the().setScene(this); // Load assets @@ -122,14 +113,14 @@ namespace Inferno { void Scene::validEntity(uint32_t entity) const { - ASSERT(m_registry->valid(entt::entity {entity}), "Entity is not valid"); + ASSERT(m_registry->valid(entt::entity { entity }), "Entity is not valid"); } -// ---------------------------------------- + // ------------------------------------- const LogStream& operator<<(const LogStream& stream, entt::entity entity) { return stream << static_cast(entity); } -} +} // namespace Inferno diff --git a/src/inferno/system/camerasystem.h b/src/inferno/system/camerasystem.h index 3af416b..3372936 100644 --- a/src/inferno/system/camerasystem.h +++ b/src/inferno/system/camerasystem.h @@ -5,8 +5,7 @@ #include "entt/entity/registry.hpp" // entt::entity, entt::registry #include "glm/ext/matrix_float4x4.hpp" // glm::mat4 - -#include "inferno/singleton.h" +#include "ruc/singleton.h" #define NEAR_PLANE 0.1f #define FAR_PLANE 100.0f @@ -16,7 +15,7 @@ namespace Inferno { struct TransformComponent; struct CameraComponent; - class CameraSystem final : public Singleton { + class CameraSystem final : public ruc::Singleton { public: CameraSystem(s); virtual ~CameraSystem(); @@ -34,6 +33,6 @@ namespace Inferno { std::shared_ptr m_registry; }; -} +} // namespace Inferno #endif // CAMERA_SYSTEM_H diff --git a/src/inferno/system/rendersystem.h b/src/inferno/system/rendersystem.h index 103cc6a..33e1644 100644 --- a/src/inferno/system/rendersystem.h +++ b/src/inferno/system/rendersystem.h @@ -4,13 +4,13 @@ #include //std::shared_ptr #include "entt/entity/registry.hpp" // entt::entity, entt::registry +#include "ruc/singleton.h" #include "inferno/render/renderer.h" -#include "inferno/singleton.h" namespace Inferno { - class RenderSystem final : public Singleton { + class RenderSystem final : public ruc::Singleton { public: RenderSystem(s); virtual ~RenderSystem(); @@ -23,6 +23,6 @@ namespace Inferno { std::shared_ptr m_registry; }; -} +} // namespace Inferno #endif // RENDER_SYSTEM_H diff --git a/src/inferno/system/scriptsystem.h b/src/inferno/system/scriptsystem.h index f62ff7b..3823a81 100644 --- a/src/inferno/system/scriptsystem.h +++ b/src/inferno/system/scriptsystem.h @@ -3,7 +3,7 @@ #include // uint32_t -#include "inferno/singleton.h" +#include "ruc/singleton.h" namespace Inferno { @@ -12,7 +12,7 @@ namespace Inferno { class Scene; - class ScriptSystem final : public Singleton { + class ScriptSystem final : public ruc::Singleton { public: ScriptSystem(s); virtual ~ScriptSystem(); @@ -29,6 +29,6 @@ namespace Inferno { Scene* m_scene { nullptr }; }; -} +} // namespace Inferno #endif // SCRIPT_SYSTEM_H diff --git a/src/inferno/system/textareasystem.h b/src/inferno/system/textareasystem.h index d5e40c8..0e03e07 100644 --- a/src/inferno/system/textareasystem.h +++ b/src/inferno/system/textareasystem.h @@ -7,9 +7,9 @@ #include // std::vector #include "glm/ext/vector_float3.hpp" // glm::vec3 +#include "ruc/singleton.h" #include "inferno/render/renderer.h" -#include "inferno/singleton.h" namespace Inferno { @@ -18,7 +18,7 @@ namespace Inferno { class Font; class Scene; - class TextAreaSystem final : public Singleton { + class TextAreaSystem final : public ruc::Singleton { public: TextAreaSystem(s); virtual ~TextAreaSystem(); @@ -35,5 +35,4 @@ namespace Inferno { } // namespace Inferno - #endif // TEXTAREA_H diff --git a/src/inferno/system/transformsystem.h b/src/inferno/system/transformsystem.h index a5acc12..8e164ff 100644 --- a/src/inferno/system/transformsystem.h +++ b/src/inferno/system/transformsystem.h @@ -4,11 +4,11 @@ #include //std::shared_ptr #include "entt/entity/registry.hpp" // entt::entity, entt::registry -#include "inferno/singleton.h" +#include "ruc/singleton.h" namespace Inferno { - class TransformSystem final : public Singleton { + class TransformSystem final : public ruc::Singleton { public: TransformSystem(s); virtual ~TransformSystem(); @@ -21,6 +21,6 @@ namespace Inferno { std::shared_ptr m_registry; }; -} +} // namespace Inferno #endif // TRANSFORM_SYSTEM_H