From c24d0c0ace7faad1135f45031c7e9ad3e75a3016 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Wed, 26 Jun 2024 20:05:20 +0200 Subject: [PATCH 1/6] Remove unnecessary `CNamePlates::RenderNameplate` function The client ID of `CNetObj_PlayerInfo` objects is validated prior to calling the `CNamePlates::RenderNameplate` function, so the additional checks and arguments to handle the client ID not being valid where never used. Rename existing `RenderNameplatePos` function to `RenderNameplate`. --- src/game/client/components/nameplates.cpp | 25 +++-------------------- src/game/client/components/nameplates.h | 6 +----- 2 files changed, 4 insertions(+), 27 deletions(-) diff --git a/src/game/client/components/nameplates.cpp b/src/game/client/components/nameplates.cpp index e125375bd..1eba446ba 100644 --- a/src/game/client/components/nameplates.cpp +++ b/src/game/client/components/nameplates.cpp @@ -14,23 +14,7 @@ #include "controls.h" #include "nameplates.h" -void CNamePlates::RenderNameplate( - const CNetObj_Character *pPrevChar, - const CNetObj_Character *pPlayerChar, - const CNetObj_PlayerInfo *pPlayerInfo) -{ - int ClientId = pPlayerInfo->m_ClientId; - - vec2 Position; - if(ClientId >= 0 && ClientId < MAX_CLIENTS) - Position = m_pClient->m_aClients[ClientId].m_RenderPos; - else - Position = mix(vec2(pPrevChar->m_X, pPrevChar->m_Y), vec2(pPlayerChar->m_X, pPlayerChar->m_Y), Client()->IntraGameTick(g_Config.m_ClDummy)); - - RenderNameplatePos(Position, pPlayerInfo, 1.0f); -} - -void CNamePlates::RenderNameplatePos(vec2 Position, const CNetObj_PlayerInfo *pPlayerInfo, float Alpha, bool ForceAlpha) +void CNamePlates::RenderNameplate(vec2 Position, const CNetObj_PlayerInfo *pPlayerInfo, float Alpha, bool ForceAlpha) { int ClientId = pPlayerInfo->m_ClientId; @@ -328,7 +312,7 @@ void CNamePlates::OnRender() // don't render offscreen if(!(pRenderPos->x < ScreenX0) && !(pRenderPos->x > ScreenX1) && !(pRenderPos->y < ScreenY0) && !(pRenderPos->y > ScreenY1)) { - RenderNameplatePos(m_pClient->m_aClients[i].m_SpecChar, pInfo, 0.4f, true); + RenderNameplate(m_pClient->m_aClients[i].m_SpecChar, pInfo, 0.4f, true); } } if(m_pClient->m_Snap.m_aCharacters[i].m_Active) @@ -338,10 +322,7 @@ void CNamePlates::OnRender() // don't render offscreen if(!(pRenderPos->x < ScreenX0) && !(pRenderPos->x > ScreenX1) && !(pRenderPos->y < ScreenY0) && !(pRenderPos->y > ScreenY1)) { - RenderNameplate( - &m_pClient->m_Snap.m_aCharacters[i].m_Prev, - &m_pClient->m_Snap.m_aCharacters[i].m_Cur, - pInfo); + RenderNameplate(m_pClient->m_aClients[i].m_RenderPos, pInfo, 1.0f, false); } } } diff --git a/src/game/client/components/nameplates.h b/src/game/client/components/nameplates.h index b8b81e2e3..48dc3dfff 100644 --- a/src/game/client/components/nameplates.h +++ b/src/game/client/components/nameplates.h @@ -42,11 +42,7 @@ struct SPlayerNamePlate class CNamePlates : public CComponent { - void RenderNameplate( - const CNetObj_Character *pPrevChar, - const CNetObj_Character *pPlayerChar, - const CNetObj_PlayerInfo *pPlayerInfo); - void RenderNameplatePos(vec2 Position, const CNetObj_PlayerInfo *pPlayerInfo, float Alpha, bool ForceAlpha = false); + void RenderNameplate(vec2 Position, const CNetObj_PlayerInfo *pPlayerInfo, float Alpha, bool ForceAlpha); SPlayerNamePlate m_aNamePlates[MAX_CLIENTS]; From f0fb1fd196f2de66e3505f983b6eb955c79042fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Wed, 26 Jun 2024 20:05:20 +0200 Subject: [PATCH 2/6] Avoid using pointer for `vec2`, use `in_range` function --- src/game/client/components/nameplates.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/game/client/components/nameplates.cpp b/src/game/client/components/nameplates.cpp index 1eba446ba..f0407938b 100644 --- a/src/game/client/components/nameplates.cpp +++ b/src/game/client/components/nameplates.cpp @@ -304,25 +304,24 @@ void CNamePlates::OnRender() continue; } - vec2 *pRenderPos; if(m_pClient->m_aClients[i].m_SpecCharPresent) { // Each player can also have a spec char whose nameplate is displayed independently - pRenderPos = &m_pClient->m_aClients[i].m_SpecChar; + const vec2 RenderPos = m_pClient->m_aClients[i].m_SpecChar; // don't render offscreen - if(!(pRenderPos->x < ScreenX0) && !(pRenderPos->x > ScreenX1) && !(pRenderPos->y < ScreenY0) && !(pRenderPos->y > ScreenY1)) + if(in_range(RenderPos.x, ScreenX0, ScreenX1) && in_range(RenderPos.y, ScreenY0, ScreenY1)) { - RenderNameplate(m_pClient->m_aClients[i].m_SpecChar, pInfo, 0.4f, true); + RenderNameplate(RenderPos, pInfo, 0.4f, true); } } if(m_pClient->m_Snap.m_aCharacters[i].m_Active) { // Only render nameplates for active characters - pRenderPos = &m_pClient->m_aClients[i].m_RenderPos; + const vec2 RenderPos = m_pClient->m_aClients[i].m_RenderPos; // don't render offscreen - if(!(pRenderPos->x < ScreenX0) && !(pRenderPos->x > ScreenX1) && !(pRenderPos->y < ScreenY0) && !(pRenderPos->y > ScreenY1)) + if(in_range(RenderPos.x, ScreenX0, ScreenX1) && in_range(RenderPos.y, ScreenY0, ScreenY1)) { - RenderNameplate(m_pClient->m_aClients[i].m_RenderPos, pInfo, 1.0f, false); + RenderNameplate(RenderPos, pInfo, 1.0f, false); } } } From b30dde56b6e0b48e0e927e582e7205d99cc65879 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Wed, 26 Jun 2024 20:05:20 +0200 Subject: [PATCH 3/6] Extract variables to improve readability and reduce duplicate code --- src/game/client/components/nameplates.cpp | 81 ++++++++++++----------- 1 file changed, 44 insertions(+), 37 deletions(-) diff --git a/src/game/client/components/nameplates.cpp b/src/game/client/components/nameplates.cpp index f0407938b..1c667325f 100644 --- a/src/game/client/components/nameplates.cpp +++ b/src/game/client/components/nameplates.cpp @@ -16,9 +16,9 @@ void CNamePlates::RenderNameplate(vec2 Position, const CNetObj_PlayerInfo *pPlayerInfo, float Alpha, bool ForceAlpha) { - int ClientId = pPlayerInfo->m_ClientId; - - bool OtherTeam = m_pClient->IsOtherTeam(ClientId); + SPlayerNamePlate &NamePlate = m_aNamePlates[pPlayerInfo->m_ClientId]; + const auto &ClientData = m_pClient->m_aClients[pPlayerInfo->m_ClientId]; + bool OtherTeam = m_pClient->IsOtherTeam(pPlayerInfo->m_ClientId); float FontSize = 18.0f + 20.0f * g_Config.m_ClNameplatesSize / 100.0f; float FontSizeClan = 18.0f + 20.0f * g_Config.m_ClNameplatesClanSize / 100.0f; @@ -33,7 +33,7 @@ void CNamePlates::RenderNameplate(vec2 Position, const CNetObj_PlayerInfo *pPlay if(IVideo::Current()) ShowDirection = g_Config.m_ClVideoShowDirection; #endif - if((ShowDirection && ShowDirection != 3 && !pPlayerInfo->m_Local) || (ShowDirection >= 2 && pPlayerInfo->m_Local) || (ShowDirection == 3 && Client()->DummyConnected() && Client()->State() != IClient::STATE_DEMOPLAYBACK && ClientId == m_pClient->m_aLocalIds[!g_Config.m_ClDummy])) + if((ShowDirection && ShowDirection != 3 && !pPlayerInfo->m_Local) || (ShowDirection >= 2 && pPlayerInfo->m_Local) || (ShowDirection == 3 && Client()->DummyConnected() && Client()->State() != IClient::STATE_DEMOPLAYBACK && pPlayerInfo->m_ClientId == m_pClient->m_aLocalIds[!g_Config.m_ClDummy])) { Graphics()->SetColor(1.0f, 1.0f, 1.0f, 1.0f); Graphics()->QuadsSetRotation(0); @@ -42,21 +42,24 @@ void CNamePlates::RenderNameplate(vec2 Position, const CNetObj_PlayerInfo *pPlay YOffset -= ShowDirectionImgSize; vec2 ShowDirectionPos = vec2(Position.x - 11.0f, YOffset); - bool DirLeft = m_pClient->m_Snap.m_aCharacters[pPlayerInfo->m_ClientId].m_Cur.m_Direction == -1; - bool DirRight = m_pClient->m_Snap.m_aCharacters[pPlayerInfo->m_ClientId].m_Cur.m_Direction == 1; - bool Jump = m_pClient->m_Snap.m_aCharacters[pPlayerInfo->m_ClientId].m_Cur.m_Jumped & 1; + const auto &Character = m_pClient->m_Snap.m_aCharacters[pPlayerInfo->m_ClientId]; + bool DirLeft = Character.m_Cur.m_Direction == -1; + bool DirRight = Character.m_Cur.m_Direction == 1; + bool Jump = Character.m_Cur.m_Jumped & 1; if(pPlayerInfo->m_Local && Client()->State() != IClient::STATE_DEMOPLAYBACK) { - DirLeft = m_pClient->m_Controls.m_aInputData[g_Config.m_ClDummy].m_Direction == -1; - DirRight = m_pClient->m_Controls.m_aInputData[g_Config.m_ClDummy].m_Direction == 1; - Jump = m_pClient->m_Controls.m_aInputData[g_Config.m_ClDummy].m_Jump == 1; + const auto &InputData = m_pClient->m_Controls.m_aInputData[g_Config.m_ClDummy]; + DirLeft = InputData.m_Direction == -1; + DirRight = InputData.m_Direction == 1; + Jump = InputData.m_Jump == 1; } if(Client()->DummyConnected() && Client()->State() != IClient::STATE_DEMOPLAYBACK && pPlayerInfo->m_ClientId == m_pClient->m_aLocalIds[!g_Config.m_ClDummy]) { - DirLeft = m_pClient->m_Controls.m_aInputData[!g_Config.m_ClDummy].m_Direction == -1; - DirRight = m_pClient->m_Controls.m_aInputData[!g_Config.m_ClDummy].m_Direction == 1; - Jump = m_pClient->m_Controls.m_aInputData[!g_Config.m_ClDummy].m_Jump == 1; + const auto &InputData = m_pClient->m_Controls.m_aInputData[!g_Config.m_ClDummy]; + DirLeft = InputData.m_Direction == -1; + DirRight = InputData.m_Direction == 1; + Jump = InputData.m_Jump == 1; } if(DirLeft) { @@ -86,11 +89,10 @@ void CNamePlates::RenderNameplate(vec2 Position, const CNetObj_PlayerInfo *pPlay if(g_Config.m_ClNameplatesAlways == 0) a = clamp(1 - std::pow(distance(m_pClient->m_Controls.m_aTargetPos[g_Config.m_ClDummy], Position) / 200.0f, 16.0f), 0.0f, 1.0f); - const char *pName = m_pClient->m_aClients[pPlayerInfo->m_ClientId].m_aName; - if(str_comp(pName, m_aNamePlates[ClientId].m_aName) != 0 || FontSize != m_aNamePlates[ClientId].m_NameTextFontSize) + if(str_comp(ClientData.m_aName, NamePlate.m_aName) != 0 || FontSize != NamePlate.m_NameTextFontSize) { - str_copy(m_aNamePlates[ClientId].m_aName, pName); - m_aNamePlates[ClientId].m_NameTextFontSize = FontSize; + str_copy(NamePlate.m_aName, ClientData.m_aName); + NamePlate.m_NameTextFontSize = FontSize; CTextCursor Cursor; TextRender()->SetCursor(&Cursor, 0, 0, FontSize, TEXTFLAG_RENDER); @@ -101,19 +103,18 @@ void CNamePlates::RenderNameplate(vec2 Position, const CNetObj_PlayerInfo *pPlay Graphics()->GetScreen(&ScreenX0, &ScreenY0, &ScreenX1, &ScreenY1); RenderTools()->MapScreenToInterface(m_pClient->m_Camera.m_Center.x, m_pClient->m_Camera.m_Center.y); - m_aNamePlates[ClientId].m_NameTextWidth = TextRender()->TextWidth(FontSize, pName, -1, -1.0f); + NamePlate.m_NameTextWidth = TextRender()->TextWidth(FontSize, ClientData.m_aName, -1, -1.0f); - TextRender()->RecreateTextContainer(m_aNamePlates[ClientId].m_NameTextContainerIndex, &Cursor, pName); + TextRender()->RecreateTextContainer(NamePlate.m_NameTextContainerIndex, &Cursor, ClientData.m_aName); Graphics()->MapScreen(ScreenX0, ScreenY0, ScreenX1, ScreenY1); } if(g_Config.m_ClNameplatesClan) { - const char *pClan = m_pClient->m_aClients[ClientId].m_aClan; - if(str_comp(pClan, m_aNamePlates[ClientId].m_aClanName) != 0 || FontSizeClan != m_aNamePlates[ClientId].m_ClanNameTextFontSize) + if(str_comp(ClientData.m_aClan, NamePlate.m_aClanName) != 0 || FontSizeClan != NamePlate.m_ClanNameTextFontSize) { - str_copy(m_aNamePlates[ClientId].m_aClanName, pClan); - m_aNamePlates[ClientId].m_ClanNameTextFontSize = FontSizeClan; + str_copy(NamePlate.m_aClanName, ClientData.m_aClan); + NamePlate.m_ClanNameTextFontSize = FontSizeClan; CTextCursor Cursor; TextRender()->SetCursor(&Cursor, 0, 0, FontSizeClan, TEXTFLAG_RENDER); @@ -124,16 +125,22 @@ void CNamePlates::RenderNameplate(vec2 Position, const CNetObj_PlayerInfo *pPlay Graphics()->GetScreen(&ScreenX0, &ScreenY0, &ScreenX1, &ScreenY1); RenderTools()->MapScreenToInterface(m_pClient->m_Camera.m_Center.x, m_pClient->m_Camera.m_Center.y); - m_aNamePlates[ClientId].m_ClanNameTextWidth = TextRender()->TextWidth(FontSizeClan, pClan, -1, -1.0f); + NamePlate.m_ClanNameTextWidth = TextRender()->TextWidth(FontSizeClan, ClientData.m_aClan, -1, -1.0f); - TextRender()->RecreateTextContainer(m_aNamePlates[ClientId].m_ClanNameTextContainerIndex, &Cursor, pClan); + TextRender()->RecreateTextContainer(NamePlate.m_ClanNameTextContainerIndex, &Cursor, ClientData.m_aClan); Graphics()->MapScreen(ScreenX0, ScreenY0, ScreenX1, ScreenY1); } } - float tw = m_aNamePlates[ClientId].m_NameTextWidth; - if(g_Config.m_ClNameplatesTeamcolors && m_pClient->m_Teams.Team(ClientId)) - rgb = m_pClient->GetDDTeamColor(m_pClient->m_Teams.Team(ClientId), 0.75f); + float tw = NamePlate.m_NameTextWidth; + if(g_Config.m_ClNameplatesTeamcolors) + { + const int Team = m_pClient->m_Teams.Team(pPlayerInfo->m_ClientId); + if(Team) + { + rgb = m_pClient->GetDDTeamColor(Team, 0.75f); + } + } ColorRGBA TColor; ColorRGBA TOutlineColor; @@ -150,29 +157,29 @@ void CNamePlates::RenderNameplate(vec2 Position, const CNetObj_PlayerInfo *pPlay } if(g_Config.m_ClNameplatesTeamcolors && m_pClient->m_Snap.m_pGameInfoObj && m_pClient->m_Snap.m_pGameInfoObj->m_GameFlags & GAMEFLAG_TEAMS) { - if(m_pClient->m_aClients[ClientId].m_Team == TEAM_RED) + if(ClientData.m_Team == TEAM_RED) TColor = ColorRGBA(1.0f, 0.5f, 0.5f, a); - else if(m_pClient->m_aClients[ClientId].m_Team == TEAM_BLUE) + else if(ClientData.m_Team == TEAM_BLUE) TColor = ColorRGBA(0.7f, 0.7f, 1.0f, a); } TOutlineColor.a *= Alpha; TColor.a *= Alpha; - if(m_aNamePlates[ClientId].m_NameTextContainerIndex.Valid()) + if(NamePlate.m_NameTextContainerIndex.Valid()) { YOffset -= FontSize; - TextRender()->RenderTextContainer(m_aNamePlates[ClientId].m_NameTextContainerIndex, TColor, TOutlineColor, Position.x - tw / 2.0f, YOffset); + TextRender()->RenderTextContainer(NamePlate.m_NameTextContainerIndex, TColor, TOutlineColor, Position.x - tw / 2.0f, YOffset); } if(g_Config.m_ClNameplatesClan) { YOffset -= FontSizeClan; - if(m_aNamePlates[ClientId].m_ClanNameTextContainerIndex.Valid()) - TextRender()->RenderTextContainer(m_aNamePlates[ClientId].m_ClanNameTextContainerIndex, TColor, TOutlineColor, Position.x - m_aNamePlates[ClientId].m_ClanNameTextWidth / 2.0f, YOffset); + if(NamePlate.m_ClanNameTextContainerIndex.Valid()) + TextRender()->RenderTextContainer(NamePlate.m_ClanNameTextContainerIndex, TColor, TOutlineColor, Position.x - NamePlate.m_ClanNameTextWidth / 2.0f, YOffset); } - if(g_Config.m_ClNameplatesFriendMark && m_pClient->m_aClients[ClientId].m_Friend) + if(g_Config.m_ClNameplatesFriendMark && ClientData.m_Friend) { YOffset -= FontSize; char aFriendMark[] = "♥"; @@ -209,10 +216,10 @@ void CNamePlates::RenderNameplate(vec2 Position, const CNetObj_PlayerInfo *pPlay { int SelectedId = Following ? m_pClient->m_Snap.m_SpecInfo.m_SpectatorId : m_pClient->m_Snap.m_LocalClientId; const CGameClient::CSnapState::CCharacterInfo &Selected = m_pClient->m_Snap.m_aCharacters[SelectedId]; - const CGameClient::CSnapState::CCharacterInfo &Other = m_pClient->m_Snap.m_aCharacters[ClientId]; + const CGameClient::CSnapState::CCharacterInfo &Other = m_pClient->m_Snap.m_aCharacters[pPlayerInfo->m_ClientId]; if(Selected.m_HasExtendedData && Other.m_HasExtendedData) { - if(SelectedId == ClientId) + if(SelectedId == pPlayerInfo->m_ClientId) TextRender()->TextColor(rgb); else { From 0222e4ae9659d56b6dd5b8cdb2db43e229fae528 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Wed, 26 Jun 2024 20:05:20 +0200 Subject: [PATCH 4/6] Avoid duplicate calculations of nameplate text width Get name/clan bounding box size from the text container index instead of calculating it twice. --- src/game/client/components/nameplates.cpp | 13 ++----------- src/game/client/components/nameplates.h | 7 ++----- 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/src/game/client/components/nameplates.cpp b/src/game/client/components/nameplates.cpp index 1c667325f..0813bb3b4 100644 --- a/src/game/client/components/nameplates.cpp +++ b/src/game/client/components/nameplates.cpp @@ -96,15 +96,11 @@ void CNamePlates::RenderNameplate(vec2 Position, const CNetObj_PlayerInfo *pPlay CTextCursor Cursor; TextRender()->SetCursor(&Cursor, 0, 0, FontSize, TEXTFLAG_RENDER); - Cursor.m_LineWidth = -1; // create nameplates at standard zoom float ScreenX0, ScreenY0, ScreenX1, ScreenY1; Graphics()->GetScreen(&ScreenX0, &ScreenY0, &ScreenX1, &ScreenY1); RenderTools()->MapScreenToInterface(m_pClient->m_Camera.m_Center.x, m_pClient->m_Camera.m_Center.y); - - NamePlate.m_NameTextWidth = TextRender()->TextWidth(FontSize, ClientData.m_aName, -1, -1.0f); - TextRender()->RecreateTextContainer(NamePlate.m_NameTextContainerIndex, &Cursor, ClientData.m_aName); Graphics()->MapScreen(ScreenX0, ScreenY0, ScreenX1, ScreenY1); } @@ -118,21 +114,16 @@ void CNamePlates::RenderNameplate(vec2 Position, const CNetObj_PlayerInfo *pPlay CTextCursor Cursor; TextRender()->SetCursor(&Cursor, 0, 0, FontSizeClan, TEXTFLAG_RENDER); - Cursor.m_LineWidth = -1; // create nameplates at standard zoom float ScreenX0, ScreenY0, ScreenX1, ScreenY1; Graphics()->GetScreen(&ScreenX0, &ScreenY0, &ScreenX1, &ScreenY1); RenderTools()->MapScreenToInterface(m_pClient->m_Camera.m_Center.x, m_pClient->m_Camera.m_Center.y); - - NamePlate.m_ClanNameTextWidth = TextRender()->TextWidth(FontSizeClan, ClientData.m_aClan, -1, -1.0f); - TextRender()->RecreateTextContainer(NamePlate.m_ClanNameTextContainerIndex, &Cursor, ClientData.m_aClan); Graphics()->MapScreen(ScreenX0, ScreenY0, ScreenX1, ScreenY1); } } - float tw = NamePlate.m_NameTextWidth; if(g_Config.m_ClNameplatesTeamcolors) { const int Team = m_pClient->m_Teams.Team(pPlayerInfo->m_ClientId); @@ -169,14 +160,14 @@ void CNamePlates::RenderNameplate(vec2 Position, const CNetObj_PlayerInfo *pPlay if(NamePlate.m_NameTextContainerIndex.Valid()) { YOffset -= FontSize; - TextRender()->RenderTextContainer(NamePlate.m_NameTextContainerIndex, TColor, TOutlineColor, Position.x - tw / 2.0f, YOffset); + TextRender()->RenderTextContainer(NamePlate.m_NameTextContainerIndex, TColor, TOutlineColor, Position.x - TextRender()->GetBoundingBoxTextContainer(NamePlate.m_NameTextContainerIndex).m_W / 2.0f, YOffset); } if(g_Config.m_ClNameplatesClan) { YOffset -= FontSizeClan; if(NamePlate.m_ClanNameTextContainerIndex.Valid()) - TextRender()->RenderTextContainer(NamePlate.m_ClanNameTextContainerIndex, TColor, TOutlineColor, Position.x - NamePlate.m_ClanNameTextWidth / 2.0f, YOffset); + TextRender()->RenderTextContainer(NamePlate.m_ClanNameTextContainerIndex, TColor, TOutlineColor, Position.x - TextRender()->GetBoundingBoxTextContainer(NamePlate.m_ClanNameTextContainerIndex).m_W / 2.0f, YOffset); } if(g_Config.m_ClNameplatesFriendMark && ClientData.m_Friend) diff --git a/src/game/client/components/nameplates.h b/src/game/client/components/nameplates.h index 48dc3dfff..ec70944bb 100644 --- a/src/game/client/components/nameplates.h +++ b/src/game/client/components/nameplates.h @@ -23,19 +23,16 @@ struct SPlayerNamePlate { m_NameTextContainerIndex.Reset(); m_ClanNameTextContainerIndex.Reset(); - m_aName[0] = 0; - m_aClanName[0] = 0; - m_NameTextWidth = m_ClanNameTextWidth = 0.f; + m_aName[0] = '\0'; + m_aClanName[0] = '\0'; m_NameTextFontSize = m_ClanNameTextFontSize = 0; } char m_aName[MAX_NAME_LENGTH]; - float m_NameTextWidth; STextContainerIndex m_NameTextContainerIndex; float m_NameTextFontSize; char m_aClanName[MAX_CLAN_LENGTH]; - float m_ClanNameTextWidth; STextContainerIndex m_ClanNameTextContainerIndex; float m_ClanNameTextFontSize; }; From 3afdc20df257407dfcac1258b0786b6b35eb8f26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Wed, 26 Jun 2024 20:05:20 +0200 Subject: [PATCH 5/6] Rename clan related variables, removing `Name` from name --- src/game/client/components/nameplates.cpp | 14 +++++++------- src/game/client/components/nameplates.h | 12 ++++++------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/game/client/components/nameplates.cpp b/src/game/client/components/nameplates.cpp index 0813bb3b4..e6cfec9c2 100644 --- a/src/game/client/components/nameplates.cpp +++ b/src/game/client/components/nameplates.cpp @@ -107,10 +107,10 @@ void CNamePlates::RenderNameplate(vec2 Position, const CNetObj_PlayerInfo *pPlay if(g_Config.m_ClNameplatesClan) { - if(str_comp(ClientData.m_aClan, NamePlate.m_aClanName) != 0 || FontSizeClan != NamePlate.m_ClanNameTextFontSize) + if(str_comp(ClientData.m_aClan, NamePlate.m_aClan) != 0 || FontSizeClan != NamePlate.m_ClanTextFontSize) { - str_copy(NamePlate.m_aClanName, ClientData.m_aClan); - NamePlate.m_ClanNameTextFontSize = FontSizeClan; + str_copy(NamePlate.m_aClan, ClientData.m_aClan); + NamePlate.m_ClanTextFontSize = FontSizeClan; CTextCursor Cursor; TextRender()->SetCursor(&Cursor, 0, 0, FontSizeClan, TEXTFLAG_RENDER); @@ -119,7 +119,7 @@ void CNamePlates::RenderNameplate(vec2 Position, const CNetObj_PlayerInfo *pPlay float ScreenX0, ScreenY0, ScreenX1, ScreenY1; Graphics()->GetScreen(&ScreenX0, &ScreenY0, &ScreenX1, &ScreenY1); RenderTools()->MapScreenToInterface(m_pClient->m_Camera.m_Center.x, m_pClient->m_Camera.m_Center.y); - TextRender()->RecreateTextContainer(NamePlate.m_ClanNameTextContainerIndex, &Cursor, ClientData.m_aClan); + TextRender()->RecreateTextContainer(NamePlate.m_ClanTextContainerIndex, &Cursor, ClientData.m_aClan); Graphics()->MapScreen(ScreenX0, ScreenY0, ScreenX1, ScreenY1); } } @@ -166,8 +166,8 @@ void CNamePlates::RenderNameplate(vec2 Position, const CNetObj_PlayerInfo *pPlay if(g_Config.m_ClNameplatesClan) { YOffset -= FontSizeClan; - if(NamePlate.m_ClanNameTextContainerIndex.Valid()) - TextRender()->RenderTextContainer(NamePlate.m_ClanNameTextContainerIndex, TColor, TOutlineColor, Position.x - TextRender()->GetBoundingBoxTextContainer(NamePlate.m_ClanNameTextContainerIndex).m_W / 2.0f, YOffset); + if(NamePlate.m_ClanTextContainerIndex.Valid()) + TextRender()->RenderTextContainer(NamePlate.m_ClanTextContainerIndex, TColor, TOutlineColor, Position.x - TextRender()->GetBoundingBoxTextContainer(NamePlate.m_ClanTextContainerIndex).m_W / 2.0f, YOffset); } if(g_Config.m_ClNameplatesFriendMark && ClientData.m_Friend) @@ -330,7 +330,7 @@ void CNamePlates::ResetNamePlates() for(auto &NamePlate : m_aNamePlates) { TextRender()->DeleteTextContainer(NamePlate.m_NameTextContainerIndex); - TextRender()->DeleteTextContainer(NamePlate.m_ClanNameTextContainerIndex); + TextRender()->DeleteTextContainer(NamePlate.m_ClanTextContainerIndex); NamePlate.Reset(); } diff --git a/src/game/client/components/nameplates.h b/src/game/client/components/nameplates.h index ec70944bb..7148dd7d5 100644 --- a/src/game/client/components/nameplates.h +++ b/src/game/client/components/nameplates.h @@ -22,19 +22,19 @@ struct SPlayerNamePlate void Reset() { m_NameTextContainerIndex.Reset(); - m_ClanNameTextContainerIndex.Reset(); + m_ClanTextContainerIndex.Reset(); m_aName[0] = '\0'; - m_aClanName[0] = '\0'; - m_NameTextFontSize = m_ClanNameTextFontSize = 0; + m_aClan[0] = '\0'; + m_NameTextFontSize = m_ClanTextFontSize = 0.0f; } char m_aName[MAX_NAME_LENGTH]; STextContainerIndex m_NameTextContainerIndex; float m_NameTextFontSize; - char m_aClanName[MAX_CLAN_LENGTH]; - STextContainerIndex m_ClanNameTextContainerIndex; - float m_ClanNameTextFontSize; + char m_aClan[MAX_CLAN_LENGTH]; + STextContainerIndex m_ClanTextContainerIndex; + float m_ClanTextFontSize; }; class CNamePlates : public CComponent From 3d804910e654a7a8024be1770e2c993143b21d62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Wed, 26 Jun 2024 20:05:20 +0200 Subject: [PATCH 6/6] Minor refactoring of `RenderNameplate` - Reduce duplicate code and dead stores for color and input data. - Move variable declarations closer to their usages. - Remove unnecessary `Graphics()->TextureClear` before `Graphics()->TextureSet`. - Mark variables `const` and remove unnecessary temporary variables. - Remove unnecessary default arguments of `Text` and `TextWidth` functions. - Use `WithAlpha` for colors. --- src/game/client/components/nameplates.cpp | 99 +++++++++++------------ 1 file changed, 49 insertions(+), 50 deletions(-) diff --git a/src/game/client/components/nameplates.cpp b/src/game/client/components/nameplates.cpp index e6cfec9c2..b5ec8a65d 100644 --- a/src/game/client/components/nameplates.cpp +++ b/src/game/client/components/nameplates.cpp @@ -18,10 +18,10 @@ void CNamePlates::RenderNameplate(vec2 Position, const CNetObj_PlayerInfo *pPlay { SPlayerNamePlate &NamePlate = m_aNamePlates[pPlayerInfo->m_ClientId]; const auto &ClientData = m_pClient->m_aClients[pPlayerInfo->m_ClientId]; - bool OtherTeam = m_pClient->IsOtherTeam(pPlayerInfo->m_ClientId); + const bool OtherTeam = m_pClient->IsOtherTeam(pPlayerInfo->m_ClientId); - float FontSize = 18.0f + 20.0f * g_Config.m_ClNameplatesSize / 100.0f; - float FontSizeClan = 18.0f + 20.0f * g_Config.m_ClNameplatesClanSize / 100.0f; + const float FontSize = 18.0f + 20.0f * g_Config.m_ClNameplatesSize / 100.0f; + const float FontSizeClan = 18.0f + 20.0f * g_Config.m_ClNameplatesClanSize / 100.0f; TextRender()->SetRenderFlags(ETextRenderFlags::TEXT_RENDER_FLAG_NO_FIRST_CHARACTER_X_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_LAST_CHARACTER_ADVANCE); float YOffset = Position.y - 38; @@ -35,25 +35,9 @@ void CNamePlates::RenderNameplate(vec2 Position, const CNetObj_PlayerInfo *pPlay #endif if((ShowDirection && ShowDirection != 3 && !pPlayerInfo->m_Local) || (ShowDirection >= 2 && pPlayerInfo->m_Local) || (ShowDirection == 3 && Client()->DummyConnected() && Client()->State() != IClient::STATE_DEMOPLAYBACK && pPlayerInfo->m_ClientId == m_pClient->m_aLocalIds[!g_Config.m_ClDummy])) { - Graphics()->SetColor(1.0f, 1.0f, 1.0f, 1.0f); - Graphics()->QuadsSetRotation(0); - - const float ShowDirectionImgSize = 22.0f; - YOffset -= ShowDirectionImgSize; - vec2 ShowDirectionPos = vec2(Position.x - 11.0f, YOffset); - - const auto &Character = m_pClient->m_Snap.m_aCharacters[pPlayerInfo->m_ClientId]; - bool DirLeft = Character.m_Cur.m_Direction == -1; - bool DirRight = Character.m_Cur.m_Direction == 1; - bool Jump = Character.m_Cur.m_Jumped & 1; - - if(pPlayerInfo->m_Local && Client()->State() != IClient::STATE_DEMOPLAYBACK) - { - const auto &InputData = m_pClient->m_Controls.m_aInputData[g_Config.m_ClDummy]; - DirLeft = InputData.m_Direction == -1; - DirRight = InputData.m_Direction == 1; - Jump = InputData.m_Jump == 1; - } + bool DirLeft; + bool DirRight; + bool Jump; if(Client()->DummyConnected() && Client()->State() != IClient::STATE_DEMOPLAYBACK && pPlayerInfo->m_ClientId == m_pClient->m_aLocalIds[!g_Config.m_ClDummy]) { const auto &InputData = m_pClient->m_Controls.m_aInputData[!g_Config.m_ClDummy]; @@ -61,6 +45,26 @@ void CNamePlates::RenderNameplate(vec2 Position, const CNetObj_PlayerInfo *pPlay DirRight = InputData.m_Direction == 1; Jump = InputData.m_Jump == 1; } + else if(pPlayerInfo->m_Local && Client()->State() != IClient::STATE_DEMOPLAYBACK) + { + const auto &InputData = m_pClient->m_Controls.m_aInputData[g_Config.m_ClDummy]; + DirLeft = InputData.m_Direction == -1; + DirRight = InputData.m_Direction == 1; + Jump = InputData.m_Jump == 1; + } + else + { + const auto &Character = m_pClient->m_Snap.m_aCharacters[pPlayerInfo->m_ClientId]; + DirLeft = Character.m_Cur.m_Direction == -1; + DirRight = Character.m_Cur.m_Direction == 1; + Jump = Character.m_Cur.m_Jumped & 1; + } + + Graphics()->SetColor(1.0f, 1.0f, 1.0f, 1.0f); + + const float ShowDirectionImgSize = 22.0f; + YOffset -= ShowDirectionImgSize; + const vec2 ShowDirectionPos = vec2(Position.x - 11.0f, YOffset); if(DirLeft) { Graphics()->TextureSet(g_pData->m_aImages[IMAGE_ARROW].m_Id); @@ -70,6 +74,7 @@ void CNamePlates::RenderNameplate(vec2 Position, const CNetObj_PlayerInfo *pPlay else if(DirRight) { Graphics()->TextureSet(g_pData->m_aImages[IMAGE_ARROW].m_Id); + Graphics()->QuadsSetRotation(0); Graphics()->RenderQuadContainerAsSprite(m_DirectionQuadContainerIndex, 0, ShowDirectionPos.x + 30.f, ShowDirectionPos.y); } if(Jump) @@ -85,9 +90,11 @@ void CNamePlates::RenderNameplate(vec2 Position, const CNetObj_PlayerInfo *pPlay // render name plate if((!pPlayerInfo->m_Local || g_Config.m_ClNameplatesOwn) && g_Config.m_ClNameplates) { - float a = 1; + float a; if(g_Config.m_ClNameplatesAlways == 0) a = clamp(1 - std::pow(distance(m_pClient->m_Controls.m_aTargetPos[g_Config.m_ClDummy], Position) / 200.0f, 16.0f), 0.0f, 1.0f); + else + a = 1.0f; if(str_comp(ClientData.m_aName, NamePlate.m_aName) != 0 || FontSize != NamePlate.m_NameTextFontSize) { @@ -139,12 +146,12 @@ void CNamePlates::RenderNameplate(vec2 Position, const CNetObj_PlayerInfo *pPlay if(OtherTeam && !ForceAlpha) { TOutlineColor = ColorRGBA(0.0f, 0.0f, 0.0f, 0.2f * g_Config.m_ClShowOthersAlpha / 100.0f); - TColor = ColorRGBA(rgb.r, rgb.g, rgb.b, g_Config.m_ClShowOthersAlpha / 100.0f); + TColor = rgb.WithAlpha(g_Config.m_ClShowOthersAlpha / 100.0f); } else { TOutlineColor = ColorRGBA(0.0f, 0.0f, 0.0f, 0.5f * a); - TColor = ColorRGBA(rgb.r, rgb.g, rgb.b, a); + TColor = rgb.WithAlpha(a); } if(g_Config.m_ClNameplatesTeamcolors && m_pClient->m_Snap.m_pGameInfoObj && m_pClient->m_Snap.m_pGameInfoObj->m_GameFlags & GAMEFLAG_TEAMS) { @@ -173,50 +180,43 @@ void CNamePlates::RenderNameplate(vec2 Position, const CNetObj_PlayerInfo *pPlay if(g_Config.m_ClNameplatesFriendMark && ClientData.m_Friend) { YOffset -= FontSize; - char aFriendMark[] = "♥"; - - ColorRGBA Color; - + ColorRGBA Color = ColorRGBA(1.0f, 0.0f, 0.0f, Alpha); if(OtherTeam && !ForceAlpha) - Color = ColorRGBA(1.0f, 0.0f, 0.0f, g_Config.m_ClShowOthersAlpha / 100.0f); + Color.a *= g_Config.m_ClShowOthersAlpha / 100.0f; else - Color = ColorRGBA(1.0f, 0.0f, 0.0f, a); - - Color.a *= Alpha; + Color.a *= a; + const char *pFriendMark = "♥"; TextRender()->TextColor(Color); - float XOffSet = TextRender()->TextWidth(FontSize, aFriendMark, -1, -1.0f) / 2.0f; - TextRender()->Text(Position.x - XOffSet, YOffset, FontSize, aFriendMark, -1.0f); + TextRender()->Text(Position.x - TextRender()->TextWidth(FontSize, pFriendMark) / 2.0f, YOffset, FontSize, pFriendMark); } if(g_Config.m_Debug || g_Config.m_ClNameplatesIds) // render client id when in debug as well { YOffset -= FontSize; - char aBuf[128]; + char aBuf[12]; str_format(aBuf, sizeof(aBuf), "%d", pPlayerInfo->m_ClientId); - float XOffset = TextRender()->TextWidth(FontSize, aBuf, -1, -1.0f) / 2.0f; TextRender()->TextColor(rgb); - TextRender()->Text(Position.x - XOffset, YOffset, FontSize, aBuf, -1.0f); + TextRender()->Text(Position.x - TextRender()->TextWidth(FontSize, aBuf) / 2.0f, YOffset, FontSize, aBuf); } } if((g_Config.m_Debug || g_Config.m_ClNameplatesStrong) && g_Config.m_ClNameplates) { - bool Following = (m_pClient->m_Snap.m_SpecInfo.m_Active && !GameClient()->m_MultiViewActivated && m_pClient->m_Snap.m_SpecInfo.m_SpectatorId != SPEC_FREEVIEW); + const bool Following = (m_pClient->m_Snap.m_SpecInfo.m_Active && !GameClient()->m_MultiViewActivated && m_pClient->m_Snap.m_SpecInfo.m_SpectatorId != SPEC_FREEVIEW); if(m_pClient->m_Snap.m_LocalClientId != -1 || Following) { - int SelectedId = Following ? m_pClient->m_Snap.m_SpecInfo.m_SpectatorId : m_pClient->m_Snap.m_LocalClientId; + const int SelectedId = Following ? m_pClient->m_Snap.m_SpecInfo.m_SpectatorId : m_pClient->m_Snap.m_LocalClientId; const CGameClient::CSnapState::CCharacterInfo &Selected = m_pClient->m_Snap.m_aCharacters[SelectedId]; const CGameClient::CSnapState::CCharacterInfo &Other = m_pClient->m_Snap.m_aCharacters[pPlayerInfo->m_ClientId]; if(Selected.m_HasExtendedData && Other.m_HasExtendedData) { if(SelectedId == pPlayerInfo->m_ClientId) + { TextRender()->TextColor(rgb); + } else { - float ScaleX, ScaleY; - const float StrongWeakImgSize = 40.0f; - Graphics()->TextureClear(); Graphics()->TextureSet(g_pData->m_aImages[IMAGE_STRONGWEAK].m_Id); Graphics()->QuadsBegin(); ColorRGBA StrongWeakStatusColor; @@ -232,21 +232,21 @@ void CNamePlates::RenderNameplate(vec2 Position, const CNetObj_PlayerInfo *pPlay StrongWeakSpriteId = SPRITE_HOOK_WEAK; } - float ClampedAlpha = 1; - if(g_Config.m_ClNameplatesAlways == 0) - ClampedAlpha = clamp(1 - std::pow(distance(m_pClient->m_Controls.m_aTargetPos[g_Config.m_ClDummy], Position) / 200.0f, 16.0f), 0.0f, 1.0f); - if(OtherTeam && !ForceAlpha) StrongWeakStatusColor.a = g_Config.m_ClShowOthersAlpha / 100.0f; + else if(g_Config.m_ClNameplatesAlways == 0) + StrongWeakStatusColor.a = clamp(1 - std::pow(distance(m_pClient->m_Controls.m_aTargetPos[g_Config.m_ClDummy], Position) / 200.0f, 16.0f), 0.0f, 1.0f); else - StrongWeakStatusColor.a = ClampedAlpha; + StrongWeakStatusColor.a = 1.0f; StrongWeakStatusColor.a *= Alpha; Graphics()->SetColor(StrongWeakStatusColor); + float ScaleX, ScaleY; RenderTools()->SelectSprite(StrongWeakSpriteId); RenderTools()->GetSpriteScale(StrongWeakSpriteId, ScaleX, ScaleY); TextRender()->TextColor(StrongWeakStatusColor); + const float StrongWeakImgSize = 40.0f; YOffset -= StrongWeakImgSize * ScaleY; RenderTools()->DrawSprite(Position.x, YOffset + (StrongWeakImgSize / 2.0f) * ScaleY, StrongWeakImgSize); Graphics()->QuadsEnd(); @@ -256,8 +256,7 @@ void CNamePlates::RenderNameplate(vec2 Position, const CNetObj_PlayerInfo *pPlay YOffset -= FontSize; char aBuf[12]; str_format(aBuf, sizeof(aBuf), "%d", Other.m_ExtendedData.m_StrongWeakId); - float XOffset = TextRender()->TextWidth(FontSize, aBuf, -1, -1.0f) / 2.0f; - TextRender()->Text(Position.x - XOffset, YOffset, FontSize, aBuf, -1.0f); + TextRender()->Text(Position.x - TextRender()->TextWidth(FontSize, aBuf) / 2.0f, YOffset, FontSize, aBuf); } } }