21 #include <engine/config.hpp>
31 const std::string& title,
40 Uint32 window_flags = SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
43 window_flags |= SDL_WINDOW_MAXIMIZED;
47 window_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
52 m_internal_window = SDL_CreateWindow
55 windowed_position.
x(),
56 windowed_position.
y(),
61 if (!m_internal_window)
64 throw std::runtime_error(
"Failed to create SDL window");
70 m_internal_context = SDL_GL_CreateContext(m_internal_window);
71 if (!m_internal_context)
74 throw std::runtime_error(
"Failed to create OpenGL context");
79 int opengl_context_version_major = -1;
80 int opengl_context_version_minor = -1;
81 int opengl_context_red_size = -1;
82 int opengl_context_green_size = -1;
83 int opengl_context_blue_size = -1;
84 int opengl_context_alpha_size = -1;
85 int opengl_context_depth_size = -1;
86 int opengl_context_stencil_size = -1;
87 SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &opengl_context_version_major);
88 SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &opengl_context_version_minor);
89 SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &opengl_context_red_size);
90 SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &opengl_context_green_size);
91 SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &opengl_context_blue_size);
92 SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE, &opengl_context_alpha_size);
93 SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &opengl_context_depth_size);
94 SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, &opengl_context_stencil_size);
99 "OpenGL context version: {}.{}; format: R{}G{}B{}A{}D{}S{}",
100 opengl_context_version_major,
101 opengl_context_version_minor,
102 opengl_context_red_size,
103 opengl_context_green_size,
104 opengl_context_blue_size,
105 opengl_context_alpha_size,
106 opengl_context_depth_size,
107 opengl_context_stencil_size
111 if (opengl_context_version_major != config::opengl_version_major ||
112 opengl_context_version_minor != config::opengl_version_minor)
114 debug::log_warning(
"Requested OpenGL context version {}.{} but got version {}.{}", config::opengl_version_major, config::opengl_version_minor, opengl_context_version_major, opengl_context_version_minor);
118 if (opengl_context_red_size < config::opengl_min_red_size ||
119 opengl_context_green_size < config::opengl_min_green_size ||
120 opengl_context_blue_size < config::opengl_min_blue_size ||
121 opengl_context_alpha_size < config::opengl_min_alpha_size ||
122 opengl_context_depth_size < config::opengl_min_depth_size ||
123 opengl_context_stencil_size < config::opengl_min_stencil_size)
127 "OpenGL context format (R{}G{}B{}A{}D{}S{}) does not meet minimum requested format (R{}G{}B{}A{}D{}S{})",
128 opengl_context_red_size,
129 opengl_context_green_size,
130 opengl_context_blue_size,
131 opengl_context_alpha_size,
132 opengl_context_depth_size,
133 opengl_context_stencil_size,
134 config::opengl_min_red_size,
135 config::opengl_min_green_size,
136 config::opengl_min_blue_size,
137 config::opengl_min_alpha_size,
138 config::opengl_min_depth_size,
139 config::opengl_min_stencil_size
145 if (!gladLoadGL(
reinterpret_cast<GLADloadfunc
>(SDL_GL_GetProcAddress)))
148 throw std::runtime_error(
"Failed to load OpenGL functions");
155 "OpenGL vendor: {}; renderer: {}; version: {}; shading language version: {}",
156 reinterpret_cast<const char*
>(glGetString(GL_VENDOR)),
157 reinterpret_cast<const char*
>(glGetString(GL_RENDERER)),
158 reinterpret_cast<const char*
>(glGetString(GL_VERSION)),
159 reinterpret_cast<const char*
>(glGetString(GL_SHADING_LANGUAGE_VERSION))
163 m_graphics_pipeline = std::make_unique<gl::pipeline>();
178 SDL_GetWindowPosition(m_internal_window, &this->
m_position.
x(), &this->m_position.y());
179 SDL_GetWindowSize(m_internal_window, &this->
m_size.
x(), &this->m_size.y());
180 SDL_GetWindowMinimumSize(m_internal_window, &this->
m_minimum_size.
x(), &this->m_minimum_size.y());
181 SDL_GetWindowMaximumSize(m_internal_window, &this->
m_maximum_size.
x(), &this->m_maximum_size.y());
182 SDL_GL_GetDrawableSize(m_internal_window, &this->
m_viewport_size.
x(), &this->m_viewport_size.y());
188 m_graphics_pipeline.reset();
191 SDL_GL_DeleteContext(m_internal_context);
194 SDL_DestroyWindow(m_internal_window);
199 SDL_SetWindowTitle(m_internal_window, title.c_str());
210 SDL_SetWindowSize(m_internal_window, size.
x(), size.
y());
215 SDL_SetWindowMinimumSize(m_internal_window, size.
x(), size.
y());
221 SDL_SetWindowMaximumSize(m_internal_window, size.
x(), size.
y());
229 SDL_MaximizeWindow(m_internal_window);
233 SDL_RestoreWindow(m_internal_window);
240 SDL_SetWindowFullscreen(m_internal_window, (fullscreen) ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
250 if (SDL_GL_SetSwapInterval(-1) != 0)
254 if (SDL_GL_SetSwapInterval(1) != 0)
256 debug::log_error(
"Failed to enable synchronized v-sync: {}", SDL_GetError());
272 if (SDL_GL_SetSwapInterval(0) != 0)
288 SDL_GL_MakeCurrent(m_internal_window, m_internal_context);
293 SDL_GL_SwapWindow(m_internal_window);
sdl_window(const std::string &title, const math::ivec2 &windowed_position, const math::ivec2 &windowed_size, bool maximized, bool fullscreen, bool v_sync)
void make_current() override
Makes the window's graphics context current.
void swap_buffers() override
Swaps the front and back buffers of the window's graphics context.
void set_v_sync(bool v_sync) override
Enables or disables v-sync.
void set_title(const std::string &title) override
Changes the title of the window.
void set_position(const math::ivec2 &position) override
Changes the position of the window.
void set_maximized(bool maximized) override
Maximizes or unmaximizes the window.
void set_fullscreen(bool fullscreen) override
Enables or disables fullscreen mode.
void set_minimum_size(const math::ivec2 &size) override
Sets the minimum size of the window.
void set_size(const math::ivec2 &size) override
Changes the size of the window.
void set_maximum_size(const math::ivec2 &size) override
Sets the maximum size of the window.
math::ivec2 m_minimum_size
math::ivec2 m_viewport_size
math::ivec2 m_windowed_position
math::ivec2 m_windowed_size
math::ivec2 m_maximum_size
log_message< log_message_severity::fatal, Args... > log_fatal
Formats and logs a fatal error message.
log_message< log_message_severity::warning, Args... > log_warning
Formats and logs a warning message.
log_message< log_message_severity::trace, Args... > log_trace
Formats and logs a trace message.
log_message< log_message_severity::debug, Args... > log_debug
Formats and logs a debug message.
log_message< log_message_severity::error, Args... > log_error
Formats and logs an error message.
log_message< log_message_severity::info, Args... > log_info
Formats and logs an info message.
@ color_clear_bit
Indicates the color buffer should be cleared.
@ position
Vertex position (vec3)
constexpr element_type & x() noexcept
Returns a reference to the first element.
constexpr element_type & y() noexcept
Returns a reference to the second element.