Implement a frame limiter.

This commit is contained in:
Skyth 2024-12-21 18:47:26 +03:00
parent 11dea728fd
commit 939f4a4771
5 changed files with 45 additions and 1 deletions

5
.gitmodules vendored
View file

@ -57,4 +57,7 @@
url = https://github.com/martinus/unordered_dense.git url = https://github.com/martinus/unordered_dense.git
[submodule "thirdparty/SDL_mixer"] [submodule "thirdparty/SDL_mixer"]
path = thirdparty/SDL_mixer path = thirdparty/SDL_mixer
url = https://github.com/libsdl-org/SDL_mixer url = https://github.com/libsdl-org/SDL_mixer
[submodule "thirdparty/implot"]
path = thirdparty/implot
url = https://github.com/epezent/implot.git

View file

@ -6,6 +6,32 @@
#include <user/config.h> #include <user/config.h>
#include <os/process.h> #include <os/process.h>
void FrameLimiter::execute(int64_t fps)
{
using namespace std::chrono_literals;
auto next = start + frame * 1000000000ns / fps;
auto now = std::chrono::steady_clock::now();
if (next > now && (next - now) < (2000000000ns / fps))
{
std::this_thread::sleep_for(std::chrono::floor<std::chrono::milliseconds>(next - now - 1ms));
while ((now = std::chrono::steady_clock::now()) < next)
std::this_thread::yield();
}
frame = std::chrono::nanoseconds(std::chrono::steady_clock::now() - start).count() * fps / 1000000000ll + 1;
}
static FrameLimiter g_frameLimiter;
void ApplicationUpdateMidAsmHook()
{
if (Config::FPS >= 15 && Config::FPS < 240)
g_frameLimiter.execute(Config::FPS);
}
void App::Restart(std::vector<std::string> restartArgs) void App::Restart(std::vector<std::string> restartArgs)
{ {
os::process::StartProcess(os::process::GetExecutablePath(), restartArgs, os::process::GetWorkingDirectory()); os::process::StartProcess(os::process::GetExecutablePath(), restartArgs, os::process::GetWorkingDirectory());
@ -48,3 +74,4 @@ PPC_FUNC(sub_822C1130)
__imp__sub_822C1130(ctx, base); __imp__sub_822C1130(ctx, base);
} }

View file

@ -2,6 +2,14 @@
#include <user/config.h> #include <user/config.h>
struct FrameLimiter
{
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
int64_t frame = 0;
void execute(int64_t fps);
};
class App class App
{ {
public: public:
@ -15,3 +23,4 @@ public:
static void Restart(std::vector<std::string> restartArgs = {}); static void Restart(std::vector<std::string> restartArgs = {});
static void Exit(); static void Exit();
}; };

View file

@ -588,3 +588,7 @@ registers = ["r3"]
name = "PostureDPadSupportMidAsmHook" name = "PostureDPadSupportMidAsmHook"
address = 0x823CDA2C address = 0x823CDA2C
registers = ["r3"] registers = ["r3"]
[[midasm_hook]]
name = "ApplicationUpdateMidAsmHook"
address = 0x822C0EC8

1
thirdparty/implot vendored Submodule

@ -0,0 +1 @@
Subproject commit 77674d270e851d3f3718aad00234201af2b76ac9