From 4f8dd981353b02ce0eb5fe76746d0f55cd951ea7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Dobra?= Date: Mon, 17 Jan 2022 07:30:17 +0100 Subject: [PATCH] Add Target UPS registry option (#85) --- SpaceCadetPinball/options.cpp | 4 ++++ SpaceCadetPinball/options.h | 1 + SpaceCadetPinball/pb.cpp | 6 +++++- SpaceCadetPinball/winmain.cpp | 7 +++++-- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/SpaceCadetPinball/options.cpp b/SpaceCadetPinball/options.cpp index 85ca203..7c8854e 100644 --- a/SpaceCadetPinball/options.cpp +++ b/SpaceCadetPinball/options.cpp @@ -136,6 +136,9 @@ void options::ReadOptions() Options.BottomTableBumpKey = get_int(nullptr, "Bottom Table Bump key", Options.BottomTableBumpKey); Options.UniformScaling = get_int(nullptr, "Uniform scaling", true); Options.AlternativeRender = get_int(nullptr, "Alternative Render", false); + Options.TargetUps = get_int(nullptr, "Target UPS", 120); + Options.TargetUps = max(60, Options.TargetUps); + Options.TargetUps = min(Options.TargetUps, 360); auto defaultLanguage = Languages::English; auto language = static_cast(get_int(nullptr, "Language", static_cast(defaultLanguage))); @@ -235,6 +238,7 @@ void options::uninit() set_int(nullptr, "Uniform scaling", Options.UniformScaling); set_int(nullptr, "Alternative Render", Options.AlternativeRender); set_int(nullptr, "Language", static_cast(Options.Language)); + set_int(nullptr, "Target UPS", Options.TargetUps); } void options::path_init(LPCSTR regPath) diff --git a/SpaceCadetPinball/options.h b/SpaceCadetPinball/options.h index 9f347e4..0b489d0 100644 --- a/SpaceCadetPinball/options.h +++ b/SpaceCadetPinball/options.h @@ -59,6 +59,7 @@ struct optionsStruct int Resolution; bool UniformScaling; bool AlternativeRender; + int TargetUps; Languages Language; }; diff --git a/SpaceCadetPinball/pb.cpp b/SpaceCadetPinball/pb.cpp index 19fde64..fe98f42 100644 --- a/SpaceCadetPinball/pb.cpp +++ b/SpaceCadetPinball/pb.cpp @@ -270,9 +270,13 @@ void pb::frame(float dtMilliSec) // Retained render prevents frame skip. The next best thing - complete refresh at fixed rate. render::update(false); + auto targetFps = options::Options.TargetUps / 2.0f; + targetFps = max(targetFps, 60.0f); // at least 60 + // Frame time at 60 FPS = 16.(6) ms - auto targetTime = 1000 / 60.0f; + auto targetTime = 1000.0f / targetFps; frameTime += dtMilliSec; + if (frameTime >= targetTime) { frameTime = min(frameTime - targetTime, 100); diff --git a/SpaceCadetPinball/winmain.cpp b/SpaceCadetPinball/winmain.cpp index a54e666..cac5784 100644 --- a/SpaceCadetPinball/winmain.cpp +++ b/SpaceCadetPinball/winmain.cpp @@ -12,8 +12,6 @@ #include "splash.h" #include "render.h" -const float TargetUPS = 120, TargetFrameTime = 1000 / TargetUPS; - HINSTANCE winmain::hinst = nullptr; HWND winmain::hwnd_frame = nullptr; HCURSOR winmain::mouse_hsave; @@ -226,6 +224,11 @@ int winmain::WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi pb::toggle_demo(); else pb::replay_level(0); + + // To have a smooth display, Updates Per Sec (UPS) should be fps*2. + // Defaulted to 125 UPS, to leave some time for rendering. + float TargetFrameTime = 1000.0f / options::Options.TargetUps; + TargetFrameTime = max(TargetFrameTime, 1); DWORD someTimeCounter = 300u, prevTime = 0u, frameStart = timeGetTime(); float sleepRemainder = 0, frameDuration = TargetFrameTime;