Files
esuo1198_TaikoArcadeLoader/src/patches/fpslimiter.cpp
T
KIT! 91a9f9ad25 Switch compiler to MSVC (#16)
* Initial working cmake version
Code style changes according to CLion suggestions
Changed logging to use template instead of macros
---------
Co-authored-by: asesidaa <1061472754@qq.com>

* Reformat everything, updated readme for MSVC
* Warnings annoy me deeply
* Update artifact export
* Simplifying file hierarchy
Build result will now be in dist
2024-11-27 21:07:17 +01:00

67 lines
1.8 KiB
C++

#include "helpers.h"
#include "patches.h"
namespace patches::FpsLimiter {
/*
* Reference: https://github.com/teknogods/OpenParrot/blob/master/OpenParrot/src/Functions/FpsLimiter.cpp
*/
static LARGE_INTEGER PerformanceCount1;
static LARGE_INTEGER PerformanceCount2;
static bool bOnce1 = false;
static double targetFrameTime = 1000.0 / 60.0;
static double t = 0.0;
static u32 i = 0;
void
Init (const float fpsLimit) {
if (fpsLimit > 0) LogMessage (LogLevel::INFO, "Init Fps Limiter");
targetFrameTime = 1000.0 / fpsLimit;
}
void
Update () {
if (!bOnce1) {
bOnce1 = true;
QueryPerformanceCounter (&PerformanceCount1);
PerformanceCount1.QuadPart = PerformanceCount1.QuadPart >> i;
}
while (true) {
QueryPerformanceCounter (&PerformanceCount2);
if (t == 0.0) {
LARGE_INTEGER PerformanceCount3;
static bool bOnce2 = false;
if (!bOnce2) {
bOnce2 = true;
QueryPerformanceFrequency (&PerformanceCount3);
i = 0;
t = 1000.0 / static_cast<double> (PerformanceCount3.QuadPart);
auto v = t * 2147483648.0;
if (60000.0 > v) {
while (true) {
++i;
v *= 2.0;
t *= 2.0;
if (60000.0 <= v) break;
}
}
}
SleepEx (0, 1);
break;
}
if ((static_cast<double> ((PerformanceCount2.QuadPart >> i) - PerformanceCount1.QuadPart) * t) >= targetFrameTime) break;
SleepEx (0, 1);
}
QueryPerformanceCounter (&PerformanceCount2);
PerformanceCount1.QuadPart = PerformanceCount2.QuadPart >> i;
}
} // namespace patches::FpsLimiter