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