Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
Engine.h
Go to the documentation of this file.
1/**
2 * @file Engine.h
3 * @ingroup mnd_core
4 * @brief Central singleton that owns and coordinates all engine subsystems.
5 *
6 * ## Startup sequence (see main.cpp)
7 * @code
8 * Engine &engine = Engine::GetInstance();
9 * engine.SetApplication(new Game()); // hand over ownership
10 * engine.Init(1280, 720); // GLFW → window → GLEW → app->Init()
11 * engine.Run(); // main loop until close requested
12 * engine.Destroy(); // app->Destroy() → GLFW cleanup
13 * @endcode
14 *
15 * ## Main loop (Engine::Run)
16 * Each iteration:
17 * 1. `glfwPollEvents()` → GLFW callbacks update InputManager.
18 * 2. Compute `deltaTime` with a steady_clock timestamp.
19 * 3. `app->Update(dt)` — game logic runs here.
20 * 4. Collect CameraData and LightData from the active Scene.
21 * 5. `RenderQueue::Draw()` drains all submitted RenderCommands.
22 * 6. `glfwSwapBuffers()` — present frame.
23 *
24 * @note Engine is a non-copyable, non-movable singleton (Meyer's singleton pattern).
25 * Access it anywhere via `Engine::GetInstance()`.
26 *
27 * @see Application, RenderQueue, Scene
28 */
29
30#pragma once
31#include <chrono>
32#include <memory>
33
34#include "audio/AudioManager.h"
35#include "editor/Editor.h"
36#include "graphics/GraphicsAPI.h"
41#include "graphics/Texture.h"
42#include "input/InputManager.h"
43#include "io/FileSystem.h"
44#include "physics/PhysicsManager.h"
45#include "render/RenderQueue.h"
48#include "scene/Scene.h"
49
50struct GLFWwindow;
51
52namespace mnd
53{
54class Application;
55
56/**
57 * @brief Singleton façade that drives the full engine lifecycle.
58 *
59 * Owns one instance of every major subsystem (InputManager, GraphicsAPI,
60 * RenderQueue, FileSystem, TextureManager, Scene) and the user Application.
61 * All subsystems are reachable via the Get*() accessors from anywhere in
62 * the codebase without passing references through layers.
63 */
64class Engine
65{
66public:
67 /// Returns the single Engine instance (created on first call).
68 static Engine &GetInstance();
69
70 Engine() = default;
71 Engine(const Engine &) = delete;
72 Engine(Engine &&) = delete;
73 Engine &operator=(const Engine &) = delete;
74 Engine &operator=(Engine &&) = delete;
75
76 /**
77 * @brief Initialise the window, OpenGL context, and subsystems.
78 *
79 * Sequence: GLFW init → window create → GLFW callbacks → GLEW init →
80 * GraphicsAPI::Init() → Application::Init().
81 *
82 * @param width Framebuffer width in pixels.
83 * @param height Framebuffer height in pixels.
84 * @return true on success; false if any step fails (logged to stderr).
85 */
86 bool Init(int width, int height);
87
88 /**
89 * @brief Enter the blocking main loop.
90 *
91 * Returns when the window close button is pressed or the application
92 * calls SetNeedsToBeClosed(true). See the class-level documentation for
93 * the per-frame execution order.
94 */
95 void Run();
96
97 /**
98 * @brief Shut down the application and release all GL resources.
99 *
100 * Calls Application::Destroy(), then terminates GLFW. Must be called
101 * after Run() returns to ensure clean resource cleanup.
102 */
103 void Destroy();
104
105 /**
106 * @brief Transfer ownership of the application to the engine.
107 * @param app Heap-allocated Application subclass. Engine takes ownership.
108 */
109 void SetApplication(Application *app);
110
111 /// Returns a raw pointer to the active application (owned by the engine).
113
114 /// Returns the InputManager that tracks keyboard and mouse state.
116
117 /// Returns the low-level OpenGL wrapper used for all draw calls.
119
120 /// Returns the per-frame command queue that batches and issues draw calls.
122
123 /// Returns the 2D sprite/font renderer. Queue draw calls during Application::Update().
124 SpriteRenderer &GetSpriteRenderer() { return m_spriteRenderer; }
125
126 /// Returns the global particle pool. Emitter components push particles via Spawn().
127 ParticleSystem &GetParticleSystem() { return m_particleSystem; }
128
129 /// Returns the file system helper for asset-relative path resolution.
131
132 /// Returns the TextureManager that caches loaded textures by path.
134
136
138
139 /**
140 * @brief Replace the active scene (takes ownership).
141 * @param scene Heap-allocated Scene. Previous scene is destroyed.
142 */
143 void SetScene(Scene *scene);
144
145 /// Returns the currently active scene, or nullptr if none is set.
146 Scene *GetScene();
147
148 /// ImGui-based editor overlay.
149 Editor &GetEditor() { return m_editor; }
150
151 /// Mutable render settings edited by the Editor's Render panel.
152 RenderSettings &GetRenderSettings() { return m_renderSettings; }
153
154 /// Offscreen low-res render target (only active when RenderSettings::useInternalRes is true).
155 RenderTarget &GetSceneTarget() { return m_sceneTarget; }
156
157 /// Outline / highlight post-pass; tunables exposed via the Editor render panel.
158 PostProcess &GetPostProcess() { return m_postProcess; }
159
160 GLFWwindow *GetWindow() { return m_window; }
161
162 /// Game-time multiplier applied to deltaTime each frame (1.0 = realtime).
163 float GetTimeScale() const { return m_timeScale; }
164 void SetTimeScale(float scale) { m_timeScale = scale; }
165
166 /// When true, deltaTime is forced to 0.0 (Application::Update still runs).
167 bool IsPaused() const { return m_paused; }
168 void SetPaused(bool paused) { m_paused = paused; }
169
170 /// Render a centered loading panel with optional progress bar.
171 /// Pass `progress < 0` for an indeterminate (animated) bar.
172 /// Performs two swaps on first call so the frame is presented before
173 /// blocking work begins on platforms that delay the first present.
174 void DrawLoadingScreen(const char *message, float progress = -1.0F);
175
176 /// Update the loading panel mid-load (single swap). Cheap to call per step.
177 void UpdateLoadingProgress(float progress, const char *message);
178
179private:
180 std::unique_ptr<Application> m_application; ///< The user game/lab instance.
181 std::chrono::steady_clock::time_point m_lastTimePoint; ///< Timestamp of the previous frame.
182 GLFWwindow *m_window = nullptr; ///< GLFW window handle.
183 InputManager m_inputManager; ///< Keyboard + mouse state.
184 GraphicsAPI m_graphicsAPI; ///< GL wrapper (shaders, buffers, draw).
185 std::unique_ptr<RendererBackend> m_rendererBackend; ///< Active renderer backend facade.
186 RenderQueue m_renderQueue; ///< Per-frame render command list.
187 SpriteRenderer m_spriteRenderer; ///< 2D sprite/text overlay queue.
188 ParticleSystem m_particleSystem; ///< CPU billboard particle pool.
189 FileSystem m_fileSystem; ///< Asset path resolver.
190 TextureManager m_textureManager; ///< Texture cache.
191 AudioManager m_audioManager;
192
193 PhysicsManager m_physicsManager;
194 std::unique_ptr<Scene> m_currentScene; ///< Active scene graph.
195
196 float m_timeScale = 1.0F;
197 bool m_paused = false;
198
199 float m_fpsSmoothed = 0.0F; ///< EMA of 1/dt for the overlay counter.
200 float m_fpsRefreshTimer = 0.0F; ///< Throttles the displayed value's refresh.
201 float m_fpsDisplayed = 0.0F; ///< The currently rendered FPS value.
202
203 Editor m_editor; ///< ImGui overlay.
204 RenderTarget m_sceneTarget; ///< Low-res FBO for pixelated look.
205 PostProcess m_postProcess; ///< Outline post-pass on the scene target.
206 RenderSettings m_renderSettings; ///< Editor-tweakable render params.
207};
208
209} // namespace mnd
In-game ImGui overlay: hierarchy, inspector, console, stats.
Pool-allocated CPU particles drawn as additive billboards.
Three.js-style pixel-art post-pass.
Per-frame renderer tuning knobs (PSX-style pixelation, fog, ambient).
Offscreen FBO with colour, view-normal and depth attachments.
Interface that every runnable game or lab must implement.
Definition Application.h:36
Global audio backend wrapper.
ImGui-based in-game editor overlay.
Definition Editor.h:43
Singleton façade that drives the full engine lifecycle.
Definition Engine.h:65
GraphicsAPI & GetGraphicsAPI()
Returns the low-level OpenGL wrapper used for all draw calls.
Definition Engine.cpp:418
SpriteRenderer & GetSpriteRenderer()
Returns the 2D sprite/font renderer. Queue draw calls during Application::Update().
Definition Engine.h:124
RenderQueue & GetRenderQueue()
Returns the per-frame command queue that batches and issues draw calls.
Definition Engine.cpp:423
PostProcess & GetPostProcess()
Outline / highlight post-pass; tunables exposed via the Editor render panel.
Definition Engine.h:158
Scene * GetScene()
Returns the currently active scene, or nullptr if none is set.
Definition Engine.cpp:448
InputManager & GetInputManager()
Returns the InputManager that tracks keyboard and mouse state.
Definition Engine.cpp:413
GLFWwindow * GetWindow()
Definition Engine.h:160
Editor & GetEditor()
ImGui-based editor overlay.
Definition Engine.h:149
FileSystem & GetFileSystem()
Returns the file system helper for asset-relative path resolution.
Definition Engine.cpp:453
bool IsPaused() const
When true, deltaTime is forced to 0.0 (Application::Update still runs).
Definition Engine.h:167
void SetScene(Scene *scene)
Replace the active scene (takes ownership).
Definition Engine.cpp:428
bool Init(int width, int height)
Initialise the window, OpenGL context, and subsystems.
Definition Engine.cpp:65
void Run()
Enter the blocking main loop.
Definition Engine.cpp:174
float GetTimeScale() const
Game-time multiplier applied to deltaTime each frame (1.0 = realtime).
Definition Engine.h:163
Application * GetApplication()
Returns a raw pointer to the active application (owned by the engine).
Definition Engine.cpp:408
AudioManager & GetAudioManager()
Definition Engine.cpp:438
static Engine & GetInstance()
Returns the single Engine instance (created on first call).
Definition Engine.cpp:59
RenderSettings & GetRenderSettings()
Mutable render settings edited by the Editor's Render panel.
Definition Engine.h:152
void SetPaused(bool paused)
Definition Engine.h:168
void SetApplication(Application *app)
Transfer ownership of the application to the engine.
Definition Engine.cpp:403
void DrawLoadingScreen(const char *message, float progress=-1.0F)
Definition Engine.cpp:513
Engine & operator=(Engine &&)=delete
TextureManager & GetTextureManager()
Returns the TextureManager that caches loaded textures by path.
Definition Engine.cpp:433
ParticleSystem & GetParticleSystem()
Returns the global particle pool. Emitter components push particles via Spawn().
Definition Engine.h:127
Engine(const Engine &)=delete
Engine()=default
void SetTimeScale(float scale)
Definition Engine.h:164
Engine(Engine &&)=delete
void UpdateLoadingProgress(float progress, const char *message)
Update the loading panel mid-load (single swap). Cheap to call per step.
Definition Engine.cpp:525
PhysicsManager & GetPhysicsManager()
Definition Engine.cpp:443
RenderTarget & GetSceneTarget()
Offscreen low-res render target (only active when RenderSettings::useInternalRes is true).
Definition Engine.h:155
void Destroy()
Shut down the application and release all GL resources.
Definition Engine.cpp:382
Engine & operator=(const Engine &)=delete
Resolves asset paths and loads files into memory.
Definition FileSystem.h:28
OpenGL 3.3 Core Profile abstraction layer.
Definition GraphicsAPI.h:41
Stores per-frame keyboard and mouse input state.
Owns the Bullet btDiscreteDynamicsWorld and all auxiliary collision structures (broadphase,...
Accumulates RenderCommands during Update and draws them in one pass.
Definition RenderQueue.h:57
Offscreen FBO with MRT colour + sampleable depth for low-resolution rendering.
Container for the entire game object graph.
Definition Scene.h:59
Path-keyed cache that prevents the same image from being uploaded twice.
Definition Texture.h:94