From cc96d69ed11c60a782cd8b993d4bdf2ce8c99560 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 4 Dec 2021 18:31:18 -0800 Subject: Add timer library. --- timer/include/timer.h | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 timer/include/timer.h (limited to 'timer/include/timer.h') diff --git a/timer/include/timer.h b/timer/include/timer.h new file mode 100644 index 0000000..0274c69 --- /dev/null +++ b/timer/include/timer.h @@ -0,0 +1,55 @@ +#pragma once + +#include + +/// A particular point in time. +#ifdef _WIN32 +typedef uint64_t time_point; +#else +#include +typedef struct timespec time_point; +#endif + +/// Time elapsed between two time points. +typedef uint64_t time_delta; + +/// A high resolution timer. +typedef struct { + time_point start_time; // The instant the timer was last started. + time_point last_tick; // The instant the timer was last ticked. + time_delta running_time; // Time elapsed since the timer was last started. + time_delta delta_time; // Time elapsed since the last tick. +} Timer; + +/// Construct a new timer. +Timer timer_make(void); + +/// Start the timer. +/// This sets the time point from which time deltas are measured. +/// Calling this multilple times resets the timer. +void timer_start(Timer*); + +/// Update the timer's running and delta times. +void timer_tick(Timer*); + +/// Get the current time. +time_point time_now(void); + +/// Return the time elapsed between two timestamps. +time_delta time_diff(time_point start, time_point end); + +/// Return the time elapsed in seconds. +double time_delta_to_sec(time_delta dt); + +/// Convert the time elapsed in seconds to a time delta. +time_delta sec_to_time_delta(double seconds); + +/// Put the caller thread to sleep for the given amount of time. +void time_sleep(time_delta dt); + +/// The time point 0. +#ifdef _WIN32 +static const time_point time_zero = 0; +#else +static const time_point time_zero = {0, 0}; +#endif -- cgit v1.2.3