43 lines
857 B
C
43 lines
857 B
C
#include "timer.h"
|
|
#include <stdio.h>
|
|
#include "stb_ds.h"
|
|
|
|
timer **timers;
|
|
|
|
timer *timer_make(JSContext *js, JSValue fn)
|
|
{
|
|
timer *t = calloc(sizeof(*t),1);
|
|
t->fn = JS_DupValue(js,fn);
|
|
arrput(timers, t);
|
|
return t;
|
|
}
|
|
|
|
void timer_free(JSRuntime *rt, timer *t)
|
|
{
|
|
for (int i = 0; i < arrlen(timers); i++) {
|
|
if (timers[i] == t) {
|
|
arrdelswap(timers,i);
|
|
break;
|
|
}
|
|
}
|
|
|
|
JS_FreeValueRT(rt, t->fn);
|
|
|
|
free(t);
|
|
}
|
|
|
|
void timer_update(JSContext *js, double dt)
|
|
{
|
|
for (int i = 0; i < arrlen(timers); i++) {
|
|
if (timers[i]->remain <= -10000) continue;
|
|
timers[i]->remain -= dt;
|
|
if (timers[i]->remain <= 0) {
|
|
timers[i]->remain = -10000;
|
|
JSValue fn = JS_DupValue(js, timers[i]->fn);
|
|
JSValue r = JS_Call(js,fn, JS_UNDEFINED, 0, NULL);
|
|
uncaught_exception(js,r);
|
|
JS_FreeValue(js, fn);
|
|
}
|
|
}
|
|
}
|