Refactor CInfoMessages::AddInfoMsg usage

Reduce duplicate code by moving screen remapping and validation of the info message into the `AddInfoMsg` function.

Check for valid kill message before creating text containers, so they are not created and deleted unnecessarily.

Assign info message type directly instead of passing it as a separate parameter.
This commit is contained in:
Robert Müller 2024-02-08 22:42:51 +01:00
parent 109b6bfb8e
commit ebe60738d9
2 changed files with 37 additions and 76 deletions

View file

@ -18,7 +18,7 @@ void CInfoMessages::OnWindowResize()
{ {
for(auto &InfoMsg : m_aInfoMsgs) for(auto &InfoMsg : m_aInfoMsgs)
{ {
DeleteTextContainers(&InfoMsg); DeleteTextContainers(InfoMsg);
} }
} }
@ -28,16 +28,16 @@ void CInfoMessages::OnReset()
for(auto &InfoMsg : m_aInfoMsgs) for(auto &InfoMsg : m_aInfoMsgs)
{ {
InfoMsg.m_Tick = -100000; InfoMsg.m_Tick = -100000;
DeleteTextContainers(&InfoMsg); DeleteTextContainers(InfoMsg);
} }
} }
void CInfoMessages::DeleteTextContainers(CInfoMsg *pInfoMsg) void CInfoMessages::DeleteTextContainers(CInfoMsg &InfoMsg)
{ {
TextRender()->DeleteTextContainer(pInfoMsg->m_VictimTextContainerIndex); TextRender()->DeleteTextContainer(InfoMsg.m_VictimTextContainerIndex);
TextRender()->DeleteTextContainer(pInfoMsg->m_KillerTextContainerIndex); TextRender()->DeleteTextContainer(InfoMsg.m_KillerTextContainerIndex);
TextRender()->DeleteTextContainer(pInfoMsg->m_DiffTextContainerIndex); TextRender()->DeleteTextContainer(InfoMsg.m_DiffTextContainerIndex);
TextRender()->DeleteTextContainer(pInfoMsg->m_TimeTextContainerIndex); TextRender()->DeleteTextContainer(InfoMsg.m_TimeTextContainerIndex);
} }
void CInfoMessages::OnInit() void CInfoMessages::OnInit()
@ -65,14 +65,30 @@ void CInfoMessages::OnInit()
Graphics()->QuadContainerUpload(m_SpriteQuadContainerIndex); Graphics()->QuadContainerUpload(m_SpriteQuadContainerIndex);
} }
void CInfoMessages::AddInfoMsg(EType Type, CInfoMsg NewMsg) void CInfoMessages::AddInfoMsg(const CInfoMsg &InfoMsg)
{ {
NewMsg.m_Type = Type; if(InfoMsg.m_KillerID >= 0 && !InfoMsg.m_KillerRenderInfo.Valid())
NewMsg.m_Tick = Client()->GameTick(g_Config.m_ClDummy); return;
for(int i = 0; i < InfoMsg.m_TeamSize; i++)
{
if(InfoMsg.m_aVictimIds[i] < 0 || !InfoMsg.m_aVictimRenderInfo[i].Valid())
return;
}
const float Height = 1.5f * 400.0f * 3.0f;
const float Width = Height * Graphics()->ScreenAspect();
float ScreenX0, ScreenY0, ScreenX1, ScreenY1;
Graphics()->GetScreen(&ScreenX0, &ScreenY0, &ScreenX1, &ScreenY1);
Graphics()->MapScreen(0, 0, Width, Height);
m_InfoMsgCurrent = (m_InfoMsgCurrent + 1) % MAX_INFOMSGS; m_InfoMsgCurrent = (m_InfoMsgCurrent + 1) % MAX_INFOMSGS;
DeleteTextContainers(&m_aInfoMsgs[m_InfoMsgCurrent]); DeleteTextContainers(m_aInfoMsgs[m_InfoMsgCurrent]);
m_aInfoMsgs[m_InfoMsgCurrent] = NewMsg; m_aInfoMsgs[m_InfoMsgCurrent] = InfoMsg;
CreateTextContainersIfNotCreated(m_aInfoMsgs[m_InfoMsgCurrent]);
m_aInfoMsgs[m_InfoMsgCurrent].m_Tick = Client()->GameTick(g_Config.m_ClDummy);
Graphics()->MapScreen(ScreenX0, ScreenY0, ScreenX1, ScreenY1);
} }
void CInfoMessages::CreateTextContainersIfNotCreated(CInfoMsg &InfoMsg) void CInfoMessages::CreateTextContainersIfNotCreated(CInfoMsg &InfoMsg)
@ -160,6 +176,7 @@ void CInfoMessages::OnMessage(int MsgType, void *pRawMsg)
void CInfoMessages::OnTeamKillMessage(const CNetMsg_Sv_KillMsgTeam *pMsg) void CInfoMessages::OnTeamKillMessage(const CNetMsg_Sv_KillMsgTeam *pMsg)
{ {
CInfoMsg Kill{}; CInfoMsg Kill{};
Kill.m_Type = TYPE_KILL;
std::vector<std::pair<int, int>> vStrongWeakSorted; std::vector<std::pair<int, int>> vStrongWeakSorted;
for(int i = 0; i < MAX_CLIENTS; i++) for(int i = 0; i < MAX_CLIENTS; i++)
@ -207,36 +224,13 @@ void CInfoMessages::OnTeamKillMessage(const CNetMsg_Sv_KillMsgTeam *pMsg)
Kill.m_VictimTextWidth = Kill.m_KillerTextWidth = 0.f; Kill.m_VictimTextWidth = Kill.m_KillerTextWidth = 0.f;
float Height = 400 * 3.0f; AddInfoMsg(Kill);
float Width = Height * Graphics()->ScreenAspect();
float ScreenX0, ScreenY0, ScreenX1, ScreenY1;
Graphics()->GetScreen(&ScreenX0, &ScreenY0, &ScreenX1, &ScreenY1);
Graphics()->MapScreen(0, 0, Width * 1.5f, Height * 1.5f);
CreateTextContainersIfNotCreated(Kill);
bool KillMsgValid = true;
for(int i = 0; i < Kill.m_TeamSize; i++)
{
KillMsgValid = KillMsgValid && Kill.m_aVictimIds[i] >= 0 && Kill.m_aVictimRenderInfo[i].Valid();
}
if(KillMsgValid)
{
AddInfoMsg(EType::TYPE_KILL, Kill);
}
else
{
DeleteTextContainers(&Kill);
}
Graphics()->MapScreen(ScreenX0, ScreenY0, ScreenX1, ScreenY1);
} }
void CInfoMessages::OnKillMessage(const CNetMsg_Sv_KillMsg *pMsg) void CInfoMessages::OnKillMessage(const CNetMsg_Sv_KillMsg *pMsg)
{ {
CInfoMsg Kill{}; CInfoMsg Kill{};
Kill.m_Type = TYPE_KILL;
Kill.m_TeamSize = 1; Kill.m_TeamSize = 1;
Kill.m_aVictimIds[0] = pMsg->m_Victim; Kill.m_aVictimIds[0] = pMsg->m_Victim;
@ -276,25 +270,7 @@ void CInfoMessages::OnKillMessage(const CNetMsg_Sv_KillMsg *pMsg)
Kill.m_VictimTextWidth = Kill.m_KillerTextWidth = 0.f; Kill.m_VictimTextWidth = Kill.m_KillerTextWidth = 0.f;
float Height = 400 * 3.0f; AddInfoMsg(Kill);
float Width = Height * Graphics()->ScreenAspect();
float ScreenX0, ScreenY0, ScreenX1, ScreenY1;
Graphics()->GetScreen(&ScreenX0, &ScreenY0, &ScreenX1, &ScreenY1);
Graphics()->MapScreen(0, 0, Width * 1.5f, Height * 1.5f);
CreateTextContainersIfNotCreated(Kill);
if(Kill.m_aVictimRenderInfo[0].Valid() && (Kill.m_KillerID == -1 || Kill.m_KillerRenderInfo.Valid()))
{
AddInfoMsg(EType::TYPE_KILL, Kill);
}
else
{
DeleteTextContainers(&Kill);
}
Graphics()->MapScreen(ScreenX0, ScreenY0, ScreenX1, ScreenY1);
} }
void CInfoMessages::OnRaceFinishMessage(const CNetMsg_Sv_RaceFinish *pMsg) void CInfoMessages::OnRaceFinishMessage(const CNetMsg_Sv_RaceFinish *pMsg)
@ -302,6 +278,7 @@ void CInfoMessages::OnRaceFinishMessage(const CNetMsg_Sv_RaceFinish *pMsg)
char aBuf[256]; char aBuf[256];
CInfoMsg Finish{}; CInfoMsg Finish{};
Finish.m_Type = TYPE_FINISH;
Finish.m_TeamSize = 1; Finish.m_TeamSize = 1;
Finish.m_aVictimIds[0] = pMsg->m_ClientID; Finish.m_aVictimIds[0] = pMsg->m_ClientID;
Finish.m_VictimDDTeam = m_pClient->m_Teams.Team(Finish.m_aVictimIds[0]); Finish.m_VictimDDTeam = m_pClient->m_Teams.Team(Finish.m_aVictimIds[0]);
@ -337,24 +314,7 @@ void CInfoMessages::OnRaceFinishMessage(const CNetMsg_Sv_RaceFinish *pMsg)
// finish time text // finish time text
str_time_float(pMsg->m_Time / 1000.0f, TIME_HOURS_CENTISECS, Finish.m_aTimeText, sizeof(Finish.m_aTimeText)); str_time_float(pMsg->m_Time / 1000.0f, TIME_HOURS_CENTISECS, Finish.m_aTimeText, sizeof(Finish.m_aTimeText));
float ScreenX0, ScreenY0, ScreenX1, ScreenY1; AddInfoMsg(Finish);
float Height = 400 * 3.0f;
float Width = Height * Graphics()->ScreenAspect();
Graphics()->GetScreen(&ScreenX0, &ScreenY0, &ScreenX1, &ScreenY1);
Graphics()->MapScreen(0, 0, Width * 1.5f, Height * 1.5f);
CreateTextContainersIfNotCreated(Finish);
if(Finish.m_aVictimRenderInfo[0].Valid())
{
AddInfoMsg(EType::TYPE_FINISH, Finish);
}
else
{
DeleteTextContainers(&Finish);
}
Graphics()->MapScreen(ScreenX0, ScreenY0, ScreenX1, ScreenY1);
} }
void CInfoMessages::RenderKillMsg(CInfoMsg *pInfoMsg, float x, float y) void CInfoMessages::RenderKillMsg(CInfoMsg *pInfoMsg, float x, float y)
@ -520,6 +480,7 @@ void CInfoMessages::OnRender()
continue; continue;
CreateTextContainersIfNotCreated(*pInfoMsg); CreateTextContainersIfNotCreated(*pInfoMsg);
if(pInfoMsg->m_Type == EType::TYPE_KILL && g_Config.m_ClShowKillMessages) if(pInfoMsg->m_Type == EType::TYPE_KILL && g_Config.m_ClShowKillMessages)
{ {
RenderKillMsg(pInfoMsg, StartX, y); RenderKillMsg(pInfoMsg, StartX, y);

View file

@ -57,7 +57,7 @@ public:
}; };
private: private:
void AddInfoMsg(EType Type, CInfoMsg NewMsg); void AddInfoMsg(const CInfoMsg &InfoMsg);
void RenderKillMsg(CInfoMsg *pInfoMsg, float x, float y); void RenderKillMsg(CInfoMsg *pInfoMsg, float x, float y);
void RenderFinishMsg(CInfoMsg *pInfoMsg, float x, float y); void RenderFinishMsg(CInfoMsg *pInfoMsg, float x, float y);
@ -66,7 +66,7 @@ private:
void OnRaceFinishMessage(const struct CNetMsg_Sv_RaceFinish *pMsg); void OnRaceFinishMessage(const struct CNetMsg_Sv_RaceFinish *pMsg);
void CreateTextContainersIfNotCreated(CInfoMsg &InfoMsg); void CreateTextContainersIfNotCreated(CInfoMsg &InfoMsg);
void DeleteTextContainers(CInfoMsg *pInfoMsg); void DeleteTextContainers(CInfoMsg &InfoMsg);
public: public:
CInfoMsg m_aInfoMsgs[MAX_INFOMSGS]; CInfoMsg m_aInfoMsgs[MAX_INFOMSGS];