From b5ef8e4ab52fda7a1d7f125cad798d6d54ab0a46 Mon Sep 17 00:00:00 2001 From: Riley King-Saunders Date: Sun, 22 Jun 2025 03:04:28 +1000 Subject: [PATCH] Library: Added a very basic window object that essentially just wraps an GLFW window object. TODO: Set the window userdata pointer to point to its wrapping object --- include/vulkan/basalt_window.h | 32 +++++++++++++++ src/vulkan/basalt_window.cpp | 72 ++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 include/vulkan/basalt_window.h create mode 100644 src/vulkan/basalt_window.cpp diff --git a/include/vulkan/basalt_window.h b/include/vulkan/basalt_window.h new file mode 100644 index 0000000..c4e2601 --- /dev/null +++ b/include/vulkan/basalt_window.h @@ -0,0 +1,32 @@ +#pragma once +#define GLFW_INCLUDE_VULKAN +#include "GLFW/glfw3.h" +#ifndef VK_SUCCESS +#include +#endif + +namespace basalt +{ + class Window + { + public: + Window(const Window& src) = delete; + Window& operator =(const Window& src) = delete; + Window(Window&& other) noexcept; + Window& operator =(Window&& other) noexcept; + + Window(uint16_t width, uint16_t height, const char* title); + ~Window(void) noexcept; + + void swap(Window& other) noexcept; + operator GLFWwindow* (void) const noexcept; + + const bool should_close(void) const noexcept; + + protected: + GLFWwindow* window = nullptr; + const char* window_title = "N/A"; + uint16_t width = 0; + uint16_t height = 0; + }; +} \ No newline at end of file diff --git a/src/vulkan/basalt_window.cpp b/src/vulkan/basalt_window.cpp new file mode 100644 index 0000000..92da1cc --- /dev/null +++ b/src/vulkan/basalt_window.cpp @@ -0,0 +1,72 @@ +#include "basalt_window.h" + +basalt::Window::Window(Window&& other) noexcept +{ + this->window = other.window; + this->window_title = other.window_title; + this->height = other.height; + this->width = other.width; + other.height = 0; + other.width = 0; + other.window = nullptr; + other.window_title = nullptr; + +} + +basalt::Window& basalt::Window::operator=(Window&& other) noexcept +{ + if (&other == this) return *this; + this->~Window(); + this->window = other.window; + this->window_title = other.window_title; + this->height = other.height; + this->width = other.width; + other.height = 0; + other.width = 0; + other.window = nullptr; + other.window_title = nullptr; + return *this; +} + +basalt::Window::Window(uint16_t width, uint16_t height, const char* title) : + width(width), height(height), window_title(title) +{ + glfwInit(); + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); + glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE); + this->window = glfwCreateWindow(width, height, title, nullptr, nullptr); +} + +basalt::Window::~Window(void) noexcept +{ + glfwDestroyWindow(this->window); +} + +void basalt::Window::swap(Window& other) noexcept +{ + { + uint16_t tmp = this->height; + this->height = other.height; + other.height = tmp; + tmp = this->width; + this->width = other.width; + other.width = tmp; + } + { + void* tmp_ptr = this->window; + this->window = other.window; + other.window = static_cast(tmp_ptr); + tmp_ptr = const_cast(this->window_title); + this->window_title = other.window_title; + other.window_title = static_cast(tmp_ptr); + } +} + +basalt::Window::operator GLFWwindow* (void) const noexcept +{ return this->window; } + +const bool basalt::Window::should_close(void) const noexcept +{ + return glfwWindowShouldClose(this->window); +}