SleakEngine
0.1.0
C++23 multi-backend game engine
Toggle main menu visibility
Loading...
Searching...
No Matches
Window.cpp
Go to the documentation of this file.
1
#include "
../../include/private/Core/Window.hpp
"
2
3
#include <
Core/CommandLine.hpp
>
4
#include <
Core/Logger.hpp
>
5
#include <
Events/ApplicationEvent.hpp
>
6
#include <
Events/KeyboardEvent.hpp
>
7
#include <
Events/MouseEvent.hpp
>
8
#include <cstdlib>
9
#include <cstring>
10
11
#include "
../../include/private/Graphics/Common/RendererFactory.hpp
"
12
13
namespace
Sleak
{
14
15
namespace
{
16
17
/// True for keyboard and mouse events that carry user input.
18
bool
IsUserInputEvent(Uint32 type) {
19
switch
(type) {
20
case
SDL_EVENT_KEY_DOWN:
21
case
SDL_EVENT_KEY_UP:
22
case
SDL_EVENT_TEXT_INPUT:
23
case
SDL_EVENT_MOUSE_MOTION:
24
case
SDL_EVENT_MOUSE_BUTTON_DOWN:
25
case
SDL_EVENT_MOUSE_BUTTON_UP:
26
case
SDL_EVENT_MOUSE_WHEEL:
27
return
true
;
28
default
:
29
return
false
;
30
}
31
}
32
33
/// SDL event filter that drops user input while a capture runs.
34
bool
SDLCALL DropUserInput(
void
* userdata, SDL_Event* event) {
35
(void)userdata;
36
return
!IsUserInputEvent(event->type);
37
}
38
39
}
// namespace
40
41
int
Window::Width = 0;
42
int
Window::Height = 0;
43
44
Window::Window
() :
Window
(
DEF_WIDTH
,
DEF_HEIGHT
,
""
){}
45
46
Window::Window
(
int
width
,
int
height
, std::string name)
47
: WindowName(name),event()
48
{
49
Width =
width
;
50
Height =
height
;
51
}
52
53
Window::~Window
() {
54
if
(SDLWindow) {
55
SDL_DestroyWindow(SDLWindow);
56
}
57
SDL_Quit();
58
59
SLEAK_LOG
(
"The window has been destroyed"
);
60
}
61
62
bool
Window::InitializeWindow
() {
63
64
if
(bIsInitialized)
65
return
false
;
66
67
m_inputLocked = !
CommandLine::GetValue
(
"-capture"
).empty();
68
69
auto
type =
RenderEngine::RendererFactory::GetRendererType
();
70
71
int
GraphicsAPI = 0;
72
73
if
(type ==
RenderEngine::RendererType::OpenGL
)
74
GraphicsAPI = SDL_WINDOW_OPENGL;
75
else
if
(type ==
RenderEngine::RendererType::Vulkan
)
76
GraphicsAPI = SDL_WINDOW_VULKAN;
77
78
if
(!SDL_Init(SDL_INIT_VIDEO)) {
79
SLEAK_FATAL
(
"Failed to initialize SDL: {0}"
, SDL_GetError());
80
return
false
;
81
}
82
if
(m_inputLocked) SDL_SetEventFilter(DropUserInput,
nullptr
);
83
84
SDLWindow = SDL_CreateWindow(WindowName.c_str(), Width, Height,
85
GraphicsAPI | SDL_WINDOW_RESIZABLE);
86
87
if
(!SDLWindow) {
88
SLEAK_FATAL
(
"Failed to create window! {0} "
, SDL_GetError());
89
return
false
;
90
}
91
92
if
(SDL_Surface* icon = SDL_LoadBMP(
"assets/branding/icon.bmp"
)) {
93
SDL_SetWindowIcon(SDLWindow, icon);
94
SDL_DestroySurface(icon);
95
}
96
97
if
(!SDL_ShowWindow(SDLWindow)) {
98
SLEAK_FATAL
(
"Failed to show window! {0} "
, SDL_GetError());
99
return
false
;
100
101
}
102
103
DispatchEvent<Sleak::Events::WindowOpenEvent>
();
104
105
SLEAK_INFO
(
"Window has been initialized and created without any errors!"
);
106
107
SLEAK_INFO
(
"Current used operating system: {0}"
, OS);
108
#ifdef WINDOW_SYSTEM
109
SLEAK_INFO
(
"Current used window system: {0}"
, WINDOW_SYSTEM);
110
#else
111
SLEAK_FATAL
(
112
"No window system has been detected! are you sure you use a computer?"
);
113
#endif
114
115
return
true
;
116
}
117
118
void
Window::Update
() {
119
while
(SDL_PollEvent(&event)) {
120
121
if
(m_imguiReady)
122
ImGui_ImplSDL3_ProcessEvent(&event);
123
124
switch
(event.type)
125
{
126
case
SDL_EVENT_WINDOW_RESIZED:
127
{
128
uint16_t
width
=
event
.window.data1;
129
uint16_t
height
=
event
.window.data2;
130
131
Window::Width =
width
;
132
Window::Height =
height
;
133
134
DispatchEvent<Sleak::Events::WindowResizeEvent>
(
width
,
height
);
135
136
break
;
137
}
138
case
SDL_EVENT_WINDOW_ENTER_FULLSCREEN:
139
{
140
DispatchEvent<Sleak::Events::WindowFullScreen>
(
true
);
141
break
;
142
}
143
case
SDL_EVENT_WINDOW_LEAVE_FULLSCREEN:
144
{
145
DispatchEvent<Sleak::Events::WindowFullScreen>
(
false
);
146
break
;
147
}
148
case
SDL_EVENT_QUIT:
149
{
150
DispatchEvent<Sleak::Events::WindowCloseEvent>
();
151
EventDispatcher::UnregisterAllEvents
();
152
bShouldClose =
true
;
153
return
;
154
}
155
156
case
SDL_EVENT_KEY_DOWN:
157
{
158
KeyCode
key =
static_cast<
KeyCode
>
(
event
.key.scancode);
159
bool
isRepeat =
event
.key.repeat;
160
161
DispatchEvent<Sleak::Events::Input::KeyPressedEvent>
(key,isRepeat);
162
break
;
163
}
164
165
case
SDL_EVENT_KEY_UP:
166
{
167
KeyCode
key =
static_cast<
KeyCode
>
(
event
.key.scancode);
168
169
DispatchEvent<Sleak::Events::Input::KeyReleasedEvent>
(key);
170
break
;
171
}
172
173
case
SDL_EVENT_MOUSE_BUTTON_DOWN:
174
{
175
MouseCode
button = (
MouseCode
)event.button.button;
176
int
x =
event
.button.x;
177
int
y =
event
.button.y;
178
179
DispatchEvent<Sleak::Events::Input::MouseButtonPressedEvent>
(button,x,y);
180
break
;
181
}
182
183
case
SDL_EVENT_MOUSE_BUTTON_UP:
184
{
185
MouseCode
button = (
MouseCode
)event.button.button;
186
int
x =
event
.button.x;
187
int
y =
event
.button.y;
188
189
DispatchEvent<Sleak::Events::Input::MouseButtonReleasedEvent>
(button,x,y);
190
break
;
191
}
192
193
case
SDL_EVENT_MOUSE_MOTION:
194
{
195
int
x =
event
.motion.x;
196
int
y =
event
.motion.y;
197
int
dx =
event
.motion.xrel;
198
int
dy =
event
.motion.yrel;
199
200
DispatchEvent<Sleak::Events::Input::MouseMovedEvent>
(x,y);
201
break
;
202
}
203
204
case
SDL_EVENT_MOUSE_WHEEL:
205
{
206
int
xOffset =
event
.wheel.x;
207
int
yOffset =
event
.wheel.y;
208
209
DispatchEvent<Sleak::Events::Input::MouseScrolledEvent>
(xOffset, yOffset);
210
break
;
211
}
212
}
213
}
214
215
if
(m_inputLocked) SDL_GetRelativeMouseState(
nullptr
,
nullptr
);
216
}
217
218
void
Window::SetFullScreen
(
bool
isFullscreen) {
219
if
(SDL_SetWindowFullscreen(SDLWindow, isFullscreen)) {
220
bIsFullScreen = isFullscreen;
221
SLEAK_INFO
(
222
"Successfully changed window state: {}"
,
223
isFullscreen ?
"Fullscreen is enabled"
:
"Fullscreen is disabled"
);
224
}
else
{
225
SLEAK_ERROR
(
"Failed to set full screen state! {}"
, SDL_GetError());
226
}
227
}
228
229
void
Window::SetRelativeMouseMode
(
bool
enabled) {
230
SDL_SetWindowRelativeMouseMode(SDLWindow, enabled);
231
}
232
233
void
Window::Close
() {
234
bShouldClose =
true
;
235
}
236
237
}
ApplicationEvent.hpp
width
int width
Definition
Application.cpp:44
height
int height
Definition
Application.cpp:45
CommandLine.hpp
MouseCode
Sleak::Input::MOUSE_CODE MouseCode
Definition
KeyCodes.hpp:592
KeyCode
Sleak::Input::KEY_CODE KeyCode
Definition
KeyCodes.hpp:591
KeyboardEvent.hpp
Logger.hpp
SLEAK_ERROR
#define SLEAK_ERROR(...)
Definition
Logger.hpp:22
SLEAK_FATAL
#define SLEAK_FATAL(...)
Definition
Logger.hpp:23
SLEAK_INFO
#define SLEAK_INFO(...)
Definition
Logger.hpp:20
SLEAK_LOG
#define SLEAK_LOG(...)
Definition
Logger.hpp:19
MouseEvent.hpp
RendererFactory.hpp
Window.hpp
DEF_HEIGHT
#define DEF_HEIGHT
Definition
Window.hpp:12
DEF_WIDTH
#define DEF_WIDTH
Definition
Window.hpp:11
Sleak::CommandLine::GetValue
static std::string GetValue(const std::string &flag, const std::string &defaultVal="")
Returns the raw string value stored for -flag, or defaultVal if it was not passed.
Definition
CommandLine.cpp:43
Sleak::EventDispatcher::UnregisterAllEvents
static void UnregisterAllEvents()
Drops every handler for every event type.
Definition
Event.hpp:178
Sleak::RenderEngine::RendererFactory::GetRendererType
static RendererType GetRendererType()
Definition
RendererFactory.hpp:24
Sleak::Window::SetRelativeMouseMode
void SetRelativeMouseMode(bool enabled)
Enables or disables relative (locked, delta-only) mouse mode.
Definition
Window.cpp:229
Sleak::Window::SetFullScreen
void SetFullScreen(bool bEnable)
Enables or disables fullscreen mode.
Definition
Window.cpp:218
Sleak::Window::InitializeWindow
bool InitializeWindow()
Definition
Window.cpp:62
Sleak::Window::Window
Window()
Constructs a window with the default size and no title.
Definition
Window.cpp:44
Sleak::Window::Update
void Update()
Definition
Window.cpp:118
Sleak::Window::Close
void Close()
Marks the window for close; actual teardown happens in the destructor.
Definition
Window.cpp:233
Sleak::Window::~Window
~Window()
Destroys the SDL window and shuts down SDL.
Definition
Window.cpp:53
Sleak::RenderEngine::RendererType::Vulkan
@ Vulkan
Definition
Renderer.hpp:18
Sleak::RenderEngine::RendererType::OpenGL
@ OpenGL
Definition
Renderer.hpp:19
Sleak
Root namespace for everything the engine exposes.
Definition
Camera.hpp:10
Sleak::DispatchEvent
void DispatchEvent(Args &&... args)
Constructs a T from args and dispatches it through EventDispatcher.
Definition
Event.hpp:217
src
Core
Window.cpp
Generated on
for SleakEngine by
1.18.0