Browse Source

Add a stopwatch to time.odin

Phil 3 years ago
parent
commit
a8425cfb47
1 changed files with 30 additions and 1 deletions
  1. 30 1
      core/time/time.odin

+ 30 - 1
core/time/time.odin

@@ -43,6 +43,36 @@ Weekday :: enum int {
 	Saturday,
 	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 {
 diff :: proc(start, end: Time) -> Duration {
 	d := end._nsec - start._nsec
 	d := end._nsec - start._nsec
 	return Duration(d)
 	return Duration(d)
@@ -52,7 +82,6 @@ since :: proc(start: Time) -> Duration {
 	return diff(start, now())
 	return diff(start, now())
 }
 }
 
 
-
 duration_nanoseconds :: proc(d: Duration) -> i64 {
 duration_nanoseconds :: proc(d: Duration) -> i64 {
 	return i64(d)
 	return i64(d)
 }
 }