Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 50 additions & 16 deletions src/frame/opengl/sdl_opengl_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <shtypes.h>
#pragma comment(lib, "Shcore.lib")
#endif
#include <SDL3/SDL_video.h>
#include <SDL3/SDL_video.h>
#include <format>

#include "frame/gui/draw_gui_interface.h"
Expand Down Expand Up @@ -122,8 +122,10 @@ WindowReturnEnum SDLOpenGLWindow::Run(std::function<bool()> lambda)
plugin_interface->Startup(size_);
}
}
WindowReturnEnum window_return_enum = WindowReturnEnum::CONTINUE;
double previous_count = 0.0;
WindowReturnEnum window_return_enum = WindowReturnEnum::CONTINUE;
if (SDL_GetKeyboardFocus() == sdl_window_)
SDL_StartTextInput(sdl_window_);
double previous_count = 0.0;
// Timing counter.
auto start = std::chrono::system_clock::now();
while (window_return_enum == WindowReturnEnum::CONTINUE)
Expand Down Expand Up @@ -188,19 +190,51 @@ WindowReturnEnum SDLOpenGLWindow::Run(std::function<bool()> lambda)

bool SDLOpenGLWindow::RunEvent(const SDL_Event& event, const double dt)
{
if (event.type == SDL_EVENT_QUIT)
{
return false;
}
if (event.type == SDL_EVENT_WINDOW_FOCUS_GAINED)
{
SDL_StartTextInput(sdl_window_);
}
if (event.type == SDL_EVENT_WINDOW_FOCUS_LOST)
{
SDL_StopTextInput(sdl_window_);
}
bool has_window_plugin = false;
const Uint32 window_id = SDL_GetWindowID(sdl_window_);

switch (event.type)
{
case SDL_EVENT_WINDOW_FOCUS_GAINED:
if (event.window.windowID != window_id)
return true;
SDL_StartTextInput(sdl_window_);
break;
case SDL_EVENT_WINDOW_FOCUS_LOST:
if (event.window.windowID != window_id)
return true;
SDL_StopTextInput(sdl_window_);
break;
case SDL_EVENT_KEY_DOWN:
case SDL_EVENT_KEY_UP:
if (event.key.windowID != window_id)
return true;
break;
case SDL_EVENT_TEXT_INPUT:
if (event.text.windowID != window_id)
return true;
break;
case SDL_EVENT_MOUSE_MOTION:
if (event.motion.windowID != window_id)
return true;
break;
case SDL_EVENT_MOUSE_BUTTON_DOWN:
case SDL_EVENT_MOUSE_BUTTON_UP:
if (event.button.windowID != window_id)
return true;
break;
case SDL_EVENT_MOUSE_WHEEL:
if (event.wheel.windowID != window_id)
return true;
break;
default:
break;
}

if (event.type == SDL_EVENT_QUIT)
{
return false;
}
bool has_window_plugin = false;
for (PluginInterface* plugin : device_->GetPluginPtrs())
{
if (dynamic_cast<frame::gui::DrawGuiInterface*>(plugin))
Expand Down
98 changes: 67 additions & 31 deletions src/frame/vulkan/sdl_vulkan_window.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "frame/vulkan/sdl_vulkan_window.h"

#include <SDL3/SDL.h>
#include <SDL3/SDL_vulkan.h>
#include <vulkan/vulkan.hpp>

Expand All @@ -27,10 +26,7 @@ SDLVulkanWindow::SDLVulkanWindow(glm::uvec2 size) : size_(size)

// Create an SDL window to use as a surface for Vulkan.
sdl_window_ = SDL_CreateWindow(
"Vulkan Headless",
size_.x,
size_.y,
SDL_WINDOW_VULKAN);
"Vulkan Headless", size_.x, size_.y, SDL_WINDOW_VULKAN);
if (!sdl_window_)
{
throw std::runtime_error(
Expand All @@ -40,16 +36,17 @@ SDLVulkanWindow::SDLVulkanWindow(glm::uvec2 size) : size_(size)
// Get the required extensions for creating a Vulkan surface.
uint32_t extension_count = 0;
std::vector<const char*> extensions(extension_count);
const char* const* extentions_c =
const char* const* extensions_c =
SDL_Vulkan_GetInstanceExtensions(&extension_count);
for (uint32_t i = 0; i < extension_count; i++)
{
extensions.push_back(extentions_c[i]);
extensions.push_back(extensions_c[i]);
}
if (extension_count == 0)
{
throw std::runtime_error(std::format(
"Could not get the extension count: {}", SDL_GetError()));
throw std::runtime_error(
std::format(
"Could not get the extension count: {}", SDL_GetError()));
}
#ifdef VK_EXT_ENABLE_DEBUG_EXTENSION
extensions.push_back("VK_EXT_debug_utils");
Expand Down Expand Up @@ -80,12 +77,12 @@ SDLVulkanWindow::SDLVulkanWindow(glm::uvec2 size) : size_(size)
vk::DebugUtilsMessengerCreateInfoEXT(
{},
vk::DebugUtilsMessageSeverityFlagBitsEXT::eVerbose |
vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning |
vk::DebugUtilsMessageSeverityFlagBitsEXT::eError,
vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning |
vk::DebugUtilsMessageSeverityFlagBitsEXT::eError,
vk::DebugUtilsMessageTypeFlagBitsEXT::eGeneral |
vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation |
vk::DebugUtilsMessageTypeFlagBitsEXT::ePerformance,
vk::PFN_DebugUtilsMessengerCallbackEXT(DebugCallback)));
vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation |
vk::DebugUtilsMessageTypeFlagBitsEXT::ePerformance,
vk::PFN_DebugUtilsMessengerCallbackEXT(DebugCallback)));
#endif

// Create the surface.
Expand All @@ -94,8 +91,9 @@ SDLVulkanWindow::SDLVulkanWindow(glm::uvec2 size) : size_(size)
if (!SDL_Vulkan_CreateSurface(
sdl_window_, *vk_unique_instance_, nullptr, &vk_surface))
{
throw std::runtime_error(std::format(
"Error while create vulkan surface: {}", SDL_GetError()));
throw std::runtime_error(
std::format(
"Error while create vulkan surface: {}", SDL_GetError()));
}
vk_surface_ = vk::UniqueSurfaceKHR(vk_surface);

Expand All @@ -112,7 +110,7 @@ SDLVulkanWindow::~SDLVulkanWindow()
SDL_Quit();
}

WindowReturnEnum SDLVulkanWindow::Run(
WindowReturnEnum SDLVulkanWindow::Run(
std::function<bool()> lambda /* = []{ return true; }*/)
{
// Called only once at the beginning.
Expand All @@ -126,6 +124,8 @@ WindowReturnEnum SDLVulkanWindow::Run(
}
}
WindowReturnEnum window_return_enum = WindowReturnEnum::CONTINUE;
if (SDL_GetKeyboardFocus() == sdl_window_)
SDL_StartTextInput(sdl_window_);
// Timing counter.
auto start = std::chrono::system_clock::now();
do
Expand All @@ -144,7 +144,7 @@ WindowReturnEnum SDLVulkanWindow::Run(
{
if (plugin_interface)
{
if (plugin_interface->PollEvent(&event))
if (plugin_interface->PollEvent(&event))
{
skip = true;
}
Expand Down Expand Up @@ -176,14 +176,15 @@ WindowReturnEnum SDLVulkanWindow::Run(

SetWindowTitle(
"SDL Vulkan - " + std::to_string(static_cast<float>(GetFPS(dt))));
if (!lambda())
if (!lambda())
{
window_return_enum = WindowReturnEnum::RESTART;
}

// Swap the window.
throw std::runtime_error("Implement swap window.");
} while (window_return_enum == WindowReturnEnum::CONTINUE);
return window_return_enum;
}

void* SDLVulkanWindow::GetGraphicContext() const
Expand All @@ -210,15 +211,17 @@ void SDLVulkanWindow::Resize(glm::uvec2 size, FullScreenEnum fullscreen_enum)
SDL_DisplayID display = SDL_GetDisplayForWindow(sdl_window_);
if (!display)
{
throw std::runtime_error(std::format(
"SDL_GetDisplayForWindow failed: {}", SDL_GetError()));
throw std::runtime_error(
std::format(
"SDL_GetDisplayForWindow failed: {}", SDL_GetError()));
}

SDL_Rect bounds;
if (!SDL_GetDisplayBounds(display, &bounds))
{
throw std::runtime_error(std::format(
"SDL_GetDisplayBounds failed: {}", SDL_GetError()));
throw std::runtime_error(
std::format(
"SDL_GetDisplayBounds failed: {}", SDL_GetError()));
}

// Use desired resolution if needed
Expand All @@ -236,8 +239,9 @@ void SDLVulkanWindow::Resize(glm::uvec2 size, FullScreenEnum fullscreen_enum)

if (!SDL_SetWindowFullscreenMode(sdl_window_, mode_ptr))
{
throw std::runtime_error(std::format(
"Error switching fullscreen mode: {}", SDL_GetError()));
throw std::runtime_error(
std::format(
"Error switching fullscreen mode: {}", SDL_GetError()));
}

// Only resize in windowed mode — fullscreen modes will auto-resize the
Expand All @@ -258,16 +262,48 @@ frame::FullScreenEnum SDLVulkanWindow::GetFullScreenEnum() const

bool SDLVulkanWindow::RunEvent(const SDL_Event& event, const double dt)
{
if (event.type == SDL_EVENT_QUIT)
return false;
if (event.type == SDL_EVENT_WINDOW_FOCUS_GAINED)
const Uint32 window_id = SDL_GetWindowID(sdl_window_);

switch (event.type)
{
case SDL_EVENT_WINDOW_FOCUS_GAINED:
if (event.window.windowID != window_id)
return true;
SDL_StartTextInput(sdl_window_);
}
if (event.type == SDL_EVENT_WINDOW_FOCUS_LOST)
{
break;
case SDL_EVENT_WINDOW_FOCUS_LOST:
if (event.window.windowID != window_id)
return true;
SDL_StopTextInput(sdl_window_);
break;
case SDL_EVENT_KEY_DOWN:
case SDL_EVENT_KEY_UP:
if (event.key.windowID != window_id)
return true;
break;
case SDL_EVENT_TEXT_INPUT:
if (event.text.windowID != window_id)
return true;
break;
case SDL_EVENT_MOUSE_MOTION:
if (event.motion.windowID != window_id)
return true;
break;
case SDL_EVENT_MOUSE_BUTTON_DOWN:
case SDL_EVENT_MOUSE_BUTTON_UP:
if (event.button.windowID != window_id)
return true;
break;
case SDL_EVENT_MOUSE_WHEEL:
if (event.wheel.windowID != window_id)
return true;
break;
default:
break;
}

if (event.type == SDL_EVENT_QUIT)
return false;
bool has_window_plugin = false;
for (PluginInterface* plugin : device_->GetPluginPtrs())
{
Expand Down
Loading