Limit frame rate to 60fps.

This commit is contained in:
Steve 2018-07-07 14:46:51 +01:00
parent 35885f78f4
commit 23a588f2ef
1 changed files with 17 additions and 8 deletions

View File

@ -22,12 +22,13 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
static void handleMissionArgs(int argc, char *argv[]); static void handleMissionArgs(int argc, char *argv[]);
static void handleLoggingArgs(int argc, char *argv[]); static void handleLoggingArgs(int argc, char *argv[]);
static long capFrameRate(const long then); static void capFrameRate(long *then, float *remainder);
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
long then, lastFrameTime, frames; long then, lastFrameTime, frames;
long expireTextTimer; long expireTextTimer;
float remainder;
SDL_Event event; SDL_Event event;
memset(&app, 0, sizeof(App)); memset(&app, 0, sizeof(App));
@ -63,7 +64,7 @@ int main(int argc, char *argv[])
while (1) while (1)
{ {
then = capFrameRate(then); capFrameRate(&then, &remainder);
while (SDL_PollEvent(&event)) while (SDL_PollEvent(&event))
{ {
@ -190,18 +191,26 @@ int main(int argc, char *argv[])
return 0; return 0;
} }
static long capFrameRate(const long then) static void capFrameRate(long *then, float *remainder)
{ {
long wait; long wait;
wait = 16 - (SDL_GetTicks() - then); wait = 16 + *remainder;
if (wait > 0) *remainder -= (int)*remainder;
wait -= (SDL_GetTicks() - *then);
if (wait < 1)
{ {
SDL_Delay(wait); wait = 1;
} }
return SDL_GetTicks(); SDL_Delay(wait);
*remainder += 0.667;
*then = SDL_GetTicks();
} }
static void handleLoggingArgs(int argc, char *argv[]) static void handleLoggingArgs(int argc, char *argv[])