diff options
| author | 3gg <3gg@shellblade.net> | 2021-12-04 18:31:18 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2021-12-04 18:31:18 -0800 |
| commit | cc96d69ed11c60a782cd8b993d4bdf2ce8c99560 (patch) | |
| tree | 96ebf632b79f609ef341f747ec9cc27373c374e1 /timer/include | |
| parent | f8217d240d598f39f70047f7a623dd46312542c6 (diff) | |
Add timer library.
Diffstat (limited to 'timer/include')
| -rw-r--r-- | timer/include/timer.h | 55 |
1 files changed, 55 insertions, 0 deletions
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 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <stdint.h> | ||
| 4 | |||
| 5 | /// A particular point in time. | ||
| 6 | #ifdef _WIN32 | ||
| 7 | typedef uint64_t time_point; | ||
| 8 | #else | ||
| 9 | #include <time.h> | ||
| 10 | typedef struct timespec time_point; | ||
| 11 | #endif | ||
| 12 | |||
| 13 | /// Time elapsed between two time points. | ||
| 14 | typedef uint64_t time_delta; | ||
| 15 | |||
| 16 | /// A high resolution timer. | ||
| 17 | typedef struct { | ||
| 18 | time_point start_time; // The instant the timer was last started. | ||
| 19 | time_point last_tick; // The instant the timer was last ticked. | ||
| 20 | time_delta running_time; // Time elapsed since the timer was last started. | ||
| 21 | time_delta delta_time; // Time elapsed since the last tick. | ||
| 22 | } Timer; | ||
| 23 | |||
| 24 | /// Construct a new timer. | ||
| 25 | Timer timer_make(void); | ||
| 26 | |||
| 27 | /// Start the timer. | ||
| 28 | /// This sets the time point from which time deltas are measured. | ||
| 29 | /// Calling this multilple times resets the timer. | ||
| 30 | void timer_start(Timer*); | ||
| 31 | |||
| 32 | /// Update the timer's running and delta times. | ||
| 33 | void timer_tick(Timer*); | ||
| 34 | |||
| 35 | /// Get the current time. | ||
| 36 | time_point time_now(void); | ||
| 37 | |||
| 38 | /// Return the time elapsed between two timestamps. | ||
| 39 | time_delta time_diff(time_point start, time_point end); | ||
| 40 | |||
| 41 | /// Return the time elapsed in seconds. | ||
| 42 | double time_delta_to_sec(time_delta dt); | ||
| 43 | |||
| 44 | /// Convert the time elapsed in seconds to a time delta. | ||
| 45 | time_delta sec_to_time_delta(double seconds); | ||
| 46 | |||
| 47 | /// Put the caller thread to sleep for the given amount of time. | ||
| 48 | void time_sleep(time_delta dt); | ||
| 49 | |||
| 50 | /// The time point 0. | ||
| 51 | #ifdef _WIN32 | ||
| 52 | static const time_point time_zero = 0; | ||
| 53 | #else | ||
| 54 | static const time_point time_zero = {0, 0}; | ||
| 55 | #endif | ||
