Skip to content

Commit

Permalink
Add lutro.timer.getFPS()
Browse files Browse the repository at this point in the history
  • Loading branch information
RobLoach committed Sep 5, 2016
1 parent 9f8464d commit f38a755
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lutro.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ lutro_settings_t settings = {
.live_enable = 0,
.live_call_load = 0,
.input_cb = NULL,
.delta = 0
.delta = 0,
.deltaCounter = 0,
.frameCounter = 0,
.fps = 0
};

#if 0
Expand Down Expand Up @@ -539,6 +542,14 @@ void lutro_gamepadevent(lua_State* L)
void lutro_run(double delta)
{
settings.delta = delta;
settings.deltaCounter += delta;
settings.frameCounter += 1;
if (settings.deltaCounter >= 1000.0) {
settings.fps = settings.frameCounter;
settings.frameCounter = 0;
settings.deltaCounter = 0;
}

#ifdef HAVE_INOTIFY
if (settings.live_enable)
lutro_live_update(L);
Expand Down
3 changes: 3 additions & 0 deletions lutro.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ typedef struct lutro_settings_t {
char gamedir[PATH_MAX_LENGTH];
char identity[PATH_MAX_LENGTH];
double delta;
double deltaCounter;
int frameCounter;
int fps;
} lutro_settings_t;

extern lutro_settings_t settings;
Expand Down
3 changes: 3 additions & 0 deletions test/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ function lutro.draw()
-- Testing the keyboard/joystick pressed event.
lutro.graphics.print(joystickButton, 50, 50)
lutro.graphics.print(keypressed, 500, 400)

local fps = lutro.timer.getFPS()
lutro.graphics.print(fps, 500, 500)
end

function lutro.joystickpressed(joystick, button)
Expand Down
13 changes: 13 additions & 0 deletions timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ int lutro_timer_preload(lua_State *L)
static luaL_Reg gfx_funcs[] = {
{ "getTime", timer_getTime },
{ "getDelta", timer_getDelta },
{ "getFPS", timer_getFPS },
{NULL, NULL}
};

Expand Down Expand Up @@ -43,3 +44,15 @@ int timer_getDelta(lua_State *L)

return 1;
}

/**
* lutro.timer.getFPS()
*
* @see https://love2d.org/wiki/love.timer.getFPS
*/
int timer_getFPS(lua_State *L)
{
lua_pushnumber(L, settings.fps);

return 1;
}
1 change: 1 addition & 0 deletions timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ int lutro_timer_preload(lua_State *L);

int timer_getTime(lua_State *L);
int timer_getDelta(lua_State *L);
int timer_getFPS(lua_State *L);

#endif // TIMER_H

0 comments on commit f38a755

Please sign in to comment.