123 lines
3.0 KiB
C++
123 lines
3.0 KiB
C++
// =============================================
|
|
// Aster: window.cpp
|
|
// Copyright (c) 2020-2024 Anish Bhobe
|
|
// =============================================
|
|
|
|
#include "window.h"
|
|
|
|
#include "context.h"
|
|
#include "logger.h"
|
|
|
|
std::atomic_uint64_t Window::m_WindowCount = 0;
|
|
std::atomic_bool Window::m_IsGlfwInit = false;
|
|
|
|
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<i32>(width), Cast<i32>(height));
|
|
}
|
|
|
|
Size2D
|
|
Window::GetSize() const
|
|
{
|
|
int width;
|
|
int height;
|
|
glfwGetFramebufferSize(m_Window, &width, &height);
|
|
return {Cast<u32>(width), Cast<u32>(height)};
|
|
}
|
|
|
|
Window::Window(const cstr title, Size2D extent, const b8 isFullScreen)
|
|
{
|
|
m_Name = title;
|
|
|
|
if (!m_IsGlfwInit)
|
|
{
|
|
if (!glfwInit())
|
|
{
|
|
const char *error = nullptr;
|
|
const auto code = glfwGetError(&error);
|
|
ERROR("GLFW Init failed. Cause: ({}) {}", code, error)
|
|
THEN_ABORT(code);
|
|
}
|
|
m_WindowCount = 0;
|
|
m_IsGlfwInit = true;
|
|
}
|
|
|
|
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.m_Width), Cast<i32>(extent.m_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, extent.m_Width, extent.m_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<i32>(windowWidth - extent.m_Width) / 2,
|
|
Cast<i32>(windowHeight - extent.m_Height) / 2);
|
|
}
|
|
glfwSetInputMode(m_Window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
|
|
|
++m_WindowCount;
|
|
}
|
|
|
|
Window::~Window()
|
|
{
|
|
if (m_Window)
|
|
{
|
|
glfwDestroyWindow(m_Window);
|
|
m_Window = nullptr;
|
|
|
|
--m_WindowCount;
|
|
}
|
|
|
|
if (m_WindowCount== 0 && m_IsGlfwInit)
|
|
{
|
|
glfwTerminate();
|
|
m_IsGlfwInit = false;
|
|
}
|
|
|
|
DEBUG("Window '{}' Destroyed", m_Name);
|
|
}
|
|
|
|
Window::Window(Window &&other) noexcept
|
|
: m_Window(Take(other.m_Window))
|
|
, m_Name(Take(other.m_Name))
|
|
{
|
|
}
|
|
|
|
Window &
|
|
Window::operator=(Window &&other) noexcept
|
|
{
|
|
if (this == &other)
|
|
return *this;
|
|
m_Window = Take(other.m_Window);
|
|
m_Name = Take(other.m_Name);
|
|
return *this;
|
|
}
|