From 46cab832ed1ee0da095a41885519472b32cfec7f Mon Sep 17 00:00:00 2001 From: Strangedusk Date: Sun, 10 Jul 2022 18:25:38 +0200 Subject: [PATCH] Add Timers --- src/timer.zig | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/timer.zig diff --git a/src/timer.zig b/src/timer.zig new file mode 100644 index 0000000..85b4fa2 --- /dev/null +++ b/src/timer.zig @@ -0,0 +1,38 @@ +const SDL = @import("sdl2"); + +const Self = @This(); + +started: bool, +initial: u64, +progress: u64, +target: u64, + +pub fn init(target: u64) Self { + return Self{ + .started = false, + .initial = 0, + .progress = 0, + .target = target, + }; +} + +pub fn start(self: *Self) void { + self.started = true; + self.progress = 0; + self.initial = SDL.SDL_GetTicks64(); +} + +pub fn finished(self: *Self) bool { + if (self.started) { + self.progress = SDL.SDL_GetTicks64() - self.initial; + if (self.progress >= self.target) { + return true; + } else return false; + } else { + return false; + } +} + +pub fn stop(self: *Self) void { + self.started = false; +}