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

This commit is contained in:
bart@bartbes.ath.cx
2009-11-22 13:19:13 +01:00
parent f76fd131d8
commit 868c2577a7
4 changed files with 67 additions and 20 deletions
+26
View File
@@ -90,6 +90,32 @@ namespace sdl
return (SDL_GetTicks() - time_init)/1000.0f; 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 } // sdl
} // timer } // timer
} // love } // love
+13
View File
@@ -24,6 +24,12 @@
// SDL // SDL
#include <SDL.h> #include <SDL.h>
#ifdef LOVE_WINDOWS
#include <time.h>
#else
#include <sys/time.h>
#endif
// LOVE // LOVE
#include <common/Module.h> #include <common/Module.h>
@@ -112,6 +118,13 @@ namespace sdl
**/ **/
float getTime() const; 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 }; // Timer
} // sdl } // sdl
+7
View File
@@ -60,6 +60,12 @@ namespace sdl
return 1; return 1;
} }
int w_getMicroTime(lua_State * L)
{
lua_pushnumber(L, instance->getMicroTime());
return 1;
}
// List of functions to wrap. // List of functions to wrap.
static const luaL_Reg functions[] = { static const luaL_Reg functions[] = {
{ "step", w_step }, { "step", w_step },
@@ -67,6 +73,7 @@ namespace sdl
{ "getFPS", w_getFPS }, { "getFPS", w_getFPS },
{ "sleep", w_sleep }, { "sleep", w_sleep },
{ "getTime", w_getTime }, { "getTime", w_getTime },
{ "getMicroTime", w_getMicroTime },
{ 0, 0 } { 0, 0 }
}; };
+1
View File
@@ -36,6 +36,7 @@ namespace sdl
int w_getFPS(lua_State * L); int w_getFPS(lua_State * L);
int w_sleep(lua_State * L); int w_sleep(lua_State * L);
int w_getTime(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); extern "C" LOVE_EXPORT int luaopen_love_timer(lua_State * L);
} // sdl } // sdl