From da91ed9188c4ab92fb1b3114ba3c92e45f0587bf Mon Sep 17 00:00:00 2001
From: Sebastian Gaida <gaida@ca-digit.com>
Date: Thu, 20 May 2021 15:19:32 +0200
Subject: [PATCH] [#14] add event boilerplate for swapchain recreation

added event boilerplate for swapchain recreation
needed to change window in create and constructor to not const
---
 include/vkcv/Core.hpp | 12 ++++++++++--
 src/vkcv/Core.cpp     | 13 +++++++++++--
 2 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/include/vkcv/Core.hpp b/include/vkcv/Core.hpp
index 45e55515..065c21d2 100644
--- a/include/vkcv/Core.hpp
+++ b/include/vkcv/Core.hpp
@@ -33,7 +33,7 @@ namespace vkcv
          *
          * @param context encapsulates various Vulkan objects
          */
-        Core(Context &&context, const Window &window, SwapChain swapChain,  std::vector<vk::ImageView> imageViews, 
+        Core(Context &&context, Window &window, SwapChain swapChain,  std::vector<vk::ImageView> imageViews,
 			const CommandResources& commandResources, const SyncResources& syncResources) noexcept;
         // explicit destruction of default constructor
         Core() = delete;
@@ -53,6 +53,14 @@ namespace vkcv
 		SyncResources m_SyncResources;
 		uint32_t m_currentSwapchainImageIndex;
 		std::vector<vk::Framebuffer> m_TemporaryFramebuffers;
+
+        /**
+         * recreates the swapchain
+         * @param[in] width new window width
+         * @param[in] height new window hight
+         */
+        static void recreateSwapchain(int width, int height);
+
     public:
         /**
          * Destructor of #Core destroys the Vulkan objects contained in the core's context.
@@ -107,7 +115,7 @@ namespace vkcv
              * @param[in] deviceExtensions (optional) Requested device extensions
              * @return New instance of #Context
              */
-        static Core create(const Window &window,
+        static Core create(Window &window,
                            const char *applicationName,
                            uint32_t applicationVersion,
                            std::vector<vk::QueueFlagBits> queueFlags    = {},
diff --git a/src/vkcv/Core.cpp b/src/vkcv/Core.cpp
index 5b019147..48871511 100644
--- a/src/vkcv/Core.cpp
+++ b/src/vkcv/Core.cpp
@@ -16,7 +16,7 @@
 namespace vkcv
 {
 
-    Core Core::create(const Window &window,
+    Core Core::create(Window &window,
                       const char *applicationName,
                       uint32_t applicationVersion,
                       std::vector<vk::QueueFlagBits> queueFlags,
@@ -70,6 +70,10 @@ namespace vkcv
 		const auto defaultCommandResources = createDefaultCommandResources(context.getDevice(), graphicQueueFamilyIndex);
 		const auto defaultSyncResources = createDefaultSyncResources(context.getDevice());
 
+        window.e_resize.add([&](int width, int height){
+            recreateSwapchain(width,height);
+        });
+
         return Core(std::move(context) , window, swapChain, imageViews, defaultCommandResources, defaultSyncResources);
     }
 
@@ -78,7 +82,7 @@ namespace vkcv
         return m_Context;
     }
 
-	Core::Core(Context &&context, const Window &window , SwapChain swapChain,  std::vector<vk::ImageView> imageViews, 
+	Core::Core(Context &&context, Window &window , SwapChain swapChain,  std::vector<vk::ImageView> imageViews,
 		const CommandResources& commandResources, const SyncResources& syncResources) noexcept :
             m_Context(std::move(context)),
             m_window(window),
@@ -213,4 +217,9 @@ namespace vkcv
 	vk::Format Core::getSwapchainImageFormat() {
 		return m_swapchain.getSurfaceFormat().format;
 	}
+
+    void Core::recreateSwapchain(int width, int height) {
+        /* boilerplate for #34 */
+        std::cout << "Resized to : " << width << " , " << height << std::endl;
+    }
 }
-- 
GitLab