From dccbcf3cca64e57313581040401e483d7a2443d9 Mon Sep 17 00:00:00 2001 From: Jimmy Tremblay-Bernier Date: Fri, 22 Nov 2024 06:49:48 -0500 Subject: [PATCH] CONGE-2 : (WIP) adding the basic structure for the engine --- ConjureEngine/CMakeLists.txt | 22 +++- .../src/ConjureEngine/Application.cpp | 8 ++ ConjureEngine/src/ConjureEngine/Application.h | 13 ++ .../src/ConjureEngine/ConjureEngine.cpp | 10 -- ConjureEngine/src/ConjureEngine/Engine.cpp | 4 + .../{ConjureEngine.h => Engine.h} | 3 +- ConjureEngine/src/ConjureEngine/PCH.h | 21 ++++ ConjureEngine/src/ConjureEngine/Window.cpp | 119 ++++++++++++++++++ ConjureEngine/src/ConjureEngine/Window.h | 29 +++++ Demo1/src/main.cpp | 3 +- 10 files changed, 215 insertions(+), 17 deletions(-) create mode 100644 ConjureEngine/src/ConjureEngine/Application.cpp create mode 100644 ConjureEngine/src/ConjureEngine/Application.h delete mode 100644 ConjureEngine/src/ConjureEngine/ConjureEngine.cpp create mode 100644 ConjureEngine/src/ConjureEngine/Engine.cpp rename ConjureEngine/src/ConjureEngine/{ConjureEngine.h => Engine.h} (58%) create mode 100644 ConjureEngine/src/ConjureEngine/PCH.h create mode 100644 ConjureEngine/src/ConjureEngine/Window.cpp create mode 100644 ConjureEngine/src/ConjureEngine/Window.h diff --git a/ConjureEngine/CMakeLists.txt b/ConjureEngine/CMakeLists.txt index 100cb66..813d541 100644 --- a/ConjureEngine/CMakeLists.txt +++ b/ConjureEngine/CMakeLists.txt @@ -1,19 +1,35 @@ cmake_minimum_required(VERSION 3.26) - set(CMAKE_CXX_STANDARD 20) project(ConjureEngine) +set(HEADER_FILES + src/ConjureEngine/Application.h + src/ConjureEngine/Engine.h + src/ConjureEngine/Window.h +) + +set(SOURCES_FILES + src/ConjureEngine/Application.cpp + src/ConjureEngine/Engine.cpp + src/ConjureEngine/Window.cpp +) find_package(glm REQUIRED) find_package(SDL2 REQUIRED) find_package(Vulkan REQUIRED) -add_library(${PROJECT_NAME} STATIC src/ConjureEngine/ConjureEngine.h src/ConjureEngine/ConjureEngine.cpp) +add_library(${PROJECT_NAME} STATIC ${HEADER_FILES} ${SOURCES_FILES}) # Specify include directories target_include_directories(${PROJECT_NAME} PUBLIC include) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_BINARY_DIR}/include) -target_link_libraries(${PROJECT_NAME} SDL2::SDL2 glm::glm) \ No newline at end of file +target_link_libraries(${PROJECT_NAME} SDL2::SDL2 glm::glm Vulkan::Vulkan) + +target_precompile_headers( + ${PROJECT_NAME} + PUBLIC + src/ConjureEngine/PCH.h +) \ No newline at end of file diff --git a/ConjureEngine/src/ConjureEngine/Application.cpp b/ConjureEngine/src/ConjureEngine/Application.cpp new file mode 100644 index 0000000..e0417f4 --- /dev/null +++ b/ConjureEngine/src/ConjureEngine/Application.cpp @@ -0,0 +1,8 @@ +// +// Created by calap on 11/21/2024. +// + +#include "Application.h" + +namespace ConjureEngine { +} // ConjureEngine \ No newline at end of file diff --git a/ConjureEngine/src/ConjureEngine/Application.h b/ConjureEngine/src/ConjureEngine/Application.h new file mode 100644 index 0000000..161eb0e --- /dev/null +++ b/ConjureEngine/src/ConjureEngine/Application.h @@ -0,0 +1,13 @@ +// +// Created by calap on 11/21/2024. +// + +#pragma once + +namespace ConjureEngine { + +class Application { + +}; + +} // ConjureEngine \ No newline at end of file diff --git a/ConjureEngine/src/ConjureEngine/ConjureEngine.cpp b/ConjureEngine/src/ConjureEngine/ConjureEngine.cpp deleted file mode 100644 index 107dd0c..0000000 --- a/ConjureEngine/src/ConjureEngine/ConjureEngine.cpp +++ /dev/null @@ -1,10 +0,0 @@ -// -// Created by Jimmy Tremblay-bernier on 2024-11-14. -// -#include "ConjureEngine.h" -#include - -void ConjureEngine::SayHello() -{ - printf("Hello World\n"); -} \ No newline at end of file diff --git a/ConjureEngine/src/ConjureEngine/Engine.cpp b/ConjureEngine/src/ConjureEngine/Engine.cpp new file mode 100644 index 0000000..693210c --- /dev/null +++ b/ConjureEngine/src/ConjureEngine/Engine.cpp @@ -0,0 +1,4 @@ +// +// Created by Jimmy Tremblay-bernier on 2024-11-14. +// +#include "Engine.h" diff --git a/ConjureEngine/src/ConjureEngine/ConjureEngine.h b/ConjureEngine/src/ConjureEngine/Engine.h similarity index 58% rename from ConjureEngine/src/ConjureEngine/ConjureEngine.h rename to ConjureEngine/src/ConjureEngine/Engine.h index a076112..8ff00e5 100644 --- a/ConjureEngine/src/ConjureEngine/ConjureEngine.h +++ b/ConjureEngine/src/ConjureEngine/Engine.h @@ -3,6 +3,5 @@ #include "glm/glm.hpp" namespace ConjureEngine { - void SayHello(); - glm::vec3& Forward(); + } diff --git a/ConjureEngine/src/ConjureEngine/PCH.h b/ConjureEngine/src/ConjureEngine/PCH.h new file mode 100644 index 0000000..f1f8d61 --- /dev/null +++ b/ConjureEngine/src/ConjureEngine/PCH.h @@ -0,0 +1,21 @@ +// +// Created by calap on 11/21/2024. +// + +#pragma once + +// STANDARD LIBS +#include +#include +#include + +// GLM +#include "glm/glm.hpp" + +// SDL2 +#include "SDL2/SDL.h" +#include "SDL2/SDL_vulkan.h" +#include "SDL2/SDL_video.h" + +// VULKAN +#include "Vulkan/vulkan.h" \ No newline at end of file diff --git a/ConjureEngine/src/ConjureEngine/Window.cpp b/ConjureEngine/src/ConjureEngine/Window.cpp new file mode 100644 index 0000000..0dde45f --- /dev/null +++ b/ConjureEngine/src/ConjureEngine/Window.cpp @@ -0,0 +1,119 @@ +// +// Created by calap on 11/21/2024. +// +#include "Window.h" + +namespace ConjureEngine { + + Window::Window(const WindowInfo& windowInfo) + { + SDL_Init(SDL_INIT_VIDEO); + SDL_Vulkan_LoadLibrary(nullptr); + m_window = std::shared_ptr( + SDL_CreateWindow(windowInfo.title.c_str(), 0, 0, windowInfo.width, windowInfo.height, SDL_WINDOW_SHOWN |SDL_WINDOW_VULKAN) + ); + + + SDL_Vulkan_GetInstanceExtensions(m_window.get(), &m_extensionCount, nullptr); + m_extensionNames = new const char *[m_extensionCount]; + SDL_Vulkan_GetInstanceExtensions(m_window.get(), &m_extensionCount, m_extensionNames); + const VkInstanceCreateInfo instInfo = { + VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType + nullptr, // pNext + 0, // flags + nullptr, // pApplicationInfo + 0, // enabledLayerCount + nullptr, // ppEnabledLayerNames + m_extensionCount, // enabledExtensionCount + m_extensionNames, // ppEnabledExtensionNames + }; + vkCreateInstance(&instInfo, nullptr, &m_vkInst); + + vkEnumeratePhysicalDevices(m_vkInst, &m_physicalDeviceCount, nullptr); + m_physicalDevices = std::vector(m_physicalDeviceCount); + + vkEnumeratePhysicalDevices(m_vkInst, &m_physicalDeviceCount, m_physicalDevices.data()); + + // TODO - I'M AT THIS POINT + VkPhysicalDevice physicalDevice = m_physicalDevices[0]; + + uint32_t queueFamilyCount; + vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, nullptr); + std::vector queueFamilies(queueFamilyCount); + vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, queueFamilies.data()); + + VkSurfaceKHR surface; + SDL_Vulkan_CreateSurface(window, vkInst, &surface); + + uint32_t graphicsQueueIndex = UINT32_MAX; + uint32_t presentQueueIndex = UINT32_MAX; + VkBool32 support; + uint32_t i = 0; + for (VkQueueFamilyProperties queueFamily : queueFamilies) { + if (graphicsQueueIndex == UINT32_MAX && queueFamily.queueCount > 0 && queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) + graphicsQueueIndex = i; + if (presentQueueIndex == UINT32_MAX) { + vkGetPhysicalDeviceSurfaceSupportKHR(physicalDevice, i, surface, &support); + if(support) + presentQueueIndex = i; + } + ++i; + } + + float queuePriority = 1.0f; + VkDeviceQueueCreateInfo queueInfo = { + VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType + nullptr, // pNext + 0, // flags + graphicsQueueIndex, // graphicsQueueIndex + 1, // queueCount + &queuePriority, // pQueuePriorities + }; + + VkPhysicalDeviceFeatures deviceFeatures = {}; + const char* deviceExtensionNames[] = {VK_KHR_SWAPCHAIN_EXTENSION_NAME}; + VkDeviceCreateInfo createInfo = { + VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType + nullptr, // pNext + 0, // flags + 1, // queueCreateInfoCount + &queueInfo, // pQueueCreateInfos + 0, // enabledLayerCount + nullptr, // ppEnabledLayerNames + 1, // enabledExtensionCount + deviceExtensionNames, // ppEnabledExtensionNames + &deviceFeatures, // pEnabledFeatures + }; + VkDevice device; + vkCreateDevice(physicalDevice, &createInfo, nullptr, &device); + + VkQueue graphicsQueue; + vkGetDeviceQueue(device, graphicsQueueIndex, 0, &graphicsQueue); + + VkQueue presentQueue; + vkGetDeviceQueue(device, presentQueueIndex, 0, &presentQueue); + + SDL_Log("Initialized with errors: %s", SDL_GetError()); + + bool running = true; + while(running) { + SDL_Event windowEvent; + while(SDL_PollEvent(&windowEvent)) + if(windowEvent.type == SDL_QUIT) { + running = false; + break; + } + } + + vkDestroyDevice(device, nullptr); + vkDestroyInstance(vkInst, nullptr); + SDL_DestroyWindow(window); + SDL_Vulkan_UnloadLibrary(); + SDL_Quit(); + + SDL_Log("Cleaned up with errors: %s", SDL_GetError()); + + return 0; + } + +} // ConjureEngine \ No newline at end of file diff --git a/ConjureEngine/src/ConjureEngine/Window.h b/ConjureEngine/src/ConjureEngine/Window.h new file mode 100644 index 0000000..ebc1b7e --- /dev/null +++ b/ConjureEngine/src/ConjureEngine/Window.h @@ -0,0 +1,29 @@ +// +// Created by calap on 11/21/2024. +// + +#pragma once + +namespace ConjureEngine { + +struct WindowInfo { + std::string title; + int width; + int height; +}; + +class Window { + public: + explicit Window(const WindowInfo& windowInfo); + private: + public: + private: + std::shared_ptr m_window; + uint32_t m_extensionCount{0}; + const char** m_extensionNames{nullptr}; + VkInstance m_vkInst; + uint32_t m_physicalDeviceCount{0}; + std::vector m_physicalDevices; +}; + +} // ConjureEngine \ No newline at end of file diff --git a/Demo1/src/main.cpp b/Demo1/src/main.cpp index 5668f0f..1acdc2d 100644 --- a/Demo1/src/main.cpp +++ b/Demo1/src/main.cpp @@ -1,6 +1,5 @@ -#include "ConjureEngine/ConjureEngine.h" +#include "ConjureEngine/Engine.h" int main(int argc, char** argv) { - ConjureEngine::SayHello(); return 0; } \ No newline at end of file