Add a better FPS limiter

This commit is contained in:
Skyth
2019-08-29 15:43:26 +03:00
parent 3ebf184615
commit 244b6eca25
10 changed files with 32 additions and 107 deletions
@@ -7,6 +7,10 @@
#include <windows.h>
#include <iostream>
#include <chrono>
using namespace std::chrono;
int hookedCreateWindow(const char* title, void(__cdecl* exit_function)(int))
{
SetProcessDPIAware();
@@ -91,6 +95,21 @@ __int64 hookedParseParameters(int a1, __int64* a2)
return divaParseParameters(a1, a2);
}
time_point<high_resolution_clock> nextUpdate;
microseconds expectedFrameDuration(1000000 / nFpsLimit);
__int64 __fastcall hookedEngineUpdate(__int64 a1)
{
if (high_resolution_clock::now() < nextUpdate)
return 0;
const auto result = divaEngineUpdate(a1);
nextUpdate = high_resolution_clock::now() + expectedFrameDuration;
return result;
}
BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call,
@@ -109,6 +128,14 @@ BOOL APIENTRY DllMain(HMODULE hModule,
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)divaParseParameters, hookedParseParameters);
DetourTransactionCommit();
if (nFpsLimit > 0)
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)divaEngineUpdate, hookedEngineUpdate);
DetourTransactionCommit();
}
}
return TRUE;
}
@@ -7,6 +7,7 @@
static int(__cdecl* divaCreateWindow)(const char* title, void(__cdecl* exitfunc)(int)) = (int(__cdecl*)(const char* title, void(__cdecl * exitfunc)(int)))0x140194D00;
__int64 (__fastcall* divaParseParameters)(int a1, __int64* a2) = (__int64(__fastcall*)(int a1, __int64* a2))0x140193630;
__int64(__fastcall* divaEngineUpdate)(__int64 a1) = (__int64(__fastcall*)(__int64 a1))0x140194D20;
uint8_t* fullScreenFlag = (uint8_t*)0x140EDA5D1;
DWORD* resolutionType = (DWORD*)0x140EDA5D4;
@@ -34,6 +35,8 @@ int nIntResHeight = GetPrivateProfileIntW(L"resolution", L"r.height", 720, CONFI
int nBitDepth = GetPrivateProfileIntW(L"resolution", L"bitdepth", 32, CONFIG_FILE);
int nRefreshRate = GetPrivateProfileIntW(L"resolution", L"refreshrate", 60, CONFIG_FILE);
int nFpsLimit = GetPrivateProfileIntW(L"graphics", L"fps.limit", 60, CONFIG_FILE);
// used to trick Optimus into switching to the NVIDIA GPU
HMODULE nvcudaModule = LoadLibraryW(L"nvcuda.dll");
// cuInit actually returns a CUresult, but we don't really care about it