2022-06-21 12:48:19 -05:00
|
|
|
#include "timer.h"
|
|
|
|
#include <stdlib.h>
|
2023-01-18 17:15:36 -06:00
|
|
|
#include "log.h"
|
2022-06-21 12:48:19 -05:00
|
|
|
|
2022-12-12 10:48:21 -06:00
|
|
|
#include <stb_ds.h>
|
2022-06-21 12:48:19 -05:00
|
|
|
|
2022-08-25 15:48:15 -05:00
|
|
|
struct timer *timers;
|
2023-03-05 22:05:22 -06:00
|
|
|
static int first = -1;
|
2022-08-22 08:55:54 -05:00
|
|
|
|
2022-12-28 16:50:54 -06:00
|
|
|
void check_timer(struct timer *t, double dt)
|
2022-08-22 08:55:54 -05:00
|
|
|
{
|
|
|
|
if (!t->on)
|
|
|
|
return;
|
|
|
|
|
2022-12-28 16:50:54 -06:00
|
|
|
t->remain_time -= dt;
|
|
|
|
|
|
|
|
if (t->remain_time <= 0) {
|
2022-08-22 08:55:54 -05:00
|
|
|
t->cb(t->data);
|
|
|
|
if (t->repeat) {
|
2022-12-28 16:50:54 -06:00
|
|
|
t->remain_time = t->interval;
|
2022-08-22 08:55:54 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-28 12:49:18 -05:00
|
|
|
timer_pause(t);
|
2022-08-22 08:55:54 -05:00
|
|
|
return;
|
|
|
|
}
|
2022-06-21 12:48:19 -05:00
|
|
|
}
|
|
|
|
|
2022-12-28 16:50:54 -06:00
|
|
|
void timer_update(double dt) {
|
2022-08-25 15:48:15 -05:00
|
|
|
for (int i = 0; i < arrlen(timers); i++)
|
2022-12-28 16:50:54 -06:00
|
|
|
check_timer(&timers[i], dt);
|
2022-08-25 15:48:15 -05:00
|
|
|
}
|
2022-08-22 08:55:54 -05:00
|
|
|
|
2023-03-17 10:25:35 -05:00
|
|
|
int timer_make(double interval, void (*callback)(void *param), void *param, int own) {
|
2022-08-25 15:48:15 -05:00
|
|
|
struct timer new;
|
|
|
|
new.remain_time = interval;
|
|
|
|
new.interval = interval;
|
|
|
|
new.cb = callback;
|
|
|
|
new.data = param;
|
|
|
|
new.repeat = 1;
|
2023-01-19 10:44:29 -06:00
|
|
|
new.owndata = own;
|
2023-03-17 10:25:35 -05:00
|
|
|
new.next = -1;
|
2023-03-05 22:05:22 -06:00
|
|
|
|
2023-03-17 10:25:35 -05:00
|
|
|
if (first < 0) {
|
2023-03-05 22:05:22 -06:00
|
|
|
timer_start(&new);
|
|
|
|
arrput(timers, new);
|
2023-03-17 10:25:35 -05:00
|
|
|
return arrlen(timers)-1;
|
2023-03-05 22:05:22 -06:00
|
|
|
} else {
|
|
|
|
int retid = first;
|
|
|
|
first = id2timer(first)->next;
|
|
|
|
*id2timer(retid) = new;
|
|
|
|
timer_start(id2timer(retid));
|
2023-03-17 10:25:35 -05:00
|
|
|
return retid;
|
2023-03-05 22:05:22 -06:00
|
|
|
}
|
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
|
|
|
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;
|
2022-06-30 10:31:23 -05:00
|
|
|
}
|
|
|
|
|
2023-03-17 10:25:35 -05:00
|
|
|
void timer_remove(int id) {
|
|
|
|
struct timer *t = id2timer(id);
|
2023-01-19 10:44:29 -06:00
|
|
|
if (t->owndata) free(t->data);
|
2023-03-17 10:25:35 -05:00
|
|
|
t->next = first;
|
|
|
|
first = id;
|
2022-08-22 08:55:54 -05:00
|
|
|
}
|
|
|
|
|
2022-12-16 11:54:05 -06:00
|
|
|
void timerr_settime(struct timer *t, double interval) {
|
2022-12-28 16:50:54 -06:00
|
|
|
t->remain_time += (interval - t->interval);
|
2022-08-22 08:55:54 -05:00
|
|
|
t->interval = interval;
|
2023-01-18 17:15:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
struct timer *id2timer(int id)
|
|
|
|
{
|
|
|
|
return &timers[id];
|
2023-03-05 22:05:22 -06:00
|
|
|
}
|