Skip to content
Snippets Groups Projects
Select Git revision
  • master
  • hf2
2 results

requireOption.js

Blame
  • Window.cpp 2.88 KiB
    #include "Window.hpp"
    
    #include <format>
    #include <stdexcept>
    
    static auto init_glfw() -> void
    {
        if (const int success{ glfwInit() }; success != GLFW_TRUE) {
            const char* description{};
            const int   error_code{ glfwGetError(&description) };
    
            if (description == nullptr) {
                throw std::runtime_error{
                    std::format("glfwInit failed with error code {}", error_code)
                };
            }
            throw std::runtime_error{ std::format(
                "glfwInit failed with error code {} - '{}'", error_code, description
            ) };
        }
    
        if (const int result{ std::atexit(glfwTerminate) }; result != 0) {
            throw std::runtime_error{
                std::format("std::atexit failed with error code {}", result)
            };
        }
    }
    
    [[nodiscard]]
    static auto
        create_window(const uint16_t width, const uint16_t height, const std::string& title)
            -> GLFWwindow*
    {
        init_glfw();
    
        glfwDefaultWindowHints();
        glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
    
        const auto window{ glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr) };
        if (window == nullptr) {
            throw std::runtime_error{ std::format(
                "glfwCreateWindow failed with error code `{}`", glfwGetError(nullptr)
            ) };
        }
        return window;
    }
    
    auto Window::vulkan_instance_extensions() -> std::span<const char* const>
    {
        static const std::vector s_extension_names{ []() -> std::vector<const char*> {
            init_glfw();
    
            uint32_t             count{};
            const char**         glfw_extension_names{ glfwGetRequiredInstanceExtensions(&count) };
            if (glfw_extension_names == nullptr) {
                return {};
            }
    
            return std::vector<const char*>{ glfw_extension_names,
                                             glfw_extension_names + count };
        }() };
    
        return s_extension_names;
    }
    
    Window::Window(const uint16_t width, const uint16_t height, const std::string& title)
        : m_impl{ create_window(width, height, title), glfwDestroyWindow }
    {}
    
    auto Window::get() const noexcept -> GLFWwindow*
    {
        return m_impl.get();
    }
    
    auto Window::framebuffer_size() const noexcept -> vk::Extent2D
    {
        int width{};
        int height{};
        glfwGetFramebufferSize(m_impl.get(), &width, &height);
    
        return vk::Extent2D{
            .width  = static_cast<uint32_t>(width),
            .height = static_cast<uint32_t>(height),
        };
    }
    
    auto Window::create_vulkan_surface(const vk::Instance instance
    ) const -> vk::UniqueSurfaceKHR
    {
        VkSurfaceKHR surface{};
    
        if (const vk::Result error_code{
                glfwCreateWindowSurface(instance, m_impl.get(), nullptr, &surface) };
            error_code != vk::Result::eSuccess)
        {
            throw std::runtime_error{ std::format(
                "glfwCreateWindowSurface failed with error code {}",
                static_cast<int>(error_code)
            ) };
        }
    
        return vk::UniqueSurfaceKHR{ surface, instance };
    }