Browse Source

Engine: Switch to ruc's singleton

master
Riyyi 2 years ago
parent
commit
4af037a7fb
  1. 3
      example/CMakeLists.txt
  2. 1
      example/src/game.cpp
  3. 11
      src/inferno/application.cpp
  4. 6
      src/inferno/application.h
  5. 17
      src/inferno/render/font.h
  6. 5
      src/inferno/render/gltf.h
  7. 20
      src/inferno/render/renderer.h
  8. 10
      src/inferno/render/shader.h
  9. 8
      src/inferno/render/texture.h
  10. 17
      src/inferno/scene/scene.cpp
  11. 7
      src/inferno/system/camerasystem.h
  12. 6
      src/inferno/system/rendersystem.h
  13. 6
      src/inferno/system/scriptsystem.h
  14. 5
      src/inferno/system/textareasystem.h
  15. 6
      src/inferno/system/transformsystem.h

3
example/CMakeLists.txt

@ -14,7 +14,8 @@ file(GLOB_RECURSE GAME_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
add_executable(${GAME} ${GAME_SOURCES}) add_executable(${GAME} ${GAME_SOURCES})
target_include_directories(${GAME} PRIVATE target_include_directories(${GAME} PRIVATE
"src" "src"
"${CMAKE_SOURCE_DIR}/src") "${CMAKE_SOURCE_DIR}/src"
"${CMAKE_SOURCE_DIR}/vendor/ruc/src")
target_link_libraries(${GAME} ${ENGINE}) target_link_libraries(${GAME} ${ENGINE})
target_precompile_headers(${GAME} REUSE_FROM ${ENGINE}) target_precompile_headers(${GAME} REUSE_FROM ${ENGINE})

1
example/src/game.cpp

@ -10,6 +10,5 @@ public:
Inferno::Application& Inferno::createApplication() Inferno::Application& Inferno::createApplication()
{ {
Game::initialize();
return Game::the(); return Game::the();
} }

11
src/inferno/application.cpp

@ -27,9 +27,6 @@ namespace Inferno {
Application::Application(s) Application::Application(s)
{ {
// Set singleton instance early
s_instance = this;
// Initialize // Initialize
Settings::initialize(); Settings::initialize();
@ -38,12 +35,7 @@ namespace Inferno {
m_window->setEventCallback(NF_BIND_EVENT(Application::onEvent)); m_window->setEventCallback(NF_BIND_EVENT(Application::onEvent));
Input::initialize(); Input::initialize();
ShaderManager::initialize();
TextureManager::initialize();
RenderCommand::initialize(); RenderCommand::initialize();
Renderer2D::initialize();
RendererCharacter::initialize();
FontManager::initialize();
m_scene = std::make_shared<Scene>(); m_scene = std::make_shared<Scene>();
m_scene->initialize(); m_scene->initialize();
@ -162,7 +154,6 @@ namespace Inferno {
RendererCharacter::the().endScene(); RendererCharacter::the().endScene();
m_window->render(); m_window->render();
} }
dbg() << "Application shutdown"; dbg() << "Application shutdown";
@ -226,4 +217,4 @@ namespace Inferno {
return Input::onMousePosition(e); return Input::onMousePosition(e);
} }
} } // namespace Inferno

6
src/inferno/application.h

@ -3,7 +3,7 @@
#include <memory> // std::unique_ptr, std::shared_ptr #include <memory> // std::unique_ptr, std::shared_ptr
#include "inferno/singleton.h" #include "ruc/singleton.h"
namespace Inferno { namespace Inferno {
@ -16,7 +16,7 @@ namespace Inferno {
class WindowCloseEvent; class WindowCloseEvent;
class WindowResizeEvent; class WindowResizeEvent;
class Application : public Singleton<Application> { class Application : public ruc::Singleton<Application> {
public: public:
Application(s); Application(s);
virtual ~Application(); virtual ~Application();
@ -48,7 +48,7 @@ namespace Inferno {
// To be defined in the game // To be defined in the game
extern Application& createApplication(); extern Application& createApplication();
} } // namespace Inferno
#endif // APPLICATION_H #endif // APPLICATION_H

17
src/inferno/render/font.h

@ -7,15 +7,15 @@
#include <unordered_map> // std::unordered_map #include <unordered_map> // std::unordered_map
#include <vector> // std::vector #include <vector> // std::vector
#include "glm/ext/vector_uint2.hpp" // glm::uvec2
#include "glm/ext/vector_int2.hpp" // glm::ivec2 #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/io/log.h"
#include "inferno/singleton.h"
namespace Inferno { namespace Inferno {
class Texture; class Texture;
struct Character { struct Character {
glm::uvec2 position; // Position glm::uvec2 position; // Position
@ -24,7 +24,7 @@ class Texture;
uint32_t advance; // Amount to advance to next glyph uint32_t advance; // Amount to advance to next glyph
}; };
// ----------------------------------------- // -------------------------------------
class Font { class Font {
public: public:
@ -50,9 +50,9 @@ class Texture;
std::unordered_map<unsigned char, std::shared_ptr<Character>> m_characterList; std::unordered_map<unsigned char, std::shared_ptr<Character>> m_characterList;
}; };
// ----------------------------------------- // -------------------------------------
class FontManager final : public Singleton<FontManager> { class FontManager final : public ruc::Singleton<FontManager> {
public: public:
FontManager(s); FontManager(s);
virtual ~FontManager(); virtual ~FontManager();
@ -69,12 +69,11 @@ class Texture;
std::unordered_map<std::string, std::shared_ptr<Font>> m_fontList; std::unordered_map<std::string, std::shared_ptr<Font>> m_fontList;
}; };
// -------------------------------------
// -----------------------------------------
const LogStream& operator<<(const LogStream& stream, const glm::ivec2& value); const LogStream& operator<<(const LogStream& stream, const glm::ivec2& value);
} } // namespace Inferno
#endif // FONT_H #endif // FONT_H

5
src/inferno/render/gltf.h

@ -8,7 +8,8 @@
#include <unordered_map> // std::unordered_map #include <unordered_map> // std::unordered_map
#include <vector> // std::vector #include <vector> // std::vector
#include "inferno/singleton.h" #include "ruc/singleton.h"
#include "inferno/util/json.h" #include "inferno/util/json.h"
#define GLTF_TYPE_SCALAR 1 #define GLTF_TYPE_SCALAR 1
@ -149,7 +150,7 @@ namespace Inferno {
// ----------------------------------------- // -----------------------------------------
class GltfManager final : Singleton<GltfManager> { class GltfManager final : ruc::Singleton<GltfManager> {
public: public:
GltfManager(s) {} GltfManager(s) {}
virtual ~GltfManager() {} virtual ~GltfManager() {}

20
src/inferno/render/renderer.h

@ -8,9 +8,9 @@
#include "glm/ext/vector_float2.hpp" // glm::vec2 #include "glm/ext/vector_float2.hpp" // glm::vec2
#include "glm/ext/vector_float3.hpp" // glm::vec3 #include "glm/ext/vector_float3.hpp" // glm::vec3
#include "glm/ext/vector_float4.hpp" // glm::vec4 #include "glm/ext/vector_float4.hpp" // glm::vec4
#include "ruc/singleton.h"
#include "inferno/component/transformcomponent.h" #include "inferno/component/transformcomponent.h"
#include "inferno/singleton.h"
namespace Inferno { namespace Inferno {
@ -39,7 +39,7 @@ namespace Inferno {
float offset = 0.0f; float offset = 0.0f;
}; };
// ----------------------------------------- // -------------------------------------
class RenderCommand { class RenderCommand {
public: public:
@ -57,7 +57,7 @@ namespace Inferno {
static int32_t textureUnitAmount(); static int32_t textureUnitAmount();
}; };
// ----------------------------------------- // -------------------------------------
class Renderer { class Renderer {
public: public:
@ -92,16 +92,15 @@ namespace Inferno {
std::shared_ptr<VertexArray> m_vertexArray; std::shared_ptr<VertexArray> m_vertexArray;
}; };
// ----------------------------------------- // -------------------------------------
class Renderer2D final class Renderer2D final
: public Renderer : public Renderer
, public Singleton<Renderer2D> { , public ruc::Singleton<Renderer2D> {
public: public:
Renderer2D(s); Renderer2D(s);
virtual ~Renderer2D(); virtual ~Renderer2D();
using Singleton<Renderer2D>::initialize;
using Singleton<Renderer2D>::destroy; using Singleton<Renderer2D>::destroy;
static const uint32_t quadCount = 1000; 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::mat4 color);
void drawQuad(const TransformComponent& transform, glm::vec4 color, std::shared_ptr<Texture> texture); 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); void drawQuad(const TransformComponent& transform, glm::mat4 color, std::shared_ptr<Texture> texture);
private: private:
void loadShader() override; void loadShader() override;
void flush() override; void flush() override;
@ -129,16 +129,15 @@ namespace Inferno {
glm::vec4 m_vertexPositions[vertexPerQuad]; glm::vec4 m_vertexPositions[vertexPerQuad];
}; };
// ----------------------------------------- // -------------------------------------
class RendererCharacter final class RendererCharacter final
: public Renderer : public Renderer
, public Singleton<RendererCharacter> { , public ruc::Singleton<RendererCharacter> {
public: public:
RendererCharacter(s); RendererCharacter(s);
virtual ~RendererCharacter(); virtual ~RendererCharacter();
using Singleton<RendererCharacter>::initialize;
using Singleton<RendererCharacter>::destroy; using Singleton<RendererCharacter>::destroy;
static const uint32_t quadCount = 1000; static const uint32_t quadCount = 1000;
@ -149,6 +148,7 @@ namespace Inferno {
void endScene(); void endScene();
void drawCharacter(std::array<CharacterVertex, vertexPerQuad>& characterQuad, std::shared_ptr<Texture> texture); void drawCharacter(std::array<CharacterVertex, vertexPerQuad>& characterQuad, std::shared_ptr<Texture> texture);
private: private:
void loadShader() override; void loadShader() override;
void flush() override; void flush() override;
@ -160,6 +160,6 @@ namespace Inferno {
CharacterVertex* m_vertexBufferPtr { nullptr }; CharacterVertex* m_vertexBufferPtr { nullptr };
}; };
} } // namespace Inferno
#endif // RENDERER_H #endif // RENDERER_H

10
src/inferno/render/shader.h

@ -7,8 +7,7 @@
#include <unordered_map> // std::unordered_map #include <unordered_map> // std::unordered_map
#include "glm/glm.hpp" #include "glm/glm.hpp"
#include "ruc/singleton.h"
#include "inferno/singleton.h"
namespace Inferno { namespace Inferno {
@ -45,9 +44,9 @@ namespace Inferno {
uint32_t m_id; uint32_t m_id;
}; };
// ----------------------------------------- // -------------------------------------
class ShaderManager final : public Singleton<ShaderManager> { class ShaderManager final : public ruc::Singleton<ShaderManager> {
public: public:
ShaderManager(s); ShaderManager(s);
virtual ~ShaderManager(); virtual ~ShaderManager();
@ -70,7 +69,6 @@ namespace Inferno {
std::unordered_map<std::string, std::shared_ptr<Shader>> m_shaderList; std::unordered_map<std::string, std::shared_ptr<Shader>> m_shaderList;
}; };
} } // namespace Inferno
#endif // SHADER_H #endif // SHADER_H

8
src/inferno/render/texture.h

@ -6,7 +6,7 @@
#include <string> // std::string #include <string> // std::string
#include <unordered_map> // std::unordered_map #include <unordered_map> // std::unordered_map
#include "inferno/singleton.h" #include "ruc/singleton.h"
namespace Inferno { namespace Inferno {
@ -37,9 +37,9 @@ namespace Inferno {
uint32_t m_dataFormat; uint32_t m_dataFormat;
}; };
// ----------------------------------------- // -------------------------------------
class TextureManager final : public Singleton<TextureManager> { class TextureManager final : public ruc::Singleton<TextureManager> {
public: public:
TextureManager(s); TextureManager(s);
virtual ~TextureManager(); virtual ~TextureManager();
@ -56,6 +56,6 @@ namespace Inferno {
std::unordered_map<std::string, std::shared_ptr<Texture>> m_textureList; std::unordered_map<std::string, std::shared_ptr<Texture>> m_textureList;
}; };
} } // namespace Inferno
#endif // TEXTURE_H #endif // TEXTURE_H

17
src/inferno/scene/scene.cpp

@ -1,3 +1,4 @@
#include "inferno/scene/scene.h"
#include "inferno/assert.h" #include "inferno/assert.h"
#include "inferno/component/cameracomponent.h" #include "inferno/component/cameracomponent.h"
#include "inferno/component/luascriptcomponent.h" #include "inferno/component/luascriptcomponent.h"
@ -5,7 +6,6 @@
#include "inferno/component/spritecomponent.h" #include "inferno/component/spritecomponent.h"
#include "inferno/component/tagcomponent.h" #include "inferno/component/tagcomponent.h"
#include "inferno/component/textareacomponent.h" #include "inferno/component/textareacomponent.h"
#include "inferno/scene/scene.h"
#include "inferno/script/cameracontroller.h" #include "inferno/script/cameracontroller.h"
#include "inferno/script/nativescript.h" #include "inferno/script/nativescript.h"
#include "inferno/system/camerasystem.h" #include "inferno/system/camerasystem.h"
@ -23,19 +23,10 @@ namespace Inferno {
m_registry = std::make_shared<entt::registry>(); m_registry = std::make_shared<entt::registry>();
TransformSystem::initialize();
TransformSystem::the().setRegistry(m_registry); TransformSystem::the().setRegistry(m_registry);
CameraSystem::initialize();
CameraSystem::the().setRegistry(m_registry); CameraSystem::the().setRegistry(m_registry);
RenderSystem::initialize();
RenderSystem::the().setRegistry(m_registry); RenderSystem::the().setRegistry(m_registry);
ScriptSystem::initialize();
ScriptSystem::the().setScene(this); ScriptSystem::the().setScene(this);
TextAreaSystem::initialize();
TextAreaSystem::the().setScene(this); TextAreaSystem::the().setScene(this);
// Load assets // Load assets
@ -122,14 +113,14 @@ namespace Inferno {
void Scene::validEntity(uint32_t entity) const 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) const LogStream& operator<<(const LogStream& stream, entt::entity entity)
{ {
return stream << static_cast<uint32_t>(entity); return stream << static_cast<uint32_t>(entity);
} }
} } // namespace Inferno

7
src/inferno/system/camerasystem.h

@ -5,8 +5,7 @@
#include "entt/entity/registry.hpp" // entt::entity, entt::registry #include "entt/entity/registry.hpp" // entt::entity, entt::registry
#include "glm/ext/matrix_float4x4.hpp" // glm::mat4 #include "glm/ext/matrix_float4x4.hpp" // glm::mat4
#include "ruc/singleton.h"
#include "inferno/singleton.h"
#define NEAR_PLANE 0.1f #define NEAR_PLANE 0.1f
#define FAR_PLANE 100.0f #define FAR_PLANE 100.0f
@ -16,7 +15,7 @@ namespace Inferno {
struct TransformComponent; struct TransformComponent;
struct CameraComponent; struct CameraComponent;
class CameraSystem final : public Singleton<CameraSystem> { class CameraSystem final : public ruc::Singleton<CameraSystem> {
public: public:
CameraSystem(s); CameraSystem(s);
virtual ~CameraSystem(); virtual ~CameraSystem();
@ -34,6 +33,6 @@ namespace Inferno {
std::shared_ptr<entt::registry> m_registry; std::shared_ptr<entt::registry> m_registry;
}; };
} } // namespace Inferno
#endif // CAMERA_SYSTEM_H #endif // CAMERA_SYSTEM_H

6
src/inferno/system/rendersystem.h

@ -4,13 +4,13 @@
#include <memory> //std::shared_ptr #include <memory> //std::shared_ptr
#include "entt/entity/registry.hpp" // entt::entity, entt::registry #include "entt/entity/registry.hpp" // entt::entity, entt::registry
#include "ruc/singleton.h"
#include "inferno/render/renderer.h" #include "inferno/render/renderer.h"
#include "inferno/singleton.h"
namespace Inferno { namespace Inferno {
class RenderSystem final : public Singleton<RenderSystem> { class RenderSystem final : public ruc::Singleton<RenderSystem> {
public: public:
RenderSystem(s); RenderSystem(s);
virtual ~RenderSystem(); virtual ~RenderSystem();
@ -23,6 +23,6 @@ namespace Inferno {
std::shared_ptr<entt::registry> m_registry; std::shared_ptr<entt::registry> m_registry;
}; };
} } // namespace Inferno
#endif // RENDER_SYSTEM_H #endif // RENDER_SYSTEM_H

6
src/inferno/system/scriptsystem.h

@ -3,7 +3,7 @@
#include <cstdint> // uint32_t #include <cstdint> // uint32_t
#include "inferno/singleton.h" #include "ruc/singleton.h"
namespace Inferno { namespace Inferno {
@ -12,7 +12,7 @@ namespace Inferno {
class Scene; class Scene;
class ScriptSystem final : public Singleton<ScriptSystem> { class ScriptSystem final : public ruc::Singleton<ScriptSystem> {
public: public:
ScriptSystem(s); ScriptSystem(s);
virtual ~ScriptSystem(); virtual ~ScriptSystem();
@ -29,6 +29,6 @@ namespace Inferno {
Scene* m_scene { nullptr }; Scene* m_scene { nullptr };
}; };
} } // namespace Inferno
#endif // SCRIPT_SYSTEM_H #endif // SCRIPT_SYSTEM_H

5
src/inferno/system/textareasystem.h

@ -7,9 +7,9 @@
#include <vector> // std::vector #include <vector> // std::vector
#include "glm/ext/vector_float3.hpp" // glm::vec3 #include "glm/ext/vector_float3.hpp" // glm::vec3
#include "ruc/singleton.h"
#include "inferno/render/renderer.h" #include "inferno/render/renderer.h"
#include "inferno/singleton.h"
namespace Inferno { namespace Inferno {
@ -18,7 +18,7 @@ namespace Inferno {
class Font; class Font;
class Scene; class Scene;
class TextAreaSystem final : public Singleton<TextAreaSystem> { class TextAreaSystem final : public ruc::Singleton<TextAreaSystem> {
public: public:
TextAreaSystem(s); TextAreaSystem(s);
virtual ~TextAreaSystem(); virtual ~TextAreaSystem();
@ -35,5 +35,4 @@ namespace Inferno {
} // namespace Inferno } // namespace Inferno
#endif // TEXTAREA_H #endif // TEXTAREA_H

6
src/inferno/system/transformsystem.h

@ -4,11 +4,11 @@
#include <memory> //std::shared_ptr #include <memory> //std::shared_ptr
#include "entt/entity/registry.hpp" // entt::entity, entt::registry #include "entt/entity/registry.hpp" // entt::entity, entt::registry
#include "inferno/singleton.h" #include "ruc/singleton.h"
namespace Inferno { namespace Inferno {
class TransformSystem final : public Singleton<TransformSystem> { class TransformSystem final : public ruc::Singleton<TransformSystem> {
public: public:
TransformSystem(s); TransformSystem(s);
virtual ~TransformSystem(); virtual ~TransformSystem();
@ -21,6 +21,6 @@ namespace Inferno {
std::shared_ptr<entt::registry> m_registry; std::shared_ptr<entt::registry> m_registry;
}; };
} } // namespace Inferno
#endif // TRANSFORM_SYSTEM_H #endif // TRANSFORM_SYSTEM_H

Loading…
Cancel
Save