Skip to content

Commit f7b9124

Browse files
committed
Various fixes for camera-related code: Performance, Memory safety, and alignment (interplay of functions with eachother and RPC's).
Fixes identified performance regression and improves smoothness of looking around/aiming with mouse on low spec machines, due to less overhead each tick.
1 parent 5eb8d8b commit f7b9124

File tree

10 files changed

+811
-157
lines changed

10 files changed

+811
-157
lines changed

Client/game_sa/CCamSA.cpp

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,23 @@
1313
#include "CCamSA.h"
1414
#include "CGameSA.h"
1515
#include <cmath>
16+
#include <cfloat>
17+
18+
namespace
19+
{
20+
constexpr float kPi = 3.14159265358979323846f;
21+
constexpr float kTwoPi = 6.28318530717958647692f;
22+
23+
inline float WrapAngleRad(float angle) noexcept
24+
{
25+
angle -= kTwoPi * std::floor((angle + kPi) / kTwoPi);
26+
if (angle <= -kPi)
27+
angle += kTwoPi;
28+
else if (angle > kPi)
29+
angle -= kTwoPi;
30+
return angle;
31+
}
32+
}
1633

1734
extern CGameSA* pGame;
1835

@@ -21,12 +38,16 @@ CEntity* CCamSA::GetTargetEntity() const
2138

2239
if (!m_pInterface)
2340
return nullptr;
41+
42+
if (!pGame)
43+
return nullptr;
2444

2545
CEntitySAInterface* pInterface = m_pInterface->CamTargetEntity;
2646
if (pInterface)
2747
{
2848
CPools* pPools = pGame->GetPools();
29-
return pPools->GetEntity((DWORD*)pInterface);
49+
if (pPools)
50+
return pPools->GetEntity((DWORD*)pInterface);
3051
}
3152
return nullptr;
3253
}
@@ -59,16 +80,15 @@ void CCamSA::GetDirection(float& fHorizontal, float& fVertical)
5980
return;
6081
}
6182

62-
float fHoriz = m_pInterface->m_fHorizontalAngle;
63-
float fVert = m_pInterface->m_fVerticalAngle;
64-
65-
if (!std::isfinite(fHoriz) || !std::isfinite(fVert))
83+
const float fHoriz = std::isfinite(m_pInterface->m_fHorizontalAngle) ? WrapAngleRad(m_pInterface->m_fHorizontalAngle) : 0.0f;
84+
const float fVert = std::isfinite(m_pInterface->m_fVerticalAngle) ? WrapAngleRad(m_pInterface->m_fVerticalAngle) : 0.0f;
85+
86+
if (!std::isfinite(m_pInterface->m_fHorizontalAngle) || !std::isfinite(m_pInterface->m_fVerticalAngle))
6687
{
67-
fHorizontal = 0.0f;
68-
fVertical = 0.0f;
69-
return;
88+
m_pInterface->m_fHorizontalAngle = fHoriz;
89+
m_pInterface->m_fVerticalAngle = fVert;
7090
}
71-
91+
7292
fHorizontal = fHoriz;
7393
fVertical = fVert;
7494
}
@@ -82,15 +102,6 @@ void CCamSA::SetDirection(float fHorizontal, float fVertical)
82102
if (!std::isfinite(fHorizontal) || !std::isfinite(fVertical))
83103
return;
84104

85-
// Clamp angle values to prevent overflow
86-
constexpr float MIN_ANGLE = -360.0f;
87-
constexpr float MAX_ANGLE = 360.0f;
88-
89-
if (fHorizontal < MIN_ANGLE || fHorizontal > MAX_ANGLE ||
90-
fVertical < MIN_ANGLE || fVertical > MAX_ANGLE)
91-
return;
92-
93-
// Calculation @ sub 0x50F970
94-
m_pInterface->m_fHorizontalAngle = fHorizontal;
95-
m_pInterface->m_fVerticalAngle = fVertical;
105+
m_pInterface->m_fHorizontalAngle = WrapAngleRad(fHorizontal);
106+
m_pInterface->m_fVerticalAngle = WrapAngleRad(fVertical);
96107
}

0 commit comments

Comments
 (0)