mirror of https://github.com/bjornbytes/lovr.git
Improve lovr.timer;
This commit is contained in:
parent
38c46106da
commit
1cdde81e35
|
@ -2,6 +2,8 @@
|
|||
#include "../timer/timer.h"
|
||||
|
||||
const luaL_Reg lovrTimer[] = {
|
||||
{ "getDelta", l_lovrTimerGetDelta },
|
||||
{ "getTime", l_lovrTimerGetTime },
|
||||
{ "step", l_lovrTimerStep },
|
||||
{ "sleep", l_lovrTimerSleep },
|
||||
{ NULL, NULL }
|
||||
|
@ -10,6 +12,17 @@ const luaL_Reg lovrTimer[] = {
|
|||
int l_lovrTimerInit(lua_State* L) {
|
||||
lua_newtable(L);
|
||||
luaL_register(L, NULL, lovrTimer);
|
||||
lovrTimerInit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_lovrTimerGetDelta(lua_State* L) {
|
||||
lua_pushnumber(L, lovrTimerGetDelta());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_lovrTimerGetTime(lua_State* L) {
|
||||
lua_pushnumber(L, lovrTimerGetTime());
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
|
@ -4,5 +4,7 @@
|
|||
|
||||
extern const luaL_Reg lovrTimer[];
|
||||
int l_lovrTimerInit(lua_State* L);
|
||||
int l_lovrTimerGetDelta(lua_State* L);
|
||||
int l_lovrTimerGetTime(lua_State* L);
|
||||
int l_lovrTimerStep(lua_State* L);
|
||||
int l_lovrTimerSleep(lua_State* L);
|
||||
|
|
|
@ -6,10 +6,25 @@
|
|||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
static TimerState timerState;
|
||||
|
||||
void lovrTimerInit() {
|
||||
lovrTimerStep();
|
||||
}
|
||||
|
||||
double lovrTimerGetDelta() {
|
||||
return timerState.dt;
|
||||
}
|
||||
|
||||
double lovrTimerGetTime() {
|
||||
return glfwGetTime();
|
||||
}
|
||||
|
||||
double lovrTimerStep() {
|
||||
double time = glfwGetTime();
|
||||
glfwSetTime(0);
|
||||
return time;
|
||||
timerState.lastTime = timerState.time;
|
||||
timerState.time = glfwGetTime();
|
||||
timerState.dt = timerState.time - timerState.lastTime;
|
||||
return timerState.dt;
|
||||
}
|
||||
|
||||
void lovrTimerSleep(double seconds) {
|
||||
|
|
|
@ -1,2 +1,13 @@
|
|||
#ifndef LOVR_TIMER_TYPES
|
||||
typedef struct {
|
||||
double lastTime;
|
||||
double time;
|
||||
double dt;
|
||||
} TimerState;
|
||||
#endif
|
||||
|
||||
void lovrTimerInit();
|
||||
double lovrTimerGetDelta();
|
||||
double lovrTimerGetTime();
|
||||
double lovrTimerStep();
|
||||
void lovrTimerSleep(double seconds);
|
||||
|
|
Loading…
Reference in New Issue