// ============================================= // Aster: window.cpp // Copyright (c) 2020-2024 Anish Bhobe // ============================================= #include "window.h" #include "context.h" #include "logger.h" void Window::RequestExit() const noexcept { glfwSetWindowShouldClose(m_Window, true); } void Window::SetWindowSize(const vk::Extent2D &extent) const noexcept { SetWindowSize(extent.width, extent.height); } void Window::SetWindowSize(const u32 width, const u32 height) const noexcept { glfwSetWindowSize(m_Window, Cast(width), Cast(height)); } vk::Extent2D Window::GetSize() const { int width; int height; glfwGetFramebufferSize(m_Window, &width, &height); return {Cast(width), Cast(height)}; } void Window::Init(cstr title, vk::Extent2D extent, b8 isFullScreen) { if (!glfwInit()) { const char *error = nullptr; const auto code = glfwGetError(&error); ERROR("GLFW Init failed. Cause: ({}) {}", code, error) THEN_ABORT(code); } GLFWmonitor *monitor = glfwGetPrimaryMonitor(); ERROR_IF(!monitor, "No monitor found"); i32 windowX, windowY, windowWidth, windowHeight; glfwGetMonitorWorkarea(monitor, &windowX, &windowY, &windowWidth, &windowHeight); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); glfwWindowHint(GLFW_CENTER_CURSOR, GLFW_TRUE); m_Window = glfwCreateWindow(Cast(extent.width), Cast(extent.height), title, isFullScreen ? monitor : nullptr, nullptr); ERROR_IF(m_Window == nullptr, "Window creation failed") ELSE_DEBUG("Window '{}' created with resolution '{}x{}'", title, extent.width, extent.height); if (m_Window == nullptr) { const char *error = nullptr; const auto code = glfwGetError(&error); ERROR("GLFW Window Creation failed. Cause: ({}) {}", code, error) THEN_ABORT(code); } if (isFullScreen == false) { glfwSetWindowPos(m_Window, Cast(windowWidth - extent.width) / 2, Cast(windowHeight - extent.height) / 2); } glfwSetInputMode(m_Window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); } void Window::Destroy() { if (!m_Window) return; const char *title = glfwGetWindowTitle(m_Window); DEBUG("Window {} Destroyed", title); glfwDestroyWindow(m_Window); m_Window = nullptr; // Currently only one window is supported. glfwTerminate(); }