Add a stopwatch to time.odin

This commit is contained in:
Phil
2021-09-30 09:49:00 -07:00
committed by Phil H
parent ef29ffeb21
commit a8425cfb47

View File

@@ -43,6 +43,36 @@ Weekday :: enum int {
Saturday,
}
Stopwatch :: struct {
running: bool,
_start_time: Tick,
_stop_time: Duration,
}
start :: proc(using stopwatch: ^Stopwatch) {
if !running {
_start_time = tick_now()
running = true
}
}
stop :: proc(using stopwatch: ^Stopwatch) {
if running {
_stop_time += tick_diff(_start_time, tick_now())
running = false
}
}
reset :: proc(using stopwatch: ^Stopwatch) {
_stop_time = {}
running = false
}
duration :: proc(using stopwatch: Stopwatch) -> Duration {
if !running { return _stop_time }
return _stop_time + tick_diff(_start_time, tick_now())
}
diff :: proc(start, end: Time) -> Duration {
d := end._nsec - start._nsec
return Duration(d)
@@ -52,7 +82,6 @@ since :: proc(start: Time) -> Duration {
return diff(start, now())
}
duration_nanoseconds :: proc(d: Duration) -> i64 {
return i64(d)
}