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`.
This commit is contained in:
Robert Müller 2024-06-23 15:21:54 +02:00
parent 5f9d97a2aa
commit d1516a14a5
2 changed files with 33 additions and 25 deletions

View file

@ -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<ColorRGBA>(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;

View file

@ -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];