97 lines
2.8 KiB
C++
97 lines
2.8 KiB
C++
#include "window.h"
|
|
|
|
#include <iostream>
|
|
|
|
Engine::Window::Window(uint32_t width, uint32_t height) :
|
|
sdl(createWindow(width, height)),
|
|
extent({width, height})
|
|
{}
|
|
|
|
Engine::Window::~Window() {
|
|
SDL_DestroyWindow(sdl);
|
|
SDL_Quit();
|
|
}
|
|
|
|
SDL_Window* Engine::Window::createWindow(uint32_t width, uint32_t height) {
|
|
SDL_Init(SDL_INIT_VIDEO);
|
|
SDL_Window* wnd = SDL_CreateWindow(
|
|
"Stories", //TODO parametrize window title
|
|
SDL_WINDOWPOS_UNDEFINED,
|
|
SDL_WINDOWPOS_UNDEFINED,
|
|
width,
|
|
height,
|
|
SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE
|
|
);
|
|
|
|
return wnd;
|
|
}
|
|
|
|
std::vector<const char *> Engine::Window::getRequiredVulkanExtensions() const {
|
|
unsigned int extCount = 0;
|
|
if (!SDL_Vulkan_GetInstanceExtensions(sdl, &extCount, nullptr)) {
|
|
throw std::runtime_error("Unable to query the number of Vulkan instance extensions");
|
|
}
|
|
|
|
std::vector<const char*> extract(extCount);
|
|
if (!SDL_Vulkan_GetInstanceExtensions(sdl, &extCount, extract.data())) {
|
|
throw std::runtime_error("Unable to query the number of Vulkan instance extension names");
|
|
}
|
|
|
|
return extract;
|
|
}
|
|
|
|
VkExtent2D Engine::Window::getDrawableSize() const {
|
|
int width, height;
|
|
SDL_Vulkan_GetDrawableSize(sdl, &width, &height);
|
|
|
|
VkExtent2D actualExtent = {
|
|
static_cast<uint32_t>(width),
|
|
static_cast<uint32_t>(height)
|
|
};
|
|
|
|
return actualExtent;
|
|
}
|
|
|
|
VkExtent2D Engine::Window::getSize() const {
|
|
int width, height;
|
|
SDL_GetWindowSize(sdl, &width, &height);
|
|
|
|
VkExtent2D actualExtent = {
|
|
static_cast<uint32_t>(width),
|
|
static_cast<uint32_t>(height)
|
|
};
|
|
|
|
return actualExtent;
|
|
}
|
|
|
|
void Engine::Window::createSurface(VkInstance instance, VkSurfaceKHR* out) const {
|
|
if (SDL_Vulkan_CreateSurface(sdl, instance, out) != SDL_TRUE) {
|
|
throw std::runtime_error("failed to create window surface!");
|
|
}
|
|
}
|
|
|
|
VkExtent2D Engine::Window::chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities) const {
|
|
if (capabilities.currentExtent.width != std::numeric_limits<uint32_t>::max()) {
|
|
return capabilities.currentExtent;
|
|
} else {
|
|
VkExtent2D actualExtent = getDrawableSize();
|
|
|
|
actualExtent.width = std::clamp(actualExtent.width, capabilities.minImageExtent.width, capabilities.maxImageExtent.width);
|
|
actualExtent.height = std::clamp(actualExtent.height, capabilities.minImageExtent.height, capabilities.maxImageExtent.height);
|
|
|
|
std::cout << actualExtent.width << ":" << actualExtent.height << std::endl;
|
|
return actualExtent;
|
|
}
|
|
}
|
|
|
|
VkExtent2D Engine::Window::waitForResize() const {
|
|
VkExtent2D extent = getDrawableSize();
|
|
while (extent.width == 0 || extent.height == 0) {
|
|
extent = getDrawableSize();
|
|
SDL_WaitEvent(nullptr);
|
|
}
|
|
|
|
return extent;
|
|
}
|
|
|