Added timers to the game

This commit is contained in:
2025-01-06 09:21:35 -07:00
parent 52097b3861
commit 41a208846a
4 changed files with 100 additions and 15 deletions
+56
View File
@@ -0,0 +1,56 @@
const sdl = @import("./../sdl.zig").c;
pub const TimerStatus = enum { started, stopped, paused };
pub const Timer = struct {
start_ticks_ms: u32 = 0,
pause_ticks_ms: u32 = 0,
status: TimerStatus = TimerStatus.stopped,
pub fn start(self: *Timer) void {
if (self.status == .started) return;
self.status = .started;
self.start_ticks_ms = sdl.SDL_GetTicks();
self.pause_ticks_ms = 0;
}
pub fn stop(self: *Timer) void {
if (self.status == .stopped) return;
self.status = .stopped;
self.start_ticks_ms = 0;
self.pause_ticks_ms = 0;
}
pub fn reset(self: *Timer) void {
self.status = .started;
self.start_ticks_ms = sdl.SDL_GetTicks();
self.pause_ticks_ms = 0;
}
pub fn pause(self: *Timer) void {
if (self.status == .paused or self.status == .stopped) return;
self.status = .paused;
self.pause_ticks_ms = sdl.SDL_GetTicks();
}
pub fn unpause(self: *Timer) void {
if (self.status == .started or self.status == .stopped) return;
self.status = .started;
self.start_ticks_ms = sdl.SDL_GetTicks() - self.pause_ticks_ms;
self.pause_ticks_ms = 0;
}
pub fn getTicks(self: *Timer) u32 {
switch (self.status) {
.stopped => {
return 0;
},
.paused => {
return self.pause_ticks_ms;
},
.started => {
return sdl.SDL_GetTicks() - self.start_ticks_ms;
},
}
}
};