Files
LearningVulkan/GlfwWindowManager.cpp
2026-01-05 10:36:27 -05:00

186 lines
3.0 KiB
C++

#include "GlfwWindowManager.h"
#include <algorithm>
#include "Logger.h"
// bool WindowManager::bGlfwInitialized = false;
GlfwWindowManager::GlfwWindowManager() = default;
GlfwWindowManager::~GlfwWindowManager()
{
Cleanup();
}
GlfwWindowManager::GlfwWindowManager(GlfwWindowManager&& Other) noexcept
: Window(Other.Window), Title(std::move(Other.Title)), Width(Other.Width), Height(Other.Height)
{
Other.Window = nullptr;
}
GlfwWindowManager& GlfwWindowManager::operator=(GlfwWindowManager&& Other) noexcept
{
if (this != &Other)
{
Cleanup();
Window = Other.Window;
Title = std::move(Other.Title);
Width = Other.Width;
Height = Other.Height;
Other.Window = nullptr;
}
return *this;
}
void GlfwWindowManager::Initialize(const FWindowConfig& Config)
{
if (IsInitialized())
{
return;
}
InitializeGlfw();
Title = Config.Title;
Width = Config.Width;
Height = Config.Height;
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, Config.bResizable ? GLFW_TRUE : GLFW_FALSE);
Window = glfwCreateWindow(
Width,
Height,
Title.c_str(),
Config.bFullscreen ? glfwGetPrimaryMonitor() : nullptr,
nullptr);
if (!Window)
{
Log::Error("Failed to create GLFW window");
}
}
void GlfwWindowManager::Cleanup()
{
if (!IsInitialized())
{
Log::Warning("Not initialized");
return;
}
DestroyWindow();
glfwTerminate();
}
bool GlfwWindowManager::ShouldClose() const
{
return Window && glfwWindowShouldClose(Window);
}
void GlfwWindowManager::PollEvents()
{
if (Window)
{
glfwPollEvents();
}
}
void GlfwWindowManager::WaitEvents() const
{
if (Window)
{
glfwWaitEvents();
}
}
void GlfwWindowManager::SetTitle(const std::string& Title)
{
this->Title = Title;
if (Window)
{
glfwSetWindowTitle(Window, Title.c_str());
}
}
VkSurfaceKHR GlfwWindowManager::CreateSurface(VkInstance Instance) const
{
if (!Window || !Instance)
{
return VK_NULL_HANDLE;
}
VkSurfaceKHR Surface = VK_NULL_HANDLE;
if (glfwCreateWindowSurface(Instance, Window, nullptr, &Surface) != VK_SUCCESS)
{
Log::Error("Failed to create window surface");
}
return Surface;
}
void GlfwWindowManager::SetResizeCallback(GLFWwindowsizefun Callback)
{
if (Window)
{
glfwSetWindowSizeCallback(Window, Callback);
}
}
void GlfwWindowManager::SetKeyCallback(GLFWkeyfun Callback)
{
if (Window)
{
glfwSetKeyCallback(Window, Callback);
}
}
void GlfwWindowManager::SetMouseButtonCallback(GLFWmousebuttonfun Callback)
{
if (Window)
{
glfwSetMouseButtonCallback(Window, Callback);
}
}
void GlfwWindowManager::SetCursorPositionCallback(GLFWcursorposfun Callback)
{
if (Window)
{
glfwSetCursorPosCallback(Window, Callback);
}
}
void GlfwWindowManager::SetScrollCallback(GLFWscrollfun Callback)
{
if (Window)
{
glfwSetScrollCallback(Window, Callback);
}
}
void GlfwWindowManager::DestroyWindow()
{
if (Window)
{
glfwDestroyWindow(Window);
Window = nullptr;
}
}
void GlfwWindowManager::InitializeGlfw()
{
if (!IsInitialized())
{
if (!glfwInit())
{
Log::Error("Failed to initialize GLFW");
}
}
}