From a0a0cd48fabf0a042e2a49ac73ad80dce1b6c2ed Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Fri, 28 Feb 2020 16:59:46 +1000 Subject: [PATCH] SDL: Reimplement improved fullscreen toggling --- src/duckstation-sdl/d3d11_host_display.cpp | 2 + src/duckstation-sdl/opengl_host_display.cpp | 2 + src/duckstation-sdl/sdl_host_interface.cpp | 48 ++++++++++----------- src/duckstation-sdl/sdl_host_interface.h | 5 ++- 4 files changed, 30 insertions(+), 27 deletions(-) diff --git a/src/duckstation-sdl/d3d11_host_display.cpp b/src/duckstation-sdl/d3d11_host_display.cpp index a03e4b81c..0e95016ca 100644 --- a/src/duckstation-sdl/d3d11_host_display.cpp +++ b/src/duckstation-sdl/d3d11_host_display.cpp @@ -157,6 +157,8 @@ std::tuple D3D11HostDisplay::GetWindowSize() const void D3D11HostDisplay::WindowResized() { SDL_GetWindowSize(m_window, &m_window_width, &m_window_height); + ImGui::GetIO().DisplaySize.x = static_cast(m_window_width); + ImGui::GetIO().DisplaySize.y = static_cast(m_window_height); m_swap_chain_rtv.Reset(); diff --git a/src/duckstation-sdl/opengl_host_display.cpp b/src/duckstation-sdl/opengl_host_display.cpp index 29401884e..b70b4ed01 100644 --- a/src/duckstation-sdl/opengl_host_display.cpp +++ b/src/duckstation-sdl/opengl_host_display.cpp @@ -139,6 +139,8 @@ std::tuple OpenGLHostDisplay::GetWindowSize() const void OpenGLHostDisplay::WindowResized() { SDL_GetWindowSize(m_window, &m_window_width, &m_window_height); + ImGui::GetIO().DisplaySize.x = static_cast(m_window_width); + ImGui::GetIO().DisplaySize.y = static_cast(m_window_height); } const char* OpenGLHostDisplay::GetGLSLVersionString() const diff --git a/src/duckstation-sdl/sdl_host_interface.cpp b/src/duckstation-sdl/sdl_host_interface.cpp index 073ea94da..69d68f0d5 100644 --- a/src/duckstation-sdl/sdl_host_interface.cpp +++ b/src/duckstation-sdl/sdl_host_interface.cpp @@ -68,6 +68,9 @@ bool SDLHostInterface::CreateSDLWindow() SDL_FreeSurface(icon_surface); } + if (m_fullscreen) + SDL_SetWindowFullscreen(m_window, SDL_WINDOW_FULLSCREEN_DESKTOP); + return true; } @@ -96,6 +99,7 @@ bool SDLHostInterface::CreateDisplay() if (!display) return false; + display->SetDisplayTopMargin(m_fullscreen ? 0 : static_cast(20.0f * ImGui::GetIO().DisplayFramebufferScale.x)); m_display = display.release(); return true; } @@ -218,13 +222,18 @@ void SDLHostInterface::UpdateSettings() HostInterface::UpdateSettings([this]() { m_settings = m_settings_copy; }); } -void SDLHostInterface::UpdateFullscreen() +void SDLHostInterface::SetFullscreen(bool enabled) { - SDL_SetWindowFullscreen(m_window, m_settings.display_fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0); + if (m_fullscreen == enabled) + return; + + SDL_SetWindowFullscreen(m_window, enabled ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0); // We set the margin only in windowed mode, the menu bar is drawn on top in fullscreen. - m_display->SetDisplayTopMargin( - m_settings.display_fullscreen ? 0 : static_cast(20.0f * ImGui::GetIO().DisplayFramebufferScale.x)); + m_display->SetDisplayTopMargin(enabled ? 0 : static_cast(20.0f * ImGui::GetIO().DisplayFramebufferScale.x)); + + m_display->WindowResized(); + m_fullscreen = enabled; } std::unique_ptr SDLHostInterface::Create() @@ -235,6 +244,7 @@ std::unique_ptr SDLHostInterface::Create() SDLSettingsInterface si(intf->GetSettingsFileName().c_str()); intf->m_settings_copy.Load(si); intf->m_settings = intf->m_settings_copy; + intf->m_fullscreen = intf->m_settings_copy.display_fullscreen; if (!intf->CreateSDLWindow()) { @@ -257,8 +267,6 @@ std::unique_ptr SDLHostInterface::Create() ImGui::NewFrame(); - intf->UpdateFullscreen(); - return intf; } @@ -382,10 +390,11 @@ void SDLHostInterface::HandleSDLKeyEvent(const SDL_Event* event) } break; - case SDL_SCANCODE_F11: + case SDL_SCANCODE_RETURN: + case SDL_SCANCODE_KP_ENTER: { - if (!pressed) - DoToggleFullscreen(); + if ((event->key.keysym.mod & (KMOD_LALT | KMOD_RALT)) && !pressed) + SetFullscreen(!m_fullscreen); } break; @@ -573,7 +582,7 @@ void SDLHostInterface::DrawMainMenuBar() { // We skip drawing the menu bar if we're in fullscreen and the mouse pointer isn't in range. const float SHOW_THRESHOLD = 20.0f; - if (m_settings.display_fullscreen && + if (m_fullscreen && !m_system && ImGui::GetIO().MousePos.y >= (SHOW_THRESHOLD * ImGui::GetIO().DisplayFramebufferScale.x) && !ImGui::IsWindowFocused(ImGuiFocusedFlags_AnyWindow)) { @@ -783,11 +792,9 @@ void SDLHostInterface::DrawQuickSettingsMenu() ImGui::EndMenu(); } - if (ImGui::MenuItem("Fullscreen", nullptr, &m_settings_copy.display_fullscreen)) - { - settings_changed = true; - UpdateFullscreen(); - } + bool fullscreen = m_fullscreen; + if (ImGui::MenuItem("Fullscreen", nullptr, &fullscreen)) + RunLater([this, fullscreen] { SetFullscreen(fullscreen); }); settings_changed |= ImGui::MenuItem("VSync", nullptr, &m_settings_copy.video_sync_enabled); @@ -1144,11 +1151,8 @@ void SDLHostInterface::DrawSettingsWindow() if (DrawSettingsSectionHeader("Display Output")) { - if (ImGui::Checkbox("Fullscreen", &m_settings_copy.display_fullscreen)) - { - UpdateFullscreen(); + if (ImGui::Checkbox("Start Fullscreen", &m_settings_copy.display_fullscreen)) settings_changed = true; - } settings_changed |= ImGui::Checkbox("Linear Filtering", &m_settings_copy.display_linear_filtering); settings_changed |= ImGui::Checkbox("VSync", &m_settings_copy.video_sync_enabled); @@ -1308,12 +1312,6 @@ void SDLHostInterface::DoFrameStep() m_paused = false; } -void SDLHostInterface::DoToggleFullscreen() -{ - m_settings.display_fullscreen = !m_settings.display_fullscreen; - UpdateFullscreen(); -} - void SDLHostInterface::Run() { while (!m_quit_request) diff --git a/src/duckstation-sdl/sdl_host_interface.h b/src/duckstation-sdl/sdl_host_interface.h index cc43fd980..2df660f43 100644 --- a/src/duckstation-sdl/sdl_host_interface.h +++ b/src/duckstation-sdl/sdl_host_interface.h @@ -90,14 +90,14 @@ private: void SaveSettings(); void UpdateSettings(); - void UpdateFullscreen(); + bool IsFullscreen() const { return m_fullscreen; } + void SetFullscreen(bool enabled); // We only pass mouse input through if it's grabbed void DrawImGui(); void DoStartDisc(); void DoChangeDisc(); void DoFrameStep(); - void DoToggleFullscreen(); void HandleSDLEvent(const SDL_Event* event); void HandleSDLKeyEvent(const SDL_Event* event); @@ -121,6 +121,7 @@ private: u32 m_run_later_event_id = 0; + bool m_fullscreen = false; bool m_quit_request = false; bool m_frame_step_request = false; bool m_focus_main_menu_bar = false;