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>
|
||||||
@@ -30,7 +30,9 @@ FetchContent_MakeAvailable(glfw)
|
|||||||
# Creates the output binary
|
# Creates the output binary
|
||||||
project(VulkanRenderer LANGUAGES CXX)
|
project(VulkanRenderer LANGUAGES CXX)
|
||||||
add_executable(VulkanRenderer
|
add_executable(VulkanRenderer
|
||||||
main.cpp
|
main.cpp
|
||||||
|
src/GLFWManager.h
|
||||||
|
src/GLFWManager.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
# Links the libraries to the binary #
|
# Links the libraries to the binary #
|
||||||
|
|||||||
29
main.cpp
29
main.cpp
@@ -1,29 +1,16 @@
|
|||||||
#include <vulkan/vulkan.h>
|
#include "src/GLFWManager.h"
|
||||||
#include <GLFW/glfw3.h>
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
using namespace PB::Renderer; // Project namespace
|
||||||
|
|
||||||
/* Initializes GLFW and creates window */
|
/* Initializes GLFW and creates window */
|
||||||
if (!glfwInit())
|
if (!GLFWManager::Init())
|
||||||
{
|
|
||||||
std::cout << "Failed to initialize GLFW" << std::endl;
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
|
|
||||||
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
GLFWwindow* window = GLFWManager::CreateWindow(800, 600, "Vulkan window");
|
||||||
|
|
||||||
const int WIDTH = 800;
|
|
||||||
const int HEIGHT = 600;
|
|
||||||
|
|
||||||
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan Window", nullptr, nullptr);
|
|
||||||
if (!window)
|
if (!window)
|
||||||
{
|
|
||||||
std::cout << "Failed to create GLFW window" << std::endl;
|
|
||||||
glfwTerminate();
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
|
|
||||||
/* Polls window events whilst it is still open */
|
/* Polls window events whilst it is still open */
|
||||||
while (!glfwWindowShouldClose(window))
|
while (!glfwWindowShouldClose(window))
|
||||||
@@ -32,6 +19,6 @@ int main()
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Cleans up GLFW */
|
/* Cleans up GLFW */
|
||||||
glfwDestroyWindow(window);
|
if (!GLFWManager::Cleanup())
|
||||||
glfwTerminate();
|
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