From dcc38678a6c452150c5c1eca4fffe225d0725cca Mon Sep 17 00:00:00 2001 From: Malkierian Date: Fri, 5 Jul 2024 20:33:05 -0700 Subject: [PATCH] Eliminate Graphics Menu Lag (#733) * Moved non-dynamic window backend data to external variables and update function called on init and combobox change. * clang --- mm/2s2h/BenGui/BenMenuBar.cpp | 42 +++++++++++++++++++++-------------- mm/2s2h/BenGui/BenMenuBar.h | 2 +- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/mm/2s2h/BenGui/BenMenuBar.cpp b/mm/2s2h/BenGui/BenMenuBar.cpp index d1c3741fa..2b80702bc 100644 --- a/mm/2s2h/BenGui/BenMenuBar.cpp +++ b/mm/2s2h/BenGui/BenMenuBar.cpp @@ -56,6 +56,25 @@ static const std::unordered_map alwaysWinDoggyraceOptions }; namespace BenGui { +std::shared_ptr> availableWindowBackends; +std::unordered_map availableWindowBackendsMap; +Ship::WindowBackend configWindowBackend; + +void UpdateWindowBackendObjects() { + Ship::WindowBackend runningWindowBackend = Ship::Context::GetInstance()->GetWindow()->GetWindowBackend(); + Ship::WindowBackend configWindowBackend; + int32_t configWindowBackendId = Ship::Context::GetInstance()->GetConfig()->GetInt("Window.Backend.Id", -1); + if (configWindowBackendId != -1 && configWindowBackendId < static_cast(Ship::WindowBackend::BACKEND_COUNT)) { + configWindowBackend = static_cast(configWindowBackendId); + } else { + configWindowBackend = runningWindowBackend; + } + + availableWindowBackends = Ship::Context::GetInstance()->GetWindow()->GetAvailableWindowBackends(); + for (auto& backend : *availableWindowBackends) { + availableWindowBackendsMap[backend] = windowBackendsMap[backend]; + } +} void DrawMenuBarIcon() { static bool gameIconLoaded = false; @@ -244,32 +263,17 @@ void DrawSettingsMenu() { // UIWidgets::PaddedSeparator(true, true, 3.0f, 3.0f); // #endregion */ - Ship::WindowBackend runningWindowBackend = Ship::Context::GetInstance()->GetWindow()->GetWindowBackend(); - Ship::WindowBackend configWindowBackend; - int32_t configWindowBackendId = Ship::Context::GetInstance()->GetConfig()->GetInt("Window.Backend.Id", -1); - if (configWindowBackendId != -1 && - configWindowBackendId < static_cast(Ship::WindowBackend::BACKEND_COUNT)) { - configWindowBackend = static_cast(configWindowBackendId); - } else { - configWindowBackend = runningWindowBackend; - } - - auto availableWindowBackends = Ship::Context::GetInstance()->GetWindow()->GetAvailableWindowBackends(); - std::unordered_map availableWindowBackendsMap; - for (auto& backend : *availableWindowBackends) { - availableWindowBackendsMap[backend] = windowBackendsMap[backend]; - } - if (UIWidgets::Combobox( "Renderer API (Needs reload)", &configWindowBackend, availableWindowBackendsMap, { .tooltip = "Sets the renderer API used by the game. Requires a relaunch to take effect.", - .disabled = Ship::Context::GetInstance()->GetWindow()->GetAvailableWindowBackends()->size() <= 1, + .disabled = availableWindowBackends->size() <= 1, .disabledTooltip = "Only one renderer API is available on this platform." })) { Ship::Context::GetInstance()->GetConfig()->SetInt("Window.Backend.Id", static_cast(configWindowBackend)); Ship::Context::GetInstance()->GetConfig()->SetString("Window.Backend.Name", windowBackendsMap.at(configWindowBackend)); Ship::Context::GetInstance()->GetConfig()->Save(); + UpdateWindowBackendObjects(); } if (Ship::Context::GetInstance()->GetWindow()->CanDisableVerticalSync()) { @@ -738,6 +742,10 @@ void DrawDeveloperToolsMenu() { } } +void BenMenuBar::InitElement() { + UpdateWindowBackendObjects(); +} + void BenMenuBar::DrawElement() { if (ImGui::BeginMenuBar()) { DrawMenuBarIcon(); diff --git a/mm/2s2h/BenGui/BenMenuBar.h b/mm/2s2h/BenGui/BenMenuBar.h index 53c21b0a4..7f509f8fb 100644 --- a/mm/2s2h/BenGui/BenMenuBar.h +++ b/mm/2s2h/BenGui/BenMenuBar.h @@ -15,7 +15,7 @@ class BenMenuBar : public Ship::GuiMenuBar { protected: void DrawElement() override; - void InitElement() override{}; + void InitElement() override; void UpdateElement() override{}; }; } // namespace BenGui