From f4bbb6ef6a13473aeef05c1e0ec3502ba845cd2b Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Thu, 7 Mar 2024 23:18:59 -0500 Subject: [PATCH] Added a rudimentary timer implementation --- includes/timer.h | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 includes/timer.h diff --git a/includes/timer.h b/includes/timer.h new file mode 100644 index 0000000..2faf70b --- /dev/null +++ b/includes/timer.h @@ -0,0 +1,35 @@ +#ifdef __cplusplus +extern "C" { +#endif + +/* This file defines a simple timer struct, and methods to initialize it, +and keep track of time elapsed. +It was copied from https://github.com/raysan5/raylib/wiki/Frequently-Asked-Questions#how-do-i-make-a-timer */ + +typedef struct Timer { + double start_time; // Start time (seconds) + double lifetime; // Lifetime (seconds) +} Timer; + +Timer timer_init(double lifetime_secs) +{ + Timer timer; + timer.start_time = GetTime(); + timer.lifetime = lifetime_secs; + + return timer; +} + +bool timer_done(Timer timer) +{ + return GetTime() - timer.start_time >= timer.lifetime; +} + +double timer_get_elapsed(Timer timer) +{ + return GetTime() - timer.start_time; +} + +#ifdef __cplusplus +} +#endif