Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define GLFW_INCLUDE_VULKAN
- #include <GLFW/glfw3.h>
- #include <vulkan/vulkan.hpp>
- #include <iostream>
- #include <stdexcept>
- #include <cstdlib>
- #include <chrono>
- const uint32_t WIDTH = 800;
- const uint32_t HEIGHT = 600;
- // Validation Layers are used for debugging
- const std::vector<const char*> validationLayers =
- {
- "VK_LAYER_KHRONOS_validation"
- };
- #ifdef NDEBUG //Not Debug
- const bool enableValidationLayers = false;
- #else
- const bool enableValidationLayers = true;
- #endif
- class HelloTriangleApplication
- {
- public:
- void run()
- {
- initWindow(); //GLFW window init
- initVulkan();
- mainLoop();
- cleanup();
- }
- private:
- GLFWwindow* window;
- vk::UniqueInstance instance; //can be Instance or UniqueInstance for smart pointers instead of RAII
- void initWindow() //GLFW window init
- {
- glfwInit(); //Init glfw library
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); //Tell glfw to not use opengl context. Enabled by default.
- glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); //Disable window resizing. Needs explicit control.
- window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr); //Window context
- //std::this_thread::sleep_for (std::chrono::seconds(1));
- //glfwSetWindowPos(window, 100, 150); // Set window Position
- }
- void initVulkan()
- {
- createInstance();
- }
- void createInstance()
- {
- checkValidationLayerSupport();
- // Optional for InstanceCreateInfo
- vk::ApplicationInfo appInfo
- (
- "Hello Triangle", // Application Name
- VK_MAKE_VERSION(1, 0, 0), // Application Version
- "No Engine", // Engine Name
- VK_MAKE_VERSION(1, 0, 0), // Engine Version
- VK_API_VERSION_1_0 // API Version
- );
- uint32_t glfwExtensionCount = 0;
- const char** glfwExtensions;
- glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
- // Required for OS interaction
- vk::InstanceCreateInfo createInfo
- (
- vk::InstanceCreateFlags(), // Can be empty with {}, null or vk::InstanceCreateFlags()
- &appInfo, // AppInfo object we created
- 0, nullptr, // enabled layers. Size and Data
- glfwExtensionCount, glfwExtensions // enabled extensions. Size and Data
- );
- try
- {
- instance = vk::createInstanceUnique(createInfo, nullptr); //can be createInstance or createInstanceUnique for smart pointers instead of normal RAII
- }
- catch (vk::SystemError err)
- {
- throw std::runtime_error("failed to create instance!");
- }
- std::cout << "Available Extensions:\n";
- for (const auto& extension : vk::enumerateInstanceExtensionProperties())
- {
- std::cout << "\t" << extension.extensionName << std::endl;
- }
- }
- bool checkValidationLayerSupport()
- {
- std::vector<vk::LayerProperties> availableLayers = vk::enumerateInstanceLayerProperties();
- for(const char* layerName : validationLayers)
- {
- std::cout << "Selected Layer Names:\n" << "\t" << layerName << "\n";
- std::cout << "Available Layer Properties:\n";
- for (const auto& layerProperties : availableLayers)
- {
- std::cout << "\t" << layerProperties.layerName << "\n";
- }
- }
- return false;
- }
- void mainLoop()
- {
- uint32_t x{0}, y{0};
- while (!glfwWindowShouldClose(window))
- {
- glfwPollEvents();
- // Delete when needed
- #if (0)
- for(uint32_t i{0}; i < 1000 ; i++)
- {
- glfwSetWindowPos(window, x++, y++);
- glfwPollEvents();
- }
- for(uint32_t i{1000}; i > 0 ; i--)
- {
- glfwSetWindowPos(window, x--, y--);
- glfwPollEvents();
- }
- #endif
- }
- }
- void cleanup()
- {
- //vkDestroyInstance(instance, nullptr);
- glfwDestroyWindow(window);
- glfwTerminate();
- }
- };
- int main() {
- HelloTriangleApplication app;
- try
- {
- app.run();
- }
- catch (const std::exception& e)
- {
- std::cerr << e.what() << std::endl;
- return EXIT_FAILURE;
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment