Ragnarokkr/timer.odin

64 lines
940 B
Odin

package main
import "core:math"
import "core:math/linalg"
import "core:slice"
Timer :: struct {
duration: f32,
time: f32,
active: bool,
finished: proc(data: rawptr),
data: rawptr
}
TIMER_SIZE :: 128
timers : [dynamic]^Timer
timers_clean :: proc() {
for timer, i in timers {
free(timer)
}
}
timer_start :: proc(
time: f32,
data: rawptr = nil,
callback: proc(data: rawptr) = nil
) -> ^Timer {
timer := new(Timer)
timer.duration = time
timer.active = true
timer.data = data
timer.finished = callback
append(&timers, timer)
return timer
}
timer_cancel :: proc(t: ^Timer) {
t.active = false
}
timers_process :: proc(delta: f32) {
#reverse for timer, i in timers {
if !timer.active {
free(timer)
unordered_remove(&timers, i)
continue
}
timer.time += delta
if timer.time >= timer.duration {
timer.active = false
if timer.finished != nil {
timer.finished(timer.data)
}
}
}
}