6#include <brenta/audio.hpp>
7#include <brenta/camera.hpp>
8#include <brenta/input.hpp>
9#include <brenta/logger.hpp>
10#include <brenta/window.hpp>
15using namespace brenta;
23GLFWwindow *window::window_backend;
24std::string window::title;
25const std::string window::subsystem_name =
"window";
36bool window::initialized =
false;
42std::expected<void, subsystem::error> window::initialize()
46 if (glfwInit() == GLFW_FALSE)
48 return std::unexpected(window::subsystem_name +
49 ": failed to initialize GLFW");
52 set_context_version(3, 3);
55 if (window::init_config.msaa)
57 glfwWindowHint(GLFW_SAMPLES, 4);
58 INFO(
"{}: enabled MSAA", window::subsystem_name);
60 INFO(
"{}: disabled MSAA", window::subsystem_name);
63 if (!window::init_config.vsync)
66 INFO(
"{}: disabled VSync", window::subsystem_name);
68 INFO(
"{}: enabled VSync", window::subsystem_name);
71 if (window::init_config.debug)
73 glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT,
true);
74 INFO(
"{}: enabled OPENGL_DEBUG_CONTEXT", window::subsystem_name);
81 create_window(window::init_config.width,
82 window::init_config.height, title);
83 make_context_current();
84 set_mouse_capture(window::init_config.capture_mouse);
86 set_size_callback(framebuffer_size_callback);
88 window::initialized =
true;
89 INFO(
"{}: initialized", window::subsystem_name)
93std::expected<void, subsystem::error> window::terminate()
97 glfwDestroyWindow(window::window_backend);
100 window::initialized =
false;
101 INFO(
"{}: terminated", window::subsystem_name);
107 return window::subsystem_name;
112 return window::initialized;
125bool window::should_close()
127 return glfwWindowShouldClose(window::window_backend);
130void window::set_width_height(
int width,
int height)
132 window::width = width;
133 window::height = height;
137bool window::is_key_pressed(
int key)
139 return glfwGetKey(window::window_backend, key) == GLFW_PRESS;
142float window::get_time()
144 return glfwGetTime();
147GLFWwindow *window::get_window()
149 return window::window_backend;
154 return reinterpret_cast<void (*)()
>(glfwGetProcAddress);
157int window::get_width()
159 return window::width;
162int window::get_height()
164 return window::height;
167void window::set_mouse_callback(GLFWcursorposfun callback)
169 glfwSetCursorPosCallback(window::window_backend, callback);
172void window::set_size_callback(GLFWframebuffersizefun callback)
174 glfwSetFramebufferSizeCallback(window::window_backend, callback);
176 DEBUG(
"{}: set framebuffer size callback", window::subsystem_name);
179void window::set_mouse_capture(
bool is_captured)
183 glfwSetInputMode(window::window_backend, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
184 INFO(
"{}: mouse capture enabled", window::subsystem_name);
188 glfwSetInputMode(window::window_backend, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
189 INFO(
"{}: mouse capture disabled", window::subsystem_name);
195 glfwSetWindowShouldClose(window::window_backend, GLFW_TRUE);
200 glfwSwapBuffers(window::window_backend);
208void window::set_context_version(
int major,
int minor)
210 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, major);
211 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, minor);
213 INFO(
"{}: set context to OpenGL version: {}.{}",
214 window::subsystem_name, major, minor);
217void window::set_key_callback(GLFWkeyfun callback)
219 glfwSetKeyCallback(window::window_backend, callback);
222void window::set_mouse_pos_callback(GLFWcursorposfun callback)
224 glfwSetCursorPosCallback(window::get_window(), callback);
227void window::use_core_profile()
229 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
231 INFO(
"{}: set OpenGL profile to core", window::subsystem_name);
234void window::set_hints_apple()
236 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
239void window::create_window(
int width,
int height, std::string title)
241 window::window_backend = glfwCreateWindow(width, height, title.c_str(),
243 if (window::window_backend == NULL)
245 ERROR(
"{}: failed to create GLFW window", window::subsystem_name);
248 window::width = width;
249 window::height = height;
250 window::title = title;
253void window::make_context_current()
255 glfwMakeContextCurrent(window::window_backend);
258void window::framebuffer_size_callback([[maybe_unused]] GLFWwindow *
window,
259 [[maybe_unused]]
int width,
260 [[maybe_unused]]
int height)
262#ifndef BRENTA_USE_IMGUI
263 glViewport(0, 0, width, height);
264 window::width = width;
265 window::height = height;
275 this->conf.width = width;
281 this->conf.height = height;
287 this->conf.title = title;
293 this->conf.capture_mouse =
true;
299 this->conf.msaa =
true;
305 this->conf.debug =
true;
311 this->conf.vsync =
true;
317 window::init_config = this->conf;
std::string name() override
Returns the name of the sybsystem.
static void poll_events()
Poll all pending events.
static brenta::window & instance()
Get a static instance of the window.
bool is_initialized() override
Returns true if the subsystem is initialized.
static void swap_buffers()
Swap the front and back buffers.
static GLFWglproc get_proc_address()
Get the OpenGL function pointer.