diff options
| author | Miguel <m.i@gmx.at> | 2025-01-09 12:39:13 +0100 |
|---|---|---|
| committer | Miguel <m.i@gmx.at> | 2025-01-09 12:39:13 +0100 |
| commit | ef4538478075dada9161c1f0175ae7409f977603 (patch) | |
| tree | 7783898d21161c0c8b2ef64cb24d1492e846d385 | |
| parent | 6d7e26adf0d090038cd2dae6c6e4fa93e55fc575 (diff) | |
building on win
| -rw-r--r-- | build.bat | 28 | ||||
| -rw-r--r-- | main.cpp | 152 | ||||
| -rw-r--r-- | test.cpp | 81 |
3 files changed, 195 insertions, 66 deletions
diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..d3d9e06 --- /dev/null +++ b/build.bat @@ -0,0 +1,28 @@ +REM
+
+call "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" x64
+
+echo COMPILING
+
+cl /EHsc /nologo /W4 /std:c++17 /c ^
+ /I"C:\VulkanSDK\1.3.296.0\Include" ^
+ /I"C:\Users\idzio\Desktop\c++\glm-1.0.1-light" ^
+ /I"C:\Users\idzio\Desktop\c++\glfw-3.4.bin.WIN64\glfw-3.4.bin.WIN64\include" ^
+ "\\wsl$\Ubuntu\home\miguel\git\vulkan\main.cpp" ^
+ /Fo"\\wsl$\Ubuntu\home\miguel\git\vulkan\main.out"
+
+echo LINKING
+
+cl /nologo ^
+ "\\wsl$\Ubuntu\home\miguel\git\vulkan\main.out" ^
+ /link ^
+ "C:\VulkanSDK\1.3.296.0\Lib\vulkan-1.lib" ^
+ "C:\Users\idzio\Desktop\c++\glfw-3.4.bin.WIN64\glfw-3.4.bin.WIN64\lib-vc2022\glfw3_mt.lib" ^
+ user32.lib ^
+ gdi32.lib ^
+ shell32.lib ^
+ /OUT:"\\wsl$\Ubuntu\home\miguel\git\vulkan\main.exe"
+
+echo FIN
+
+pause
\ No newline at end of file @@ -14,40 +14,16 @@ #include <limits> #include <algorithm> #include <fstream> - -#ifdef NDEBUG - const bool enableValidationLayers = false; -#else - const bool enableValidationLayers = true; -#endif - -const uint32_t WIDTH = 800; -const uint32_t HEIGHT = 600; - -const std::vector<const char*> validationLayers = { - "VK_LAYER_KHRONOS_validation" -}; - -static std::vector<char> readFile(const std::string& filename) { - std::ifstream file(filename, std::ios::ate | std::ios::binary); - - if (!file.is_open()) { - throw std::runtime_error("failed to open file!"); - } - - size_t fileSize = (size_t) file.tellg(); - std::vector<char> buffer(fileSize); - file.seekg(0); - file.read(buffer.data(), fileSize); - file.close(); - return buffer; -} +#include <functional> class HelloTriangleApplication { public: void run() { + + std::cout << "enableValidationLayers = " << enableValidationLayers << std::endl; + initWindow(); initVulkan(); mainLoop(); @@ -56,11 +32,38 @@ public: private: + #ifdef NDEBUG + const bool enableValidationLayers = false; + #else + const bool enableValidationLayers = true; + #endif + + const uint32_t WIDTH = 800; + const uint32_t HEIGHT = 600; + const std::vector<const char*> deviceExtensions = { VK_KHR_SWAPCHAIN_EXTENSION_NAME }; + const std::vector<const char*> validationLayers = { + "VK_LAYER_KHRONOS_validation" + }; + + static std::vector<char> readFile(const std::string& filename) { + std::ifstream file(filename, std::ios::ate | std::ios::binary); + if (!file.is_open()) throw std::runtime_error("failed to open file!"); + size_t fileSize = (size_t) file.tellg(); + std::vector<char> buffer(fileSize); + file.seekg(0); + file.read(buffer.data(), fileSize); + file.close(); + return buffer; + } + + bool quit; + GLFWwindow* window; + VkInstance instance; VkDebugUtilsMessengerEXT debugMessenger; VkSurfaceKHR surface; @@ -92,6 +95,23 @@ private: VkSemaphore renderFinishedSemaphore; VkFence inFlightFence; + void initWindow() { + glfwInit(); + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); // no OpenGL + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); // no resizing + window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr); + glfwSetWindowUserPointer(window, this); + glfwSetKeyCallback(window, key_callback); + } + + static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) + { + if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) { + std::cout << "exit" << std::endl; + static_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window))->quit = true; + } + } + void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo) { createInfo = {}; createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT; @@ -111,13 +131,7 @@ private: return VK_FALSE; } - void initWindow() { - std::cout << enableValidationLayers << std::endl; - glfwInit(); - glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); - glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); - window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr); - } + void initVulkan() { createInstance(); // 01 Setup -> Instance @@ -654,12 +668,7 @@ private: } } - void mainLoop() { - while (!glfwWindowShouldClose(window)) { - glfwPollEvents(); - drawFrame(); // 14 Drawing -> Rendering - } - } + void drawFrame() { @@ -704,31 +713,6 @@ private: vkQueuePresentKHR(presentQueue, &presentInfo); } - void cleanup() { - vkDestroySemaphore(device, imageAvailableSemaphore, nullptr); - vkDestroySemaphore(device, renderFinishedSemaphore, nullptr); - vkDestroyFence(device, inFlightFence, nullptr); - vkDestroyCommandPool(device, commandPool, nullptr); - for (auto framebuffer : swapChainFramebuffers) { - vkDestroyFramebuffer(device, framebuffer, nullptr); - } - vkDestroyPipeline(device, graphicsPipeline, nullptr); - vkDestroyPipelineLayout(device, pipelineLayout, nullptr); - vkDestroyRenderPass(device, renderPass, nullptr); - for (auto imageView : swapChainImageViews) { - vkDestroyImageView(device, imageView, nullptr); - } - vkDestroySwapchainKHR(device, swapChain, nullptr); - vkDestroyDevice(device, nullptr); - if (enableValidationLayers) { - DestroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr); - } - vkDestroySurfaceKHR(instance, surface, nullptr); - vkDestroyInstance(instance, nullptr); - glfwDestroyWindow(window); - glfwTerminate(); - } - void createInstance() { if (enableValidationLayers && !checkValidationLayerSupport()) { throw std::runtime_error("validation layers requested, but not available!"); @@ -942,6 +926,41 @@ private: } } + void mainLoop() { + std::cout << "Enter Main Loop" << std::endl; + while (!glfwWindowShouldClose(window) && !quit) { + glfwPollEvents(); + drawFrame(); // 14 Drawing -> Rendering + } + vkDeviceWaitIdle(device); + std::cout << "Exit Main Loop" << std::endl; + } + + void cleanup() { + vkDestroySemaphore(device, imageAvailableSemaphore, nullptr); + vkDestroySemaphore(device, renderFinishedSemaphore, nullptr); + vkDestroyFence(device, inFlightFence, nullptr); + vkDestroyCommandPool(device, commandPool, nullptr); + for (auto framebuffer : swapChainFramebuffers) { + vkDestroyFramebuffer(device, framebuffer, nullptr); + } + vkDestroyPipeline(device, graphicsPipeline, nullptr); + vkDestroyPipelineLayout(device, pipelineLayout, nullptr); + vkDestroyRenderPass(device, renderPass, nullptr); + for (auto imageView : swapChainImageViews) { + vkDestroyImageView(device, imageView, nullptr); + } + vkDestroySwapchainKHR(device, swapChain, nullptr); + vkDestroyDevice(device, nullptr); + if (enableValidationLayers) { + DestroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr); + } + vkDestroySurfaceKHR(instance, surface, nullptr); + vkDestroyInstance(instance, nullptr); + glfwDestroyWindow(window); + glfwTerminate(); + } + }; int main() { @@ -950,6 +969,7 @@ int main() { try { app.run(); } catch (const std::exception& e) { + std::cout << "Exception" << std::endl; std::cerr << e.what() << std::endl; return EXIT_FAILURE; } diff --git a/test.cpp b/test.cpp new file mode 100644 index 0000000..b987af9 --- /dev/null +++ b/test.cpp @@ -0,0 +1,81 @@ +// Some stupid experiments as I forgot C++ + +#include <cstdlib> +#include <iostream> +#include <string> +#include <utility> +#include <initializer_list> + +class C1 { + +public: + + C1() : name{"default"} { + std::cout << "C1 default cons: " << name << std::endl; + } + + C1(const std::string& str) : name{str} { + std::cout << "C1 cons: " << name << std::endl; + } + + C1(std::initializer_list<std::string> list) { + for (std::string val : list) name+=val; + std::cout << "C1 initializer_list cons: " << name << std::endl; + } + + ~C1() { + std::cout << "C1 decons: " << name << std::endl; + } + + C1(C1&& other) : name{other.name} { + std::cout << "C1 move constructor: " << name << std::endl; + other.name+=" (moved away)"; + } + + C1& operator=(C1&& other) { + std::cout << "C1 move called: " << other.name << " -> " << name << std::endl; + name = other.name; + other.name+=" (moved away)"; + return *this; + } + + // Disable copy and copy-assign + C1(const C1&) = delete; + C1& operator=(const C1&) = delete; + + void rename (const std::string &str){ + name = str; + } + + void say() { + std::cout << "C1 says: " << name << std::endl; + } + +private: + + std::string name; + +}; + + +int main() { + + std::cout << "Miguel's C++ Tests" << std::endl; + + C1 a ("x"); + C1 b (std::move(a)); + + std::cout << "--" << std::endl; + a.say(); + b.say(); + a.rename("y"); + a.say(); + b.say(); + std::cout << "--" << std::endl; + + C1 c{"c","d"}; + c.say(); + + return EXIT_SUCCESS; + +} |
