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 /main.cpp | |
| parent | 6d7e26adf0d090038cd2dae6c6e4fa93e55fc575 (diff) | |
building on win
Diffstat (limited to 'main.cpp')
| -rw-r--r-- | main.cpp | 152 |
1 files changed, 86 insertions, 66 deletions
@@ -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; } |
