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 <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
81 "No window system has been detected! are you sure you use a computer?");
82#endif
83
84 return true;
85}
86
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
104
105 break;
106 }
107 case SDL_EVENT_WINDOW_ENTER_FULLSCREEN:
108 {
110 break;
111 }
112 case SDL_EVENT_WINDOW_LEAVE_FULLSCREEN:
113 {
115 break;
116 }
117 case SDL_EVENT_QUIT:
118 {
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
131 break;
132 }
133
134 case SDL_EVENT_KEY_UP:
135 {
136 KeyCode key = static_cast<KeyCode>(event.key.scancode);
137
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
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
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
170 break;
171 }
172
173 case SDL_EVENT_MOUSE_WHEEL:
174 {
175 int xOffset = event.wheel.x;
176 int yOffset = event.wheel.y;
177
179 break;
180 }
181 }
182 }
183}
184
185void Window::SetFullScreen(bool isFullscreen) {
186 if (SDL_SetWindowFullscreen(SDLWindow, isFullscreen)) {
187 bIsFullScreen = isFullscreen;
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
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)
Enables or disables relative (locked, delta-only) mouse mode.
Definition Window.cpp:196
void SetFullScreen(bool bEnable)
Enables or disables fullscreen mode.
Definition Window.cpp:185
bool InitializeWindow()
Definition Window.cpp:35
Window()
Constructs a window with the default size and no title.
Definition Window.cpp:17
void Update()
Definition Window.cpp:87
void Close()
Marks the window for close; actual teardown happens in the destructor.
Definition Window.cpp:200
~Window()
Destroys the SDL window and shuts down SDL.
Definition Window.cpp:26
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