Engine
Raylib based game framework
Loading...
Searching...
No Matches
Engine.h
1#pragma once
2
3#include "Types.h"
4
5#include "LuaManager.h"
6#include "NetworkManager.h"
7#include "Renderer.h"
8#include "ResourceManager.h"
9#include "SceneManager.h"
10#include "SystemManager.h"
11#include "bsThreadPool/BS_thread_pool.hpp"
12#include "entt/entt.h"
13#include "raylib.h"
14
15#include <string>
16
17using Entity = entt::entity;
18using Registry = entt::registry;
19using Dispatcher = entt::dispatcher;
20
21namespace Event
22{
27 struct CloseGame
28 {
37 static void LuaRegister(sol::state& lua)
38 {
39 Lua::RegisterType<Event::CloseGame>(lua, DemangleWithoutNamespace<Event::CloseGame>().c_str(),
40 sol::constructors<CloseGame()>());
41 }
42 };
43}
44
55{
56 u32 width = 1280;
57 u32 height = 720;
58 std::string title;
59
60 u32 virutalWidth = 1920;
61 u32 virtualHeight = 1080;
62
63 bool fullscreen = false;
64 bool borderlessFullscreen = false;
65 bool resizable = false;
66 bool undecorated = false;
67 bool topmost = false;
68 bool alwaysRun = false;
69 bool transparent = false;
70 bool highDpi = false;
71 bool msaa4x = false;
72};
73
82class Engine
83{
84public:
85
92 Engine(const WindowInfo& windowInfo);
93 ~Engine();
94
103 template <typename T, typename... Args>
104 void SetFirstScene(Args&&... args)
105 {
106 m_sceneManager.AddScene<T>(std::forward<Args>(args)...);
107 m_sceneManager.ChangeScene<T>();
108 }
109
124 void Run(const u32 targetFps, const u32 updateFrequency, const u8 maxUpdatesPerFrame = 5);
125
130 Vector2 GetVirtualMousePos() const;
131
136 double GetUpdateTime() const;
137
142 double GetDrawTime() const;
143
151 static Engine& Get();
152
153 Registry& registry = m_registry;
154 Dispatcher& dispatcher = m_dispatcher;
155
156 // Core systems
157 Renderer& renderer = m_renderer;
158 ResourceManager& resourceManager = m_resourceManager;
159 SceneManager& sceneManager = m_sceneManager;
160 SystemManager& systemManager = m_systemManager;
161 LuaManager& luaManager = m_luaManager;
162 NetworkManager& networkManager = m_networkManager;
163
164 // Thread pool
165 BS::thread_pool<BS::tp::none> threadPool;
166
167private:
168
169 static void SetFlags(const WindowInfo& windowInfo);
170
171 // Event handling
172 void OnCloseGameEvent(const Event::CloseGame& event);
173
174 // Ecs
175 Registry m_registry;
176 Dispatcher m_dispatcher;
177
178 // Core systems
179 Renderer m_renderer;
180 ResourceManager m_resourceManager;
181 SceneManager m_sceneManager;
182 SystemManager m_systemManager;
183 LuaManager m_luaManager;
184 NetworkManager m_networkManager;
185
186 // Mouse
187 Vector2 m_virtualMousePos = {};
188
189 // Timers
190 double m_updateTime = 0;
191 double m_drawTime = 0;
192
193 // Canvas
194 RenderTexture2D m_canvas;
195 u32 m_virtualWidth = 0;
196 u32 m_virtualHeight = 0;
197
198 bool m_running = true;
199};
Core engine.
Definition Engine.h:83
double GetDrawTime() const
Returns how long the average draw loop took in ms.
Definition Engine.cpp:148
void SetFirstScene(Args &&... args)
Sets the initial scene.
Definition Engine.h:104
static Engine & Get()
Returns the engine instance.
Definition Engine.cpp:153
void Run(const u32 targetFps, const u32 updateFrequency, const u8 maxUpdatesPerFrame=5)
Runs the engine loop.
Definition Engine.cpp:51
double GetUpdateTime() const
Returns how long the average update loop took in ms.
Definition Engine.cpp:143
Vector2 GetVirtualMousePos() const
Returns the mouse position adjusted to the virtual canvas.
Definition Engine.cpp:138
Definition LuaManager.h:18
Definition NetworkManager.h:13
Definition Renderer.h:7
Definition ResourceManager.h:89
Manages scenes and the the execution of the current scene.
Definition SceneManager.h:64
void AddScene(Args &&... args)
Adds a scene to the manager.
Definition SceneManager.h:99
void ChangeScene()
Queues a scene change at the end of the frame.
Definition SceneManager.h:136
Manages execution and order of systems.
Definition SystemManager.h:57
Event signalling that the game should close.
Definition Engine.h:28
static void LuaRegister(sol::state &lua)
Registers the event type with Lua.
Definition Engine.h:37
Initial window info.
Definition Engine.h:55