aboutsummaryrefslogtreecommitdiff
path: root/timer/src
diff options
context:
space:
mode:
Diffstat (limited to 'timer/src')
-rw-r--r--timer/src/timer.c24
1 files changed, 16 insertions, 8 deletions
diff --git a/timer/src/timer.c b/timer/src/timer.c
index 2ee8ef5..0c33e51 100644
--- a/timer/src/timer.c
+++ b/timer/src/timer.c
@@ -32,17 +32,17 @@ Timer timer_make(void) {
32} 32}
33 33
34void timer_start(Timer* timer) { 34void timer_start(Timer* timer) {
35 timer->start_time = time_now(); 35 timer->start_time = time_now();
36 timer->last_tick = timer->start_time; 36 timer->last_tick = timer->start_time;
37 timer->running_time = 0; 37 timer->running_time = 0;
38 timer->delta_time = 0; 38 timer->delta_time = 0;
39} 39}
40 40
41void timer_tick(Timer* timer) { 41void timer_tick(Timer* timer) {
42 const time_point this_tick = time_now(); 42 const time_point this_tick = time_now();
43 timer->running_time = time_diff(timer->start_time, this_tick); 43 timer->running_time = time_diff(timer->start_time, this_tick);
44 timer->delta_time = time_diff(timer->last_tick, this_tick); 44 timer->delta_time = time_diff(timer->last_tick, this_tick);
45 timer->last_tick = this_tick; 45 timer->last_tick = this_tick;
46} 46}
47 47
48time_point time_now(void) { 48time_point time_now(void) {
@@ -82,14 +82,22 @@ time_delta sec_to_time_delta(double seconds) {
82#endif 82#endif
83} 83}
84 84
85uint64_t time_point_to_ns(time_point t) {
86#ifdef _WIN32
87 return (uint64_t)((double)t * seconds_per_count * 1.0e+9);
88#else
89 return (uint64_t)t.tv_sec * 1e+9 + (uint64_t)t.tv_nsec;
90#endif
91}
92
85void time_sleep(time_delta dt) { 93void time_sleep(time_delta dt) {
86#ifdef _WIN32 94#ifdef _WIN32
87 const int64_t ms = dt / microseconds; 95 const int64_t ms = dt / microseconds;
88 Sleep((DWORD)(ms)); 96 Sleep((DWORD)(ms));
89#else 97#else
90 const int64_t sec = dt / nanoseconds; 98 const int64_t sec = dt / nanoseconds;
91 struct timespec ts; 99 struct timespec ts;
92 ts.tv_sec = (long)sec; 100 ts.tv_sec = (long)sec;
93 ts.tv_nsec = (long)(dt % nanoseconds); 101 ts.tv_nsec = (long)(dt % nanoseconds);
94 nanosleep(&ts, NULL); 102 nanosleep(&ts, NULL);
95#endif 103#endif