Browse Source

Add love.timer.getAverageDelta (issue #570) and constify love.timer.sleep

Bart van Strien 12 years ago
parent
commit
28af989a00

+ 1 - 0
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.

+ 2 - 1
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

+ 8 - 1
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;

+ 3 - 1
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;

+ 7 - 0
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 },

+ 1 - 0
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);