added target fps
authorPatrik Gornicz <Gornicz.P@gmail.com>
Thu, 19 Feb 2009 18:40:51 +0000 (13:40 -0500)
committerPatrik Gornicz <Gornicz.P@gmail.com>
Thu, 19 Feb 2009 18:40:51 +0000 (13:40 -0500)
src/main.cpp

index 2744e86..c9e8808 100644 (file)
@@ -74,6 +74,9 @@ static const int s_iMinWaitMicro = s_iMinWaitMilli * 1000;
 static int      s_iTargetUPS     = 100;
 static float    s_fAccUpdateWait = 0;
 
+static int      s_iTargetFPS     = 100;
+static float    s_fAccDrawWait   = 0;
+
 static int s_iUPS, s_iFPS;
 static int s_iUpdateCount, s_iDrawCount;
 static MICRO s_micLastSecond;
@@ -240,7 +243,7 @@ int startUpdateThread(void*)
 
         if(s_iMinWaitMicro < s_fAccUpdateWait)
         {
-            int iWaits = s_fAccUpdateWait / s_iMinWaitMicro;
+            int iWaits = (int)(s_fAccUpdateWait / s_iMinWaitMicro);
             s_fAccUpdateWait -= iWaits * s_iMinWaitMicro;
             SDL_Delay(iWaits * s_iMinWaitMilli);
         }
@@ -257,7 +260,19 @@ int startDrawThread(void*)
 
     while(s_bIsRunning)
     {
-        draw();
+        MICRO time = tickCountMicro();
+            draw();
+        time = tickCountMicro() - time;
+
+        float wait = (1000000.0 / s_iTargetFPS - time);
+        s_fAccDrawWait += 0 < wait ? wait : 0;
+
+        if(s_iMinWaitMicro < s_fAccDrawWait)
+        {
+            int iWaits = (int)(s_fAccDrawWait / s_iMinWaitMicro);
+            s_fAccDrawWait -= iWaits * s_iMinWaitMicro;
+            SDL_Delay(iWaits * s_iMinWaitMilli);
+        }
     }
 
     drawClean();