[client] use clock_nanosleep for more accurate frame timing

This commit is contained in:
Geoffrey McRae 2018-12-13 01:21:56 +11:00
parent 5fe2db7e56
commit 49bd091359

View File

@ -189,6 +189,9 @@ int renderThread(void * unused)
if (!state.lgr->render_startup(state.lgrData, state.window)) if (!state.lgr->render_startup(state.lgrData, state.window))
return 1; return 1;
struct timespec time;
clock_gettime(CLOCK_MONOTONIC, &time);
while(state.running) while(state.running)
{ {
if (state.lgrResize) if (state.lgrResize)
@ -198,8 +201,6 @@ int renderThread(void * unused)
state.lgrResize = false; state.lgrResize = false;
} }
const uint64_t start = microtime();
if (!state.lgr->render(state.lgrData, state.window)) if (!state.lgr->render(state.lgrData, state.window))
break; break;
@ -212,22 +213,23 @@ int renderThread(void * unused)
{ {
const float avgUPS = 1000.0f / (((float)state.renderTime / state.frameCount ) / 1e6f); const float avgUPS = 1000.0f / (((float)state.renderTime / state.frameCount ) / 1e6f);
const float avgFPS = 1000.0f / (((float)state.renderTime / state.renderCount) / 1e6f); const float avgFPS = 1000.0f / (((float)state.renderTime / state.renderCount) / 1e6f);
state.lgr->update_fps(state.lgrData, avgUPS, avgFPS);
state.renderTime = 0; state.renderTime = 0;
state.frameCount = 0; state.frameCount = 0;
state.renderCount = 0; state.renderCount = 0;
state.lgr->update_fps(state.lgrData, avgUPS, avgFPS);
} }
const uint64_t total = microtime() - start; uint64_t nsec = time.tv_nsec + (1e9 / params.fpsLimit);
if (total < state.fpsSleep) if (nsec > 1e9)
{ {
usleep(state.fpsSleep - total); time.tv_nsec = nsec - 1e9;
int64_t delta = (1000000 / params.fpsLimit) - (microtime() - start); ++time.tv_sec;
state.fpsSleep += delta / 16;
if (state.fpsSleep < 0)
state.fpsSleep = 0;
} }
else
time.tv_nsec = nsec;
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &time, NULL);
} }
return 0; return 0;