2022-06-21 12:48:19 -05:00
|
|
|
#include "timer.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2022-12-12 01:35:42 -06:00
|
|
|
#include <stb_ds.h>
|
2022-06-21 12:48:19 -05:00
|
|
|
|
2022-08-25 15:48:15 -05:00
|
|
|
static double time;
|
2022-08-22 08:55:54 -05:00
|
|
|
|
2022-08-25 15:48:15 -05:00
|
|
|
struct timer *timers;
|
2022-08-22 08:55:54 -05:00
|
|
|
|
|
|
|
void check_timer(struct timer *t)
|
|
|
|
{
|
|
|
|
if (!t->on)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (time >= t->fire_time) {
|
|
|
|
t->cb(t->data);
|
|
|
|
|
|
|
|
if (t->repeat) {
|
|
|
|
t->fire_time = time + t->interval;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
t->on = 0;
|
|
|
|
return;
|
|
|
|
}
|
2022-06-21 12:48:19 -05:00
|
|
|
}
|
|
|
|
|
2022-08-22 08:55:54 -05:00
|
|
|
void timer_update(double s) {
|
|
|
|
time = s;
|
|
|
|
|
|
|
|
|
2022-08-25 15:48:15 -05:00
|
|
|
for (int i = 0; i < arrlen(timers); i++)
|
|
|
|
check_timer(&timers[i]);
|
2022-08-22 08:55:54 -05:00
|
|
|
|
2022-08-25 15:48:15 -05:00
|
|
|
}
|
2022-08-22 08:55:54 -05:00
|
|
|
|
2022-08-25 15:48:15 -05:00
|
|
|
struct timer *timer_make(double interval, void (*callback)(void *param), void *param) {
|
|
|
|
struct timer new;
|
|
|
|
new.remain_time = interval;
|
|
|
|
new.interval = interval;
|
|
|
|
new.cb = callback;
|
|
|
|
new.data = param;
|
|
|
|
new.repeat = 1;
|
|
|
|
new.timerid = arrlen(timers);
|
|
|
|
|
|
|
|
timer_start(&new);
|
|
|
|
arrput(timers, new);
|
|
|
|
|
|
|
|
return &arrlast(timers);
|
2022-06-21 12:48:19 -05:00
|
|
|
}
|
|
|
|
|
2022-06-30 10:31:23 -05:00
|
|
|
void timer_pause(struct timer *t) {
|
2022-08-25 15:48:15 -05:00
|
|
|
if (!t->on) return;
|
|
|
|
|
2022-08-22 08:55:54 -05:00
|
|
|
t->on = 0;
|
2022-06-30 10:31:23 -05:00
|
|
|
|
2022-08-22 08:55:54 -05:00
|
|
|
t->remain_time = t->fire_time - time;
|
2022-06-30 10:31:23 -05:00
|
|
|
}
|
|
|
|
|
2022-08-22 08:55:54 -05:00
|
|
|
void timer_stop(struct timer *t) {
|
2022-08-25 15:48:15 -05:00
|
|
|
if (!t->on) return;
|
|
|
|
|
2022-08-22 08:55:54 -05:00
|
|
|
t->on = 0;
|
|
|
|
t->remain_time = t->interval;
|
|
|
|
}
|
2022-06-30 10:31:23 -05:00
|
|
|
|
2022-08-22 08:55:54 -05:00
|
|
|
void timer_start(struct timer *t) {
|
2022-08-25 15:48:15 -05:00
|
|
|
if (t->on) return;
|
|
|
|
|
2022-08-22 08:55:54 -05:00
|
|
|
t->on = 1;
|
|
|
|
t->fire_time = time + t->remain_time;
|
2022-06-30 10:31:23 -05:00
|
|
|
}
|
|
|
|
|
2022-06-21 12:48:19 -05:00
|
|
|
void timer_remove(struct timer *t) {
|
2022-08-25 15:48:15 -05:00
|
|
|
int i = t->timerid;
|
|
|
|
arrdelswap(timers, i);
|
|
|
|
timers[i].timerid = i;
|
2022-08-22 08:55:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void timer_settime(struct timer *t, double interval) {
|
2022-08-25 15:48:15 -05:00
|
|
|
//double elapsed = time - (t->fire_time - t->interval);
|
2022-08-22 08:55:54 -05:00
|
|
|
t->interval = interval;
|
2022-08-25 15:48:15 -05:00
|
|
|
t->remain_time = t->interval;
|
|
|
|
|
|
|
|
// TODO: timer_settime reacts to elapsed time
|
2022-08-22 08:55:54 -05:00
|
|
|
}
|
|
|
|
|
2022-08-26 09:19:17 -05:00
|
|
|
void *arrfind(void *arr, int (*valid)(void *arr, void *cmp), void *cmp)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < arrlen(arr); i++) {
|
|
|
|
if (valid(&arr[i], cmp))
|
|
|
|
return &arr[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void arrwalk(void *arr, void (*fn)(void *data))
|
|
|
|
{
|
|
|
|
for (int i = 0; i < arrlen(arr); i++)
|
|
|
|
fn(&arr[i]);
|
2022-11-23 17:29:50 -06:00
|
|
|
}
|