Added GLFWManager
This commit is contained in:
7
.idea/vcs.xml
generated
Normal file
7
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
<mapping directory="$PROJECT_DIR$/cmake-build-debug/_deps/glfw-src" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -31,6 +31,8 @@ FetchContent_MakeAvailable(glfw)
|
||||
project(VulkanRenderer LANGUAGES CXX)
|
||||
add_executable(VulkanRenderer
|
||||
main.cpp
|
||||
src/GLFWManager.h
|
||||
src/GLFWManager.cpp
|
||||
)
|
||||
|
||||
# Links the libraries to the binary #
|
||||
|
||||
27
main.cpp
27
main.cpp
@@ -1,29 +1,16 @@
|
||||
#include <vulkan/vulkan.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include <iostream>
|
||||
#include "src/GLFWManager.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
using namespace PB::Renderer; // Project namespace
|
||||
|
||||
/* Initializes GLFW and creates window */
|
||||
if (!glfwInit())
|
||||
{
|
||||
std::cout << "Failed to initialize GLFW" << std::endl;
|
||||
if (!GLFWManager::Init())
|
||||
return -1;
|
||||
}
|
||||
|
||||
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
||||
|
||||
const int WIDTH = 800;
|
||||
const int HEIGHT = 600;
|
||||
|
||||
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan Window", nullptr, nullptr);
|
||||
GLFWwindow* window = GLFWManager::CreateWindow(800, 600, "Vulkan window");
|
||||
if (!window)
|
||||
{
|
||||
std::cout << "Failed to create GLFW window" << std::endl;
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Polls window events whilst it is still open */
|
||||
while (!glfwWindowShouldClose(window))
|
||||
@@ -32,6 +19,6 @@ int main()
|
||||
}
|
||||
|
||||
/* Cleans up GLFW */
|
||||
glfwDestroyWindow(window);
|
||||
glfwTerminate();
|
||||
if (!GLFWManager::Cleanup())
|
||||
return -1;
|
||||
}
|
||||
57
src/GLFWManager.cpp
Normal file
57
src/GLFWManager.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#include "GLFWManager.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace PB::Renderer
|
||||
{
|
||||
std::vector<GLFWwindow*> GLFWManager::s_Windows;
|
||||
|
||||
bool GLFWManager::Init()
|
||||
{
|
||||
const bool success = glfwInit();
|
||||
if (!success)
|
||||
std::cout << "PB::Renderer::GLFWManager::Init(): glfwInit() failed" << std::endl;
|
||||
|
||||
/* Stops GLFW from creating openGL contexts */
|
||||
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
bool GLFWManager::Cleanup()
|
||||
{
|
||||
/* Closes all windows */
|
||||
for (GLFWwindow* window : s_Windows)
|
||||
glfwDestroyWindow(window);
|
||||
|
||||
glfwTerminate();
|
||||
return true; // Cannot fail (yet)
|
||||
}
|
||||
|
||||
GLFWwindow* GLFWManager::CreateWindow(int width, int height, const char* title)
|
||||
{
|
||||
/* Checks for valid window size before creation */
|
||||
if (width < 0 || height < 0)
|
||||
{
|
||||
std::cout << "PB::Renderer::GLFWManager::CreateWindow(): width or height must be < 0" << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
GLFWwindow* window = glfwCreateWindow(width, height, title, nullptr, nullptr);
|
||||
|
||||
/* Stores the window and returns it to the user */
|
||||
s_Windows.push_back(window);
|
||||
return window;
|
||||
}
|
||||
|
||||
GLFWwindow* GLFWManager::GetWindow(int index)
|
||||
{
|
||||
/* Checks input is within the bounds before fetching */
|
||||
if (index >= s_Windows.size() || index < 0)
|
||||
{
|
||||
std::cout << "PB::Renderer::GLFWManager::GetWindow(): index out of bounds" << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return s_Windows[index];
|
||||
}
|
||||
}
|
||||
21
src/GLFWManager.h
Normal file
21
src/GLFWManager.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include "GLFW/glfw3.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace PB::Renderer
|
||||
{
|
||||
class GLFWManager
|
||||
{
|
||||
private:
|
||||
static std::vector<GLFWwindow*> s_Windows;
|
||||
|
||||
public:
|
||||
static bool Init();
|
||||
static bool Cleanup();
|
||||
|
||||
static GLFWwindow* CreateWindow(int width, int height, const char* title = "Unnamed window");
|
||||
static GLFWwindow* GetWindow(int index = 0);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user