From ca1646954595e6519f7417ebce3417700cbc7bd6 Mon Sep 17 00:00:00 2001 From: Tranquillity Codes Date: Tue, 13 Feb 2024 09:04:55 +0100 Subject: [PATCH] Initial commit --- Makefile | 44 ++++++++++++++++++++++++++++++++++++++++ lib/glfw.hpp | 28 ++++++++++++++++++++++++++ lib/vulkan.hpp | 21 ++++++++++++++++++++ lib/window.hpp | 23 +++++++++++++++++++++ src/glfw.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 18 +++++++++++++++++ src/vulkan.cpp | 39 ++++++++++++++++++++++++++++++++++++ src/window.cpp | 31 +++++++++++++++++++++++++++++ 8 files changed, 258 insertions(+) create mode 100644 Makefile create mode 100644 lib/glfw.hpp create mode 100644 lib/vulkan.hpp create mode 100644 lib/window.hpp create mode 100644 src/glfw.cpp create mode 100644 src/main.cpp create mode 100644 src/vulkan.cpp create mode 100644 src/window.cpp diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..43e33e7 --- /dev/null +++ b/Makefile @@ -0,0 +1,44 @@ +SRC := src +CDEBUG := -g -fsanitize=address,undefined +CXXFLAGS := -Ilib $(CDEBUG) +CPP := c++ +LIBS := -lvulkan -lglfw +OUT := ./out +BIN := $(OUT)/main +CXXCMD := $(CPP) $(CXXFLAGS) $(LIBS) + +define linit +$(wordlist 2,$(words $(1)),x $(1)) +endef +define llast +$(lastword $(1)) +endef + +SRCS=$(wildcard $(SRC)/*) + +all: $(BIN) + + +run: all + VK_ADD_LAYER_PATH=/usr/share/vulkan/explicit_layer.d/VkLayer_khronos_validation.json VK_LOADER_LAYERS_ENABLE=*validation ./out/main + +clean: + rm -f out/* + +cleanlsp: + rm -f compile_commands.json + +cleanall: clean cleanlsp + +lsp: $(SRCS) + rm -f compile_commands.json # TODO figure out how to depend on cleanlsp + echo [ >> compile_commands.json + for src in $(call linit,$^) ; do \ + echo {'"'directory'"': '"'$(PWD)'"', '"'command'"': '"'$(CXXCMD) $^ -o $(BIN)'"', '"'file'"': '"'$$src'"'}, >> compile_commands.json ;\ + done + echo {'"'directory'"': '"'$(PWD)'"', '"'command'"': '"'$(CXXCMD) $^ -o $(BIN)'"', '"'file'"': '"'$(call llast,$^)'"'} >> compile_commands.json ;\ + echo ] >> compile_commands.json + +$(BIN): $(SRCS) + $(CXXCMD) $^ -o $@ + diff --git a/lib/glfw.hpp b/lib/glfw.hpp new file mode 100644 index 0000000..1bd96c5 --- /dev/null +++ b/lib/glfw.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include +#include +#include + +#include +#include + +namespace ls { + class GlfwState { + public: + GlfwState(); + + std::vector get_vulkan_exts(); + }; + class GlfwWindow { + private: + GLFWwindow* __window; + std::string __title; + public: + GlfwWindow(GlfwState* state, uint32_t x_size, uint32_t y_size, std::string title); + void show(); + void set_title(const std::string title); + GLFWwindow* __get_backing(); + VkResult __make_surface(VkInstance instance, VkSurfaceKHR* surface); + }; +} diff --git a/lib/vulkan.hpp b/lib/vulkan.hpp new file mode 100644 index 0000000..d2c294c --- /dev/null +++ b/lib/vulkan.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include +#include + +namespace ls { + class VulkanState; +} + +#include "window.hpp" + +namespace ls { + class VulkanState { + private: + VkInstance __instance; + public: + VulkanState(GlfwState* state); + + void __make_surface(GlfwWindow* win, VkSurfaceKHR* surf); + }; +} diff --git a/lib/window.hpp b/lib/window.hpp new file mode 100644 index 0000000..35f971e --- /dev/null +++ b/lib/window.hpp @@ -0,0 +1,23 @@ +#pragma once + +namespace ls { + class Window; +} + +#include "glfw.hpp" +#include "vulkan.hpp" + +namespace ls { + class Window { + public: + Window(GlfwState* glfw_state); + ~Window(); + void show(); + void set_title(const std::string title); + void init_surface(VulkanState* vulkan_state); + GlfwWindow* __get_native(); + private: + GlfwWindow* __window; + VkSurfaceKHR __surface; + }; +} diff --git a/src/glfw.cpp b/src/glfw.cpp new file mode 100644 index 0000000..c009c17 --- /dev/null +++ b/src/glfw.cpp @@ -0,0 +1,54 @@ +// NOTE: Order matters. +// Including glfw before Vulkan makes glfw not load Vulkan-specific functions +#include +#include + +#include +#include + +#include +#include + +#include "glfw.hpp" + +namespace ls { + // GlfwState + GlfwState::GlfwState() { + assert(glfwInit() == GLFW_TRUE); + }; + + std::vector GlfwState::get_vulkan_exts() { + std::vector res; + uint32_t glfw_exts_count = 0; + const char** glfw_exts = glfwGetRequiredInstanceExtensions(&glfw_exts_count); + for(int i = 0; i < glfw_exts_count; i++) { + res.push_back(glfw_exts[i]); + } + return res; + } + + // GlfwWindow + GlfwWindow::GlfwWindow(GlfwState* state, uint32_t x_size, uint32_t y_size, std::string title) { + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); + glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); + this->__window = glfwCreateWindow(x_size, y_size, title.c_str(), NULL, NULL); + }; + + void GlfwWindow::show() { + glfwShowWindow(__window); + } + + void GlfwWindow::set_title(const std::string title) { + this->__title = title; + glfwSetWindowTitle(this->__window, this->__title.c_str()); + } + + VkResult GlfwWindow::__make_surface(VkInstance instance, VkSurfaceKHR* surface) { + return glfwCreateWindowSurface(instance, this->__window, NULL, surface); + } + + GLFWwindow* GlfwWindow::__get_backing() { + return __window; + } +} diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..2e880fd --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,18 @@ +// Entry point. +// Should probably link the engine libs & run the editor but +// imma hack random shit together for now +#include "glfw.hpp" +#include "vulkan.hpp" +#include "window.hpp" + +using namespace ls; + +int main() { + auto glfw = GlfwState(); + auto vulkan = VulkanState(&glfw); + auto window = Window(&glfw); + window.set_title("LightningStrike Hello World!"); + window.init_surface(&vulkan); + window.show(); + while(1) {} +} diff --git a/src/vulkan.cpp b/src/vulkan.cpp new file mode 100644 index 0000000..b4372ba --- /dev/null +++ b/src/vulkan.cpp @@ -0,0 +1,39 @@ +#include +#include +#include + +#include "vulkan.hpp" + +#include "glfw.hpp" +#include "window.hpp" + +namespace ls { + VulkanState::VulkanState(GlfwState* state) { + VkApplicationInfo application_info = { + VK_STRUCTURE_TYPE_APPLICATION_INFO, + NULL, + "Game", + VK_MAKE_VERSION( 1, 0, 0 ), + "LightningStrike", + VK_MAKE_VERSION( 1, 0, 0 ), + VK_API_VERSION_1_3, + }; + std::vector vk_exts = state->get_vulkan_exts(); + VkInstanceCreateInfo instance_create_info = { + VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, + NULL, + 0, + &application_info, + 0, + NULL, + static_cast(vk_exts.size()), + vk_exts.data() + }; + VkResult res = vkCreateInstance(&instance_create_info, NULL, &__instance); + assert(res == VK_SUCCESS); + } + void VulkanState::__make_surface(GlfwWindow* win, VkSurfaceKHR* surf) { + VkResult res = win->__make_surface(__instance, surf); + assert(res == VK_SUCCESS); + } +} diff --git a/src/window.cpp b/src/window.cpp new file mode 100644 index 0000000..0cdc60e --- /dev/null +++ b/src/window.cpp @@ -0,0 +1,31 @@ +#include +#include +#include +#include + +#include "window.hpp" + +#include "glfw.hpp" +#include "vulkan.hpp" + +namespace ls { + Window::Window(GlfwState* glfw_state) { + this->__window = new GlfwWindow(glfw_state, 1920, 1080, ""); + }; + + Window::~Window() { + std::free(this->__window); + } + void Window::init_surface(VulkanState* vulkan_state) { + vulkan_state->__make_surface(__window, &__surface); + } + void Window::show() { + __window->show(); + } + void Window::set_title(const std::string title) { + __window->set_title(title); + } + GlfwWindow* Window::__get_native() { + return __window; + } +}