#include "window.h" #include "rendering/render_api.h" // RendererAPI::get_api() #include namespace Donut { static bool s_glfw_initialized = false; static uint32_t s_glfw_window_count = 0; Window::Window(const std::string& title, int width, int height) : m_title(title), m_width(width), m_height(height), m_is_closed(false) { init(); } Window::~Window() { shutdown(); } auto Window::init() -> void { DONUT_INFO("Initializing window: ", m_title, " (", m_width, "x", m_height, ")"); if (!s_glfw_initialized) { int success = glfwInit(); if (!success) { DONUT_ERROR("Could not initialize GLFW!"); return; } s_glfw_initialized = true; DONUT_INFO("GLFW initialized successfully"); } if (RendererAPI::get_api() == RendererAPI::API::Vulkan) { // Vulkan does its own presentation, so GLFW must NOT sneak in a GL context. glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); } else { #ifdef __APPLE__ // macOS caps OpenGL at 4.1 Core Profile and demands a forward-compat // core-profile context for anything modern (>= 3.3) to even compile. // skip these hints and GLFW hands you a dusty 2.1 context and every // single shader in the project refuses to build. so, we ask nicely: glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); #endif } m_window = glfwCreateWindow(m_width, m_height, m_title.c_str(), nullptr, nullptr); if (!m_window) { DONUT_ERROR("Could not create GLFW window!"); glfwTerminate(); return; } DONUT_INFO("GLFW window created successfully"); if (RendererAPI::get_api() != RendererAPI::API::Vulkan) glfwMakeContextCurrent(m_window); glfwSetWindowUserPointer(m_window, this); glfwSetErrorCallback(glfw_error_callback); glfwSetWindowCloseCallback(m_window, glfw_window_close_callback); glfwSetWindowSizeCallback(m_window, glfw_window_size_callback); glfwSetWindowFocusCallback(m_window, glfw_window_focus_callback); glfwSetWindowPosCallback(m_window, glfw_window_pos_callback); glfwSetKeyCallback(m_window, glfw_key_callback); glfwSetCharCallback(m_window, glfw_char_callback); glfwSetMouseButtonCallback(m_window, glfw_mouse_button_callback); glfwSetScrollCallback(m_window, glfw_mouse_scroll_callback); glfwSetCursorPosCallback(m_window, glfw_cursor_pos_callback); s_glfw_window_count++; } auto Window::shutdown() -> void { DONUT_INFO("Shutting down window: ", m_title); glfwDestroyWindow(m_window); s_glfw_window_count--; if (s_glfw_window_count == 0) { glfwTerminate(); s_glfw_initialized = false; DONUT_INFO("GLFW terminated (no more windows)"); } } auto Window::on_update() const -> void { glfwPollEvents(); if (RendererAPI::get_api() != RendererAPI::API::Vulkan) glfwSwapBuffers(m_window); // Vulkan presents via the swapchain instead } auto Window::should_close() const -> bool { return glfwWindowShouldClose(m_window) || m_is_closed; } auto Window::set_cursor_locked(bool locked) -> void { m_cursor_locked = locked; glfwSetInputMode(m_window, GLFW_CURSOR, locked ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL); } auto Window::set_cursor_visible(bool visible) -> void { m_cursor_visible = visible; glfwSetInputMode(m_window, GLFW_CURSOR, visible ? GLFW_CURSOR_NORMAL : GLFW_CURSOR_HIDDEN); } auto Window::glfw_error_callback(int error, const char* description) -> void { DONUT_ERROR("GLFW Error ({}): {}", error, description ? description : ""); } auto Window::glfw_window_close_callback(GLFWwindow* window) -> void { Window* win = static_cast(glfwGetWindowUserPointer(window)); WindowCloseEvent event; win->m_event_handler.on_event(event); } auto Window::glfw_window_size_callback(GLFWwindow* window, int width, int height) -> void { Window* win = static_cast(glfwGetWindowUserPointer(window)); win->m_width = width; win->m_height = height; WindowResizeEvent event(width, height); win->m_event_handler.on_event(event); } auto Window::glfw_window_focus_callback(GLFWwindow* window, int focused) -> void { Window* win = static_cast(glfwGetWindowUserPointer(window)); if (focused) { WindowFocusEvent event; win->m_event_handler.on_event(event); } else { WindowLostFocusEvent event; win->m_event_handler.on_event(event); } } auto Window::glfw_window_pos_callback(GLFWwindow* window, int xpos, int ypos) -> void { Window* win = static_cast(glfwGetWindowUserPointer(window)); WindowMovedEvent event(xpos, ypos); win->m_event_handler.on_event(event); } auto Window::glfw_key_callback(GLFWwindow* window, int key, int /*scancode*/, int action, int /*mods*/) -> void { Window* win = static_cast(glfwGetWindowUserPointer(window)); switch (action) { case GLFW_PRESS: { KeyPressedEvent event(key, false); win->m_event_handler.on_event(event); } break; case GLFW_RELEASE: { KeyReleasedEvent event(key); win->m_event_handler.on_event(event); } break; case GLFW_REPEAT: { KeyPressedEvent event(key, true); win->m_event_handler.on_event(event); } break; } } auto Window::glfw_char_callback(GLFWwindow* window, unsigned int keycode) -> void { Window* win = static_cast(glfwGetWindowUserPointer(window)); KeyTypedEvent event(keycode); win->m_event_handler.on_event(event); } auto Window::glfw_mouse_button_callback(GLFWwindow* window, int button, int action, int /*mods*/) -> void { Window* win = static_cast(glfwGetWindowUserPointer(window)); switch (action) { case GLFW_PRESS: { MouseButtonPressedEvent event(button); win->m_event_handler.on_event(event); } break; case GLFW_RELEASE: { MouseButtonReleasedEvent event(button); win->m_event_handler.on_event(event); } break; } } auto Window::glfw_mouse_scroll_callback(GLFWwindow* window, double x_offset, double y_offset) -> void { Window* win = static_cast(glfwGetWindowUserPointer(window)); MouseScrolledEvent event((float)x_offset, (float)y_offset); win->m_event_handler.on_event(event); } auto Window::glfw_cursor_pos_callback(GLFWwindow* window, double x_pos, double y_pos) -> void { Window* win = static_cast(glfwGetWindowUserPointer(window)); MouseMovedEvent event((float)x_pos, (float)y_pos); win->m_event_handler.on_event(event); } }