SleakEngine 0.1.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
Window.cpp
Go to the documentation of this file.
2
4#include <Core/Logger.hpp>
8#include <cstdlib>
9#include <cstring>
10
12
13namespace Sleak {
14
15namespace {
16
17/// True for keyboard and mouse events that carry user input.
18bool 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.
34bool SDLCALL DropUserInput(void* userdata, SDL_Event* event) {
35 (void)userdata;
36 return !IsUserInputEvent(event->type);
37}
38
39} // namespace
40
41int Window::Width = 0;
42int Window::Height = 0;
43
45
46Window::Window(int width, int height, std::string name)
47 : WindowName(name),event()
48 {
49 Width = width;
50 Height = height;
51 }
52
54 if (SDLWindow) {
55 SDL_DestroyWindow(SDLWindow);
56 }
57 SDL_Quit();
58
59 SLEAK_LOG("The window has been destroyed");
60}
61
63
64 if(bIsInitialized)
65 return false;
66
67 m_inputLocked = !CommandLine::GetValue("-capture").empty();
68
70
71 int GraphicsAPI = 0;
72
74 GraphicsAPI = SDL_WINDOW_OPENGL;
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
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
112 "No window system has been detected! are you sure you use a computer?");
113#endif
114
115 return true;
116}
117
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
135
136 break;
137 }
138 case SDL_EVENT_WINDOW_ENTER_FULLSCREEN:
139 {
141 break;
142 }
143 case SDL_EVENT_WINDOW_LEAVE_FULLSCREEN:
144 {
146 break;
147 }
148 case SDL_EVENT_QUIT:
149 {
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
162 break;
163 }
164
165 case SDL_EVENT_KEY_UP:
166 {
167 KeyCode key = static_cast<KeyCode>(event.key.scancode);
168
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
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
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
201 break;
202 }
203
204 case SDL_EVENT_MOUSE_WHEEL:
205 {
206 int xOffset = event.wheel.x;
207 int yOffset = event.wheel.y;
208
210 break;
211 }
212 }
213 }
214
215 if (m_inputLocked) SDL_GetRelativeMouseState(nullptr, nullptr);
216}
217
218void Window::SetFullScreen(bool isFullscreen) {
219 if (SDL_SetWindowFullscreen(SDLWindow, isFullscreen)) {
220 bIsFullScreen = isFullscreen;
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
230 SDL_SetWindowRelativeMouseMode(SDLWindow, enabled);
231}
232
234 bShouldClose = true;
235}
236
237}
int width
int height
Sleak::Input::MOUSE_CODE MouseCode
Definition KeyCodes.hpp:592
Sleak::Input::KEY_CODE KeyCode
Definition KeyCodes.hpp:591
#define SLEAK_ERROR(...)
Definition Logger.hpp:22
#define SLEAK_FATAL(...)
Definition Logger.hpp:23
#define SLEAK_INFO(...)
Definition Logger.hpp:20
#define SLEAK_LOG(...)
Definition Logger.hpp:19
#define DEF_HEIGHT
Definition Window.hpp:12
#define DEF_WIDTH
Definition Window.hpp:11
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.
static void UnregisterAllEvents()
Drops every handler for every event type.
Definition Event.hpp:178
void SetRelativeMouseMode(bool enabled)
Enables or disables relative (locked, delta-only) mouse mode.
Definition Window.cpp:229
void SetFullScreen(bool bEnable)
Enables or disables fullscreen mode.
Definition Window.cpp:218
bool InitializeWindow()
Definition Window.cpp:62
Window()
Constructs a window with the default size and no title.
Definition Window.cpp:44
void Update()
Definition Window.cpp:118
void Close()
Marks the window for close; actual teardown happens in the destructor.
Definition Window.cpp:233
~Window()
Destroys the SDL window and shuts down SDL.
Definition Window.cpp:53
Root namespace for everything the engine exposes.
Definition Camera.hpp:10
void DispatchEvent(Args &&... args)
Constructs a T from args and dispatches it through EventDispatcher.
Definition Event.hpp:217