project-aster/aster/window.cpp

90 lines
2.5 KiB
C++

// =============================================
// Aster: window.cpp
// Copyright (c) 2020-2024 Anish Bhobe
// =============================================
#include "window.h"
#include "context.h"
#include "glfw_context.h"
#include "logger.h"
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<i32>(width), cast<i32>(height));
}
vk::Extent2D
Window::GetSize() const
{
int width;
int height;
glfwGetWindowSize(m_Window, &width, &height);
return {cast<u32>(width), cast<u32>(height)};
}
Window::Window(cstr title, Context *context, vk::Extent2D extent, b8 isFullScreen)
{
m_Context = context;
m_Name = title;
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<i32>(extent.width), cast<i32>(extent.height), m_Name.c_str(),
isFullScreen ? monitor : nullptr, nullptr);
ERROR_IF(m_Window == nullptr, "Window creation failed")
ELSE_DEBUG("Window '{}' created with resolution '{}x{}'", m_Name.c_str(), extent.width, extent.height);
if (m_Window == nullptr)
{
auto code = GlfwContext::PostError();
glfwTerminate();
ABORT(code);
}
if (isFullScreen == false)
{
glfwSetWindowPos(m_Window, cast<i32>(windowWidth - extent.width) / 2,
cast<i32>(windowHeight - extent.height) / 2);
}
glfwSetInputMode(m_Window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
VkSurfaceKHR surface;
auto result =
cast<vk::Result>(glfwCreateWindowSurface(cast<VkInstance>(m_Context->m_Instance), m_Window, nullptr, &surface));
ERROR_IF(failed(result), "Failed to create Surface with {}", to_string(result))
THEN_ABORT(result)
ELSE_DEBUG("Surface {} Created", m_Name.c_str());
m_Surface = vk::SurfaceKHR(surface);
}
Window::~Window()
{
if (m_Context && m_Surface)
{
m_Context->m_Instance.destroy(m_Surface, nullptr);
DEBUG("Surface Destroyed");
}
if (m_Window != nullptr)
{
glfwDestroyWindow(m_Window);
m_Window = nullptr;
}
DEBUG("Window '{}' Destroyed", m_Name.c_str());
}