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
+31 -5
View File
@@ -1,14 +1,14 @@
/** /**
* Copyright (c) 2006-2009 LOVE Development Team * Copyright (c) 2006-2009 LOVE Development Team
* *
* This software is provided 'as-is', without any express or implied * This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages * warranty. In no event will the authors be held liable for any damages
* arising from the use of this software. * arising from the use of this software.
* *
* Permission is granted to anyone to use this software for any purpose, * Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it * including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions: * freely, subject to the following restrictions:
* *
* 1. The origin of this software must not be misrepresented; you must not * 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 * claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be * in a product, an acknowledgment in the product documentation would be
@@ -27,7 +27,7 @@ namespace timer
namespace sdl namespace sdl
{ {
Timer::Timer() 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) frames(0), dt(0)
{ {
// Init the SDL timer system. // Init the SDL timer system.
@@ -50,7 +50,7 @@ namespace sdl
{ {
// Frames rendered // Frames rendered
frames++; frames++;
// "Current" time is previous time by now. // "Current" time is previous time by now.
prevTime = currTime; prevTime = currTime;
@@ -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
+20 -7
View File
@@ -1,14 +1,14 @@
/** /**
* Copyright (c) 2006-2009 LOVE Development Team * Copyright (c) 2006-2009 LOVE Development Team
* *
* This software is provided 'as-is', without any express or implied * This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages * warranty. In no event will the authors be held liable for any damages
* arising from the use of this software. * arising from the use of this software.
* *
* Permission is granted to anyone to use this software for any purpose, * Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it * including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions: * freely, subject to the following restrictions:
* *
* 1. The origin of this software must not be misrepresented; you must not * 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 * claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be * in a product, an acknowledgment in the product documentation would be
@@ -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>
@@ -43,7 +49,7 @@ namespace sdl
// Timing vars for benchmarking. // Timing vars for benchmarking.
Uint32 time_init; Uint32 time_init;
// Frame delta vars. // Frame delta vars.
Uint32 currTime; Uint32 currTime;
Uint32 prevTime; Uint32 prevTime;
@@ -78,9 +84,9 @@ namespace sdl
* @return Always returns "love.timer.sdl". * @return Always returns "love.timer.sdl".
**/ **/
const char * getName() const; 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. * and updates internal values accordinly.
**/ **/
void step(); void step();
@@ -101,7 +107,7 @@ namespace sdl
/** /**
* Gets the average FPS over the last second. Beucase the value is only updated * 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. * once per second, it does not look erratic when displayed on screen.
* @return The "current" FPS. * @return The "current" FPS.
**/ **/
float getFPS() const; float getFPS() const;
@@ -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
+12 -5
View File
@@ -1,14 +1,14 @@
/** /**
* Copyright (c) 2006-2009 LOVE Development Team * Copyright (c) 2006-2009 LOVE Development Team
* *
* This software is provided 'as-is', without any express or implied * This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages * warranty. In no event will the authors be held liable for any damages
* arising from the use of this software. * arising from the use of this software.
* *
* Permission is granted to anyone to use this software for any purpose, * Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it * including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions: * freely, subject to the following restrictions:
* *
* 1. The origin of this software must not be misrepresented; you must not * 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 * claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be * in a product, an acknowledgment in the product documentation would be
@@ -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 }
}; };
@@ -75,10 +82,10 @@ namespace sdl
{ {
if(instance == 0) if(instance == 0)
{ {
try try
{ {
instance = new Timer(); instance = new Timer();
} }
catch(Exception & e) catch(Exception & e)
{ {
return luaL_error(L, e.what()); return luaL_error(L, e.what());
+4 -3
View File
@@ -1,14 +1,14 @@
/** /**
* Copyright (c) 2006-2009 LOVE Development Team * Copyright (c) 2006-2009 LOVE Development Team
* *
* This software is provided 'as-is', without any express or implied * This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages * warranty. In no event will the authors be held liable for any damages
* arising from the use of this software. * arising from the use of this software.
* *
* Permission is granted to anyone to use this software for any purpose, * Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it * including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions: * freely, subject to the following restrictions:
* *
* 1. The origin of this software must not be misrepresented; you must not * 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 * claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be * 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_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