|
1 | 1 | #include <algorithm> |
2 | | -#include <iostream> |
3 | | -#include <stdexcept> |
4 | 2 | #include <cstdlib> |
| 3 | +#include <iostream> |
5 | 4 | #include <memory> |
| 5 | +#include <stdexcept> |
6 | 6 |
|
7 | 7 | #ifdef __INTELLISENSE__ |
8 | | -#include <vulkan/vulkan_raii.hpp> |
| 8 | +# include <vulkan/vulkan_raii.hpp> |
9 | 9 | #else |
10 | 10 | import vulkan_hpp; |
11 | 11 | #endif |
12 | 12 |
|
13 | 13 | #include <vulkan/vk_platform.h> |
14 | 14 |
|
15 | | -#define GLFW_INCLUDE_VULKAN // REQUIRED only for GLFW CreateWindowSurface. |
| 15 | +#define GLFW_INCLUDE_VULKAN // REQUIRED only for GLFW CreateWindowSurface. |
16 | 16 | #include <GLFW/glfw3.h> |
17 | 17 |
|
18 | | -constexpr uint32_t WIDTH = 800; |
| 18 | +constexpr uint32_t WIDTH = 800; |
19 | 19 | constexpr uint32_t HEIGHT = 600; |
20 | 20 |
|
21 | | -class HelloTriangleApplication { |
22 | | -public: |
23 | | - void run() { |
24 | | - initWindow(); |
25 | | - initVulkan(); |
26 | | - mainLoop(); |
27 | | - cleanup(); |
28 | | - } |
29 | | - |
30 | | -private: |
31 | | - GLFWwindow* window = nullptr; |
32 | | - |
33 | | - vk::raii::Context context; |
34 | | - vk::raii::Instance instance = nullptr; |
35 | | - |
36 | | - void initWindow() { |
37 | | - glfwInit(); |
38 | | - |
39 | | - glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); |
40 | | - glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); |
41 | | - |
42 | | - window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr); |
43 | | - } |
44 | | - |
45 | | - void initVulkan() { |
46 | | - createInstance(); |
47 | | - } |
48 | | - |
49 | | - void mainLoop() { |
50 | | - while (!glfwWindowShouldClose(window)) { |
51 | | - glfwPollEvents(); |
52 | | - } |
53 | | - } |
54 | | - |
55 | | - void cleanup() { |
56 | | - glfwDestroyWindow(window); |
57 | | - |
58 | | - glfwTerminate(); |
59 | | - } |
60 | | - |
61 | | - void createInstance() { |
62 | | - constexpr vk::ApplicationInfo appInfo{ .pApplicationName = "Hello Triangle", |
63 | | - .applicationVersion = VK_MAKE_VERSION( 1, 0, 0 ), |
64 | | - .pEngineName = "No Engine", |
65 | | - .engineVersion = VK_MAKE_VERSION( 1, 0, 0 ), |
66 | | - .apiVersion = vk::ApiVersion14 }; |
67 | | - |
68 | | - // Get the required instance extensions from GLFW. |
69 | | - uint32_t glfwExtensionCount = 0; |
70 | | - auto glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount); |
71 | | - |
72 | | - // Check if the required GLFW extensions are supported by the Vulkan implementation. |
73 | | - auto extensionProperties = context.enumerateInstanceExtensionProperties(); |
74 | | - for (uint32_t i = 0; i < glfwExtensionCount; ++i) |
75 | | - { |
76 | | - if (std::ranges::none_of(extensionProperties, |
77 | | - [glfwExtension = glfwExtensions[i]](auto const& extensionProperty) |
78 | | - { return strcmp(extensionProperty.extensionName, glfwExtension) == 0; })) |
79 | | - { |
80 | | - throw std::runtime_error("Required GLFW extension not supported: " + std::string(glfwExtensions[i])); |
81 | | - } |
82 | | - } |
83 | | - |
84 | | - vk::InstanceCreateInfo createInfo{ |
85 | | - .pApplicationInfo = &appInfo, |
86 | | - .enabledExtensionCount = glfwExtensionCount, |
87 | | - .ppEnabledExtensionNames = glfwExtensions}; |
88 | | - instance = vk::raii::Instance(context, createInfo); |
89 | | - } |
| 21 | +class HelloTriangleApplication |
| 22 | +{ |
| 23 | + public: |
| 24 | + void run() |
| 25 | + { |
| 26 | + initWindow(); |
| 27 | + initVulkan(); |
| 28 | + mainLoop(); |
| 29 | + cleanup(); |
| 30 | + } |
| 31 | + |
| 32 | + private: |
| 33 | + GLFWwindow *window = nullptr; |
| 34 | + |
| 35 | + vk::raii::Context context; |
| 36 | + vk::raii::Instance instance = nullptr; |
| 37 | + |
| 38 | + void initWindow() |
| 39 | + { |
| 40 | + glfwInit(); |
| 41 | + |
| 42 | + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); |
| 43 | + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); |
| 44 | + |
| 45 | + window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr); |
| 46 | + } |
| 47 | + |
| 48 | + void initVulkan() |
| 49 | + { |
| 50 | + createInstance(); |
| 51 | + } |
| 52 | + |
| 53 | + void mainLoop() |
| 54 | + { |
| 55 | + while (!glfwWindowShouldClose(window)) |
| 56 | + { |
| 57 | + glfwPollEvents(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + void cleanup() |
| 62 | + { |
| 63 | + glfwDestroyWindow(window); |
| 64 | + |
| 65 | + glfwTerminate(); |
| 66 | + } |
| 67 | + |
| 68 | + void createInstance() |
| 69 | + { |
| 70 | + constexpr vk::ApplicationInfo appInfo{.pApplicationName = "Hello Triangle", |
| 71 | + .applicationVersion = VK_MAKE_VERSION(1, 0, 0), |
| 72 | + .pEngineName = "No Engine", |
| 73 | + .engineVersion = VK_MAKE_VERSION(1, 0, 0), |
| 74 | + .apiVersion = vk::ApiVersion14}; |
| 75 | + |
| 76 | + // Get the required instance extensions from GLFW. |
| 77 | + uint32_t glfwExtensionCount = 0; |
| 78 | + auto glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount); |
| 79 | + |
| 80 | + // Check if the required GLFW extensions are supported by the Vulkan implementation. |
| 81 | + auto extensionProperties = context.enumerateInstanceExtensionProperties(); |
| 82 | + for (uint32_t i = 0; i < glfwExtensionCount; ++i) |
| 83 | + { |
| 84 | + if (std::ranges::none_of(extensionProperties, |
| 85 | + [glfwExtension = glfwExtensions[i]](auto const &extensionProperty) { return strcmp(extensionProperty.extensionName, glfwExtension) == 0; })) |
| 86 | + { |
| 87 | + throw std::runtime_error("Required GLFW extension not supported: " + std::string(glfwExtensions[i])); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + vk::InstanceCreateInfo createInfo{ |
| 92 | + .pApplicationInfo = &appInfo, |
| 93 | + .enabledExtensionCount = glfwExtensionCount, |
| 94 | + .ppEnabledExtensionNames = glfwExtensions}; |
| 95 | + instance = vk::raii::Instance(context, createInfo); |
| 96 | + } |
90 | 97 | }; |
91 | 98 |
|
92 | | -int main() { |
93 | | - try { |
94 | | - HelloTriangleApplication app; |
95 | | - app.run(); |
96 | | - } catch (const std::exception& e) { |
97 | | - std::cerr << e.what() << std::endl; |
98 | | - return EXIT_FAILURE; |
99 | | - } |
100 | | - |
101 | | - return EXIT_SUCCESS; |
| 99 | +int main() |
| 100 | +{ |
| 101 | + try |
| 102 | + { |
| 103 | + HelloTriangleApplication app; |
| 104 | + app.run(); |
| 105 | + } |
| 106 | + catch (const std::exception &e) |
| 107 | + { |
| 108 | + std::cerr << e.what() << std::endl; |
| 109 | + return EXIT_FAILURE; |
| 110 | + } |
| 111 | + |
| 112 | + return EXIT_SUCCESS; |
102 | 113 | } |
0 commit comments