5300: Refactoring: Replace CGameClient::CStack with std::vector<class CComponent *> r=def- a=Robyt3


## Checklist

- [X] Tested the change ingame
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test if it works standalone, system.c especially
- [ ] Considered possible null pointers and out of bounds array indexing
- [ ] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: Robert Müller <robytemueller@gmail.com>
This commit is contained in:
bors[bot] 2022-06-01 20:59:04 +00:00 committed by GitHub
commit 264c6f969d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 105 deletions

View file

@ -71,9 +71,6 @@
using namespace std::chrono_literals; using namespace std::chrono_literals;
CGameClient::CStack::CStack() { m_Num = 0; }
void CGameClient::CStack::Add(class CComponent *pComponent) { m_paComponents[m_Num++] = pComponent; }
const char *CGameClient::Version() const { return GAME_VERSION; } const char *CGameClient::Version() const { return GAME_VERSION; }
const char *CGameClient::NetVersion() const { return GAME_NETVERSION; } const char *CGameClient::NetVersion() const { return GAME_NETVERSION; }
int CGameClient::DDNetVersion() const { return CLIENT_VERSIONNR; } int CGameClient::DDNetVersion() const { return CLIENT_VERSIONNR; }
@ -104,60 +101,58 @@ void CGameClient::OnConsoleInit()
m_NamePlates.SetPlayers(&m_Players); m_NamePlates.SetPlayers(&m_Players);
// make a list of all the systems, make sure to add them in the correct render order // make a list of all the systems, make sure to add them in the correct render order
m_All.Add(&m_Skins); m_vpAll.insert(m_vpInput.end(), {&m_Skins,
m_All.Add(&m_CountryFlags); &m_CountryFlags,
m_All.Add(&m_MapImages); &m_MapImages,
m_All.Add(&m_Effects); // doesn't render anything, just updates effects &m_Effects, // doesn't render anything, just updates effects
m_All.Add(&m_Binds); &m_Binds,
m_All.Add(&m_Binds.m_SpecialBinds); &m_Binds.m_SpecialBinds,
m_All.Add(&m_Controls); &m_Controls,
m_All.Add(&m_Camera); &m_Camera,
m_All.Add(&m_Sounds); &m_Sounds,
m_All.Add(&m_Voting); &m_Voting,
m_All.Add(&m_Particles); // doesn't render anything, just updates all the particles &m_Particles, // doesn't render anything, just updates all the particles
m_All.Add(&m_RaceDemo); &m_RaceDemo,
m_All.Add(&m_MapSounds); &m_MapSounds,
&m_BackGround, // render instead of m_MapLayersBackGround when g_Config.m_ClOverlayEntities == 100
m_All.Add(&m_BackGround); //render instead of m_MapLayersBackGround when g_Config.m_ClOverlayEntities == 100 &m_MapLayersBackGround, // first to render
m_All.Add(&m_MapLayersBackGround); // first to render &m_Particles.m_RenderTrail,
m_All.Add(&m_Particles.m_RenderTrail); &m_Items,
m_All.Add(&m_Items); &m_Players,
m_All.Add(&m_Players); &m_Ghost,
m_All.Add(&m_Ghost); &m_MapLayersForeGround,
m_All.Add(&m_MapLayersForeGround); &m_Particles.m_RenderExplosions,
m_All.Add(&m_Particles.m_RenderExplosions); &m_NamePlates,
m_All.Add(&m_NamePlates); &m_Particles.m_RenderGeneral,
m_All.Add(&m_Particles.m_RenderGeneral); &m_FreezeBars,
m_All.Add(&m_FreezeBars); &m_DamageInd,
m_All.Add(&m_DamageInd); &m_Hud,
m_All.Add(&m_Hud); &m_Spectator,
m_All.Add(&m_Spectator); &m_Emoticon,
m_All.Add(&m_Emoticon); &m_KillMessages,
m_All.Add(&m_KillMessages); &m_Chat,
m_All.Add(&m_Chat); &m_Broadcast,
m_All.Add(&m_Broadcast); &m_DebugHud,
m_All.Add(&m_DebugHud); &m_Scoreboard,
m_All.Add(&m_Scoreboard); &m_Statboard,
m_All.Add(&m_Statboard); &m_Motd,
m_All.Add(&m_Motd); &m_Menus,
m_All.Add(&m_Menus); &m_Tooltips,
m_All.Add(&m_Tooltips); &CMenus::m_Binder,
m_All.Add(&CMenus::m_Binder); &m_GameConsole,
m_All.Add(&m_GameConsole); &m_MenuBackground});
m_All.Add(&m_MenuBackground);
// build the input stack // build the input stack
m_Input.Add(&CMenus::m_Binder); // this will take over all input when we want to bind a key m_vpInput.insert(m_vpInput.end(), {&CMenus::m_Binder, // this will take over all input when we want to bind a key
m_Input.Add(&m_Binds.m_SpecialBinds); &m_Binds.m_SpecialBinds,
m_Input.Add(&m_GameConsole); &m_GameConsole,
m_Input.Add(&m_Chat); // chat has higher prio due to tha you can quit it by pressing esc &m_Chat, // chat has higher prio due to tha you can quit it by pressing esc
m_Input.Add(&m_Motd); // for pressing esc to remove it &m_Motd, // for pressing esc to remove it
m_Input.Add(&m_Menus); &m_Menus,
m_Input.Add(&m_Spectator); &m_Spectator,
m_Input.Add(&m_Emoticon); &m_Emoticon,
m_Input.Add(&m_Controls); &m_Controls,
m_Input.Add(&m_Binds); &m_Binds});
// add the some console commands // add the some console commands
Console()->Register("team", "i[team-id]", CFGFLAG_CLIENT, ConTeam, this, "Switch team"); Console()->Register("team", "i[team-id]", CFGFLAG_CLIENT, ConTeam, this, "Switch team");
@ -185,12 +180,12 @@ void CGameClient::OnConsoleInit()
// register tune zone command to allow the client prediction to load tunezones from the map // register tune zone command to allow the client prediction to load tunezones from the map
Console()->Register("tune_zone", "i[zone] s[tuning] i[value]", CFGFLAG_CLIENT | CFGFLAG_GAME, ConTuneZone, this, "Tune in zone a variable to value"); Console()->Register("tune_zone", "i[zone] s[tuning] i[value]", CFGFLAG_CLIENT | CFGFLAG_GAME, ConTuneZone, this, "Tune in zone a variable to value");
for(int i = 0; i < m_All.m_Num; i++) for(auto &pComponent : m_vpAll)
m_All.m_paComponents[i]->m_pClient = this; pComponent->m_pClient = this;
// let all the other components register their console commands // let all the other components register their console commands
for(int i = 0; i < m_All.m_Num; i++) for(auto &pComponent : m_vpAll)
m_All.m_paComponents[i]->OnConsoleInit(); pComponent->OnConsoleInit();
// //
Console()->Chain("player_name", ConchainSpecialInfoupdate, this); Console()->Chain("player_name", ConchainSpecialInfoupdate, this);
@ -253,9 +248,9 @@ void CGameClient::OnInit()
Client()->UpdateAndSwap(); Client()->UpdateAndSwap();
// init all components // init all components
for(int i = m_All.m_Num - 1; i >= 0; --i) for(int i = m_vpAll.size() - 1; i >= 0; --i)
{ {
m_All.m_paComponents[i]->OnInit(); m_vpAll[i]->OnInit();
// try to render a frame after each component, also flushes GPU uploads // try to render a frame after each component, also flushes GPU uploads
if(m_Menus.IsInit()) if(m_Menus.IsInit())
m_Menus.RenderLoading(false); m_Menus.RenderLoading(false);
@ -284,8 +279,8 @@ void CGameClient::OnInit()
m_Menus.RenderLoading(false); m_Menus.RenderLoading(false);
} }
for(int i = 0; i < m_All.m_Num; i++) for(auto &pComponent : m_vpAll)
m_All.m_paComponents[i]->OnReset(); pComponent->OnReset();
m_ServerMode = SERVERMODE_PURE; m_ServerMode = SERVERMODE_PURE;
@ -341,14 +336,14 @@ void CGameClient::OnInit()
CChecksumData *pChecksum = Client()->ChecksumData(); CChecksumData *pChecksum = Client()->ChecksumData();
pChecksum->m_SizeofGameClient = sizeof(*this); pChecksum->m_SizeofGameClient = sizeof(*this);
pChecksum->m_NumComponents = m_All.m_Num; pChecksum->m_NumComponents = m_vpAll.size();
for(int i = 0; i < m_All.m_Num; i++) for(size_t i = 0; i < m_vpAll.size(); i++)
{ {
if(i >= (int)(std::size(pChecksum->m_aComponentsChecksum))) if(i >= std::size(pChecksum->m_aComponentsChecksum))
{ {
break; break;
} }
int Size = m_All.m_paComponents[i]->Sizeof(); int Size = m_vpAll[i]->Sizeof();
pChecksum->m_aComponentsChecksum[i] = Size; pChecksum->m_aComponentsChecksum[i] = Size;
} }
} }
@ -360,9 +355,9 @@ void CGameClient::OnUpdate()
Input()->MouseRelative(&x, &y); Input()->MouseRelative(&x, &y);
if(x != 0.0f || y != 0.0f) if(x != 0.0f || y != 0.0f)
{ {
for(int h = 0; h < m_Input.m_Num; h++) for(auto &pComponent : m_vpInput)
{ {
if(m_Input.m_paComponents[h]->OnMouseMove(x, y)) if(pComponent->OnMouseMove(x, y))
break; break;
} }
} }
@ -374,9 +369,9 @@ void CGameClient::OnUpdate()
if(!Input()->IsEventValid(&e)) if(!Input()->IsEventValid(&e))
continue; continue;
for(int h = 0; h < m_Input.m_Num; h++) for(auto &pComponent : m_vpInput)
{ {
if(m_Input.m_paComponents[h]->OnInput(e)) if(pComponent->OnInput(e))
break; break;
} }
} }
@ -466,10 +461,10 @@ void CGameClient::OnConnected()
i += pGameTiles[i].m_Skip; i += pGameTiles[i].m_Skip;
} }
for(int i = 0; i < m_All.m_Num; i++) for(auto &pComponent : m_vpAll)
{ {
m_All.m_paComponents[i]->OnMapLoad(); pComponent->OnMapLoad();
m_All.m_paComponents[i]->OnReset(); pComponent->OnReset();
} }
m_ServerMode = SERVERMODE_PURE; m_ServerMode = SERVERMODE_PURE;
@ -510,8 +505,8 @@ void CGameClient::OnReset()
for(auto &Client : m_aClients) for(auto &Client : m_aClients)
Client.Reset(); Client.Reset();
for(int i = 0; i < m_All.m_Num; i++) for(auto &pComponent : m_vpAll)
m_All.m_paComponents[i]->OnReset(); pComponent->OnReset();
m_DemoSpecID = SPEC_FOLLOW; m_DemoSpecID = SPEC_FOLLOW;
m_FlagDropTick[TEAM_RED] = 0; m_FlagDropTick[TEAM_RED] = 0;
@ -607,8 +602,8 @@ void CGameClient::OnRender()
} }
// render all systems // render all systems
for(int i = 0; i < m_All.m_Num; i++) for(auto &pComponent : m_vpAll)
m_All.m_paComponents[i]->OnRender(); pComponent->OnRender();
// clear all events/input for this frame // clear all events/input for this frame
Input()->Clear(); Input()->Clear();
@ -680,8 +675,8 @@ int CGameClient::GetLastRaceTick()
void CGameClient::OnRelease() void CGameClient::OnRelease()
{ {
// release all systems // release all systems
for(int i = 0; i < m_All.m_Num; i++) for(auto &pComponent : m_vpAll)
m_All.m_paComponents[i]->OnRelease(); pComponent->OnRelease();
} }
void CGameClient::OnMessage(int MsgId, CUnpacker *pUnpacker, int Conn, bool Dummy) void CGameClient::OnMessage(int MsgId, CUnpacker *pUnpacker, int Conn, bool Dummy)
@ -737,8 +732,8 @@ void CGameClient::OnMessage(int MsgId, CUnpacker *pUnpacker, int Conn, bool Dumm
} }
// TODO: this should be done smarter // TODO: this should be done smarter
for(int i = 0; i < m_All.m_Num; i++) for(auto &pComponent : m_vpAll)
m_All.m_paComponents[i]->OnMessage(MsgId, pRawMsg); pComponent->OnMessage(MsgId, pRawMsg);
if(MsgId == NETMSGTYPE_SV_READYTOENTER) if(MsgId == NETMSGTYPE_SV_READYTOENTER)
{ {
@ -824,14 +819,14 @@ void CGameClient::OnStateChange(int NewState, int OldState)
OnReset(); OnReset();
// then change the state // then change the state
for(int i = 0; i < m_All.m_Num; i++) for(auto &pComponent : m_vpAll)
m_All.m_paComponents[i]->OnStateChange(NewState, OldState); pComponent->OnStateChange(NewState, OldState);
} }
void CGameClient::OnShutdown() void CGameClient::OnShutdown()
{ {
for(int i = 0; i < m_All.m_Num; i++) for(auto &pComponent : m_vpAll)
m_All.m_paComponents[i]->OnShutdown(); pComponent->OnShutdown();
} }
void CGameClient::OnEnterGame() void CGameClient::OnEnterGame()
@ -862,8 +857,8 @@ void CGameClient::OnFlagGrab(int TeamID)
void CGameClient::OnWindowResize() void CGameClient::OnWindowResize()
{ {
for(int i = 0; i < m_All.m_Num; i++) for(auto &pComponent : m_vpAll)
m_All.m_paComponents[i]->OnWindowResize(); pComponent->OnWindowResize();
UI()->OnWindowResize(); UI()->OnWindowResize();
TextRender()->OnWindowResize(); TextRender()->OnWindowResize();

View file

@ -146,23 +146,8 @@ public:
CTooltips m_Tooltips; CTooltips m_Tooltips;
private: private:
class CStack std::vector<class CComponent *> m_vpAll;
{ std::vector<class CComponent *> m_vpInput;
public:
enum
{
MAX_COMPONENTS = 64,
};
CStack();
void Add(class CComponent *pComponent);
class CComponent *m_paComponents[MAX_COMPONENTS];
int m_Num;
};
CStack m_All;
CStack m_Input;
CNetObjHandler m_NetObjHandler; CNetObjHandler m_NetObjHandler;
class IEngine *m_pEngine; class IEngine *m_pEngine;