132 lines
2.9 KiB
C++
132 lines
2.9 KiB
C++
// =============================================
|
|
// Aster: window.cpp
|
|
// Copyright (c) 2020-2024 Anish Bhobe
|
|
// =============================================
|
|
|
|
#include "window.h"
|
|
|
|
#include "context.h"
|
|
#include "logger.h"
|
|
|
|
#include <SDL3/SDL.h>
|
|
#include <SDL3/SDL_vulkan.h>
|
|
|
|
std::atomic_uint64_t Window::m_WindowCount = 0;
|
|
std::atomic_bool Window::m_IsGlfwInit = false;
|
|
|
|
bool
|
|
Window::Poll() const noexcept
|
|
{
|
|
SDL_Event event;
|
|
while (SDL_PollEvent(&event))
|
|
{
|
|
if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void
|
|
Window::RequestExit() const noexcept
|
|
{
|
|
SDL_Event *event = Cast<SDL_Event*>(SDL_malloc(sizeof(SDL_Event)));
|
|
event->type = SDL_EVENT_WINDOW_CLOSE_REQUESTED;
|
|
SDL_PushEvent(event);
|
|
}
|
|
|
|
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
|
|
{
|
|
SDL_SetWindowSize(m_Window, Cast<i32>(width), Cast<i32>(height));
|
|
}
|
|
|
|
Size2D
|
|
Window::GetSize() const
|
|
{
|
|
i32 width, height;
|
|
|
|
SDL_GetWindowSizeInPixels(m_Window, &width, &height);
|
|
return {Cast<u32>(width), Cast<u32>(height)};
|
|
}
|
|
|
|
eastl::vector<cstr>
|
|
Window::GetRequiredExtensions()
|
|
{
|
|
u32 count = 0;
|
|
auto extensions = SDL_Vulkan_GetInstanceExtensions(&count);
|
|
return eastl::vector<cstr>{extensions, extensions + count};
|
|
}
|
|
|
|
Window::Window(const cstr title, Size2D extent, const b8 isFullScreen)
|
|
{
|
|
m_Name = title;
|
|
|
|
if (!m_IsGlfwInit)
|
|
{
|
|
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS))
|
|
{
|
|
const char *error = SDL_GetError();
|
|
ERROR("SDL Init failed. Cause: {}", error)
|
|
THEN_ABORT(-1);
|
|
}
|
|
m_WindowCount = 0;
|
|
m_IsGlfwInit = true;
|
|
}
|
|
|
|
m_Window =
|
|
SDL_CreateWindow(m_Name.c_str(), Cast<i32>(extent.m_Width), Cast<i32>(extent.m_Height), SDL_WINDOW_VULKAN);
|
|
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 = SDL_GetError();
|
|
ERROR("SDL Window Creation failed. Cause: {}", error)
|
|
THEN_ABORT(-1);
|
|
}
|
|
|
|
++m_WindowCount;
|
|
}
|
|
|
|
Window::~Window()
|
|
{
|
|
if (m_Window)
|
|
{
|
|
SDL_DestroyWindow(m_Window);
|
|
m_Window = nullptr;
|
|
|
|
--m_WindowCount;
|
|
}
|
|
|
|
if (m_WindowCount== 0 && m_IsGlfwInit)
|
|
{
|
|
SDL_Quit();
|
|
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;
|
|
}
|