SleakEngine
0.1.0
C++23 multi-backend game engine
Toggle main menu visibility
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>
8
#include <
Events/ApplicationEvent.hpp
>
9
#include <
Events/InputEvent.hpp
>
10
#include <
Core/Timer.hpp
>
11
#include <
Core/GraphicsConfig.hpp
>
12
#include <
Debug/Benchmark.hpp
>
13
14
namespace
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
19
struct
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
53
struct
ENGINE_API
ApplicationDefaults
{
54
std::string
Name
=
""
;
55
Arguments
CommandLineArgs
;
56
};
57
58
class
Window
;
59
class
DebugOverlay
;
60
namespace
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
109
class
ENGINE_API
Application
{
110
public
:
111
Application
(
const
char
* ProjectName);
112
Application
(
ApplicationDefaults
settings);
113
Application
&
operator=
(
Application
&&) =
delete
;
114
Application
&
operator=
(
const
Application
&) =
delete
;
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
ApplicationEvent.hpp
Benchmark.hpp
InputEvent.hpp
GameBase.hpp
GraphicsConfig.hpp
OSDef.hpp
Timer.hpp
Sleak::Application
Definition
Application.hpp:109
Sleak::Application::Application
Application(const char *ProjectName)
Definition
Application.cpp:46
Sleak::Application::GetGame
GameBase * GetGame()
Definition
Application.hpp:130
Sleak::Application::GetBenchmark
Benchmark * GetBenchmark()
Definition
Application.hpp:194
Sleak::Application::CloseApplication
void CloseApplication()
Definition
Application.cpp:345
Sleak::Application::operator=
Application & operator=(const Application &)=delete
Sleak::Application::WaitGPUIdle
void WaitGPUIdle()
Blocks until the GPU finishes all in-flight work. Call before tearing down scene resources.
Definition
Application.cpp:349
Sleak::Application::GetInstance
static Application * GetInstance()
The one Application for this process, or null before construction.
Definition
Application.hpp:133
Sleak::Application::GetRenderer
RenderEngine::Renderer * GetRenderer()
Active renderer backend, or null before Run() initializes it.
Definition
Application.hpp:129
Sleak::Application::SetMouseRelativeMode
void SetMouseRelativeMode(bool enabled)
Definition
Application.cpp:360
Sleak::Application::operator=
Application & operator=(Application &&)=delete
Sleak::Application::Run
int Run(GameBase *game)
Drives the game loop until the window closes; returns the process exit code.
Definition
Application.cpp:126
Sleak::Application::SetCursorVisible
void SetCursorVisible(bool visible)
Definition
Application.cpp:353
Sleak::Application::GetWindow
Window & GetWindow()
Definition
Application.cpp:341
Sleak::Benchmark
Definition
Benchmark.hpp:23
Sleak::DebugOverlay
Definition
DebugOverlay.hpp:22
Sleak::Events::Input::KeyPressedEvent
Definition
KeyboardEvent.hpp:33
Sleak::Events::Input::MouseButtonPressedEvent
Definition
MouseEvent.hpp:86
Sleak::Events::Input::MouseMovedEvent
Definition
MouseEvent.hpp:13
Sleak::Events::WindowFullScreen
Definition
ApplicationEvent.hpp:43
Sleak::Events::WindowResizeEvent
Definition
ApplicationEvent.hpp:11
Sleak::GameBase
Definition
GameBase.hpp:51
Sleak::RenderEngine::Renderer
Abstract render backend: swapchain, frame lifecycle, and post-effect toggles.
Definition
Renderer.hpp:37
Sleak::Timer
Definition
Timer.hpp:10
Sleak::Window
Definition
Window.hpp:18
Sleak::RenderEngine
Backend-facing rendering layer shared by the four graphics backends.
Definition
Application.hpp:60
Sleak
Root namespace for everything the engine exposes.
Definition
Camera.hpp:10
Sleak::ApplicationDefaults
Definition
Application.hpp:53
Sleak::ApplicationDefaults::CommandLineArgs
Arguments CommandLineArgs
Definition
Application.hpp:55
Sleak::ApplicationDefaults::Name
std::string Name
Definition
Application.hpp:54
Sleak::Arguments
Definition
Application.hpp:19
Sleak::Arguments::operator[]
const char * operator[](int index) const
Definition
Application.hpp:30
Sleak::Arguments::operator[]
const std::string operator[](std::string key) const
Definition
Application.hpp:37
Sleak::Arguments::ArgMap
std::map< std::string, std::string > ArgMap
Definition
Application.hpp:48
Sleak::Arguments::Args
char ** Args
Definition
Application.hpp:47
Sleak::Arguments::Size
int Size
Definition
Application.hpp:46
Sleak::Arguments::Arguments
Arguments(int argc, char **argv)
Definition
Application.hpp:20
Sleak::GraphicsConfig
Definition
GraphicsConfig.hpp:18
include
public
Core
Application.hpp
Generated on
for SleakEngine by
1.18.0