SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
Application.hpp
Go to the documentation of this file.
1#ifndef APPLICATION_HPP
2#define APPLICATION_HPP
3
4#include <Core/OSDef.hpp>
5#include <Core/GameBase.hpp>
6#include <map>
7#include <string>
10#include <Core/Timer.hpp>
12#include <Debug/Benchmark.hpp>
13
14namespace Sleak {
15
16/// Parses raw argv into an index-and-flag lookup. Kept around on
17/// ApplicationDefaults for the lifetime of the Application.
18/// @ingroup core
19struct ENGINE_API Arguments {
20 Arguments(int argc, char** argv) : Size(argc), Args(argv) {
21 for (int i = 1; i < Size; i++) {
22 std::string arg = Args[i - 1];
23 std::string argnext = Args[i];
24
25 if (arg[0] == '-' && argnext[0] != '-') ArgMap[arg] = argnext;
26 }
27
28 }
29
30 const char* operator[](int index) const {
31 if (index < Size)
32 return Args[index];
33 else
34 return "";
35 }
36
37 const std::string operator[](std::string key) const {
38
39 for(auto& pair : ArgMap)
40 if(pair.first == key)
41 return pair.second;
42
43 return "";
44 }
45
46 int Size = 0;
47 char** Args = nullptr;
48 std::map<std::string, std::string> ArgMap;
49};
50
51/// Construction settings for Application: project name plus parsed CLI args.
52/// @ingroup core
53struct ENGINE_API ApplicationDefaults {
54 std::string Name = "";
56};
57
58class Window;
59class DebugOverlay;
60namespace RenderEngine { class Renderer; }
61
62/// Owns the window, renderer, and game loop. One instance per process,
63/// reachable globally through GetInstance().
64///
65/// Construct one in `main()`, hand it your GameBase, and call Run(). It
66/// creates the window, selects a graphics backend, and then drives the
67/// frame loop until the window closes: poll events, update the active
68/// scene, call GameBase::Loop, render, present.
69///
70/// The backend comes from the `-r` command line flag when
71/// Sleak::CommandLine::Parse has run, otherwise from the platform default
72/// (DirectX 11 on Windows, Vulkan elsewhere). Window size and title honor
73/// `-w`, `-h`, and `-t` the same way.
74///
75/// Everything on this class is main-thread only. Constructing a second
76/// Application throws.
77///
78/// Rendering quality settings (MSAA, VSync, SSAO, SSR, TAA, bloom, IBL)
79/// can be changed at any point after Run() starts. Settings that belong to
80/// the deferred path are ignored by backends that do not implement it, so
81/// check GetGraphicsCaps() before exposing them in a settings UI.
82///
83/// @code{.cpp}
84/// int main(int argc, char** argv) {
85/// Sleak::CommandLine::Parse(argc, argv);
86/// Sleak::Logger::Init("MyGame");
87///
88/// Sleak::ApplicationDefaults defaults{
89/// .Name = "MyGame",
90/// .CommandLineArgs = Sleak::Arguments(argc, argv)};
91///
92/// Game* game = new Game();
93/// Sleak::Application app(defaults);
94/// return app.Run(game);
95/// }
96///
97/// // Anywhere later, from game code:
98/// auto* app = Sleak::Application::GetInstance();
99/// app->SetMSAASampleCount(8);
100/// app->SetVSync(true);
101/// SLEAK_INFO("{} at {} FPS", app->GetRendererTypeStr(), app->GetFPS());
102///
103/// // Before tearing down scene resources:
104/// app->WaitGPUIdle();
105/// @endcode
106///
107/// @see GameBase, Scene, CommandLine, Logger, GraphicsConfig
108/// @ingroup core
109class ENGINE_API Application {
110 public:
111 Application(const char* ProjectName);
115 ~Application();
116
117 /// Drives the game loop until the window closes; returns the process exit code.
118 int Run(GameBase* game);
119
120 Window& GetWindow();
121 void CloseApplication();
122 /// Blocks until the GPU finishes all in-flight work. Call before tearing down scene resources.
123 void WaitGPUIdle();
124
125 void SetCursorVisible(bool visible);
126 void SetMouseRelativeMode(bool enabled);
127
128 /// Active renderer backend, or null before Run() initializes it.
129 RenderEngine::Renderer* GetRenderer() { return renderer; }
130 GameBase* GetGame() { return Game; }
131
132 /// The one Application for this process, or null before construction.
133 static Application* GetInstance() { return Instance; }
134
135 // Public renderer stat accessors for Game
136 /// Frames rendered in the last second.
137 int GetFPS() const;
138 /// Duration of the last frame, in seconds.
139 float GetFrameTime() const;
140 /// Vertices submitted in the last frame.
141 int GetVertices() const;
142 /// Triangles submitted in the last frame.
143 int GetTriangles() const;
144 /// Human-readable name of the active backend (e.g. "Vulkan").
145 const char* GetRendererTypeStr() const;
146 /// UI accent color associated with the active backend.
147 void GetRendererTypeColor(float& r, float& g, float& b) const;
148 size_t GetGPUMemoryUsed() const;
149 size_t GetGPUMemoryBudget() const;
150 uint32_t GetMSAASampleCount() const;
151 uint32_t GetMaxMSAASampleCount() const;
152 void SetMSAASampleCount(uint32_t samples);
153
154 bool GetVSync() const;
155 void SetVSync(bool enabled);
156
157 // Screen-space ambient occlusion (deferred path only)
158 bool IsSSAOEnabled() const;
159 void SetSSAOEnabled(bool enabled);
160 float GetSSAORadius() const;
161 void SetSSAORadius(float radius);
162 float GetSSAOBias() const;
163 void SetSSAOBias(float bias);
164 float GetSSAOPower() const;
165 void SetSSAOPower(float power);
166
167 // Image-based lighting (deferred path only)
168 bool IsIBLEnabled() const;
169 void SetIBLEnabled(bool enabled);
170 float GetIBLIntensity() const;
171 void SetIBLIntensity(float intensity);
172
173 // Screen-space reflections (deferred path only)
174 bool IsSSREnabled() const;
175 void SetSSREnabled(bool enabled);
176
177 // Temporal anti-aliasing (deferred path only)
178 bool IsTAAEnabled() const;
179 void SetTAAEnabled(bool enabled);
180
181 // Bloom (deferred path only)
182 bool IsBloomEnabled() const;
183 void SetBloomEnabled(bool enabled);
184
185 // Per-game graphics configuration / quality presets
186 /// Pushes every field of cfg onto the active renderer in one call.
187 void ApplyGraphicsConfig(const GraphicsConfig& cfg);
188 /// Config last passed to ApplyGraphicsConfig().
189 const GraphicsConfig& GetGraphicsConfig() const;
190
191 // Active backend's feature capability mask (RenderEngine::GraphicsCaps)
192 uint32_t GetGraphicsCaps() const;
193
194 Benchmark* GetBenchmark() { return m_benchmark; }
195
196 /// Stashes the new size; the resize is applied at the start of the next frame.
197 void OnWindowResize(const Sleak::Events::WindowResizeEvent& e);
198 /// Treats entering/leaving fullscreen as a resize to the current window size.
199 void OnWindowFullScreen(const Sleak::Events::WindowFullScreen& e);
200
201 /// Handles engine-level hotkeys (F9 camera toggle, F11 fullscreen, F12 benchmark, Esc).
202 void OnKeyPressed(const Sleak::Events::Input::KeyPressedEvent& e);
203 void onMouseMove(const Sleak::Events::Input::MouseMovedEvent& e);
204 void onMouseClick(const Sleak::Events::Input::MouseButtonPressedEvent& e);
205
206 private:
207 ApplicationDefaults Specification;
208 Window* CoreWindow;
209 GameBase* Game;
210 RenderEngine::Renderer* renderer;
211 DebugOverlay* m_DebugOverlay = nullptr;
212 Benchmark* m_benchmark = nullptr;
213 GraphicsConfig m_graphicsConfig;
214 float DeltaTime;
215
216 Timer FrameTimer;
217
218 // Deferred resize — set in the event handler, applied at start of next frame
219 bool m_pendingResize = false;
220 uint32_t m_pendingResizeW = 0;
221 uint32_t m_pendingResizeH = 0;
222
223 static Application* Instance;
224};
225
226} // namespace Sleak
227
228#endif // APPLICATION_HPP
Application(const char *ProjectName)
GameBase * GetGame()
Benchmark * GetBenchmark()
Application & operator=(const Application &)=delete
void WaitGPUIdle()
Blocks until the GPU finishes all in-flight work. Call before tearing down scene resources.
static Application * GetInstance()
The one Application for this process, or null before construction.
RenderEngine::Renderer * GetRenderer()
Active renderer backend, or null before Run() initializes it.
void SetMouseRelativeMode(bool enabled)
Application & operator=(Application &&)=delete
int Run(GameBase *game)
Drives the game loop until the window closes; returns the process exit code.
void SetCursorVisible(bool visible)
Abstract render backend: swapchain, frame lifecycle, and post-effect toggles.
Definition Renderer.hpp:37
Backend-facing rendering layer shared by the four graphics backends.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10
const char * operator[](int index) const
const std::string operator[](std::string key) const
std::map< std::string, std::string > ArgMap
Arguments(int argc, char **argv)