1
0
Fork 0
mirror of https://github.com/bjornbytes/lovr.git synced 2024-07-06 14:23:34 +00:00
lovr/src/timer/timer.c
bjorn 0e99d47394 Fix module destruction;
There is a problem when a Thread stops: it destroys all of the modules
that it required.  This is because we unconditionally call luax_atexit
when modules are required, and when the thread lua_State dies it takes
all of the modules with it.  To fix this, lovr<Module>Init will return
whether or not initialization successfully happened, which provides us
with enough info to know if we should place the luax_atexit destructor
2018-11-19 09:24:28 -08:00

52 lines
1.1 KiB
C

#include "timer/timer.h"
#include "platform.h"
#include <string.h>
static TimerState state;
bool lovrTimerInit() {
if (state.initialized) return false;
lovrTimerDestroy();
return state.initialized = true;
}
void lovrTimerDestroy() {
if (!state.initialized) return;
memset(&state, 0, sizeof(TimerState));
}
double lovrTimerGetDelta() {
return state.dt;
}
double lovrTimerGetTime() {
return lovrPlatformGetTime();
}
double lovrTimerStep() {
state.lastTime = state.time;
state.time = lovrPlatformGetTime();
state.dt = state.time - state.lastTime;
state.tickSum -= state.tickBuffer[state.tickIndex];
state.tickSum += state.dt;
state.tickBuffer[state.tickIndex] = state.dt;
state.averageDelta = state.tickSum / TICK_SAMPLES;
state.fps = (int) (1 / (state.tickSum / TICK_SAMPLES) + .5);
if (++state.tickIndex == TICK_SAMPLES) {
state.tickIndex = 0;
}
return state.dt;
}
double lovrTimerGetAverageDelta() {
return state.averageDelta;
}
int lovrTimerGetFPS() {
return state.fps;
}
void lovrTimerSleep(double seconds) {
lovrSleep(seconds);
}