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 "
../../include/private/Graphics/Common/RendererFactory.hpp
"
4
#include <cstdlib>
5
#include <cstring>
6
#include <
Core/Logger.hpp
>
7
#include <
Events/ApplicationEvent.hpp
>
8
#include <
Events/KeyboardEvent.hpp
>
9
#include <
Events/MouseEvent.hpp
>
10
11
12
namespace
Sleak
{
13
14
int
Window::Width = 0;
15
int
Window::Height = 0;
16
17
Window::Window
() :
Window
(
DEF_WIDTH
,
DEF_HEIGHT
,
""
){}
18
19
Window::Window
(
int
width
,
int
height
, std::string name)
20
: WindowName(name),event()
21
{
22
Width =
width
;
23
Height =
height
;
24
}
25
26
Window::~Window
() {
27
if
(SDLWindow) {
28
SDL_DestroyWindow(SDLWindow);
29
}
30
SDL_Quit();
31
32
SLEAK_LOG
(
"The window has been destroyed"
);
33
}
34
35
bool
Window::InitializeWindow
() {
36
37
if
(bIsInitialized)
38
return
false
;
39
40
auto
type =
RenderEngine::RendererFactory::GetRendererType
();
41
42
int
GraphicsAPI = 0;
43
44
if
(type ==
RenderEngine::RendererType::OpenGL
)
45
GraphicsAPI = SDL_WINDOW_OPENGL;
46
else
if
(type ==
RenderEngine::RendererType::Vulkan
)
47
GraphicsAPI = SDL_WINDOW_VULKAN;
48
49
if
(!SDL_Init(SDL_INIT_VIDEO)) {
50
SLEAK_FATAL
(
"Failed to initialize SDL: {0}"
, SDL_GetError());
51
return
false
;
52
}
53
54
SDLWindow = SDL_CreateWindow(WindowName.c_str(), Width, Height, GraphicsAPI | SDL_WINDOW_RESIZABLE);
55
56
if
(!SDLWindow) {
57
SLEAK_FATAL
(
"Failed to create window! {0} "
, SDL_GetError());
58
return
false
;
59
}
60
61
if
(SDL_Surface* icon = SDL_LoadBMP(
"assets/branding/icon.bmp"
)) {
62
SDL_SetWindowIcon(SDLWindow, icon);
63
SDL_DestroySurface(icon);
64
}
65
66
if
(!SDL_ShowWindow(SDLWindow)) {
67
SLEAK_FATAL
(
"Failed to show window! {0} "
, SDL_GetError());
68
return
false
;
69
70
}
71
72
DispatchEvent<Sleak::Events::WindowOpenEvent>
();
73
74
SLEAK_INFO
(
"Window has been initialized and created without any errors!"
);
75
76
SLEAK_INFO
(
"Current used operating system: {0}"
, OS);
77
#ifdef WINDOW_SYSTEM
78
SLEAK_INFO
(
"Current used window system: {0}"
, WINDOW_SYSTEM);
79
#else
80
SLEAK_FATAL
(
81
"No window system has been detected! are you sure you use a computer?"
);
82
#endif
83
84
return
true
;
85
}
86
87
void
Window::Update
() {
88
while
(SDL_PollEvent(&event)) {
89
90
if
(m_imguiReady)
91
ImGui_ImplSDL3_ProcessEvent(&event);
92
93
switch
(event.type)
94
{
95
case
SDL_EVENT_WINDOW_RESIZED:
96
{
97
uint16_t
width
=
event
.window.data1;
98
uint16_t
height
=
event
.window.data2;
99
100
Window::Width =
width
;
101
Window::Height =
height
;
102
103
DispatchEvent<Sleak::Events::WindowResizeEvent>
(
width
,
height
);
104
105
break
;
106
}
107
case
SDL_EVENT_WINDOW_ENTER_FULLSCREEN:
108
{
109
DispatchEvent<Sleak::Events::WindowFullScreen>
(
true
);
110
break
;
111
}
112
case
SDL_EVENT_WINDOW_LEAVE_FULLSCREEN:
113
{
114
DispatchEvent<Sleak::Events::WindowFullScreen>
(
false
);
115
break
;
116
}
117
case
SDL_EVENT_QUIT:
118
{
119
DispatchEvent<Sleak::Events::WindowCloseEvent>
();
120
EventDispatcher::UnregisterAllEvents
();
121
bShouldClose =
true
;
122
return
;
123
}
124
125
case
SDL_EVENT_KEY_DOWN:
126
{
127
KeyCode
key =
static_cast<
KeyCode
>
(
event
.key.scancode);
128
bool
isRepeat =
event
.key.repeat;
129
130
DispatchEvent<Sleak::Events::Input::KeyPressedEvent>
(key,isRepeat);
131
break
;
132
}
133
134
case
SDL_EVENT_KEY_UP:
135
{
136
KeyCode
key =
static_cast<
KeyCode
>
(
event
.key.scancode);
137
138
DispatchEvent<Sleak::Events::Input::KeyReleasedEvent>
(key);
139
break
;
140
}
141
142
case
SDL_EVENT_MOUSE_BUTTON_DOWN:
143
{
144
MouseCode
button = (
MouseCode
)event.button.button;
145
int
x =
event
.button.x;
146
int
y =
event
.button.y;
147
148
DispatchEvent<Sleak::Events::Input::MouseButtonPressedEvent>
(button,x,y);
149
break
;
150
}
151
152
case
SDL_EVENT_MOUSE_BUTTON_UP:
153
{
154
MouseCode
button = (
MouseCode
)event.button.button;
155
int
x =
event
.button.x;
156
int
y =
event
.button.y;
157
158
DispatchEvent<Sleak::Events::Input::MouseButtonReleasedEvent>
(button,x,y);
159
break
;
160
}
161
162
case
SDL_EVENT_MOUSE_MOTION:
163
{
164
int
x =
event
.motion.x;
165
int
y =
event
.motion.y;
166
int
dx =
event
.motion.xrel;
167
int
dy =
event
.motion.yrel;
168
169
DispatchEvent<Sleak::Events::Input::MouseMovedEvent>
(x,y);
170
break
;
171
}
172
173
case
SDL_EVENT_MOUSE_WHEEL:
174
{
175
int
xOffset =
event
.wheel.x;
176
int
yOffset =
event
.wheel.y;
177
178
DispatchEvent<Sleak::Events::Input::MouseScrolledEvent>
(xOffset, yOffset);
179
break
;
180
}
181
}
182
}
183
}
184
185
void
Window::SetFullScreen
(
bool
isFullscreen) {
186
if
(SDL_SetWindowFullscreen(SDLWindow, isFullscreen)) {
187
bIsFullScreen = isFullscreen;
188
SLEAK_INFO
(
189
"Successfully changed window state: {}"
,
190
isFullscreen ?
"Fullscreen is enabled"
:
"Fullscreen is disabled"
);
191
}
else
{
192
SLEAK_ERROR
(
"Failed to set full screen state! {}"
, SDL_GetError());
193
}
194
}
195
196
void
Window::SetRelativeMouseMode
(
bool
enabled) {
197
SDL_SetWindowRelativeMouseMode(SDLWindow, enabled);
198
}
199
200
void
Window::Close
() {
201
bShouldClose =
true
;
202
}
203
204
}
ApplicationEvent.hpp
width
int width
Definition
Application.cpp:40
height
int height
Definition
Application.cpp:41
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::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:196
Sleak::Window::SetFullScreen
void SetFullScreen(bool bEnable)
Enables or disables fullscreen mode.
Definition
Window.cpp:185
Sleak::Window::InitializeWindow
bool InitializeWindow()
Definition
Window.cpp:35
Sleak::Window::Window
Window()
Constructs a window with the default size and no title.
Definition
Window.cpp:17
Sleak::Window::Update
void Update()
Definition
Window.cpp:87
Sleak::Window::Close
void Close()
Marks the window for close; actual teardown happens in the destructor.
Definition
Window.cpp:200
Sleak::Window::~Window
~Window()
Destroys the SDL window and shuts down SDL.
Definition
Window.cpp:26
Sleak::RenderEngine::RendererType::Vulkan
@ Vulkan
Definition
Renderer.hpp:14
Sleak::RenderEngine::RendererType::OpenGL
@ OpenGL
Definition
Renderer.hpp:15
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