core: different frame limiter implementation 2/head
authornotaz <notasas@gmail.com>
Fri, 27 Jun 2014 23:06:52 +0000 (02:06 +0300)
committernotaz <notasas@gmail.com>
Sat, 28 Jun 2014 12:37:36 +0000 (15:37 +0300)
should avoid useless sleeps while lagging behind

source/mupen64plus-core/src/main/main.c

index c1bd208..6b7a632 100755 (executable)
@@ -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
 */