First commit from Arch
This commit is contained in:
212
src/GlfwWindowManager.cpp
Normal file
212
src/GlfwWindowManager.cpp
Normal file
@@ -0,0 +1,212 @@
|
||||
#include "GlfwWindowManager.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "Logger.h"
|
||||
|
||||
VkSurfaceKHR GlfwWindowManager::Surface = VK_NULL_HANDLE;
|
||||
GLFWwindow* GlfwWindowManager::Window = nullptr;
|
||||
|
||||
GlfwWindowManager::GlfwWindowManager() = default;
|
||||
|
||||
GlfwWindowManager::~GlfwWindowManager()
|
||||
{
|
||||
// Cleanup();
|
||||
}
|
||||
|
||||
// GlfwWindowManager::GlfwWindowManager(GlfwWindowManager&& Other) noexcept
|
||||
// : Window(Other.Window), Config(Other.Config), Surface(Other.Surface)
|
||||
//{
|
||||
// Other.Window = nullptr;
|
||||
// }
|
||||
//
|
||||
// GlfwWindowManager& GlfwWindowManager::operator=(GlfwWindowManager&& Other) noexcept
|
||||
//{
|
||||
// if (this != &Other)
|
||||
// {
|
||||
// Cleanup();
|
||||
//
|
||||
// Window = Other.Window;
|
||||
// Config = Other.Config;
|
||||
// Surface = Other.Surface;
|
||||
//
|
||||
// Other.Window = nullptr;
|
||||
// }
|
||||
//
|
||||
// return *this;
|
||||
// }
|
||||
|
||||
void GlfwWindowManager::Initialize(const FWindowConfig& Config)
|
||||
{
|
||||
if (IsInitialized())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this->Config = Config;
|
||||
|
||||
InitializeGlfw();
|
||||
}
|
||||
|
||||
void GlfwWindowManager::Cleanup(VkInstance Instance)
|
||||
{
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
void GlfwWindowManager::CreateSurface(VkInstance Instance)
|
||||
{
|
||||
if (!Window)
|
||||
{
|
||||
Log::Error("Window not initialized.");
|
||||
}
|
||||
|
||||
if (!Instance)
|
||||
{
|
||||
Log::Error("Instance is null.");
|
||||
}
|
||||
|
||||
VkResult result = glfwCreateWindowSurface(Instance, Window, nullptr, &Surface);
|
||||
if (result != VK_SUCCESS)
|
||||
{
|
||||
std::string errorMsg;
|
||||
switch (result)
|
||||
{
|
||||
case VK_ERROR_EXTENSION_NOT_PRESENT:
|
||||
errorMsg = "VK_ERROR_EXTENSION_NOT_PRESENT - Required extension not present";
|
||||
break;
|
||||
case VK_ERROR_INITIALIZATION_FAILED:
|
||||
errorMsg = "VK_ERROR_INITIALIZATION_FAILED - Initialization failed";
|
||||
break;
|
||||
case VK_ERROR_NATIVE_WINDOW_IN_USE_KHR:
|
||||
errorMsg = "VK_ERROR_NATIVE_WINDOW_IN_USE_KHR - Native window already in use";
|
||||
break;
|
||||
default:
|
||||
errorMsg = "Unknown error code: " + std::to_string(result);
|
||||
break;
|
||||
}
|
||||
|
||||
Log::Error("Failed to create window surface: " + errorMsg);
|
||||
}
|
||||
else
|
||||
{
|
||||
Log::Info("Window surface created successfully.");
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
||||
glfwWindowHint(GLFW_RESIZABLE, Config.bResizable ? GLFW_TRUE : GLFW_FALSE);
|
||||
|
||||
Window = glfwCreateWindow(
|
||||
Config.Width,
|
||||
Config.Height,
|
||||
Config.Title.c_str(),
|
||||
Config.bFullscreen ? glfwGetPrimaryMonitor() : nullptr,
|
||||
nullptr);
|
||||
|
||||
if (!Window)
|
||||
{
|
||||
Log::Error("Failed to create GLFW window");
|
||||
}
|
||||
else
|
||||
{
|
||||
Log::Info("Created GLFW window successfully.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user