ddnet/src/game/client/components/killmessages.cpp
Robert Müller faa9f27808 Declare variables as const when possible
According to cppcheck's `constVariable` error:

```
src\engine\client\backend\opengl\opengl_sl.cpp:74:43: style: Variable 'Define' can be declared as reference to const [constVariable]
  for(CGLSLCompiler::SGLSLCompilerDefine &Define : pCompiler->m_vDefines)
                                          ^

src\engine\client\backend\vulkan\backend_vulkan.cpp:2149:12: style: Variable 'GraphicThreadCommandBuffer' can be declared as reference to const [constVariable]
     auto &GraphicThreadCommandBuffer = m_vvThreadDrawCommandBuffers[i + 1][m_CurImageIndex];
           ^
src\engine\client\backend\vulkan\backend_vulkan.cpp:3192:9: style: Variable 'BufferObject' can be declared as reference to const [constVariable]
  auto &BufferObject = m_vBufferObjects[BufferObjectIndex];
        ^
src\engine\client\backend\vulkan\backend_vulkan.cpp:3200:10: style: Variable 'DescrSet' can be declared as reference to const [constVariable]
   auto &DescrSet = m_vTextures[State.m_Texture].m_VKStandard3DTexturedDescrSet;
         ^
src\engine\client\backend\vulkan\backend_vulkan.cpp:3810:13: style: Variable 'Mode' can be declared as reference to const [constVariable]
  for(auto &Mode : vPresentModeList)
            ^
src\engine\client\backend\vulkan\backend_vulkan.cpp:3818:13: style: Variable 'Mode' can be declared as reference to const [constVariable]
  for(auto &Mode : vPresentModeList)
            ^
src\engine\client\backend\vulkan\backend_vulkan.cpp:6511:10: style: Variable 'DescrSet' can be declared as reference to const [constVariable]
   auto &DescrSet = m_vTextures[pCommand->m_State.m_Texture].m_aVKStandardTexturedDescrSets[AddressModeIndex];
         ^
src\engine\client\backend\vulkan\backend_vulkan.cpp:6555:10: style: Variable 'DescrSet' can be declared as reference to const [constVariable]
   auto &DescrSet = m_vTextures[pCommand->m_State.m_Texture].m_VKStandard3DTexturedDescrSet;
         ^
src\engine\client\backend\vulkan\backend_vulkan.cpp:6660:9: style: Variable 'MemBlock' can be declared as reference to const [constVariable]
  auto &MemBlock = m_vBufferObjects[BufferIndex].m_BufferObject.m_Mem;
        ^
src\engine\client\backend\vulkan\backend_vulkan.cpp:6799:9: style: Variable 'BufferObject' can be declared as reference to const [constVariable]
  auto &BufferObject = m_vBufferObjects[BufferObjectIndex];
        ^
src\engine\client\backend\vulkan\backend_vulkan.cpp:6808:10: style: Variable 'DescrSet' can be declared as reference to const [constVariable]
   auto &DescrSet = m_vTextures[pCommand->m_State.m_Texture].m_aVKStandardTexturedDescrSets[AddressModeIndex];
         ^
src\engine\client\backend\vulkan\backend_vulkan.cpp:6902:9: style: Variable 'BufferObject' can be declared as reference to const [constVariable]
  auto &BufferObject = m_vBufferObjects[BufferObjectIndex];
        ^
src\engine\client\backend\vulkan\backend_vulkan.cpp:6907:9: style: Variable 'TextTextureDescr' can be declared as reference to const [constVariable]
  auto &TextTextureDescr = m_vTextures[pCommand->m_TextTextureIndex].m_VKTextDescrSet;
        ^
src\engine\client\backend\vulkan\backend_vulkan.cpp:6961:9: style: Variable 'BufferObject' can be declared as reference to const [constVariable]
  auto &BufferObject = m_vBufferObjects[BufferObjectIndex];
        ^
src\engine\client\backend\vulkan\backend_vulkan.cpp:6970:10: style: Variable 'DescrSet' can be declared as reference to const [constVariable]
   auto &DescrSet = m_vTextures[State.m_Texture].m_aVKStandardTexturedDescrSets[AddressModeIndex];
         ^

src\game\client\components\hud.cpp:178:8: style: Variable 'aFlagCarrier' can be declared as const array [constVariable]
   int aFlagCarrier[2] = {
       ^
src\game\client\components\hud.cpp:519:16: style: Variable 's_aTextWidth' can be declared as const array [constVariable]
  static float s_aTextWidth[5] = {s_TextWidth0, s_TextWidth00, s_TextWidth000, s_TextWidth0000, s_TextWidth00000};
               ^

src\game\client\components\killmessages.cpp:305:30: style: Variable 'Client' can be declared as reference to const [constVariable]
   CGameClient::CClientData &Client = GameClient()->m_aClients[m_aKillmsgs[r].m_KillerID];
                             ^
src\game\client\components\killmessages.cpp:314:30: style: Variable 'Client' can be declared as reference to const [constVariable]
   CGameClient::CClientData &Client = GameClient()->m_aClients[m_aKillmsgs[r].m_VictimID];
                             ^

src\game\client\components\menus_ingame.cpp:243:12: style: Variable 'pInfoByName' can be declared as reference to const [constVariable]
 for(auto &pInfoByName : m_pClient->m_Snap.m_apInfoByName)
           ^
src\game\client\components\menus_ingame.cpp:530:12: style: Variable 'pInfoByName' can be declared as reference to const [constVariable]
 for(auto &pInfoByName : m_pClient->m_Snap.m_apInfoByName)
           ^

src\game\client\components\players.cpp:767:44: style: Variable 'CharacterInfo' can be declared as reference to const [constVariable]
  CGameClient::CSnapState::CCharacterInfo &CharacterInfo = m_pClient->m_Snap.m_aCharacters[i];
                                           ^

src\game\client\components\spectator.cpp:122:27: style: Variable 'Snap' can be declared as reference to const [constVariable]
 CGameClient::CSnapState &Snap = pSelf->m_pClient->m_Snap;
                          ^
src\game\client\components\spectator.cpp:221:12: style: Variable 'pInfo' can be declared as reference to const [constVariable]
 for(auto &pInfo : m_pClient->m_Snap.m_apInfoByDDTeamName)
           ^

src\game\client\gameclient.cpp:2220:15: style: Variable 'OwnClientData' can be declared as reference to const [constVariable]
 CClientData &OwnClientData = m_aClients[ownID];
              ^
src\game\client\gameclient.cpp:2227:16: style: Variable 'cData' can be declared as reference to const [constVariable]
  CClientData &cData = m_aClients[i];
               ^

src\game\client\prediction\entities\character.cpp:397:11: style: Variable 'aSpreading' can be declared as const array [constVariable]
    float aSpreading[] = {-0.185f, -0.070f, 0, 0.070f, 0.185f};
          ^

src\game\client\prediction\entities\laser.cpp:53:9: style: Variable 'HitPos' can be declared as reference to const [constVariable]
  vec2 &HitPos = pHit->Core()->m_Pos;
        ^

src\game\editor\auto_map.cpp:507:18: style: Variable 'Index' can be declared as reference to const [constVariable]
       for(auto &Index : pRule->m_vIndexList)
                 ^
src\game\editor\auto_map.cpp:518:18: style: Variable 'Index' can be declared as reference to const [constVariable]
       for(auto &Index : pRule->m_vIndexList)
                 ^

src\game\editor\editor.cpp:118:12: style: Variable 'Item' can be declared as reference to const [constVariable]
 for(auto &Item : vList)
           ^
src\game\editor\editor.cpp:2983:11: style: Variable 'aAspects' can be declared as const array [constVariable]
    float aAspects[] = {4.0f / 3.0f, 16.0f / 10.0f, 5.0f / 4.0f, 16.0f / 9.0f};
          ^
src\game\editor\editor.cpp:3141:15: style: Variable 's_aShift' can be declared as const array [constVariable]
   static int s_aShift[] = {24, 16, 8, 0};
              ^

src\engine\server\server.cpp:2807:14: style: Variable 'Client' can be declared as reference to const [constVariable]
   for(auto &Client : m_aClients)
             ^

src\engine\server\sql_string_helpers.cpp:51:6: style: Variable 'aTimes' can be declared as const array [constVariable]
 int aTimes[7] =
     ^

src\test\secure_random.cpp:24:6: style: Variable 'BOUNDS' can be declared as const array [constVariable]
 int BOUNDS[] = {2, 3, 4, 5, 10, 100, 127, 128, 129};
     ^
```
2022-11-29 23:32:32 +01:00

322 lines
11 KiB
C++

/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
#include <engine/graphics.h>
#include <engine/shared/config.h>
#include <engine/textrender.h>
#include <game/generated/client_data.h>
#include <game/generated/protocol.h>
#include "engine/shared/protocol.h"
#include "killmessages.h"
#include <game/client/animstate.h>
#include <game/client/gameclient.h>
void CKillMessages::OnWindowResize()
{
for(auto &Killmsg : m_aKillmsgs)
{
TextRender()->DeleteTextContainer(Killmsg.m_VictimTextContainerIndex);
TextRender()->DeleteTextContainer(Killmsg.m_KillerTextContainerIndex);
}
}
void CKillMessages::OnReset()
{
m_KillmsgCurrent = 0;
for(auto &Killmsg : m_aKillmsgs)
{
Killmsg.m_Tick = -100000;
TextRender()->DeleteTextContainer(Killmsg.m_VictimTextContainerIndex);
TextRender()->DeleteTextContainer(Killmsg.m_KillerTextContainerIndex);
}
}
void CKillMessages::OnInit()
{
Graphics()->SetColor(1.f, 1.f, 1.f, 1.f);
m_SpriteQuadContainerIndex = Graphics()->CreateQuadContainer(false);
Graphics()->QuadsSetSubset(0, 0, 1, 1);
RenderTools()->QuadContainerAddSprite(m_SpriteQuadContainerIndex, 0.f, 0.f, 28.f, 56.f);
Graphics()->QuadsSetSubset(0, 0, 1, 1);
RenderTools()->QuadContainerAddSprite(m_SpriteQuadContainerIndex, 0.f, 0.f, 28.f, 56.f);
Graphics()->QuadsSetSubset(1, 0, 0, 1);
RenderTools()->QuadContainerAddSprite(m_SpriteQuadContainerIndex, 0.f, 0.f, 28.f, 56.f);
Graphics()->QuadsSetSubset(1, 0, 0, 1);
RenderTools()->QuadContainerAddSprite(m_SpriteQuadContainerIndex, 0.f, 0.f, 28.f, 56.f);
for(int i = 0; i < NUM_WEAPONS; ++i)
{
float ScaleX, ScaleY;
Graphics()->QuadsSetSubset(0, 0, 1, 1);
RenderTools()->GetSpriteScale(g_pData->m_Weapons.m_aId[i].m_pSpriteBody, ScaleX, ScaleY);
RenderTools()->QuadContainerAddSprite(m_SpriteQuadContainerIndex, 96.f * ScaleX, 96.f * ScaleY);
}
Graphics()->QuadContainerUpload(m_SpriteQuadContainerIndex);
}
void CKillMessages::CreateKillmessageNamesIfNotCreated(CKillMsg &Kill)
{
const float FontSize = 36.0f;
if(Kill.m_VictimTextContainerIndex == -1 && Kill.m_aVictimName[0] != 0)
{
Kill.m_VitctimTextWidth = TextRender()->TextWidth(0, FontSize, Kill.m_aVictimName, -1, -1.0f);
CTextCursor Cursor;
TextRender()->SetCursor(&Cursor, 0, 0, FontSize, TEXTFLAG_RENDER);
Cursor.m_LineWidth = -1;
unsigned Color = g_Config.m_ClKillMessageNormalColor;
if(Kill.m_VictimID == m_pClient->m_Snap.m_LocalClientID)
{
Color = g_Config.m_ClKillMessageHighlightColor;
}
TextRender()->TextColor(color_cast<ColorRGBA>(ColorHSLA(Color)));
TextRender()->CreateTextContainer(Kill.m_VictimTextContainerIndex, &Cursor, Kill.m_aVictimName);
}
if(Kill.m_KillerTextContainerIndex == -1 && Kill.m_aKillerName[0] != 0)
{
Kill.m_KillerTextWidth = TextRender()->TextWidth(0, FontSize, Kill.m_aKillerName, -1, -1.0f);
CTextCursor Cursor;
TextRender()->SetCursor(&Cursor, 0, 0, FontSize, TEXTFLAG_RENDER);
Cursor.m_LineWidth = -1;
unsigned Color = g_Config.m_ClKillMessageNormalColor;
if(Kill.m_KillerID == m_pClient->m_Snap.m_LocalClientID)
{
Color = g_Config.m_ClKillMessageHighlightColor;
}
TextRender()->TextColor(color_cast<ColorRGBA>(ColorHSLA(Color)));
TextRender()->CreateTextContainer(Kill.m_KillerTextContainerIndex, &Cursor, Kill.m_aKillerName);
}
TextRender()->TextColor(TextRender()->DefaultTextColor());
}
void CKillMessages::OnMessage(int MsgType, void *pRawMsg)
{
if(m_pClient->m_SuppressEvents)
return;
if(MsgType == NETMSGTYPE_SV_KILLMSG)
{
CNetMsg_Sv_KillMsg *pMsg = (CNetMsg_Sv_KillMsg *)pRawMsg;
// unpack messages
CKillMsg Kill;
Kill.m_aVictimName[0] = '\0';
Kill.m_aKillerName[0] = '\0';
Kill.m_VictimID = pMsg->m_Victim;
if(Kill.m_VictimID >= 0 && Kill.m_VictimID < MAX_CLIENTS)
{
Kill.m_VictimTeam = m_pClient->m_aClients[Kill.m_VictimID].m_Team;
Kill.m_VictimDDTeam = m_pClient->m_Teams.Team(Kill.m_VictimID);
str_copy(Kill.m_aVictimName, m_pClient->m_aClients[Kill.m_VictimID].m_aName);
Kill.m_VictimRenderInfo = m_pClient->m_aClients[Kill.m_VictimID].m_RenderInfo;
}
Kill.m_KillerID = pMsg->m_Killer;
if(Kill.m_KillerID >= 0 && Kill.m_KillerID < MAX_CLIENTS)
{
Kill.m_KillerTeam = m_pClient->m_aClients[Kill.m_KillerID].m_Team;
str_copy(Kill.m_aKillerName, m_pClient->m_aClients[Kill.m_KillerID].m_aName);
Kill.m_KillerRenderInfo = m_pClient->m_aClients[Kill.m_KillerID].m_RenderInfo;
}
Kill.m_Weapon = pMsg->m_Weapon;
Kill.m_ModeSpecial = pMsg->m_ModeSpecial;
Kill.m_Tick = Client()->GameTick(g_Config.m_ClDummy);
Kill.m_FlagCarrierBlue = m_pClient->m_Snap.m_pGameDataObj ? m_pClient->m_Snap.m_pGameDataObj->m_FlagCarrierBlue : -1;
Kill.m_VitctimTextWidth = Kill.m_KillerTextWidth = 0.f;
float Width = 400 * 3.0f * Graphics()->ScreenAspect();
float Height = 400 * 3.0f;
float ScreenX0, ScreenY0, ScreenX1, ScreenY1;
Graphics()->GetScreen(&ScreenX0, &ScreenY0, &ScreenX1, &ScreenY1);
Graphics()->MapScreen(0, 0, Width * 1.5f, Height * 1.5f);
CreateKillmessageNamesIfNotCreated(Kill);
bool KillMsgValid = (Kill.m_VictimRenderInfo.m_CustomColoredSkin && Kill.m_VictimRenderInfo.m_ColorableRenderSkin.m_Body.IsValid()) || (!Kill.m_VictimRenderInfo.m_CustomColoredSkin && Kill.m_VictimRenderInfo.m_OriginalRenderSkin.m_Body.IsValid());
// if killer != victim, killer must be valid too
KillMsgValid &= Kill.m_KillerID == Kill.m_VictimID || ((Kill.m_KillerRenderInfo.m_CustomColoredSkin && Kill.m_KillerRenderInfo.m_ColorableRenderSkin.m_Body.IsValid()) || (!Kill.m_KillerRenderInfo.m_CustomColoredSkin && Kill.m_KillerRenderInfo.m_OriginalRenderSkin.m_Body.IsValid()));
if(KillMsgValid)
{
// add the message
m_KillmsgCurrent = (m_KillmsgCurrent + 1) % MAX_KILLMSGS;
TextRender()->DeleteTextContainer(m_aKillmsgs[m_KillmsgCurrent].m_VictimTextContainerIndex);
TextRender()->DeleteTextContainer(m_aKillmsgs[m_KillmsgCurrent].m_KillerTextContainerIndex);
m_aKillmsgs[m_KillmsgCurrent] = Kill;
}
Graphics()->MapScreen(ScreenX0, ScreenY0, ScreenX1, ScreenY1);
}
}
void CKillMessages::OnRender()
{
if(!g_Config.m_ClShowKillMessages)
return;
float Width = 400 * 3.0f * Graphics()->ScreenAspect();
float Height = 400 * 3.0f;
Graphics()->MapScreen(0, 0, Width * 1.5f, Height * 1.5f);
Graphics()->SetColor(1.f, 1.f, 1.f, 1.f);
float StartX = Width * 1.5f - 10.0f;
float y = 30.0f + 100.0f * ((g_Config.m_ClShowfps ? 1 : 0) + g_Config.m_ClShowpred);
for(int i = 1; i <= MAX_KILLMSGS; i++)
{
int r = (m_KillmsgCurrent + i) % MAX_KILLMSGS;
if(Client()->GameTick(g_Config.m_ClDummy) > m_aKillmsgs[r].m_Tick + 50 * 10)
continue;
float x = StartX;
ColorRGBA TColor(1.f, 1.f, 1.f, 1.f);
ColorRGBA TOutlineColor(0.f, 0.f, 0.f, 0.3f);
// render victim name
x -= m_aKillmsgs[r].m_VitctimTextWidth;
if(m_aKillmsgs[r].m_VictimID >= 0 && g_Config.m_ClChatTeamColors && m_aKillmsgs[r].m_VictimDDTeam)
{
TColor = color_cast<ColorRGBA>(ColorHSLA(m_aKillmsgs[r].m_VictimDDTeam / 64.0f, 1.0f, 0.75f));
TColor.a = 1.f;
}
CreateKillmessageNamesIfNotCreated(m_aKillmsgs[r]);
if(m_aKillmsgs[r].m_VictimTextContainerIndex != -1)
TextRender()->RenderTextContainer(m_aKillmsgs[r].m_VictimTextContainerIndex, TColor, TOutlineColor, x, y + (46.f - 36.f) / 2.f);
// render victim tee
x -= 24.0f;
if(m_pClient->m_Snap.m_pGameInfoObj && m_pClient->m_Snap.m_pGameInfoObj->m_GameFlags & GAMEFLAG_FLAGS)
{
if(m_aKillmsgs[r].m_ModeSpecial & 1)
{
int QuadOffset = 0;
if(m_aKillmsgs[r].m_VictimID == m_aKillmsgs[r].m_FlagCarrierBlue)
++QuadOffset;
if(QuadOffset == 0)
Graphics()->TextureSet(GameClient()->m_GameSkin.m_SpriteFlagRed);
else
Graphics()->TextureSet(GameClient()->m_GameSkin.m_SpriteFlagBlue);
Graphics()->RenderQuadContainerAsSprite(m_SpriteQuadContainerIndex, QuadOffset, x, y - 16);
}
}
if(m_aKillmsgs[r].m_VictimID >= 0)
{
CTeeRenderInfo TeeInfo = m_aKillmsgs[r].m_VictimRenderInfo;
CAnimState *pIdleState = CAnimState::GetIdle();
vec2 OffsetToMid;
RenderTools()->GetRenderTeeOffsetToRenderedTee(pIdleState, &TeeInfo, OffsetToMid);
vec2 TeeRenderPos(x, y + 46.0f / 2.0f + OffsetToMid.y);
RenderTools()->RenderTee(pIdleState, &TeeInfo, EMOTE_PAIN, vec2(-1, 0), TeeRenderPos);
}
x -= 32.0f;
// render weapon
x -= 44.0f;
if(m_aKillmsgs[r].m_Weapon >= 0)
{
Graphics()->TextureSet(GameClient()->m_GameSkin.m_aSpriteWeapons[m_aKillmsgs[r].m_Weapon]);
Graphics()->RenderQuadContainerAsSprite(m_SpriteQuadContainerIndex, 4 + m_aKillmsgs[r].m_Weapon, x, y + 28);
}
x -= 52.0f;
if(m_aKillmsgs[r].m_VictimID != m_aKillmsgs[r].m_KillerID)
{
if(m_pClient->m_Snap.m_pGameInfoObj && m_pClient->m_Snap.m_pGameInfoObj->m_GameFlags & GAMEFLAG_FLAGS)
{
if(m_aKillmsgs[r].m_ModeSpecial & 2)
{
int QuadOffset = 2;
if(m_aKillmsgs[r].m_KillerID == m_aKillmsgs[r].m_FlagCarrierBlue)
++QuadOffset;
if(QuadOffset == 2)
Graphics()->TextureSet(GameClient()->m_GameSkin.m_SpriteFlagRed);
else
Graphics()->TextureSet(GameClient()->m_GameSkin.m_SpriteFlagBlue);
Graphics()->RenderQuadContainerAsSprite(m_SpriteQuadContainerIndex, QuadOffset, x - 56, y - 16);
}
}
// render killer tee
x -= 24.0f;
if(m_aKillmsgs[r].m_KillerID >= 0)
{
CTeeRenderInfo TeeInfo = m_aKillmsgs[r].m_KillerRenderInfo;
CAnimState *pIdleState = CAnimState::GetIdle();
vec2 OffsetToMid;
RenderTools()->GetRenderTeeOffsetToRenderedTee(pIdleState, &TeeInfo, OffsetToMid);
vec2 TeeRenderPos(x, y + 46.0f / 2.0f + OffsetToMid.y);
RenderTools()->RenderTee(pIdleState, &TeeInfo, EMOTE_ANGRY, vec2(1, 0), TeeRenderPos);
}
x -= 32.0f;
// render killer name
x -= m_aKillmsgs[r].m_KillerTextWidth;
if(m_aKillmsgs[r].m_KillerTextContainerIndex != -1)
TextRender()->RenderTextContainer(m_aKillmsgs[r].m_KillerTextContainerIndex, TColor, TOutlineColor, x, y + (46.f - 36.f) / 2.f);
}
y += 46.0f;
}
}
void CKillMessages::RefindSkins()
{
for(int i = 0; i < MAX_KILLMSGS; i++)
{
int r = i % MAX_KILLMSGS;
if(Client()->GameTick(g_Config.m_ClDummy) > m_aKillmsgs[r].m_Tick + 50 * 10)
continue;
if(m_aKillmsgs[r].m_KillerID >= 0)
{
const CGameClient::CClientData &Client = GameClient()->m_aClients[m_aKillmsgs[r].m_KillerID];
if(Client.m_aSkinName[0] != '\0')
m_aKillmsgs[r].m_KillerRenderInfo = Client.m_RenderInfo;
else
m_aKillmsgs[r].m_KillerID = -1;
}
if(m_aKillmsgs[r].m_VictimID >= 0)
{
const CGameClient::CClientData &Client = GameClient()->m_aClients[m_aKillmsgs[r].m_VictimID];
if(Client.m_aSkinName[0] != '\0')
m_aKillmsgs[r].m_VictimRenderInfo = Client.m_RenderInfo;
else
m_aKillmsgs[r].m_VictimID = -1;
}
}
}