From: Patrik Gornicz Date: Thu, 19 Feb 2009 18:40:51 +0000 (-0500) Subject: added target fps X-Git-Tag: physics-premerge~46 X-Git-Url: http://gitweb.pgornicz.com/gitweb.cgi?a=commitdiff_plain;h=fa88591393d1cece76cd30830e62bf95b684cc21;p=libbear.git added target fps --- diff --git a/src/main.cpp b/src/main.cpp index 2744e86..c9e8808 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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();