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