galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Window.cpp
Go to the documentation of this file.
1
7
8#define SDL_STBIMG_SDL3
9
10#include <display.h>
11#include <draw.h>
12#include <system.h>
13#include <glad/glad.h>
14#include <SDL_stbimage.h>
15
21
22#include "Window.hpp"
23
24#ifdef GALAXY_WIN_PLATFORM
27#endif
28
29namespace galaxy
30{
32 : m_window {nullptr}
33 , m_context {nullptr}
34 , m_open {true}
35 {
36 if (SDL_ScreenSaverEnabled())
37 {
38 SDL_DisableScreenSaver();
39 }
40
41 int driver = sl::GFX_AUTODETECT_WINDOWED;
42 SDL_WindowFlags flags = SDL_WINDOW_OPENGL | SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS | SDL_WINDOW_HIDDEN | SDL_WINDOW_HIGH_PIXEL_DENSITY;
44 {
45 flags |= SDL_WINDOW_FULLSCREEN;
46 driver = sl::GFX_AUTODETECT_FULLSCREEN;
47 }
48 else
49 {
51 {
52 flags |= SDL_WINDOW_MAXIMIZED;
53 }
54 }
55
57 {
58 flags |= SDL_WINDOW_MOUSE_GRABBED;
59 }
60
62 {
63 flags |= SDL_WINDOW_RESIZABLE;
64 }
65
66 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
67 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
68 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
69 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
70 SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
71 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
72 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
73 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
74 SDL_GL_SetAttribute(SDL_GL_STEREO, 0);
75 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
76 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
77 SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
78 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
79 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 6);
80 SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG | SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
81 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
82 SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 0);
83 SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
84 SDL_GL_SetAttribute(SDL_GL_CONTEXT_RELEASE_BEHAVIOR, SDL_GL_CONTEXT_RELEASE_BEHAVIOR_FLUSH);
85 SDL_GL_SetAttribute(SDL_GL_CONTEXT_RESET_NOTIFICATION, SDL_GL_CONTEXT_RESET_NO_NOTIFICATION);
86
87 sl::set_graphics_backend(sl::GraphicsBackend::opengl);
88 const auto result = sl::set_gfx_mode(
89 Settings::title().c_str(),
90 flags,
91 driver,
96 );
97
98 if (result)
99 {
100 m_window = sl::get_window();
101 m_context = sl::get_gl_context();
102
103 SDL_GL_SetSwapInterval(Settings::vsync());
104 SDL_CaptureMouse(false);
105 SDL_SetWindowAlwaysOnTop(m_window, false);
106 SDL_SetWindowBordered(m_window, Settings::window_border());
107 SDL_SetWindowFocusable(m_window, true);
108 SDL_SetWindowFullscreenMode(m_window, nullptr); // Borderless Fullscreen mode.
109 SDL_SetWindowKeyboardGrab(m_window, false);
110 SDL_SetWindowMinimumSize(m_window, 640, 360);
111 SDL_SetWindowPosition(m_window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
112
113 // Debug configuration.
114 glEnable(GL_DEBUG_OUTPUT);
115 glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
116
117 glDebugMessageCallback(
118 [](GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam) {
119 GALAXY_UNUSED(source);
120 GALAXY_UNUSED(type);
121 GALAXY_UNUSED(id);
122 GALAXY_UNUSED(length);
123 GALAXY_UNUSED(userParam);
124
125 switch (severity)
126 {
127 case GL_DEBUG_SEVERITY_HIGH:
128 GALAXY_LOG(GALAXY_ERROR, "[OpenGL] - {0}", message);
129 break;
130 case GL_DEBUG_SEVERITY_MEDIUM:
131 GALAXY_LOG(GALAXY_WARN, "[OpenGL] - {0}", message);
132 break;
133 case GL_DEBUG_SEVERITY_LOW:
134 GALAXY_LOG(GALAXY_INFO, "[OpenGL] - {0}", message);
135 break;
136 default:
137 GALAXY_LOG(GALAXY_INFO, "[OpenGL] - {0}", message);
138 break;
139 };
140 },
141 nullptr
142 );
143
144 glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_NOTIFICATION, 0, nullptr, GL_FALSE);
145 }
146 else
147 {
148 GALAXY_LOG(GALAXY_FATAL, "Failed to initialize SDL window.");
149 }
150 }
151
153 {
154 sl::shutdown(); // calls display_shutdown which calls wait_for_graphics.
155
156 m_context = nullptr;
157 m_window = nullptr;
158 m_open = false;
159 }
160
161 void Window::close() noexcept
162 {
163 m_open = false;
164 }
165
166 void Window::clear() noexcept
167 {
168 sl::clear_to_colour(sl::screen, {0, 0, 0, 0});
169 }
170
171 void Window::swap() const noexcept
172 {
173 sl::show_video_bitmap();
174 sl::end_frame();
175 }
176
177 void Window::show() const noexcept
178 {
179 SDL_ShowWindow(m_window);
180 }
181
182 void Window::hide() const noexcept
183 {
184 SDL_HideWindow(m_window);
185 }
186
187 void Window::minimize() const noexcept
188 {
189 SDL_MinimizeWindow(m_window);
190 SDL_SyncWindow(m_window);
191 }
192
193 void Window::maximize() const noexcept
194 {
195 SDL_MaximizeWindow(m_window);
196 SDL_SyncWindow(m_window);
197 }
198
199 void Window::restore() const noexcept
200 {
201 SDL_RestoreWindow(m_window);
202 SDL_SyncWindow(m_window);
203 }
204
205 void Window::raise() const noexcept
206 {
207 SDL_RaiseWindow(m_window);
208 }
209
210 void Window::request_attention() const noexcept
211 {
212 SDL_FlashWindow(m_window, SDL_FlashOperation::SDL_FLASH_UNTIL_FOCUSED);
213 }
214
215 void Window::resize(const int width, const int height) const noexcept
216 {
217 SDL_SetWindowSize(m_window, width, height);
218 }
219
220 void Window::set_fullscreen(const bool fullscreen) const noexcept
221 {
222 sl::set_fullscreen(fullscreen);
223 }
224
225 void Window::set_taskbar_progress(const float progress) noexcept
226 {
227 SDL_SetWindowProgressValue(m_window, std::clamp(progress, 0.0f, 1.0f));
228 }
229
230 void Window::set_title(const std::string& title)
231 {
232 sl::set_window_title(title.c_str());
233 }
234
235 void Window::append_title(const std::string& append)
236 {
237 const auto title = Settings::title() + append;
238 SDL_SetWindowTitle(m_window, title.c_str());
239 }
240
241 void Window::set_icon(const std::string& icon) noexcept
242 {
243 if (!icon.empty())
244 {
245 auto& fs = entt::locator<VirtualFileSystem>::value();
246
247 auto data = fs.read_binary(icon);
248 if (!data.empty())
249 {
250 auto surface = STBIMG_LoadFromMemory(data.data(), static_cast<int>(data.size()));
251 if (surface)
252 {
253 SDL_SetWindowIcon(m_window, surface);
254 }
255 else
256 {
257 GALAXY_LOG(GALAXY_ERROR, SDL_GetError());
258 }
259
260 SDL_DestroySurface(surface);
261 }
262 }
263 }
264
265 void Window::handle_window_events(const SDL_Event& events)
266 {
267 switch (events.type)
268 {
269 case SDL_EVENT_QUIT:
270 case SDL_EVENT_TERMINATING:
271 close();
272 break;
273
274 case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
275 case SDL_EVENT_WINDOW_DESTROYED:
276 if (events.window.windowID == SDL_GetWindowID(m_window))
277 {
278 close();
279 }
280 break;
281
282 case SDL_EVENT_WINDOW_RESIZED:
283 case SDL_EVENT_WINDOW_MAXIMIZED:
284 case SDL_EVENT_WINDOW_RESTORED:
285 case SDL_EVENT_WINDOW_ENTER_FULLSCREEN:
286 case SDL_EVENT_WINDOW_LEAVE_FULLSCREEN:
287 case SDL_EVENT_DISPLAY_USABLE_BOUNDS_CHANGED:
288 case SDL_EVENT_WINDOW_DISPLAY_CHANGED:
289 case SDL_EVENT_DISPLAY_ORIENTATION:
290 case SDL_EVENT_DISPLAY_DESKTOP_MODE_CHANGED:
291 case SDL_EVENT_DISPLAY_CURRENT_MODE_CHANGED:
292 case SDL_EVENT_DISPLAY_CONTENT_SCALE_CHANGED:
293 case SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED:
294 case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
295 // TODO
296 // SDL_GetDisplayContentScale
297 // SDL_GetWindowDisplayScale
298 // https://wiki.libsdl.org/SDL3/README-highdpi
299 sl::resize();
301 break;
302 }
303 }
304
305 int Window::screen_width() const noexcept
306 {
307 return sl::screen_width();
308 }
309
310 int Window::screen_height() const noexcept
311 {
312 return sl::screen_height();
313 }
314
315 int Window::virtual_screen_width() const noexcept
316 {
317 return sl::virtual_screen_width();
318 }
319
320 int Window::virtual_screen_height() const noexcept
321 {
322 return sl::virtual_screen_height();
323 }
324
325 bool Window::is_open() const noexcept
326 {
327 return m_open;
328 }
329
330 float Window::get_display_scale() const noexcept
331 {
332 return SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
333 }
334
335 SDL_Window* Window::handle() const noexcept
336 {
337 return m_window;
338 }
339
340 SDL_GLContext Window::context() const noexcept
341 {
342 return m_context;
343 }
344} // namespace galaxy
345
346#ifdef GALAXY_WIN_PLATFORM
348#endif
#define GALAXY_INFO
Log.hpp galaxy.
Definition Log.hpp:23
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:29
#define GALAXY_WARN
Definition Log.hpp:24
#define GALAXY_FATAL
Definition Log.hpp:26
#define GALAXY_ERROR
Definition Log.hpp:25
#define GALAXY_DISABLE_WARNING_POP
Definition Pragma.hpp:62
#define GALAXY_DISABLE_WARNING(x)
Definition Pragma.hpp:63
#define GALAXY_UNUSED(var)
Pragma.hpp galaxy.
Definition Pragma.hpp:16
#define GALAXY_DISABLE_WARNING_PUSH
Macro for windows platform detection.
Definition Pragma.hpp:61
SDL_Window * m_window
SDL window handle.
Definition Window.hpp:234
void set_fullscreen(const bool fullscreen) const noexcept
Toggle fullscreen.
Definition Window.cpp:220
~Window()
Destructor.
Definition Window.cpp:152
int screen_width() const noexcept
Return the current drawable width in pixels.
Definition Window.cpp:305
SDL_Window * handle() const noexcept
Get SDL window pointer.
Definition Window.cpp:335
SDL_GLContext context() const noexcept
Get SDL GL context.
Definition Window.cpp:340
void swap() const noexcept
Swap backbuffer with window framebuffer.
Definition Window.cpp:171
bool is_open() const noexcept
Is the window open or closed.
Definition Window.cpp:325
void raise() const noexcept
Raise the window to be on top of other windows.
Definition Window.cpp:205
void resize(const int width, const int height) const noexcept
Resizes window.
Definition Window.cpp:215
Window()
Window creation constructor.
Definition Window.cpp:31
void maximize() const noexcept
Maximize window.
Definition Window.cpp:193
int virtual_screen_height() const noexcept
Return the configured logical screen height.
Definition Window.cpp:320
int virtual_screen_width() const noexcept
Return the configured logical screen width.
Definition Window.cpp:315
void append_title(const std::string &append)
Append to window title from the settings.
Definition Window.cpp:235
void handle_window_events(const SDL_Event &events)
Process window specific events.
Definition Window.cpp:265
void close() noexcept
Close window.
Definition Window.cpp:161
void set_taskbar_progress(const float progress) noexcept
Sets the taskbar icon progress overlay.
Definition Window.cpp:225
bool m_open
Window state flag.
Definition Window.hpp:244
void hide() const noexcept
Hide window.
Definition Window.cpp:182
SDL_GLContext m_context
SDL OpenGL context.
Definition Window.hpp:239
void show() const noexcept
Show window.
Definition Window.cpp:177
float get_display_scale() const noexcept
Get display content scale.
Definition Window.cpp:330
void minimize() const noexcept
Minimize window.
Definition Window.cpp:187
int screen_height() const noexcept
Return the current drawable height in pixels.
Definition Window.cpp:310
void clear() noexcept
Clear screen framebuffer.
Definition Window.cpp:166
void set_icon(const std::string &icon) noexcept
Set window icon.
Definition Window.cpp:241
void set_title(const std::string &title)
Set window title.
Definition Window.cpp:230
void request_attention() const noexcept
Flash window on taskbar for user attention.
Definition Window.cpp:210
void restore() const noexcept
Restore window to previous *mize state.
Definition Window.cpp:199
void imgui_set_theme(const float scale) noexcept
Set imgui theme.
Tag.cpp galaxy.
Definition Tag.cpp:13
static auto fullscreen() noexcept -> bool
Is window started fullscreen.
Definition Settings.cpp:163
static auto vsync() noexcept -> bool
Vsync control.
Definition Settings.cpp:188
static auto requested_height() noexcept -> int
Window requested height.
Definition Settings.cpp:148
static auto window_border() noexcept -> bool
Controls if a window has a border around it (including titlebar).
Definition Settings.cpp:183
static auto window_resizable() noexcept -> bool
Is the window resizable.
Definition Settings.cpp:173
static auto virtual_width() noexcept -> int
Window virtual width.
Definition Settings.cpp:153
static auto maximized() noexcept -> bool
Is window started maximized?
Definition Settings.cpp:168
static auto virtual_height() noexcept -> int
Window virtual height.
Definition Settings.cpp:158
static auto title() noexcept -> const std::string &
Game title.
Definition Settings.cpp:203
static auto mouse_grabbed() noexcept -> bool
Is the cursor grabbed.
Definition Settings.cpp:178
static auto requested_width() noexcept -> int
Window requested width.
Definition Settings.cpp:143