Skip to content

Commit 339f112

Browse files
committed
fix: prevent camera drive when game unfocused
fixes #348
1 parent 3819003 commit 339f112

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

src/Features/Camera.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,23 @@ float Camera::GetCurrentPathTime() {
9595
//if in drive mode, checks if player wants to control the camera
9696
//for now it requires LMB input (as in demo drive mode)
9797
bool Camera::IsDriving() {
98+
if (!engine->IsGameFocused() || vgui->IsUIVisible()) {
99+
return false;
100+
}
101+
102+
if (camera->controlType != Drive && camera->controlType != Follow) {
103+
return false;
104+
}
105+
106+
bool userWantsToDrive = sar_cam_drive.GetInt() == 2 || inputSystem->IsKeyDown(ButtonCode_t::MOUSE_LEFT);
107+
if (!userWantsToDrive) {
108+
return false;
109+
}
110+
98111
bool drivingInGame = sar_cam_drive.GetBool() && engine->hoststate->m_activeGame;
99112
bool drivingInDemo = engine->demoplayer->IsPlaying();
100-
bool wantingToDrive = sar_cam_drive.GetInt() == 2 || inputSystem->IsKeyDown(ButtonCode_t::MOUSE_LEFT);
101-
bool isUI = vgui->IsUIVisible();
102113

103-
return (camera->controlType == Drive || camera->controlType == Follow) && wantingToDrive && (drivingInGame || drivingInDemo) && !isUI;
114+
return drivingInGame || drivingInDemo;
104115
}
105116

106117
//used by camera state interpolation function. all the math is here.

src/Modules/Engine.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ bool Engine::IsGamePaused() {
186186
return this->IsPaused(this->engineClient->ThisPtr());
187187
}
188188

189+
bool Engine::IsGameFocused() {
190+
return this->IsActiveApp(this->engineClient->ThisPtr());
191+
}
192+
189193
int Engine::GetMapIndex(const std::string map) {
190194
std::string map_lower = map;
191195
std::transform(map_lower.begin(), map_lower.end(), map_lower.begin(), tolower);
@@ -949,6 +953,7 @@ bool Engine::Init() {
949953
this->GetSaveDirName = this->engineClient->Original<_GetSaveDirName>(Offsets::GetSaveDirName);
950954
this->DebugDrawPhysCollide = this->engineClient->Original<_DebugDrawPhysCollide>(Offsets::DebugDrawPhysCollide);
951955
this->IsPaused = this->engineClient->Original<_IsPaused>(Offsets::IsPaused);
956+
this->IsActiveApp = this->engineClient->Original<_IsActiveApp>(Offsets::IsActiveApp);
952957
this->Con_IsVisible = this->engineClient->Original<_Con_IsVisible>(Offsets::Con_IsVisible);
953958
this->GetLevelNameShort = this->engineClient->Original<_GetLevelNameShort>(Offsets::GetLevelNameShort);
954959
this->GetLightForPoint = this->engineClient->Original<_GetLightForPoint>(Offsets::GetLightForPoint);

src/Modules/Engine.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class Engine : public Module {
4444
using _ClientTime = float (*)(void *thisptr);
4545
using _DebugDrawPhysCollide = bool(__rescall *)(void *thisptr, const void *collide, IMaterial *material, const matrix3x4_t &transform, const Color &color);
4646
using _IsPaused = bool (*)(void *thisptr);
47+
using _IsActiveApp = bool(*)(void *thisptr);
4748
using _TraceRay = void(__rescall *)(void *thisptr, const Ray_t &ray, unsigned int fMask, ITraceFilter *pTraceFilter, CGameTrace *pTrace);
4849
using _PointOutsideWorld = bool(__rescall *)(void *thisptr, const Vector &test);
4950
using _GetCount = int(__rescall *)(void *thisptr);
@@ -90,6 +91,7 @@ class Engine : public Module {
9091
_GetLightForPoint GetLightForPoint = nullptr;
9192
_DebugDrawPhysCollide DebugDrawPhysCollide = nullptr;
9293
_IsPaused IsPaused = nullptr;
94+
_IsActiveApp IsActiveApp = nullptr;
9395
_TraceRay TraceRay = nullptr;
9496
_TraceRay TraceRayClient = nullptr;
9597
_PointOutsideWorld PointOutsideWorld = nullptr;
@@ -144,6 +146,7 @@ class Engine : public Module {
144146
float GetHostTime();
145147
bool isRunning();
146148
bool IsGamePaused();
149+
bool IsGameFocused();
147150
int GetMapIndex(const std::string map);
148151
std::string GetCurrentMapName();
149152
std::string GetMapTitle(std::string map);

src/Offsets/Portal 2 9568.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ OFFSET_DEFAULT(GetSaveDirName, 124, 124)
3434
OFFSET_DEFAULT(ExecuteClientCmd, 104, 104)
3535
OFFSET_DEFAULT(GetActiveSplitScreenPlayerSlot, 127, 127)
3636
OFFSET_DEFAULT(GetSteamAPIContext, 177, 178)
37+
OFFSET_DEFAULT(IsActiveApp, 187, 188)
3738
OFFSET_DEFAULT(GetMouseDelta, -1, 161) // This method only exists on Linux
3839
OFFSET_DEFAULT(IsPaused, 86, 86)
3940
OFFSET_DEFAULT(DebugDrawPhysCollide, 75, 75)

0 commit comments

Comments
 (0)