aboutsummaryrefslogtreecommitdiff
path: root/src/system
diff options
context:
space:
mode:
Diffstat (limited to 'src/system')
-rw-r--r--src/system/clock.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/system/clock.c b/src/system/clock.c
new file mode 100644
index 0000000..3ec2276
--- /dev/null
+++ b/src/system/clock.c
@@ -0,0 +1,27 @@
+#include "../../include/mgl/system/clock.h"
+#include <time.h>
+
+/* TODO: Implement for macOS */
+
+static double clock_get_monotonic_seconds() {
+ struct timespec ts;
+ ts.tv_sec = 0;
+ ts.tv_nsec = 0;
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ return (double)ts.tv_sec + (double)ts.tv_nsec * 0.000000001;
+}
+
+void mgl_clock_init(mgl_clock *self) {
+ self->captured_seconds = clock_get_monotonic_seconds();
+}
+
+double mgl_clock_restart(mgl_clock *self) {
+ const double new_time_seconds = clock_get_monotonic_seconds();
+ const double elapsed_time_seconds = self->captured_seconds - new_time_seconds;
+ self->captured_seconds = new_time_seconds;
+ return elapsed_time_seconds;
+}
+
+double mgl_clock_get_elapsed_time_seconds(mgl_clock *self) {
+ return clock_get_monotonic_seconds() - self->captured_seconds;
+}