Expose GetTime() function to users

Monotonic time since InitWindow() could be retrieved with this function.
This commit is contained in:
Ray San 2017-12-19 14:06:54 +01:00
parent a7de5bf6d9
commit 5290390494
3 changed files with 33 additions and 32 deletions

View File

@ -150,11 +150,11 @@
#include <GLFW/glfw3native.h> // which are required for hiding mouse
#endif
//#include <GL/gl.h> // OpenGL functions (GLFW3 already includes gl.h)
//#define GLFW_DLL // Using GLFW DLL on Windows -> No, we use static version!
#if !defined(SUPPORT_BUSY_WAIT_LOOP) && defined(_WIN32)
__stdcall unsigned int timeBeginPeriod(unsigned int uPeriod);
__stdcall unsigned int timeEndPeriod(unsigned int uPeriod);
// NOTE: Those functions require linking with winmm library
unsigned int __stdcall timeBeginPeriod(unsigned int uPeriod);
unsigned int __stdcall timeEndPeriod(unsigned int uPeriod);
#endif
#endif
@ -351,7 +351,6 @@ extern void UnloadDefaultFont(void); // [Module: text] Unloads default fo
static void InitGraphicsDevice(int width, int height); // Initialize graphics device
static void SetupFramebufferSize(int displayWidth, int displayHeight);
static void InitTimer(void); // Initialize timer
static double GetTime(void); // Returns time since InitTimer() was run
static void Wait(float ms); // Wait for some milliseconds (stop program execution)
static bool GetKeyStatus(int key); // Returns if a key has been pressed
static bool GetMouseButtonStatus(int button); // Returns if a mouse button has been pressed
@ -421,6 +420,9 @@ void InitWindow(int width, int height, void *data)
// Init graphics device (display device and OpenGL context)
InitGraphicsDevice(width, height);
// Init hi-res timer
InitTimer();
#if defined(SUPPORT_DEFAULT_FONT)
// Load default font
@ -428,9 +430,6 @@ void InitWindow(int width, int height, void *data)
LoadDefaultFont();
#endif
// Init hi-res timer
InitTimer();
#if defined(PLATFORM_RPI)
// Init raw input system
InitMouse(); // Mouse init
@ -786,7 +785,7 @@ void ClearBackground(Color color)
// Setup canvas (framebuffer) to start drawing
void BeginDrawing(void)
{
currentTime = GetTime(); // Number of elapsed seconds since InitTimer() was called
currentTime = GetTime(); // Number of elapsed seconds since InitTimer()
updateTime = currentTime - previousTime;
previousTime = currentTime;
@ -1060,6 +1059,24 @@ float GetFrameTime(void)
return (float)frameTime;
}
// Get elapsed time measure in seconds since InitTimer()
// NOTE: On PLATFORM_DESKTOP InitTimer() is called on InitWindow()
// NOTE: On PLATFORM_DESKTOP, timer is initialized on glfwInit()
double GetTime(void)
{
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
return glfwGetTime(); // Elapsed time since glfwInit()
#endif
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI)
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
uint64_t time = (uint64_t)ts.tv_sec*1000000000LLU + (uint64_t)ts.tv_nsec;
return (double)(time - baseTime)*1e-9; // Elapsed time since InitTimer()
#endif
}
// Converts Color to float array and normalizes
float *ColorToFloat(Color color)
{
@ -2118,22 +2135,6 @@ static void InitTimer(void)
previousTime = GetTime(); // Get time as double
}
// Get elapsed time measure (in seconds)
static double GetTime(void)
{
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
return glfwGetTime(); // Elapsed time since glfwInit()
#endif
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI)
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
uint64_t time = (uint64_t)ts.tv_sec*1000000000LLU + (uint64_t)ts.tv_nsec;
return (double)(time - baseTime)*1e-9; // Elapsed time since InitTimer()
#endif
}
// Wait for some milliseconds (stop program execution)
// NOTE: Sleep() granularity could be around 10 ms, it means, Sleep() could
// take longer than expected... for that reason we use the busy wait loop
@ -2602,6 +2603,9 @@ static void AndroidCommandCallback(struct android_app *app, int32_t cmd)
{
// Init graphics device (display device and OpenGL context)
InitGraphicsDevice(screenWidth, screenHeight);
// Init hi-res timer
InitTimer();
#if defined(SUPPORT_DEFAULT_FONT)
// Load default font
@ -2624,9 +2628,6 @@ static void AndroidCommandCallback(struct android_app *app, int32_t cmd)
}
*/
// Init hi-res timer
InitTimer();
// raylib logo appearing animation (if enabled)
if (showLogo)
{

View File

@ -522,7 +522,7 @@ static double GetCurrentTime(void)
#if defined(_WIN32)
unsigned long long int clockFrequency, currentTime;
QueryPerformanceFrequency(&clockFrequency);
QueryPerformanceFrequency(&clockFrequency); // BE CAREFUL: Costly operation!
QueryPerformanceCounter(&currentTime);
time = (double)currentTime/clockFrequency*1000.0f; // Time in miliseconds
@ -538,8 +538,8 @@ static double GetCurrentTime(void)
#endif
#if defined(__APPLE__)
//#define CLOCK_REALTIME CALENDAR_CLOCK
//#define CLOCK_MONOTONIC SYSTEM_CLOCK
//#define CLOCK_REALTIME CALENDAR_CLOCK // returns UTC time since 1970-01-01
//#define CLOCK_MONOTONIC SYSTEM_CLOCK // returns the time since boot time
clock_serv_t cclock;
mach_timespec_t now;

View File

@ -722,7 +722,7 @@ RLAPI Matrix GetCameraMatrix(Camera camera); // Returns cam
RLAPI void SetTargetFPS(int fps); // Set target FPS (maximum)
RLAPI int GetFPS(void); // Returns current FPS
RLAPI float GetFrameTime(void); // Returns time in seconds for last frame drawn
//RLAPI double GetCurrentTime(void); // Return current time in seconds
RLAPI double GetTime(void); // Returns elapsed time in seconds since InitWindow()
// Color-related functions
RLAPI int GetHexValue(Color color); // Returns hexadecimal value for a Color