Created Vulkan Instance (VkInstance)
This commit is contained in:
56
src/managers/GLFWManager.cpp
Normal file
56
src/managers/GLFWManager.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#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/managers/GLFWManager.h
Normal file
21
src/managers/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);
|
||||
};
|
||||
}
|
||||
59
src/managers/VulkanManager.cpp
Normal file
59
src/managers/VulkanManager.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
#include "VulkanManager.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "GLFW/glfw3.h"
|
||||
|
||||
namespace PB::Renderer
|
||||
{
|
||||
std::optional<VkInstance> VulkanManager::s_Instance = std::nullopt;
|
||||
|
||||
std::optional<VkInstance> VulkanManager::Init()
|
||||
{
|
||||
/* Stops multi initialisation */
|
||||
if (s_Instance != std::nullopt)
|
||||
return s_Instance;
|
||||
|
||||
VkApplicationInfo appInfo;
|
||||
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
|
||||
appInfo.pNext = nullptr;
|
||||
appInfo.pApplicationName = "VulkanRenderer";
|
||||
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
|
||||
appInfo.pEngineName = "VulkanRendererEngine";
|
||||
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
|
||||
appInfo.apiVersion = VK_API_VERSION_1_3;
|
||||
|
||||
VkInstanceCreateInfo createInfo;
|
||||
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
||||
createInfo.pNext = nullptr;
|
||||
createInfo.pApplicationInfo = &appInfo;
|
||||
createInfo.enabledLayerCount = 0;
|
||||
createInfo.ppEnabledLayerNames = nullptr;
|
||||
|
||||
/* Imports GLFW extensions */
|
||||
uint32_t glfwExtensionCount = 0;
|
||||
const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
|
||||
createInfo.enabledExtensionCount = glfwExtensionCount;
|
||||
createInfo.ppEnabledExtensionNames = glfwExtensions;
|
||||
|
||||
/* Creates the Vulkan instance */
|
||||
VkInstance instance;
|
||||
if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS)
|
||||
{
|
||||
std::cout << "PB::Renderer::VulkanManager::Init(): Could not create Vulkan instance" << std::endl;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
s_Instance = instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
bool VulkanManager::Cleanup()
|
||||
{
|
||||
if (s_Instance != std::nullopt)
|
||||
vkDestroyInstance(s_Instance.value(), nullptr);
|
||||
|
||||
s_Instance = std::nullopt;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
17
src/managers/VulkanManager.h
Normal file
17
src/managers/VulkanManager.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <optional>
|
||||
#include <vulkan/vulkan.h>
|
||||
|
||||
namespace PB::Renderer
|
||||
{
|
||||
class VulkanManager
|
||||
{
|
||||
private:
|
||||
static std::optional<VkInstance> s_Instance;
|
||||
|
||||
public:
|
||||
static std::optional<VkInstance> Init();
|
||||
static bool Cleanup();
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user