-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfps.cpp
More file actions
135 lines (106 loc) · 3.08 KB
/
fps.cpp
File metadata and controls
135 lines (106 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <fps.h>
#include <SafeWrite.h>
#include <cfloat>
#pragma comment(lib, "winmm.lib")
namespace {
float* const g_fMaxTime = reinterpret_cast<float*>(0x10F2BE8);
float fMaxTimeDefault = 0.f;
constexpr float fTimerOffsetMult = 0.9875f;
constexpr float fMaxTimeLowerBoundaryS = 0.016f;
double dMaxFPSTolerance = 300;
double dMinFPSTolerance = 20;
double dFrameTimeMinMS = 0;
double dFrameTimeMaxMS = 0;
double dFrameTimeMinS = 0;
double dFrameTimeMaxS = 0;
}
namespace QPC {
double GetTime() {
LARGE_INTEGER liCounter;
QueryPerformanceCounter(&liCounter);
return double(liCounter.QuadPart) / double(liFrequency.QuadPart);
}
double GetTimeMS() {
return GetTime() * 1000.0;
}
DWORD ReturnCounter() {
return static_cast<DWORD>(GetTimeMS());
}
double GetTimeDelta() {
double dCurrentCount = GetTimeMS();
double dToReturn = dCurrentCount - dLastCount;
dLastCount = dCurrentCount;
return dToReturn;
}
void Initialize() {
QueryPerformanceFrequency(&liFrequency);
dLastCount = GetTimeMS();
}
}
void BSTimerSafe::ClampGameCounters(float& fClamp) {
float& fMaxTime = *g_fMaxTime;
if (fClamp > FLT_EPSILON) {
fClamp = 1000.f / ((1000.f / fClamp) * fTimerOffsetMult);
fMaxTime = 1000.f / ((1000.f / fMaxTime) * fTimerOffsetMult);
if (fMaxTime < FLT_EPSILON) {
fMaxTime = FLT_EPSILON;
}
}
float fClampS = fClamp / 1000.f;
if (IsInPauseFade() || fClampS < FLT_EPSILON) {
fMaxTime = fMaxTimeDefault;
}
else if (fMaxTime > fMaxTimeLowerBoundaryS) {
fMaxTime = fMaxTimeLowerBoundaryS;
}
}
void BSTimerSafe::CustomUpdate(int time) {
double dDelta = QPC::GetTimeDelta();
float fMaxTime = fMaxTimeLowerBoundaryS;
if (dDelta > FLT_EPSILON) {
if (dDelta < dFrameTimeMaxMS) {
fMaxTime = dDelta > dFrameTimeMinMS ? static_cast<float>(dDelta / 1000.0) : static_cast<float>(dFrameTimeMinS);
}
else {
fMaxTime = static_cast<float>(dFrameTimeMaxS);
}
}
*g_fMaxTime = fMaxTime;
fClamp = 0.f;
BGSSaveLoadGame* pSaveLoad = BGSSaveLoadGame::GetSingleton();
bool bSaveLoading = pSaveLoad && pSaveLoad->IsLoading();
if (!bSaveLoading && !IsLoadingNewGame() && dDelta > 0.f) {
if (dDelta < dFrameTimeMaxMS) {
fClamp = dDelta > dFrameTimeMinMS ? static_cast<float>(dDelta) : static_cast<float>(dFrameTimeMinMS);
}
else {
fClamp = static_cast<float>(dFrameTimeMaxMS);
}
}
ClampGameCounters(fClamp);
Update(QPC::ReturnCounter());
}
namespace {
void FastExit()
{
StartMenu* pStartMenu = StartMenu::GetSingleton();
if (pStartMenu && pStartMenu->GetSettingsChanged())
pStartMenu->SaveSettings();
TerminateProcess(GetCurrentProcess(), 0);
}
void __fastcall TimerUpdateHook(BSTimerSafe* a1, void* edx, int time)
{
a1->CustomUpdate(time);
}
}
void WritePatches() {
QPC::Initialize();
SafeWrite32(0xD9B090, (uintptr_t)QPC::ReturnCounter);
fMaxTimeDefault = *g_fMaxTime;
dFrameTimeMinMS = 1000.0 / dMaxFPSTolerance;
dFrameTimeMaxMS = 1000.0 / dMinFPSTolerance;
dFrameTimeMinS = dFrameTimeMinMS / 1000.0;
dFrameTimeMaxS = dFrameTimeMaxMS / 1000.0;
WriteRelCall(0x6E43C7, UInt32(TimerUpdateHook));
WriteRelJump(0x6EED54, UInt32(FastExit));
}