Engine
Raylib based game framework
Loading...
Searching...
No Matches
Engine.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "NonCopyable.hpp"
4#include "Types.hpp"
5
6#include "Events.hpp"
7#include "LuaManager.hpp"
8#include "Registry.hpp"
9#include "Renderer.hpp"
10#include "ResourceManager.hpp"
11#include "SceneManager.hpp"
12#include "SystemManager.hpp"
13#include "entt/entt.hpp"
14#include "raylib.h"
15
18
19#ifndef __EMSCRIPTEN__
20#include "Networking/AsyncNetwork.hpp"
21#include "bsThreadPool/BS_thread_pool.hpp"
22#endif
23
24#include <string>
25
30
31using Dispatcher = entt::dispatcher;
32
33#define REGISTRY Engine::Get().registry
34#define DISPATCHER Engine::Get().dispatcher
35
36// Core
37#define RENDERER Engine::Get().renderer
38#define RESOURCE_MANAGER Engine::Get().resourceManager
39#define SCENE_MANAGER Engine::Get().sceneManager
40#define SYSTEM_MANAGER Engine::Get().systemManager
41#define LUA_MANAGER Engine::Get().luaManager
42
43#ifndef __EMSCRIPTEN__
44#define NETWORK Engine::Get().network
45#define THREAD_POOL Engine::Get().threadPool
46#endif
47
48// Systems
49#define INPUT_SYSTEM SYSTEM_MANAGER.GetSystem<InputSystem>()
50#define AUDIO_SYSTEM SYSTEM_MANAGER.GetSystem<AudioSystem>()
51
61{
62 u32 width = 1280;
63 u32 height = 720;
64 std::string title;
65
66 u32 virtualWidth = 1920;
67 u32 virtualHeight = 1080;
68
69 bool fullscreen = false;
70 bool borderlessFullscreen = false;
71 bool resizable = false;
72 bool undecorated = false;
73 bool topmost = false;
74 bool alwaysRun = false;
75 bool transparent = false;
76 bool highDpi = false;
77 bool msaa4x = false;
78};
79
89class Engine : public NonCopyable<>
90{
91public:
92
103 Engine(const WindowInfo& windowInfo);
104
105 ~Engine();
106
117 template <typename T, typename... Args>
118 void SetFirstScene(Args&&... args)
119 {
120 m_sceneManager.AddScene<T>(std::forward<Args>(args)...);
121 m_sceneManager.ChangeScene<T>();
122 }
123
139 void Run(const u32 targetFps, const u32 updateFrequency, const u8 maxUpdatesPerFrame = 5);
140
144 double GetUpdateTime() const;
145
149 double GetDrawTime() const;
150
159 static Engine& Get();
160
162 Registry& registry = m_registry;
163
165 Dispatcher& dispatcher = m_dispatcher;
166
168 Renderer& renderer = m_renderer;
169
171 ResourceManager& resourceManager = m_resourceManager;
172
174 SceneManager& sceneManager = m_sceneManager;
175
177 SystemManager& systemManager = m_systemManager;
178
180 LuaManager& luaManager = m_luaManager;
181
182#ifndef __EMSCRIPTEN__
184 AsyncNetwork& network = m_network;
185
187 BS::thread_pool<BS::tp::none> threadPool;
188#endif
189
190private:
191
192 static void SetFlags(const WindowInfo& windowInfo);
193
194 void RaylibResourceManager();
195
196 // Event handling
197 void OnCloseGameEvent(const Event::CloseGame& event);
198
199 // Ecs
200 Registry m_registry;
201 Dispatcher m_dispatcher;
202
203 // Core systems
204 Renderer m_renderer;
205 ResourceManager m_resourceManager;
206 SceneManager m_sceneManager;
207 SystemManager m_systemManager;
208 LuaManager m_luaManager;
209
210#ifndef __EMSCRIPTEN__
211 AsyncNetwork m_network;
212#endif
213
214 // Timings
215 double m_updateTime = 0;
216 double m_drawTime = 0;
217
218 // Canvas
219 RenderTexture2D m_canvas;
220 u32 m_virtualWidth = 0;
221 u32 m_virtualHeight = 0;
222
223 bool m_running = true;
224
225 static inline Engine* s_engine = nullptr;
226};
Sound effect and music playback management.
Engine events.
Abstract input binding system supporting keyboard, mouse and gamepad.
Lua sandboxing and scripts.
Engine entt::registry wrapper.
Sprite rendering.
Engine resource caches and manager.
Scene organization.
Engine lifelong systems and their managing.
Core engine.
Definition Engine.hpp:90
LuaManager & luaManager
Lua scripting manager.
Definition Engine.hpp:180
double GetDrawTime() const
Returns how long the average draw loop took in milliseconds.
Definition Engine.cpp:161
void SetFirstScene(Args &&... args)
Sets the initial scene and immediately transitions to it.
Definition Engine.hpp:118
AsyncNetwork & network
Asynchronous networking (unavailable on Emscripten).
Definition Engine.hpp:184
BS::thread_pool< BS::tp::none > threadPool
Thread pool for background tasks (unavailable on Emscripten).
Definition Engine.hpp:187
ResourceManager & resourceManager
Resource cache manager.
Definition Engine.hpp:171
Registry & registry
Entity-component registry.
Definition Engine.hpp:162
static Engine & Get()
Returns the active engine instance.
Definition Engine.cpp:166
void Run(const u32 targetFps, const u32 updateFrequency, const u8 maxUpdatesPerFrame=5)
Runs the engine loop.
Definition Engine.cpp:60
Renderer & renderer
Sprite renderer.
Definition Engine.hpp:168
SystemManager & systemManager
System execution manager.
Definition Engine.hpp:177
SceneManager & sceneManager
Scene lifecycle manager.
Definition Engine.hpp:174
double GetUpdateTime() const
Returns how long the average update loop took in milliseconds.
Definition Engine.cpp:156
Dispatcher & dispatcher
Event dispatcher.
Definition Engine.hpp:165
Engine(const WindowInfo &windowInfo)
Creates the engine and all its subsystems.
Definition Engine.cpp:8
Manages sandboxed Lua scripts and their integration with the event dispatcher.
Definition LuaManager.hpp:46
Wraps entt::registry with a safer, callback-aware API.
Definition Registry.hpp:29
Draws all entities with Sprite and Transform components.
Definition Renderer.hpp:24
Owns a collection of typed resource caches.
Definition ResourceManager.hpp:179
Manages scenes and the execution of the current active scene.
Definition SceneManager.hpp:71
Manages execution order and lifetime of systems.
Definition SystemManager.hpp:59
Event signalling that the game should close.
Definition Events.hpp:24
Initial window info.
Definition Engine.hpp:61