diff --git a/.gitmodules b/.gitmodules index 6981e9f4..0784b50c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -57,4 +57,7 @@ url = https://github.com/martinus/unordered_dense.git [submodule "thirdparty/SDL_mixer"] path = thirdparty/SDL_mixer - url = https://github.com/libsdl-org/SDL_mixer \ No newline at end of file + url = https://github.com/libsdl-org/SDL_mixer +[submodule "thirdparty/implot"] + path = thirdparty/implot + url = https://github.com/epezent/implot.git diff --git a/UnleashedRecomp/app.cpp b/UnleashedRecomp/app.cpp index fa8c0982..6ab35db0 100644 --- a/UnleashedRecomp/app.cpp +++ b/UnleashedRecomp/app.cpp @@ -6,6 +6,32 @@ #include #include +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(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 restartArgs) { os::process::StartProcess(os::process::GetExecutablePath(), restartArgs, os::process::GetWorkingDirectory()); @@ -48,3 +74,4 @@ PPC_FUNC(sub_822C1130) __imp__sub_822C1130(ctx, base); } + diff --git a/UnleashedRecomp/app.h b/UnleashedRecomp/app.h index b6541358..5abf4452 100644 --- a/UnleashedRecomp/app.h +++ b/UnleashedRecomp/app.h @@ -2,6 +2,14 @@ #include +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 { public: @@ -15,3 +23,4 @@ public: static void Restart(std::vector restartArgs = {}); static void Exit(); }; + diff --git a/UnleashedRecompLib/config/SWA.toml b/UnleashedRecompLib/config/SWA.toml index cc13d6a1..269911ea 100644 --- a/UnleashedRecompLib/config/SWA.toml +++ b/UnleashedRecompLib/config/SWA.toml @@ -588,3 +588,7 @@ registers = ["r3"] name = "PostureDPadSupportMidAsmHook" address = 0x823CDA2C registers = ["r3"] + +[[midasm_hook]] +name = "ApplicationUpdateMidAsmHook" +address = 0x822C0EC8 diff --git a/thirdparty/implot b/thirdparty/implot new file mode 160000 index 00000000..77674d27 --- /dev/null +++ b/thirdparty/implot @@ -0,0 +1 @@ +Subproject commit 77674d270e851d3f3718aad00234201af2b76ac9