diff options
| -rw-r--r-- | timer/include/timer.h | 17 | ||||
| -rw-r--r-- | timer/src/timer.c | 15 |
2 files changed, 25 insertions, 7 deletions
diff --git a/timer/include/timer.h b/timer/include/timer.h index 6dc87d9..75ae367 100644 --- a/timer/include/timer.h +++ b/timer/include/timer.h | |||
| @@ -21,10 +21,10 @@ typedef uint64_t time_delta; | |||
| 21 | 21 | ||
| 22 | /// A high resolution timer. | 22 | /// A high resolution timer. |
| 23 | typedef struct Timer { | 23 | typedef struct Timer { |
| 24 | time_point start_time; // The instant the timer was last started. | 24 | time_point start_time; ///< The instant the timer was last started. |
| 25 | time_point last_tick; // The instant the timer was last ticked. | 25 | time_point last_tick; ///< The instant the timer was last ticked. |
| 26 | time_delta running_time; // Time elapsed since the timer was last started. | 26 | time_delta running_time; ///< Time elapsed since the timer was last started. |
| 27 | time_delta delta_time; // Time elapsed since the last tick. | 27 | time_delta delta_time; ///< Time elapsed since the last tick. |
| 28 | } Timer; | 28 | } Timer; |
| 29 | 29 | ||
| 30 | /// Construct a new timer. | 30 | /// Construct a new timer. |
| @@ -38,6 +38,15 @@ void timer_start(Timer*); | |||
| 38 | /// Update the timer's running and delta times. | 38 | /// Update the timer's running and delta times. |
| 39 | void timer_tick(Timer*); | 39 | void timer_tick(Timer*); |
| 40 | 40 | ||
| 41 | /// Advance the timer. | ||
| 42 | /// | ||
| 43 | /// This is used for simulations that wish control over time. It should not be | ||
| 44 | /// used together with timer_tick(). | ||
| 45 | /// | ||
| 46 | /// The input time is relative to the timer's start time. Successive calls to | ||
| 47 | /// this function must use increasing values of time. | ||
| 48 | void timer_advance(Timer* timer, time_delta t); | ||
| 49 | |||
| 41 | /// Get the current time. | 50 | /// Get the current time. |
| 42 | time_point time_now(void); | 51 | time_point time_now(void); |
| 43 | 52 | ||
diff --git a/timer/src/timer.c b/timer/src/timer.c index d886f59..c934077 100644 --- a/timer/src/timer.c +++ b/timer/src/timer.c | |||
| @@ -37,11 +37,20 @@ void timer_start(Timer* timer) { | |||
| 37 | timer->delta_time = 0; | 37 | timer->delta_time = 0; |
| 38 | } | 38 | } |
| 39 | 39 | ||
| 40 | static void timer_update(Timer* timer, time_point this_tick) { | ||
| 41 | timer->running_time = time_diff(timer->start_time, this_tick); | ||
| 42 | timer->delta_time = time_diff(timer->last_tick, this_tick); | ||
| 43 | timer->last_tick = this_tick; | ||
| 44 | } | ||
| 45 | |||
| 40 | void timer_tick(Timer* timer) { | 46 | void timer_tick(Timer* timer) { |
| 41 | const time_point this_tick = time_now(); | 47 | const time_point this_tick = time_now(); |
| 42 | timer->running_time = time_diff(timer->start_time, this_tick); | 48 | timer_update(timer, this_tick); |
| 43 | timer->delta_time = time_diff(timer->last_tick, this_tick); | 49 | } |
| 44 | timer->last_tick = this_tick; | 50 | |
| 51 | void timer_advance(Timer* timer, time_delta t) { | ||
| 52 | const time_point this_tick = time_add(timer->start_time, t); | ||
| 53 | timer_update(timer, this_tick); | ||
| 45 | } | 54 | } |
| 46 | 55 | ||
| 47 | time_point time_now(void) { | 56 | time_point time_now(void) { |
