Skip to content

Commit

Permalink
Merge pull request ddnet#9387 from Robyt3/Client-Map-Background-Loadi…
Browse files Browse the repository at this point in the history
…ng-Fixes

Improve entities/menu background map loading
  • Loading branch information
def- authored Dec 14, 2024
2 parents c0da6eb + fe88358 commit 7e30a09
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 30 deletions.
6 changes: 6 additions & 0 deletions src/game/client/components/background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <game/client/gameclient.h>
#include <game/layers.h>
#include <game/localization.h>

#include "background.h"

Expand All @@ -33,6 +34,11 @@ CBackgroundEngineMap *CBackground::CreateBGMap()
return new CBackgroundEngineMap;
}

const char *CBackground::LoadingTitle() const
{
return Localize("Loading background map");
}

void CBackground::OnInit()
{
m_pBackgroundMap = CreateBGMap();
Expand Down
2 changes: 2 additions & 0 deletions src/game/client/components/background.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class CBackground : public CMapLayers

virtual CBackgroundEngineMap *CreateBGMap();

const char *LoadingTitle() const override;

public:
CBackground(int MapType = CMapLayers::TYPE_BACKGROUND_FORCE, bool OnlineOnly = true);
virtual ~CBackground();
Expand Down
16 changes: 8 additions & 8 deletions src/game/client/components/maplayers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ CCamera *CMapLayers::GetCurCamera()
return &m_pClient->m_Camera;
}

const char *CMapLayers::LoadingTitle() const
{
return GameClient()->DemoPlayer()->IsPlaying() ? Localize("Preparing demo playback") : Localize("Connected");
}

void CMapLayers::EnvelopeEval(int TimeOffsetMillis, int Env, ColorRGBA &Result, size_t Channels, void *pUser)
{
CMapLayers *pThis = (CMapLayers *)pUser;
Expand Down Expand Up @@ -276,15 +281,10 @@ void CMapLayers::OnMapLoad()
if(!Graphics()->IsTileBufferingEnabled() && !Graphics()->IsQuadBufferingEnabled())
return;

const char *pConnectCaption = GameClient()->DemoPlayer()->IsPlaying() ? Localize("Preparing demo playback") : Localize("Connected");
const char *pLoadMapContent = Localize("Uploading map data to GPU");

auto CurTime = time_get_nanoseconds();
const char *pLoadingTitle = LoadingTitle();
const char *pLoadingMessage = Localize("Uploading map data to GPU");
auto &&RenderLoading = [&]() {
if(CanRenderMenuBackground())
GameClient()->m_Menus.RenderLoading(pConnectCaption, pLoadMapContent, 0, false);
else if(time_get_nanoseconds() - CurTime > 500ms)
GameClient()->m_Menus.RenderLoading(pConnectCaption, pLoadMapContent, 0, false, false);
GameClient()->m_Menus.RenderLoading(pLoadingTitle, pLoadingMessage, 0);
};

//clear everything and destroy all buffers
Expand Down
4 changes: 1 addition & 3 deletions src/game/client/components/maplayers.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,10 @@ class CMapLayers : public CComponent
std::vector<SQuadLayerVisuals *> m_vpQuadLayerVisuals;

virtual CCamera *GetCurCamera();
virtual const char *LoadingTitle() const;

void LayersOfGroupCount(CMapItemGroup *pGroup, int &TileLayerCount, int &QuadLayerCount, bool &PassedGameLayer);

protected:
virtual bool CanRenderMenuBackground() { return true; }

public:
enum
{
Expand Down
11 changes: 10 additions & 1 deletion src/game/client/components/menu_background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ CMenuBackground::CMenuBackground() :
m_MoveTime = 0.0f;

m_IsInit = false;
m_Loading = false;
}

CBackgroundEngineMap *CMenuBackground::CreateBGMap()
Expand Down Expand Up @@ -159,7 +160,7 @@ int CMenuBackground::ThemeScan(const char *pName, int IsDir, int DirType, void *

if(time_get_nanoseconds() - pSelf->m_ThemeScanStartTime > 500ms)
{
pSelf->GameClient()->m_Menus.RenderLoading(Localize("Loading menu themes"), "", 0, false);
pSelf->GameClient()->m_Menus.RenderLoading(Localize("Loading menu themes"), "", 0);
}
return 0;
}
Expand All @@ -183,6 +184,8 @@ void CMenuBackground::LoadMenuBackground(bool HasDayHint, bool HasNightHint)

if(g_Config.m_ClMenuMap[0] != '\0')
{
m_Loading = true;

const char *pMenuMap = g_Config.m_ClMenuMap;
if(str_comp(pMenuMap, "auto") == 0)
{
Expand Down Expand Up @@ -287,6 +290,7 @@ void CMenuBackground::LoadMenuBackground(bool HasDayHint, bool HasNightHint)
}
}
}
m_Loading = false;
}
}

Expand Down Expand Up @@ -358,6 +362,11 @@ CCamera *CMenuBackground::GetCurCamera()
return &m_Camera;
}

const char *CMenuBackground::LoadingTitle() const
{
return Localize("Loading background map");
}

void CMenuBackground::ChangePosition(int PositionNumber)
{
if(PositionNumber != m_CurrentPosition)
Expand Down
8 changes: 5 additions & 3 deletions src/game/client/components/menu_background.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ class CMenuBackground : public CBackground
{
std::chrono::nanoseconds m_ThemeScanStartTime{0};

protected:
bool CanRenderMenuBackground() override { return false; }

public:
enum
{
Expand Down Expand Up @@ -78,6 +75,7 @@ class CMenuBackground : public CBackground
PREDEFINED_THEMES_COUNT = 3,
};

private:
CCamera m_Camera;

CBackgroundEngineMap *CreateBGMap() override;
Expand All @@ -91,6 +89,7 @@ class CMenuBackground : public CBackground
float m_MoveTime;

bool m_IsInit;
bool m_Loading;

void ResetPositions();

Expand All @@ -99,6 +98,7 @@ class CMenuBackground : public CBackground

std::vector<CTheme> m_vThemes;

public:
CMenuBackground();
~CMenuBackground() override {}
virtual int Sizeof() const override { return sizeof(*this); }
Expand All @@ -110,8 +110,10 @@ class CMenuBackground : public CBackground
void LoadMenuBackground(bool HasDayHint = true, bool HasNightHint = true);

bool Render();
bool IsLoading() const { return m_Loading; }

class CCamera *GetCurCamera() override;
const char *LoadingTitle() const override;

void ChangePosition(int PositionNumber);

Expand Down
23 changes: 18 additions & 5 deletions src/game/client/components/menus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ void CMenus::RenderMenubar(CUIRect Box, IClient::EClientState ClientState)
}
}

void CMenus::RenderLoading(const char *pCaption, const char *pContent, int IncreaseCounter, bool RenderLoadingBar, bool RenderMenuBackgroundMap)
void CMenus::RenderLoading(const char *pCaption, const char *pContent, int IncreaseCounter)
{
// TODO: not supported right now due to separate render thread

Expand All @@ -770,18 +770,25 @@ void CMenus::RenderLoading(const char *pCaption, const char *pContent, int Incre
if(Now - m_LoadingState.m_LastRender < std::chrono::nanoseconds(1s) / 60l)
return;

m_LoadingState.m_LastRender = Now;

// need up date this here to get correct
ms_GuiColor = color_cast<ColorRGBA>(ColorHSLA(g_Config.m_UiColor, true));

Ui()->MapScreen();

if(!RenderMenuBackgroundMap || !GameClient()->m_MenuBackground.Render())
if(GameClient()->m_MenuBackground.IsLoading())
{
// Avoid rendering while loading the menu background as this would otherwise
// cause the regular menu background to be rendered for a few frames while
// the menu background is not loaded yet.
return;
}
if(!GameClient()->m_MenuBackground.Render())
{
RenderBackground();
}

m_LoadingState.m_LastRender = Now;

CUIRect Box;
Ui()->Screen()->Margin(160.0f, &Box);

Expand All @@ -798,7 +805,7 @@ void CMenus::RenderLoading(const char *pCaption, const char *pContent, int Incre
Box.HSplitTop(24.0f, &Label, &Box);
Ui()->DoLabel(&Label, pContent, 20.0f, TEXTALIGN_MC);

if(RenderLoadingBar)
if(m_LoadingState.m_Total > 0)
{
CUIRect ProgressBar;
Box.HSplitBottom(30.0f, &Box, nullptr);
Expand All @@ -812,6 +819,12 @@ void CMenus::RenderLoading(const char *pCaption, const char *pContent, int Incre
Client()->UpdateAndSwap();
}

void CMenus::FinishLoading()
{
m_LoadingState.m_Current = 0;
m_LoadingState.m_Total = 0;
}

void CMenus::RenderNews(CUIRect MainView)
{
GameClient()->m_MenuBackground.ChangePosition(CMenuBackground::POS_NEWS);
Expand Down
3 changes: 2 additions & 1 deletion src/game/client/components/menus.h
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,8 @@ class CMenus : public CComponent
CMenus();
virtual int Sizeof() const override { return sizeof(*this); }

void RenderLoading(const char *pCaption, const char *pContent, int IncreaseCounter, bool RenderLoadingBar = true, bool RenderMenuBackgroundMap = true);
void RenderLoading(const char *pCaption, const char *pContent, int IncreaseCounter);
void FinishLoading();

bool IsInit() { return m_IsInit; }

Expand Down
2 changes: 1 addition & 1 deletion src/game/client/components/menus_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ int CMenus::DemolistFetchCallback(const CFsFileInfo *pInfo, int IsDir, int Stora

if(time_get_nanoseconds() - pSelf->m_DemoPopulateStartTime > 500ms)
{
pSelf->RenderLoading(Localize("Loading demo files"), "", 0, false);
pSelf->RenderLoading(Localize("Loading demo files"), "", 0);
}

return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/game/client/components/menus_ingame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,7 @@ int CMenus::GhostlistFetchCallback(const CFsFileInfo *pInfo, int IsDir, int Stor

if(time_get_nanoseconds() - pSelf->m_GhostPopulateStartTime > 500ms)
{
pSelf->RenderLoading(Localize("Loading ghost files"), "", 0, false);
pSelf->RenderLoading(Localize("Loading ghost files"), "", 0);
}

return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/game/client/components/menus_settings_assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ void CMenus::RenderSettingsCustom(CUIRect MainView)
User.m_pUser = this;
User.m_LoadedFunc = [&]() {
if(time_get_nanoseconds() - LoadStartTime > 500ms)
RenderLoading(Localize("Loading assets"), "", 0, false);
RenderLoading(Localize("Loading assets"), "", 0);
};
if(s_CurCustomTab == ASSETS_TAB_ENTITIES)
{
Expand Down
2 changes: 1 addition & 1 deletion src/game/client/components/race_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ int CRaceDemo::RaceDemolistFetchCallback(const CFsFileInfo *pInfo, int IsDir, in

if(time_get_nanoseconds() - pRealUser->m_pThis->m_RaceDemosLoadStartTime > 500ms)
{
pRealUser->m_pThis->GameClient()->m_Menus.RenderLoading(Localize("Loading race demo files"), "", 0, false);
pRealUser->m_pThis->GameClient()->m_Menus.RenderLoading(Localize("Loading race demo files"), "", 0);
}

return 0;
Expand Down
11 changes: 6 additions & 5 deletions src/game/client/gameclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ void CGameClient::OnInit()
dbg_assert(false, "Invalid callback loading detail");
dbg_break();
}
m_Menus.RenderLoading(pTitle, pMessage, 0, false);
m_Menus.RenderLoading(pTitle, pMessage, 0);
});

m_pGraphics = Kernel()->RequestInterface<IGraphics>();
Expand Down Expand Up @@ -428,6 +428,7 @@ void CGameClient::OnInit()
pChecksum->m_aComponentsChecksum[i] = Size;
}

m_Menus.FinishLoading();
log_trace("gameclient", "initialization finished after %.2fms", (time_get() - OnInitStart) * 1000.0f / (float)time_freq());
}

Expand Down Expand Up @@ -553,22 +554,22 @@ void CGameClient::OnConnected()
const char *pConnectCaption = DemoPlayer()->IsPlaying() ? Localize("Preparing demo playback") : Localize("Connected");
const char *pLoadMapContent = Localize("Initializing map logic");
// render loading before skip is calculated
m_Menus.RenderLoading(pConnectCaption, pLoadMapContent, 0, false);
m_Menus.RenderLoading(pConnectCaption, pLoadMapContent, 0);
m_Layers.Init(Kernel()->RequestInterface<IMap>(), false);
m_Collision.Init(Layers());
m_GameWorld.m_Core.InitSwitchers(m_Collision.m_HighestSwitchNumber);
m_RaceHelper.Init(this);

// render loading before going through all components
m_Menus.RenderLoading(pConnectCaption, pLoadMapContent, 0, false);
m_Menus.RenderLoading(pConnectCaption, pLoadMapContent, 0);
for(auto &pComponent : m_vpAll)
{
pComponent->OnMapLoad();
pComponent->OnReset();
}

Client()->SetLoadingStateDetail(IClient::LOADING_STATE_DETAIL_GETTING_READY);
m_Menus.RenderLoading(pConnectCaption, Localize("Sending initial client info"), 0, false);
m_Menus.RenderLoading(pConnectCaption, Localize("Sending initial client info"), 0);

// send the initial info
SendInfo(true);
Expand Down Expand Up @@ -3840,7 +3841,7 @@ void CGameClient::RefreshSkins()
// if skin refreshing takes to long, swap to a loading screen
if(time_get_nanoseconds() - SkinStartLoadTime > 500ms)
{
m_Menus.RenderLoading(Localize("Loading skin files"), "", 0, false);
m_Menus.RenderLoading(Localize("Loading skin files"), "", 0);
}
});

Expand Down

0 comments on commit 7e30a09

Please sign in to comment.