diff --git a/src/gui/rt64_inspector.cpp b/src/gui/rt64_inspector.cpp index 437a244..7318103 100644 --- a/src/gui/rt64_inspector.cpp +++ b/src/gui/rt64_inspector.cpp @@ -61,13 +61,14 @@ namespace RT64 { // Inspector - Inspector::Inspector(RenderDevice *device, const RenderSwapChain *swapChain, UserConfiguration::GraphicsAPI graphicsAPI) { + Inspector::Inspector(RenderDevice *device, const RenderSwapChain *swapChain, UserConfiguration::GraphicsAPI graphicsAPI, SDL_Window *sdlWindow) { assert(device != nullptr); assert(swapChain != nullptr); this->device = device; this->swapChain = swapChain; this->graphicsAPI = graphicsAPI; + this->sdlWindow = sdlWindow; IMGUI_CHECKVERSION(); @@ -75,10 +76,15 @@ namespace RT64 { ImPlot::CreateContext(); ImGui::StyleColorsDark(); -# ifdef _WIN32 - RenderWindow renderWindow = swapChain->getWindow(); - ImGui_ImplWin32_Init(renderWindow); -# endif + if (sdlWindow != nullptr) { + ImGui_ImplSDL2_InitForOther(sdlWindow); + } + else { +# ifdef _WIN32 + RenderWindow renderWindow = swapChain->getWindow(); + ImGui_ImplWin32_Init(renderWindow); +# endif + } switch (graphicsAPI) { case UserConfiguration::GraphicsAPI::D3D12: { @@ -155,11 +161,14 @@ namespace RT64 { break; } -# ifdef _WIN32 - ImGui_ImplWin32_Shutdown(); -# else - assert(false && "Unimplemented."); -# endif + if (sdlWindow != nullptr) { + ImGui_ImplSDL2_Shutdown(); + } + else { +# ifdef _WIN32 + ImGui_ImplWin32_Shutdown(); +# endif + } ImPlot::DestroyContext(); ImGui::DestroyContext(); @@ -175,12 +184,15 @@ namespace RT64 { assert(worker != nullptr); frameMutex.lock(); - -# ifdef _WIN32 - ImGui_ImplWin32_NewFrame(); -# else - assert(false && "Unimplemented."); -# endif + + if (sdlWindow != nullptr) { + ImGui_ImplSDL2_NewFrame(); + } + else { +# ifdef _WIN32 + ImGui_ImplWin32_NewFrame(); +# endif + } switch (graphicsAPI) { case UserConfiguration::GraphicsAPI::D3D12: { @@ -244,6 +256,8 @@ namespace RT64 { #endif bool Inspector::handleSdlEvent(SDL_Event *event) { + assert((sdlWindow != nullptr) && "SDL Events shouldn't be handled if SDL was not used."); + bool processed = ImGui_ImplSDL2_ProcessEvent(event); if (processed) { if ((event->type == SDL_KEYDOWN) || (event->type == SDL_KEYUP)) { diff --git a/src/gui/rt64_inspector.h b/src/gui/rt64_inspector.h index a515742..52b2249 100644 --- a/src/gui/rt64_inspector.h +++ b/src/gui/rt64_inspector.h @@ -23,12 +23,13 @@ namespace RT64 { struct Inspector { RenderDevice *device = nullptr; const RenderSwapChain *swapChain = nullptr; + UserConfiguration::GraphicsAPI graphicsAPI; + SDL_Window *sdlWindow = nullptr; std::unique_ptr descriptorSet; std::unique_ptr vulkanContext; - UserConfiguration::GraphicsAPI graphicsAPI; std::mutex frameMutex; - Inspector(RenderDevice *device, const RenderSwapChain *swapChain, UserConfiguration::GraphicsAPI graphicsAPI); + Inspector(RenderDevice *device, const RenderSwapChain *swapChain, UserConfiguration::GraphicsAPI graphicsAPI, SDL_Window *sdlWindow); ~Inspector(); void setIniPath(const std::filesystem::path &path); void newFrame(RenderWorker *worker); diff --git a/src/hle/rt64_application.cpp b/src/hle/rt64_application.cpp index dbd1c39..cc8f5f2 100644 --- a/src/hle/rt64_application.cpp +++ b/src/hle/rt64_application.cpp @@ -513,7 +513,7 @@ namespace RT64 { if (userConfig.developerMode) { const std::lock_guard lock(presentQueue->inspectorMutex); if (presentQueue->inspector == nullptr) { - presentQueue->inspector = std::make_unique(device.get(), swapChain.get(), createdGraphicsAPI); + presentQueue->inspector = std::make_unique(device.get(), swapChain.get(), createdGraphicsAPI, appWindow->sdlWindow); if (!userPaths.isEmpty()) { presentQueue->inspector->setIniPath(userPaths.imguiPath); } diff --git a/src/hle/rt64_application_window.cpp b/src/hle/rt64_application_window.cpp index c1c99e4..58788aa 100644 --- a/src/hle/rt64_application_window.cpp +++ b/src/hle/rt64_application_window.cpp @@ -44,21 +44,24 @@ namespace RT64 { this->listener = listener; windowHandle = window; - usingSdl = SDL_WasInit(SDL_INIT_VIDEO); if (listener->usesWindowMessageFilter()) { - if (usingSdl) { + if ((sdlWindow == nullptr) && SDL_WasInit(SDL_INIT_VIDEO)) { // We'd normally install the event filter here, but Mupen does not set its own event filter // until much later. Instead, we delegate this to the first time a screen update is sent. + // FIXME: We attempt to get the first window created by SDL2. This can be improved later + // by actually passing the SDL_Window handle through as a parameter. + sdlWindow = SDL_GetWindowFromID(1); } -# ifdef _WIN32 - else { + + if (sdlWindow == nullptr) { +# ifdef _WIN32 assert(HookedApplicationWindow == nullptr); assert(threadId != 0); windowHook = SetWindowsHookEx(WH_GETMESSAGE, &windowHookCallback, NULL, threadId); HookedApplicationWindow = this; +# endif } -# endif } } @@ -109,9 +112,11 @@ namespace RT64 { # endif // Create window. - SDL_Window *sdlWindow = SDL_CreateWindow(windowTitle, bounds.left, bounds.top, bounds.width, bounds.height, SDL_WINDOW_RESIZABLE); + sdlWindow = SDL_CreateWindow(windowTitle, bounds.left, bounds.top, bounds.width, bounds.height, SDL_WINDOW_RESIZABLE); + assert((sdlWindow != nullptr) && "Failed to open window with SDL"); + + // Get native window handles from the window. SDL_SysWMinfo wmInfo; - assert(sdlWindow && "Failed to open window with SDL"); SDL_VERSION(&wmInfo.version); SDL_GetWindowWMInfo(sdlWindow, &wmInfo); # if defined(_WIN32) @@ -323,7 +328,7 @@ namespace RT64 { #endif void ApplicationWindow::sdlCheckFilterInstallation() { - if (!sdlEventFilterInstalled && usingSdl) { + if (!sdlEventFilterInstalled && (sdlWindow != nullptr)) { if (!SDL_GetEventFilter(&sdlEventFilterStored, &sdlEventFilterUserdata)) { sdlEventFilterStored = nullptr; sdlEventFilterUserdata = nullptr; diff --git a/src/hle/rt64_application_window.h b/src/hle/rt64_application_window.h index 393d82a..dc3f19c 100644 --- a/src/hle/rt64_application_window.h +++ b/src/hle/rt64_application_window.h @@ -36,9 +36,9 @@ namespace RT64 { uint32_t refreshRate = 0; bool fullScreen = false; bool lastMaximizedState = false; - bool usingSdl = false; int32_t windowLeft = INT32_MAX; int32_t windowTop = INT32_MAX; + SDL_Window *sdlWindow = nullptr; SDL_EventFilter sdlEventFilterStored = nullptr; void *sdlEventFilterUserdata = nullptr; bool sdlEventFilterInstalled = false;