Used GLFW to create vulkan surface (Breaks with wayland version of glfw :/)

This commit is contained in:
Warwick 2024-05-01 18:09:53 +01:00
parent f1186130b6
commit 5d672a8b0b
5 changed files with 30 additions and 8 deletions

1
.gitignore vendored
View file

@ -1,6 +1,7 @@
# Ignore build directories # Ignore build directories
build/ build/
.cache/ .cache/
.cmake/
# CMake mess for lazy cmake usage # CMake mess for lazy cmake usage
CMakeCache.txt CMakeCache.txt

View file

@ -1,17 +1,21 @@
#include "yave_vulkan_surface.hpp" #include "yave_vulkan_surface.hpp"
#include "yave_vulkan_instance.hpp" #include "yave_vulkan_instance.hpp"
#include <iostream>
#include <ostream>
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
namespace yave { namespace yave {
YaveVulkanSurface::YaveVulkanSurface(YaveVulkanInstance &yaveVulkanInstance, YaveVulkanSurface::YaveVulkanSurface(YaveVulkanInstance &yaveVulkanInstance,
YaveWindow &window) { YaveWindow &window) {
VkWaylandSurfaceCreateInfoKHR createInfo{}; if (glfwCreateWindowSurface(*yaveVulkanInstance.getVkInstance(), window.getGLFWWindow(), nullptr,
createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR; &surface)) {
createInfo.display = glfwGetWaylandDisplay();
createInfo.surface = glfwGetWaylandWindow(window.getGLFWWindow());
//VkInstance *vkInstance = yaveVulkanInstance.getVkInstance();
if (vkCreateWaylandSurfaceKHR((*yaveVulkanInstance.getVkInstance()), &createInfo, nullptr, &surface)) {
throw std::runtime_error("failed to create window surface!"); throw std::runtime_error("failed to create window surface!");
} }
} }
YaveVulkanSurface::~YaveVulkanSurface() {} YaveVulkanSurface::~YaveVulkanSurface() {}
} // namespace yave } // namespace yave

View file

@ -3,7 +3,6 @@
#include "yave_vulkan_instance.hpp" #include "yave_vulkan_instance.hpp"
#include "yave_window.hpp" #include "yave_window.hpp"
#include <vulkan/vulkan_core.h> #include <vulkan/vulkan_core.h>
#include <stdexcept>
// This engine is for now Wayland only!!! // This engine is for now Wayland only!!!
#define VK_USE_PLATFORM_WAYLAND_KHR #define VK_USE_PLATFORM_WAYLAND_KHR
@ -23,6 +22,7 @@ private:
public: public:
YaveVulkanSurface(YaveVulkanInstance &yaveVulkanInstance, YaveWindow &window); YaveVulkanSurface(YaveVulkanInstance &yaveVulkanInstance, YaveWindow &window);
~YaveVulkanSurface(); ~YaveVulkanSurface();
//
// Delete Copy constructors // Delete Copy constructors
// This class should match one to one with vulkan instances // This class should match one to one with vulkan instances
YaveVulkanSurface(const YaveVulkanSurface &) = delete; YaveVulkanSurface(const YaveVulkanSurface &) = delete;

View file

@ -1,25 +1,38 @@
#include "yave_window.hpp" #include "yave_window.hpp"
#include <iostream>
#include <stdexcept>
namespace yave { namespace yave {
YaveWindow::YaveWindow(int w, int h, std::string name) YaveWindow::YaveWindow(int w, int h, std::string name)
: width{w}, height{h}, windowName{name} { : width{w}, height{h}, windowName{name} {
glfwSetErrorCallback(YaveWindow::glfwErrorCallback);
initWindow(); initWindow();
} }
void YaveWindow::glfwErrorCallback(int error, const char *description) {
std::cerr<< "GLFW Error " << error << ": " << description << std::endl;
}
YaveWindow::~YaveWindow() { YaveWindow::~YaveWindow() {
glfwDestroyWindow(window); glfwDestroyWindow(window);
glfwTerminate(); glfwTerminate();
} }
void YaveWindow::initWindow() { void YaveWindow::initWindow() {
glfwInit(); if (!glfwInit()) {
throw std::runtime_error("GLFW failed to initialise!");
}
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); // Don't use OpenGL glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); // Don't use OpenGL
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); // Vulkan doesn't like to be resized glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); // Vulkan doesn't like to be resized
// Create window with no OpenGL context // Create window with no OpenGL context
window = window =
glfwCreateWindow(width, height, windowName.c_str(), nullptr, nullptr); glfwCreateWindow(width, height, windowName.c_str(), nullptr, nullptr);
if (window == nullptr) {
throw std::runtime_error("GLFW failed to create a window!");
}
} }
} // namespace yave } // namespace yave

View file

@ -1,7 +1,9 @@
#pragma once #pragma once
#define GLFW_INCLUDE_VULKAN #define GLFW_INCLUDE_VULKAN
#define GLFW_EXPOSE_NATIVE_WAYLAND
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <GLFW/glfw3native.h>
#include <string> #include <string>
@ -13,6 +15,8 @@ private:
void initWindow(); void initWindow();
static void glfwErrorCallback (int error, const char* description);
const int width; const int width;
const int height; const int height;
std::string windowName; std::string windowName;