// ============================================= // Aster: window.cpp // Copyright (c) 2020-2024 Anish Bhobe // ============================================= #include "window.h" #include "context.h" #include "glfw_context.h" #include "logger.h" Window::Window(const std::string_view &_title, Context *_context, const vk::Extent2D _extent, const bool _full_screen) : extent{ _extent }, name{ _title } { full_screen = _full_screen; monitor = glfwGetPrimaryMonitor(); ERROR_IF(monitor == nullptr, "No monitor found"); i32 x_, y_, w_, h_; glfwGetMonitorWorkarea(monitor, &x_, &y_, &w_, &h_); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); glfwWindowHint(GLFW_CENTER_CURSOR, GLFW_TRUE); window = glfwCreateWindow(cast(extent.width), cast(extent.height), name.c_str(), (_full_screen ? monitor : nullptr), nullptr); ERROR_IF(window == nullptr, "Window creation failed") ELSE_INFO(fmt::format("Window '{}' created with resolution '{}x{}'", name.data(), extent.width, extent.height)); if (window == nullptr) { auto code = GlfwContext::post_error(); glfwTerminate(); CRASH(code); } if (full_screen == false) { glfwSetWindowPos(window, cast(w_ - extent.width) / 2, cast(h_ - extent.height) / 2); } glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); VkSurfaceKHR surface_; auto result = cast(glfwCreateWindowSurface(cast(*_context->instance), window, nullptr, &surface_)); if (failed(result)) { ERROR("Failed to create Surface with "s + to_string(result)); throw std::runtime_error("Failed to create Surface with "s + to_string(result)); } INFO("Surface Created"); surface = vk::raii::SurfaceKHR{ _context->instance, surface_ }; } Window::Window(Window &&_other) noexcept : window{ std::exchange(_other.window, nullptr) }, monitor{ _other.monitor }, surface{ std::exchange(_other.surface, nullptr) }, extent{ _other.extent }, name{ std::move(_other.name) }, full_screen{ _other.full_screen } {} Window &Window::operator=(Window &&_other) noexcept { if (this == &_other) return *this; window = _other.window; monitor = _other.monitor; surface = std::exchange(_other.surface, nullptr); extent = _other.extent; name = std::move(_other.name); full_screen = _other.full_screen; return *this; } Window::~Window() { if (window != nullptr) { glfwDestroyWindow(window); window = nullptr; } monitor = nullptr; INFO(fmt::format("Window '{}' Destroyed", name)); }