prosperon/source/engine/timer.c

42 lines
758 B
C
Raw Normal View History

2024-08-04 15:20:11 -05:00
#include "timer.h"
2024-08-05 15:26:18 -05:00
#include <stdio.h>
2024-08-04 15:20:11 -05:00
#include "stb_ds.h"
2024-08-05 15:26:18 -05:00
timer **timers;
2024-08-04 15:20:11 -05:00
2024-09-30 04:36:53 -05:00
timer *timer_make(JSValue fn)
2024-08-04 15:20:11 -05:00
{
2024-08-05 15:26:18 -05:00
timer *t = calloc(sizeof(*t),1);
2024-09-30 04:36:53 -05:00
t->fn = JS_DupValue(js,fn);
2024-08-05 15:26:18 -05:00
arrput(timers, t);
return t;
2024-08-04 15:20:11 -05:00
}
void timer_free(timer *t)
{
2024-08-05 15:26:18 -05:00
for (int i = 0; i < arrlen(timers); i++) {
if (timers[i] == t) {
arrdelswap(timers,i);
break;
}
}
2024-09-30 04:36:53 -05:00
JS_FreeValue(js, t->fn);
2024-08-05 15:26:18 -05:00
free(t);
2024-08-04 15:20:11 -05:00
}
void timer_update(double dt)
{
for (int i = 0; i < arrlen(timers); i++) {
2024-09-30 04:36:53 -05:00
if (timers[i]->remain <= 0) continue;
2024-08-05 15:26:18 -05:00
timers[i]->remain -= dt;
2024-09-30 04:36:53 -05:00
if (timers[i]->remain <= 0) {
timers[i]->remain = 0;
JSValue fn = JS_DupValue(js, timers[i]->fn);
script_call_sym(timers[i]->fn, 0, NULL);
JS_FreeValue(js, fn);
}
2024-08-04 15:20:11 -05:00
}
2024-08-05 15:26:18 -05:00
}