From 670e910db50c56a7067d457d52a5af6dcef97c48 Mon Sep 17 00:00:00 2001 From: Elias Aebi Date: Sun, 19 Nov 2023 16:27:43 +0100 Subject: [PATCH] gral_time_get_monotonic --- LICENSE | 2 +- demos/events.c | 2 +- gral.h | 7 +++++++ gral_linux.c | 11 +++++++++++ gral_macos.m | 11 +++++++++++ gral_windows.cpp | 18 ++++++++++++++++++ 6 files changed, 49 insertions(+), 2 deletions(-) diff --git a/LICENSE b/LICENSE index c4a625a..c7345bd 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2016-2022 Elias Aebi +Copyright (c) 2016-2023 Elias Aebi Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/demos/events.c b/demos/events.c index 3156c98..6c8ae20 100644 --- a/demos/events.c +++ b/demos/events.c @@ -102,7 +102,7 @@ static void focus_leave(void *user_data) { } static int timer(void *user_data) { - printf("timer\n"); + printf("timer %f\n", gral_time_get_monotonic()); return 1; } diff --git a/gral.h b/gral.h index 3d9d4fc..ae80632 100644 --- a/gral.h +++ b/gral.h @@ -180,6 +180,13 @@ void gral_file_rename(char const *old_path, char const *new_path); void gral_directory_iterate(char const *path, void (*callback)(char const *name, void *user_data), void *user_data); +/*========= + TIME + =========*/ + +double gral_time_get_monotonic(void); + + /*========== AUDIO ==========*/ diff --git a/gral_linux.c b/gral_linux.c index f97b7e4..7e6288f 100644 --- a/gral_linux.c +++ b/gral_linux.c @@ -757,6 +757,17 @@ void gral_directory_iterate(char const *path, void (*callback)(char const *name, } +/*========= + TIME + =========*/ + +double gral_time_get_monotonic(void) { + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return ts.tv_sec + ts.tv_nsec / 1e9; +} + + /*========== AUDIO ==========*/ diff --git a/gral_macos.m b/gral_macos.m index 6a7260a..bab1b9b 100644 --- a/gral_macos.m +++ b/gral_macos.m @@ -792,6 +792,17 @@ void gral_directory_iterate(char const *path, void (*callback)(char const *name, } +/*========= + TIME + =========*/ + +double gral_time_get_monotonic(void) { + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return ts.tv_sec + ts.tv_nsec / 1e9; +} + + /*========== AUDIO ==========*/ diff --git a/gral_windows.cpp b/gral_windows.cpp index e37b53e..79711dc 100644 --- a/gral_windows.cpp +++ b/gral_windows.cpp @@ -1160,6 +1160,24 @@ void gral_directory_iterate(char const *path_utf8, void (*callback)(char const * } +/*========= + TIME + =========*/ + +static double get_frequency() { + LARGE_INTEGER frequency; + QueryPerformanceFrequency(&frequency); + return frequency.QuadPart; +} + +double gral_time_get_monotonic() { + static const double frequency = get_frequency(); + LARGE_INTEGER count; + QueryPerformanceCounter(&count); + return count.QuadPart / frequency; +} + + /*========== AUDIO ==========*/