From 28af989a0044ecdd23983fe294a4804edcc859b3 Mon Sep 17 00:00:00 2001 From: Bart van Strien Date: Sun, 10 Mar 2013 22:07:24 +0100 Subject: [PATCH] Add love.timer.getAverageDelta (issue #570) and constify love.timer.sleep --- changes.txt | 1 + src/modules/timer/Timer.h | 3 ++- src/modules/timer/sdl/Timer.cpp | 9 ++++++++- src/modules/timer/sdl/Timer.h | 4 +++- src/modules/timer/wrap_Timer.cpp | 7 +++++++ src/modules/timer/wrap_Timer.h | 1 + 6 files changed, 22 insertions(+), 3 deletions(-) diff --git a/changes.txt b/changes.txt index e4b5b2a81..fa1a21e4e 100644 --- a/changes.txt +++ b/changes.txt @@ -28,6 +28,7 @@ LOVE 0.9.0 [] * Added blend mode "none". * Added SpriteBatch:isEmpty and SpriteBatch:isFull. * Added support for extern Images in Shaders when drawing graphics primitives. + * Added love.timer.getAverageDelta. * OPTIONAL: Added support for GME. * Fixed crashes with font drawing on some ATI cards. diff --git a/src/modules/timer/Timer.h b/src/modules/timer/Timer.h index 412d19f0f..79421729a 100644 --- a/src/modules/timer/Timer.h +++ b/src/modules/timer/Timer.h @@ -49,7 +49,7 @@ public: * usually 1ms. * @param seconds The number of seconds to sleep for. **/ - virtual void sleep(double seconds) = 0; + virtual void sleep(double seconds) const = 0; /** * Gets the time between the last two frames, assuming step is called @@ -63,6 +63,7 @@ public: * @return The "current" FPS. **/ virtual int getFPS() const = 0; + virtual double getAverageDelta() const = 0; /** * Gets the amount of time since the program started. Only useful for timing diff --git a/src/modules/timer/sdl/Timer.cpp b/src/modules/timer/sdl/Timer.cpp index 0e2ae1b34..e4f7ec7a1 100644 --- a/src/modules/timer/sdl/Timer.cpp +++ b/src/modules/timer/sdl/Timer.cpp @@ -41,6 +41,7 @@ Timer::Timer() : currTime(0) , prevFpsUpdate(0) , fps(0) + , averageDelta(0) , fpsUpdateFrequency(1) , frames(0) , dt(0) @@ -80,12 +81,13 @@ void Timer::step() if (timeSinceLast > fpsUpdateFrequency) { fps = int((frames/timeSinceLast) + 0.5); + averageDelta = timeSinceLast/frames; prevFpsUpdate = currTime; frames = 0; } } -void Timer::sleep(double seconds) +void Timer::sleep(double seconds) const { if (seconds > 0) delay((int)(seconds*1000)); @@ -101,6 +103,11 @@ int Timer::getFPS() const return fps; } +double Timer::getAverageDelta() const +{ + return averageDelta; +} + double Timer::getTime() const { return SDL_GetTicks()/1000.0; diff --git a/src/modules/timer/sdl/Timer.h b/src/modules/timer/sdl/Timer.h index c4d138d55..913cdcb1b 100644 --- a/src/modules/timer/sdl/Timer.h +++ b/src/modules/timer/sdl/Timer.h @@ -54,9 +54,10 @@ public: const char *getName() const; void step(); - void sleep(double seconds); + void sleep(double seconds) const; double getDelta() const; int getFPS() const; + double getAverageDelta() const; double getTime() const; double getMicroTime() const; @@ -69,6 +70,7 @@ private: // Updated with a certain frequency. int fps; + double averageDelta; // The frequency by which to update the FPS. double fpsUpdateFrequency; diff --git a/src/modules/timer/wrap_Timer.cpp b/src/modules/timer/wrap_Timer.cpp index f95af3978..6379d5bb2 100644 --- a/src/modules/timer/wrap_Timer.cpp +++ b/src/modules/timer/wrap_Timer.cpp @@ -50,6 +50,12 @@ int w_getFPS(lua_State *L) return 1; } +int w_getAverageDelta(lua_State *L) +{ + lua_pushnumber(L, instance->getAverageDelta()); + return 1; +} + int w_sleep(lua_State *L) { instance->sleep((float) luaL_checknumber(L, 1)); @@ -74,6 +80,7 @@ static const luaL_Reg functions[] = { "step", w_step }, { "getDelta", w_getDelta }, { "getFPS", w_getFPS }, + { "getAverageDelta", w_getAverageDelta }, { "sleep", w_sleep }, { "getTime", w_getTime }, { "getMicroTime", w_getMicroTime }, diff --git a/src/modules/timer/wrap_Timer.h b/src/modules/timer/wrap_Timer.h index 86234daf7..10646cb70 100644 --- a/src/modules/timer/wrap_Timer.h +++ b/src/modules/timer/wrap_Timer.h @@ -32,6 +32,7 @@ namespace timer int w_step(lua_State *L); int w_getDelta(lua_State *L); int w_getFPS(lua_State *L); +int w_getAverageDelta(lua_State *L); int w_sleep(lua_State *L); int w_getTime(lua_State *L); int w_getMicroTime(lua_State *L);