#include #include "../Common/Sizes.hpp" #include "../Common/Assert.hpp" #include "../ThreadRole.hpp" #include "Window.hpp" #include "../Common/Casts.hpp" #include namespace MC::GFX { Window::Window(std::string const& title, U32 const width, U32 const height) { ASSERT_MAIN_THREAD(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_DOUBLEBUFFER, GL_TRUE); m_window = glfwCreateWindow(TO(I32, width), TO(I32, height), title.c_str(), nullptr, nullptr); if (m_window == nullptr) { throw std::runtime_error("Failed to create window."); } glfwMakeContextCurrent(m_window); glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); } Window::~Window() { ASSERT_MAIN_THREAD(); glfwDestroyWindow(m_window); } Bool Window::should_close() const { return glfwWindowShouldClose(m_window); } GLFWwindow* Window::get() const { return m_window; } void Window::close() const { glfwSetWindowShouldClose(m_window, true); } void Window::start_render() const { glfwSwapBuffers(m_window); } void Window::poll_events() const { ASSERT_MAIN_THREAD(); glfwPollEvents(); } void Window::on_size_change(void (callback)(GLFWwindow*, I32, I32)) const { glfwSetFramebufferSizeCallback(m_window, callback); } void Window::attach() const { glfwMakeContextCurrent(m_window); } void Window::detach() const { ASSERT(glfwGetCurrentContext() == m_window, "Cannot detach window that is not current"); glfwMakeContextCurrent(nullptr); } }