Files
Towards/external/loft/modules/window/src/SDLWindow.cpp
T

83 lines
2.2 KiB
C++
Raw Normal View History

2026-08-04 21:18:46 +02:00
#include "SDLWindow.h"
#include <SDL3/SDL_error.h>
#include <SDL3/SDL_video.h>
2026-07-18 14:31:15 +02:00
#include <volk.h>
#include <stdexcept>
2026-08-04 21:18:46 +02:00
2026-07-18 14:31:15 +02:00
#include "LoftException.hpp"
2026-08-04 21:18:46 +02:00
#include <SDL3/SDL_vulkan.h>
2026-07-18 14:31:15 +02:00
#include <vector>
#include <vulkan/vulkan_core.h>
namespace lft::win {
//
// Created by martin on 11/12/23.
//
void SDLWindow::resize() {
}
VkExtent2D SDLWindow::get_size() const {
VkExtent2D extent = {};
SDL_GetWindowSize(m_pWindow, (int*)&extent.width, (int*)&extent.height);
return extent;
}
SDLWindow::SDLWindow(std::string name, VkRect2D rect) :
m_extent{rect.extent}
{
2026-08-04 21:18:46 +02:00
if(!SDL_Init(SDL_INIT_VIDEO)) {
2026-07-18 14:31:15 +02:00
throw std::runtime_error("Failed to initialize sdl");
}
m_pWindow = SDL_CreateWindow(name.c_str(),
2026-08-04 21:18:46 +02:00
// SDL_WINDOWPOS_CENTERED,
// SDL_WINDOWPOS_CENTERED,
2026-07-18 14:31:15 +02:00
rect.extent.width,
rect.extent.height,
2026-08-04 21:18:46 +02:00
SDL_WINDOW_VULKAN | SDL_WINDOW_HIGH_PIXEL_DENSITY |
2026-07-18 14:31:15 +02:00
SDL_WINDOW_RESIZABLE);
if(m_pWindow == nullptr) {
throw GpuException(std::format("Failed to create window:\n{}", SDL_GetError()));
}
}
Surface SDLWindow::create_surface(const Instance* instance) const {
VkSurfaceKHR surface = VK_NULL_HANDLE;
2026-08-04 21:18:46 +02:00
SDL_Vulkan_CreateSurface(m_pWindow, instance->instance(), NULL, &surface);
2026-07-18 14:31:15 +02:00
return Surface(instance, surface);
}
bool SDLWindow::is_open() const {
return true;
}
int32_t SDLWindow::poll_event(SDL_Event *pOutEvent) const {
return SDL_PollEvent(pOutEvent);
}
std::vector<std::string> SDLWindow::get_required_extensions() const {
uint32_t count = 0;
2026-08-04 21:18:46 +02:00
const char* const *instance_extensions = SDL_Vulkan_GetInstanceExtensions(&count);
2026-07-18 14:31:15 +02:00
2026-08-04 21:18:46 +02:00
std::vector<std::string> extensions(count);
for(int i = 0; i < count; i++) {
extensions[i] = std::string(instance_extensions[i]);
}
2026-07-18 14:31:15 +02:00
2026-08-04 21:18:46 +02:00
// std::vector<std::string> extension_strings(count);
// std::transform(extensions.begin(), extensions.end(),
// extension_strings.begin(),
// [](const char* str) {
// return std::string(str);
// });
2026-07-18 14:31:15 +02:00
2026-08-04 21:18:46 +02:00
return extensions;
2026-07-18 14:31:15 +02:00
}
}