From fa5f31e0342f66382adfe0f20d74a0a6e986c170 Mon Sep 17 00:00:00 2001 From: HappyTanuki Date: Mon, 31 Mar 2025 06:09:11 +0900 Subject: [PATCH] =?UTF-8?q?vulkan=20instance=20=EA=B9=8C=EC=A7=80=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 10 ++++-- include/glfw/glfw_initialization.h | 12 +++++++ include/glfw/glfw_monitor.h | 12 +++++++ include/glfw/glfw_window.h | 21 ++++++++++++ include/precomp.h | 8 +++++ include/vulkan/graphics.h | 20 ++++++++++++ src/glfw/glfw_initialization.cpp | 14 ++++++++ src/glfw/glfw_moniotr.cpp | 34 ++++++++++++++++++++ src/glfw/glfw_window.cpp | 39 +++++++++++++++++++++++ src/main.cpp | 30 ++++++++++-------- src/vulkan/graphics.cpp | 51 ++++++++++++++++++++++++++++++ 11 files changed, 234 insertions(+), 17 deletions(-) create mode 100644 include/glfw/glfw_initialization.h create mode 100644 include/glfw/glfw_monitor.h create mode 100644 include/glfw/glfw_window.h create mode 100644 include/precomp.h create mode 100644 include/vulkan/graphics.h create mode 100644 src/glfw/glfw_initialization.cpp create mode 100644 src/glfw/glfw_moniotr.cpp create mode 100644 src/glfw/glfw_window.cpp create mode 100644 src/vulkan/graphics.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c5ac51f..cc2a0fd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,8 +30,10 @@ FetchContent_Declare( FetchContent_MakeAvailable(GSL) file(GLOB_RECURSE VulkanEngineSources CONFIGURE_DEPENDS + "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp" - "${CMAKE_CURRENT_SOURCE_DIR}/src/*.h" + "${CMAKE_CURRENT_SOURCE_DIR}/src/glfw/*.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/src/vulkan/*.cpp" ) add_executable(VulkanEngine ${VulkanEngineSources}) @@ -40,5 +42,7 @@ target_link_libraries(VulkanEngine PRIVATE glm) target_link_libraries(VulkanEngine PRIVATE glfw) target_link_libraries(VulkanEngine PRIVATE Microsoft.GSL::GSL) -target_include_directories(VulkanEngine PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src") -target_compile_features(VulkanEngine PRIVATE cxx_std_20) \ No newline at end of file +target_include_directories(VulkanEngine PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include") +target_compile_features(VulkanEngine PRIVATE cxx_std_20) + +target_precompile_headers(VulkanEngine PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include/precomp.h") \ No newline at end of file diff --git a/include/glfw/glfw_initialization.h b/include/glfw/glfw_initialization.h new file mode 100644 index 0000000..ac8113e --- /dev/null +++ b/include/glfw/glfw_initialization.h @@ -0,0 +1,12 @@ +#pragma once + +namespace veng { +struct GlfwInitialization { + public: + GlfwInitialization(); + ~GlfwInitialization(); + + GlfwInitialization(const GlfwInitialization&) = delete; + GlfwInitialization& operator=(const GlfwInitialization&) = delete; +}; +} // namespace veng diff --git a/include/glfw/glfw_monitor.h b/include/glfw/glfw_monitor.h new file mode 100644 index 0000000..c8b9047 --- /dev/null +++ b/include/glfw/glfw_monitor.h @@ -0,0 +1,12 @@ +#pragma once + +struct GLFWmonitor; +struct GLFWwindow; + +namespace veng { +gsl::span GetMonitors(); +glm::ivec2 GetMonitorPosition(gsl::not_null monitor); +glm::ivec2 GetMonitorWorkAreaSize(gsl::not_null monitor); +void MoveWindowToMonitor(gsl::not_null window, + gsl::not_null monitor); +} // namespace veng diff --git a/include/glfw/glfw_window.h b/include/glfw/glfw_window.h new file mode 100644 index 0000000..a9cf285 --- /dev/null +++ b/include/glfw/glfw_window.h @@ -0,0 +1,21 @@ +#pragma once + +struct GLFWmonitor; +struct GLFWwindow; + +namespace veng { +class Window { + public: + Window(gsl::czstring name, glm::ivec2 size); + ~Window(); + + glm::ivec2 GetWindowSize() const; + bool ShouldClose() const; + GLFWwindow* GetHandle() const; + + bool TryMoveToMonitor(std::uint16_t monitor_number); + + private: + GLFWwindow* window_; +}; +} // namespace veng diff --git a/include/precomp.h b/include/precomp.h new file mode 100644 index 0000000..5c8e3bc --- /dev/null +++ b/include/precomp.h @@ -0,0 +1,8 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include diff --git a/include/vulkan/graphics.h b/include/vulkan/graphics.h new file mode 100644 index 0000000..08bc416 --- /dev/null +++ b/include/vulkan/graphics.h @@ -0,0 +1,20 @@ +#pragma once +#include +#include "glfw/glfw_window.h" + +namespace veng { +class Graphics final { + public: + Graphics(gsl::not_null window); + ~Graphics(); + + private: + void InitializeVulkan(); + void CreateInstance(); + + gsl::span GetSuggestedExtentions(); + + VkInstance instance_ = nullptr; + gsl::not_null window_; +}; +} // namespace veng diff --git a/src/glfw/glfw_initialization.cpp b/src/glfw/glfw_initialization.cpp new file mode 100644 index 0000000..e4dd0bd --- /dev/null +++ b/src/glfw/glfw_initialization.cpp @@ -0,0 +1,14 @@ +#include "glfw/glfw_initialization.h" + +#include + +#include + +#include "precomp.h" + +namespace veng { +GlfwInitialization::GlfwInitialization() { + if (glfwInit() != GLFW_TRUE) std::exit(EXIT_FAILURE); +} +GlfwInitialization::~GlfwInitialization() { glfwTerminate(); } +} // namespace veng diff --git a/src/glfw/glfw_moniotr.cpp b/src/glfw/glfw_moniotr.cpp new file mode 100644 index 0000000..7702b99 --- /dev/null +++ b/src/glfw/glfw_moniotr.cpp @@ -0,0 +1,34 @@ +#include + +#include "glfw/glfw_monitor.h" +#include "precomp.h" + +namespace veng { +gsl::span GetMonitors() { + std::int32_t monitor_count = 0; + GLFWmonitor **monitor_pointers = glfwGetMonitors(&monitor_count); + return gsl::span(monitor_pointers, monitor_count); +} + +glm::ivec2 GetMonitorPosition(gsl::not_null monitor) { + glm::ivec2 monitor_position; + glfwGetMonitorPos(monitor, &monitor_position.x, &monitor_position.y); + return monitor_position; +} +glm::ivec2 GetMonitorWorkAreaSize(gsl::not_null monitor) { + glm::ivec2 monitor_size; + glfwGetMonitorWorkarea(monitor, nullptr, nullptr, &monitor_size.x, + &monitor_size.y); + return monitor_size; +} + +void MoveWindowToMonitor(gsl::not_null window, + gsl::not_null monitor) { + glm::ivec2 window_size; + glfwGetWindowSize(window, &window_size.x, &window_size.y); + const glm::ivec2 window_new_position = GetMonitorPosition(monitor) + + (GetMonitorWorkAreaSize(monitor) / 2) - + (window_size / 2); + glfwSetWindowPos(window, window_new_position.x, window_new_position.y); +} +} // namespace veng diff --git a/src/glfw/glfw_window.cpp b/src/glfw/glfw_window.cpp new file mode 100644 index 0000000..24747ed --- /dev/null +++ b/src/glfw/glfw_window.cpp @@ -0,0 +1,39 @@ +#include + +#include "glfw/glfw_monitor.h" +#include "glfw/glfw_window.h" +#include "precomp.h" + +namespace veng { + +Window::Window(gsl::czstring name, glm::ivec2 size) { + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); + + window_ = glfwCreateWindow(size.x, size.y, name, nullptr, nullptr); + if (window_ == nullptr) std::exit(EXIT_FAILURE); +} + +Window::~Window() { glfwDestroyWindow(window_); } + +glm::ivec2 Window::GetWindowSize() const { + glm::ivec2 window_size; + glfwGetWindowSize(window_, &window_size.x, &window_size.y); + return window_size; +} + +bool Window::ShouldClose() const { return glfwWindowShouldClose(window_); } + +GLFWwindow* Window::GetHandle() const { return window_; } + +bool Window::TryMoveToMonitor(std::uint16_t monitor_number) { + gsl::span monitors = veng::GetMonitors(); + + if (monitor_number < monitors.size()) { + veng::MoveWindowToMonitor(window_, monitors[monitor_number]); + return true; + } + return false; +} + +} // namespace veng diff --git a/src/main.cpp b/src/main.cpp index 2aebc93..148b5f6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,19 +1,21 @@ #include -#include -#include -#include -#include -#include -#include -#include +#include "glfw/glfw_initialization.h" +#include "glfw/glfw_monitor.h" +#include "glfw/glfw_window.h" +#include "vulkan/graphics.h" -int main(std::int32_t argc, gsl::zstring* argv) { - gsl::span arguments(argv, argc); - std::sort(arguments.begin(), arguments.end()); +std::int32_t main(std::int32_t argc, gsl::zstring* argv) { + const veng::GlfwInitialization _glfw; - gsl::not_null arguments_pointer_validated = argv; + veng::Window window("Vulkan Engine", {800, 600}); + window.TryMoveToMonitor(0); - std::cout << "Hello, World!" << std::endl; - return 0; -} \ No newline at end of file + veng::Graphics graphics(&window); + + while (!window.ShouldClose()) { + glfwPollEvents(); + } + + return EXIT_SUCCESS; +} diff --git a/src/vulkan/graphics.cpp b/src/vulkan/graphics.cpp new file mode 100644 index 0000000..0f23e0e --- /dev/null +++ b/src/vulkan/graphics.cpp @@ -0,0 +1,51 @@ +#include "vulkan/graphics.h" + +#include + +#include "precomp.h" + +namespace veng { + +Graphics::Graphics(gsl::not_null window) : window_(window) { + InitializeVulkan(); +} + +Graphics::~Graphics() { + if (instance_ != nullptr) vkDestroyInstance(instance_, nullptr); +} + +void Graphics::InitializeVulkan() { CreateInstance(); } + +void Graphics::CreateInstance() { + gsl::span suggested_extentions = GetSuggestedExtentions(); + + VkApplicationInfo app_info = {}; + app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; + app_info.pNext = nullptr; + app_info.pApplicationName = "Udemy Course"; + app_info.applicationVersion = VK_MAKE_API_VERSION(0, 0, 0, 0); + app_info.pEngineName = "VEng"; + app_info.engineVersion = VK_MAKE_API_VERSION(0, 1, 0, 0); + app_info.apiVersion = VK_API_VERSION_1_0; + + VkInstanceCreateInfo instance_creation_info = {}; + instance_creation_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; + instance_creation_info.pNext = nullptr; + instance_creation_info.pApplicationInfo = &app_info; + instance_creation_info.enabledExtensionCount = suggested_extentions.size(); + instance_creation_info.ppEnabledExtensionNames = suggested_extentions.data(); + instance_creation_info.enabledLayerCount = 0; + + VkResult result = + vkCreateInstance(&instance_creation_info, nullptr, &instance_); + if (result != VK_SUCCESS) std::exit(EXIT_FAILURE); +} + +gsl::span Graphics::GetSuggestedExtentions() { + std::uint32_t glfw_extention_count = 0; + gsl::czstring* glfw_extentions = + glfwGetRequiredInstanceExtensions(&glfw_extention_count); + return {glfw_extentions, glfw_extention_count}; +} + +} // namespace veng