From 1ad0b75f4090a34fbc0ad7bfcbd04e5bff5f1e71 Mon Sep 17 00:00:00 2001 From: notaz Date: Sat, 28 Jun 2014 02:06:52 +0300 Subject: [PATCH] core: different frame limiter implementation should avoid useless sleeps while lagging behind --- source/mupen64plus-core/src/main/main.c | 58 ++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/source/mupen64plus-core/src/main/main.c b/source/mupen64plus-core/src/main/main.c index c1bd208..6b7a632 100755 --- a/source/mupen64plus-core/src/main/main.c +++ b/source/mupen64plus-core/src/main/main.c @@ -673,7 +673,7 @@ void new_frame(void) } } -void new_vi(void) +void new_vi_(void) { int Dif; unsigned int CurrentFPSTime; @@ -724,6 +724,62 @@ void new_vi(void) end_section(IDLE_SECTION); } +#define TOL_FRAMES 3 + +void new_vi(void) +{ + static int prev_vilimit, prev_factor; + static float counter, interval; + unsigned int now, expect; + int diff; + + if (!l_MainSpeedLimit) + return; + + if (ROM_PARAMS.vilimit != prev_vilimit || l_SpeedFactor != prev_factor) + { + double VILimitMilliseconds = 1000.0 / ROM_PARAMS.vilimit; + interval = VILimitMilliseconds * 100.0 / l_SpeedFactor; // adjust for selected emulator speed + prev_vilimit = ROM_PARAMS.vilimit; + prev_factor = l_SpeedFactor; + } + + start_section(IDLE_SECTION); + +#ifdef DBG + if(g_DebuggerActive) DebuggerCallback(DEBUG_UI_VI, 0); +#endif + + now = SDL_GetTicks(); + counter += interval; + expect = (int)counter; + diff = now - expect; + + if (diff < -200 || diff > 200) + { + counter = now - 1 * 17; + return; + } + + if (diff > TOL_FRAMES * 17) + { + //printf("lim %d\n", diff); + counter = now - TOL_FRAMES * 17; + return; + } + + if (diff < 0) + { + int time = -diff; + time -= time / 4; + DebugMessage(M64MSG_VERBOSE, " new_vi(): Waiting %ims", time); + //printf("sleep %2d\n", time); + SDL_Delay(time); + } + + end_section(IDLE_SECTION); +} + /********************************************************************************************************* * emulation thread - runs the core */ -- 2.39.2