Thanks to visit codestin.com
Credit goes to canreader.github.io
SleakEngine
1.0.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
(
"No window system has been detected! are you sure you use a computer?"
, WINDOW_SYSTEM);
81
#endif
82
83
return
true
;
84
}
85
86
void
Window::Update
() {
87
while
(SDL_PollEvent(&event)) {
88
89
if
(m_imguiReady)
90
ImGui_ImplSDL3_ProcessEvent(&event);
91
92
switch
(event.type)
93
{
94
case
SDL_EVENT_WINDOW_RESIZED:
95
{
96
uint16_t
width
=
event
.window.data1;
97
uint16_t
height
=
event
.window.data2;
98
99
Window::Width =
width
;
100
Window::Height =
height
;
101
102
DispatchEvent<Sleak::Events::WindowResizeEvent>
(
width
,
height
);
103
104
break
;
105
}
106
case
SDL_EVENT_WINDOW_ENTER_FULLSCREEN:
107
{
108
DispatchEvent<Sleak::Events::WindowFullScreen>
(
true
);
109
break
;
110
}
111
case
SDL_EVENT_WINDOW_LEAVE_FULLSCREEN:
112
{
113
DispatchEvent<Sleak::Events::WindowFullScreen>
(
false
);
114
break
;
115
}
116
case
SDL_EVENT_QUIT:
117
{
118
DispatchEvent<Sleak::Events::WindowCloseEvent>
();
119
EventDispatcher::UnregisterAllEvents
();
120
bShouldClose =
true
;
121
return
;
122
}
123
124
case
SDL_EVENT_KEY_DOWN:
125
{
126
KeyCode
key =
static_cast<
KeyCode
>
(
event
.key.scancode);
127
bool
isRepeat =
event
.key.repeat;
128
129
DispatchEvent<Sleak::Events::Input::KeyPressedEvent>
(key,isRepeat);
130
break
;
131
}
132
133
case
SDL_EVENT_KEY_UP:
134
{
135
KeyCode
key =
static_cast<
KeyCode
>
(
event
.key.scancode);
136
137
DispatchEvent<Sleak::Events::Input::KeyReleasedEvent>
(key);
138
break
;
139
}
140
141
case
SDL_EVENT_MOUSE_BUTTON_DOWN:
142
{
143
MouseCode
button = (
MouseCode
)event.button.button;
144
int
x =
event
.button.x;
145
int
y =
event
.button.y;
146
147
DispatchEvent<Sleak::Events::Input::MouseButtonPressedEvent>
(button,x,y);
148
break
;
149
}
150
151
case
SDL_EVENT_MOUSE_BUTTON_UP:
152
{
153
MouseCode
button = (
MouseCode
)event.button.button;
154
int
x =
event
.button.x;
155
int
y =
event
.button.y;
156
157
DispatchEvent<Sleak::Events::Input::MouseButtonReleasedEvent>
(button,x,y);
158
break
;
159
}
160
161
case
SDL_EVENT_MOUSE_MOTION:
162
{
163
int
x =
event
.motion.x;
164
int
y =
event
.motion.y;
165
int
dx =
event
.motion.xrel;
166
int
dy =
event
.motion.yrel;
167
168
DispatchEvent<Sleak::Events::Input::MouseMovedEvent>
(x,y);
169
break
;
170
}
171
172
case
SDL_EVENT_MOUSE_WHEEL:
173
{
174
int
xOffset =
event
.wheel.x;
175
int
yOffset =
event
.wheel.y;
176
177
DispatchEvent<Sleak::Events::Input::MouseScrolledEvent>
(xOffset, yOffset);
178
break
;
179
}
180
}
181
}
182
}
183
184
void
Window::SetFullScreen
(
bool
isFullscreen) {
185
186
if
(!SDL_SetWindowFullscreen(SDLWindow,isFullscreen))
187
{
188
SLEAK_INFO
(
"Successfully changed window state: %s"
, isFullscreen ?
"Fullscreen is enabled"
:
"Fullscreen is disabled"
);
189
bIsFullScreen = isFullscreen;
190
}
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)
Definition
Window.cpp:196
Sleak::Window::SetFullScreen
void SetFullScreen(bool bEnable)
Definition
Window.cpp:184
Sleak::Window::InitializeWindow
bool InitializeWindow()
Creates the SDL window and picks the graphics API flag for the active renderer.
Definition
Window.cpp:35
Sleak::Window::Window
Window()
Definition
Window.cpp:17
Sleak::Window::Update
void Update()
Pumps the SDL event queue, dispatching engine events (resize, input, close, ...).
Definition
Window.cpp:86
Sleak::Window::Close
void Close()
Marks the window for close; actual teardown happens in the destructor.
Definition
Window.cpp:200
Sleak::Window::~Window
~Window()
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