Browse Source

Added (hopefully) cross-platform love.timer.getMicroTime

[email protected] 15 years ago
parent
commit
868c2577a7

+ 31 - 5
src/modules/timer/sdl/Timer.cpp

@@ -1,14 +1,14 @@
 /**
 * Copyright (c) 2006-2009 LOVE Development Team
-* 
+*
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the authors be held liable for any damages
 * arising from the use of this software.
-* 
+*
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
-* 
+*
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
@@ -27,7 +27,7 @@ namespace timer
 namespace sdl
 {
 	Timer::Timer()
-		: time_init(0), currTime(0), prevFpsUpdate(0), fps(0), fpsUpdateFrequency(1), 
+		: time_init(0), currTime(0), prevFpsUpdate(0), fps(0), fpsUpdateFrequency(1),
 		frames(0), dt(0)
 	{
 		// Init the SDL timer system.
@@ -50,7 +50,7 @@ namespace sdl
 	{
 		// Frames rendered
 		frames++;
-			
+
 		// "Current" time is previous time by now.
 		prevTime = currTime;
 
@@ -90,6 +90,32 @@ namespace sdl
 		return (SDL_GetTicks() - time_init)/1000.0f;
 	}
 
+	float Timer::getMicroTime() const
+	{
+		#ifdef LOVE_WINDOWS
+			FILETIME ft;
+			unsigned __int64 t = 0;
+			GetSystemTimeAsFileTime(&ft);
+			t |= ft.dwHighDateTime;
+			t <<= 32;
+			t |= ft.dwLowDateTime;
+			t /= 10;
+			#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
+				#define DELTA_EPOCH_IN_MICROSECS  11644473600000000Ui64
+			#else
+				#define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL
+			#endif
+			t -= DELTA_EPOCH_IN_MICROSECS;
+			float floatt = t/1000000.0f;
+			floatt %= 86400;
+			return floatt;
+		#else
+			timeval t;
+			gettimeofday(&t, NULL);
+			return t.tv_sec%86400 + t.tv_usec/1000000.0f;
+		#endif
+	}
+
 } // sdl
 } // timer
 } // love

+ 20 - 7
src/modules/timer/sdl/Timer.h

@@ -1,14 +1,14 @@
 /**
 * Copyright (c) 2006-2009 LOVE Development Team
-* 
+*
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the authors be held liable for any damages
 * arising from the use of this software.
-* 
+*
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
-* 
+*
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
@@ -24,6 +24,12 @@
 // SDL
 #include <SDL.h>
 
+#ifdef LOVE_WINDOWS
+	#include <time.h>
+#else
+	#include <sys/time.h>
+#endif
+
 // LOVE
 #include <common/Module.h>
 
@@ -43,7 +49,7 @@ namespace sdl
 
 		// Timing vars for benchmarking.
 		Uint32 time_init;
-		
+
 		// Frame delta vars.
 		Uint32 currTime;
 		Uint32 prevTime;
@@ -78,9 +84,9 @@ namespace sdl
 		* @return Always returns "love.timer.sdl".
 		**/
 		const char * getName() const;
-		
+
 		/**
-		* Measures the time between this call and the previous call, 
+		* Measures the time between this call and the previous call,
 		* and updates internal values accordinly.
 		**/
 		void step();
@@ -101,7 +107,7 @@ namespace sdl
 		/**
 		* Gets the average FPS over the last second. Beucase the value is only updated
 		* once per second, it does not look erratic when displayed on screen.
-		* @return The "current" FPS. 
+		* @return The "current" FPS.
 		**/
 		float getFPS() const;
 
@@ -112,6 +118,13 @@ namespace sdl
 		**/
 		float getTime() const;
 
+		/**
+		 * Gets the amount of time since the Epoch. Useful for timing too because
+		 * it is accurate to the microsecond.
+		 * @return The time (in seconds) since the Epoch.
+		 **/
+		float getMicroTime() const;
+
 	}; // Timer
 
 } // sdl

+ 12 - 5
src/modules/timer/sdl/wrap_Timer.cpp

@@ -1,14 +1,14 @@
 /**
 * Copyright (c) 2006-2009 LOVE Development Team
-* 
+*
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the authors be held liable for any damages
 * arising from the use of this software.
-* 
+*
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
-* 
+*
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
@@ -60,6 +60,12 @@ namespace sdl
 		return 1;
 	}
 
+	int w_getMicroTime(lua_State * L)
+	{
+		lua_pushnumber(L, instance->getMicroTime());
+		return 1;
+	}
+
 	// List of functions to wrap.
 	static const luaL_Reg functions[] = {
 		{ "step", w_step },
@@ -67,6 +73,7 @@ namespace sdl
 		{ "getFPS", w_getFPS },
 		{ "sleep", w_sleep },
 		{ "getTime", w_getTime },
+		{ "getMicroTime", w_getMicroTime },
 		{ 0, 0 }
 	};
 
@@ -75,10 +82,10 @@ namespace sdl
 	{
 		if(instance == 0)
 		{
-			try 
+			try
 			{
 				instance = new Timer();
-			} 
+			}
 			catch(Exception & e)
 			{
 				return luaL_error(L, e.what());

+ 4 - 3
src/modules/timer/sdl/wrap_Timer.h

@@ -1,14 +1,14 @@
 /**
 * Copyright (c) 2006-2009 LOVE Development Team
-* 
+*
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the authors be held liable for any damages
 * arising from the use of this software.
-* 
+*
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
-* 
+*
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
@@ -36,6 +36,7 @@ namespace sdl
 	int w_getFPS(lua_State * L);
 	int w_sleep(lua_State * L);
 	int w_getTime(lua_State * L);
+	int w_getMicroTime(lua_State * L);
 	extern "C" LOVE_EXPORT int luaopen_love_timer(lua_State * L);
 
 } // sdl