From d1516a14a5c98e4e5beced9bb7a13ed982af985a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sun, 23 Jun 2024 15:21:54 +0200 Subject: [PATCH] Refactor client loading screen rendering Use `CUi::RenderProgressBar` function also for client loading, which means the filled progress bar background will also be rendered while loading. Replace static variable `s_LastLoadRender` with member variable. Encapsulate menu loading state in class `CLoadingState`. --- src/game/client/components/menus.cpp | 48 +++++++++++++++------------- src/game/client/components/menus.h | 10 ++++-- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/game/client/components/menus.cpp b/src/game/client/components/menus.cpp index 1983ce456..9fe01f0b0 100644 --- a/src/game/client/components/menus.cpp +++ b/src/game/client/components/menus.cpp @@ -734,17 +734,16 @@ void CMenus::RenderLoading(const char *pCaption, const char *pContent, int Incre { // TODO: not supported right now due to separate render thread - static std::chrono::nanoseconds s_LastLoadRender{0}; - const int CurLoadRenderCount = m_LoadCurrent; - m_LoadCurrent += IncreaseCounter; - const float Percent = CurLoadRenderCount / (float)m_LoadTotal; + const int CurLoadRenderCount = m_LoadingState.m_Current; + m_LoadingState.m_Current += IncreaseCounter; // make sure that we don't render for each little thing we load // because that will slow down loading if we have vsync - if(time_get_nanoseconds() - s_LastLoadRender < std::chrono::nanoseconds(1s) / 60l) + const std::chrono::nanoseconds Now = time_get_nanoseconds(); + if(Now - m_LoadingState.m_LastRender < std::chrono::nanoseconds(1s) / 60l) return; - s_LastLoadRender = time_get_nanoseconds(); + m_LoadingState.m_LastRender = Now; // need up date this here to get correct ms_GuiColor = color_cast(ColorHSLA(g_Config.m_UiColor, true)); @@ -756,27 +755,30 @@ void CMenus::RenderLoading(const char *pCaption, const char *pContent, int Incre RenderBackground(); } - CUIRect Box = *Ui()->Screen(); - Box.Margin(160.0f, &Box); + CUIRect Box; + Ui()->Screen()->Margin(160.0f, &Box); Graphics()->BlendNormal(); - Graphics()->TextureClear(); - Box.Draw(ColorRGBA{0, 0, 0, 0.50f}, IGraphics::CORNER_ALL, 15.0f); + Box.Draw(ColorRGBA(0.0f, 0.0f, 0.0f, 0.5f), IGraphics::CORNER_ALL, 15.0f); + Box.Margin(20.0f, &Box); - CUIRect Part; - Box.HSplitTop(20.f, nullptr, &Box); - Box.HSplitTop(24.f, &Part, &Box); - Part.VMargin(20.f, &Part); - Ui()->DoLabel(&Part, pCaption, 24.f, TEXTALIGN_MC); + CUIRect Label; + Box.HSplitTop(24.0f, &Label, &Box); + Ui()->DoLabel(&Label, pCaption, 24.0f, TEXTALIGN_MC); - Box.HSplitTop(20.f, nullptr, &Box); - Box.HSplitTop(24.f, &Part, &Box); - Part.VMargin(20.f, &Part); - Ui()->DoLabel(&Part, pContent, 20.0f, TEXTALIGN_MC); + Box.HSplitTop(20.0f, nullptr, &Box); + Box.HSplitTop(24.0f, &Label, &Box); + Ui()->DoLabel(&Label, pContent, 20.0f, TEXTALIGN_MC); if(RenderLoadingBar) - Graphics()->DrawRect(Box.x + 40, Box.y + Box.h - 75, (Box.w - 80) * Percent, 25, ColorRGBA(1.0f, 1.0f, 1.0f, 0.75f), IGraphics::CORNER_ALL, 5.0f); + { + CUIRect ProgressBar; + Box.HSplitBottom(30.0f, &Box, nullptr); + Box.HSplitBottom(25.0f, &Box, &ProgressBar); + ProgressBar.VMargin(20.0f, &ProgressBar); + Ui()->RenderProgressBar(ProgressBar, CurLoadRenderCount / (float)m_LoadingState.m_Total); + } Client()->UpdateAndSwap(); } @@ -867,10 +869,10 @@ void CMenus::OnInit() // setup load amount const int NumMenuImages = 5; - m_LoadCurrent = 0; - m_LoadTotal = g_pData->m_NumImages + NumMenuImages + GameClient()->ComponentCount(); + m_LoadingState.m_Current = 0; + m_LoadingState.m_Total = g_pData->m_NumImages + NumMenuImages + GameClient()->ComponentCount(); if(!g_Config.m_ClThreadsoundloading) - m_LoadTotal += g_pData->m_NumSounds; + m_LoadingState.m_Total += g_pData->m_NumSounds; m_IsInit = true; diff --git a/src/game/client/components/menus.h b/src/game/client/components/menus.h index 67cf1a408..7bd0e7303 100644 --- a/src/game/client/components/menus.h +++ b/src/game/client/components/menus.h @@ -186,8 +186,14 @@ protected: const CMenuImage *FindMenuImage(const char *pName); // loading - int m_LoadCurrent; - int m_LoadTotal; + class CLoadingState + { + public: + std::chrono::nanoseconds m_LastRender{0}; + int m_Current; + int m_Total; + }; + CLoadingState m_LoadingState; // char m_aMessageTopic[512];