From 868c2577a77bd1a86cac5ea27beb4e8858e3129c Mon Sep 17 00:00:00 2001 From: "bart@bartbes.ath.cx" Date: Sun, 22 Nov 2009 13:19:13 +0100 Subject: [PATCH] Added (hopefully) cross-platform love.timer.getMicroTime --- src/modules/timer/sdl/Timer.cpp | 36 ++++++++++++++++++++++++---- src/modules/timer/sdl/Timer.h | 27 +++++++++++++++------ src/modules/timer/sdl/wrap_Timer.cpp | 17 +++++++++---- src/modules/timer/sdl/wrap_Timer.h | 7 +++--- 4 files changed, 67 insertions(+), 20 deletions(-) diff --git a/src/modules/timer/sdl/Timer.cpp b/src/modules/timer/sdl/Timer.cpp index dfd6217d4..cc19a29ec 100644 --- a/src/modules/timer/sdl/Timer.cpp +++ b/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 diff --git a/src/modules/timer/sdl/Timer.h b/src/modules/timer/sdl/Timer.h index 1879dbfb3..a37fcca74 100644 --- a/src/modules/timer/sdl/Timer.h +++ b/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 +#ifdef LOVE_WINDOWS + #include +#else + #include +#endif + // LOVE #include @@ -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 diff --git a/src/modules/timer/sdl/wrap_Timer.cpp b/src/modules/timer/sdl/wrap_Timer.cpp index 53b5a2f7d..61390187d 100644 --- a/src/modules/timer/sdl/wrap_Timer.cpp +++ b/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()); diff --git a/src/modules/timer/sdl/wrap_Timer.h b/src/modules/timer/sdl/wrap_Timer.h index e1bf0fb63..ae4948a41 100644 --- a/src/modules/timer/sdl/wrap_Timer.h +++ b/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