SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
Window.cpp
Go to the documentation of this file.
2
4#include <cstdlib>
5#include <cstring>
6#include <Core/Logger.hpp>
10
11
12namespace Sleak {
13
14int Window::Width = 0;
15int Window::Height = 0;
16
18
19Window::Window(int width, int height, std::string name)
20 : WindowName(name),event()
21 {
22 Width = width;
23 Height = height;
24 }
25
27 if (SDLWindow) {
28 SDL_DestroyWindow(SDLWindow);
29 }
30 SDL_Quit();
31
32 SLEAK_LOG("The window has been destroyed");
33}
34
36
37 if(bIsInitialized)
38 return false;
39
41
42 int GraphicsAPI = 0;
43
45 GraphicsAPI = SDL_WINDOW_OPENGL;
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
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
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
103
104 break;
105 }
106 case SDL_EVENT_WINDOW_ENTER_FULLSCREEN:
107 {
109 break;
110 }
111 case SDL_EVENT_WINDOW_LEAVE_FULLSCREEN:
112 {
114 break;
115 }
116 case SDL_EVENT_QUIT:
117 {
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
130 break;
131 }
132
133 case SDL_EVENT_KEY_UP:
134 {
135 KeyCode key = static_cast<KeyCode>(event.key.scancode);
136
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
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
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
169 break;
170 }
171
172 case SDL_EVENT_MOUSE_WHEEL:
173 {
174 int xOffset = event.wheel.x;
175 int yOffset = event.wheel.y;
176
178 break;
179 }
180 }
181 }
182}
183
184void 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
197 SDL_SetWindowRelativeMouseMode(SDLWindow, enabled);
198}
199
201 bShouldClose = true;
202}
203
204}
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 void UnregisterAllEvents()
Drops every handler for every event type.
Definition Event.hpp:178
void SetRelativeMouseMode(bool enabled)
Definition Window.cpp:196
void SetFullScreen(bool bEnable)
Definition Window.cpp:184
bool InitializeWindow()
Creates the SDL window and picks the graphics API flag for the active renderer.
Definition Window.cpp:35
void Update()
Pumps the SDL event queue, dispatching engine events (resize, input, close, ...).
Definition Window.cpp:86
void Close()
Marks the window for close; actual teardown happens in the destructor.
Definition Window.cpp:200
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