C timers
This commit is contained in:
parent
6b94349a64
commit
a476562a9e
|
@ -134,10 +134,11 @@ actor.kill.doc = `Remove this actor and all its padawans from existence.`;
|
|||
|
||||
actor.delay = function (fn, seconds) {
|
||||
var timers = this.timers;
|
||||
|
||||
var stop = function () {
|
||||
timers.remove(stop);
|
||||
stop.timer = undefined;
|
||||
stop = undefined;
|
||||
rm();
|
||||
};
|
||||
|
||||
function execute() {
|
||||
|
@ -151,15 +152,9 @@ actor.delay = function (fn, seconds) {
|
|||
stop.pct = function () {
|
||||
return 1 - stop.remain / stop.seconds;
|
||||
};
|
||||
|
||||
function update(dt) {
|
||||
profile.frame("timer");
|
||||
stop.remain -= dt;
|
||||
if (stop.remain <= 0) execute();
|
||||
profile.endframe();
|
||||
}
|
||||
|
||||
var rm = Register.appupdate.register(update);
|
||||
|
||||
stop.timer = os.make_timer(execute);
|
||||
stop.timer.remain = seconds;
|
||||
|
||||
timers.push(stop);
|
||||
return stop;
|
||||
|
|
|
@ -1,28 +1,3 @@
|
|||
/* It is EMCA6 but without a lot of builtin objects and various functions. There are no:
|
||||
* Promises and so on (Generators, async)
|
||||
* WeakMaps and so on (weakset, weakref)
|
||||
* Typed arrays
|
||||
* Proxys
|
||||
* Modules
|
||||
* Symbols (use closures)
|
||||
In addition to the removal of a bunch of stuff as seen here.
|
||||
Access prototypes through __proto__ instead of the long-winded Object.getProtoTypeOf.
|
||||
*/
|
||||
|
||||
/*
|
||||
Object.getPrototypeOf = undefined;
|
||||
Object.setPrototypeOf = undefined;
|
||||
Reflect = undefined
|
||||
Symbol = undefined;
|
||||
URIError = undefined;
|
||||
Proxy = undefined;
|
||||
Map = undefined;
|
||||
WeakMap = undefined;
|
||||
Promise = undefined;
|
||||
Set = undefined;
|
||||
WeakSet = undefined;
|
||||
*/
|
||||
|
||||
Number.roman = {
|
||||
M: 1000,
|
||||
D: 500,
|
||||
|
|
|
@ -75,34 +75,24 @@ var entity = {
|
|||
var stop = function () {
|
||||
timers.remove(stop);
|
||||
execute = undefined;
|
||||
stop.timer = undefined;
|
||||
stop = undefined;
|
||||
rm?.();
|
||||
rm = undefined;
|
||||
update = undefined;
|
||||
};
|
||||
|
||||
|
||||
function execute() {
|
||||
fn();
|
||||
stop?.();
|
||||
}
|
||||
|
||||
|
||||
stop.remain = seconds;
|
||||
stop.seconds = seconds;
|
||||
stop.pct = function () {
|
||||
return 1 - stop.remain / stop.seconds;
|
||||
};
|
||||
|
||||
function update(dt) {
|
||||
profile.frame("timer");
|
||||
if (stop) {
|
||||
// TODO: This seems broken
|
||||
stop.remain -= dt;
|
||||
if (stop.remain <= 0) execute();
|
||||
}
|
||||
profile.endframe();
|
||||
}
|
||||
|
||||
var rm = Register.update.register(update);
|
||||
|
||||
stop.timer = os.make_timer(execute);
|
||||
stop.timer.remain = seconds;
|
||||
|
||||
timers.push(stop);
|
||||
return stop;
|
||||
},
|
||||
|
@ -247,6 +237,7 @@ var entity = {
|
|||
delete ent.objects;
|
||||
ent.objects = {};
|
||||
for (var i in o) {
|
||||
console.info(`creating ${i} on ${ent.toString()}`);
|
||||
var newur = o[i].ur;
|
||||
delete o[i].ur;
|
||||
var n = ent.spawn(ur[newur], o[i]);
|
||||
|
@ -430,7 +421,7 @@ var entity = {
|
|||
|
||||
rename_obj(name, newname) {
|
||||
if (!this.objects[name]) {
|
||||
// console.warn(`No object with name ${name}. Could not rename to ${newname}.`);
|
||||
console.warn(`No object with name ${name}. Could not rename to ${newname}.`);
|
||||
return;
|
||||
}
|
||||
if (name === newname) {
|
||||
|
|
|
@ -331,8 +331,7 @@ profile.data = {};
|
|||
profile.curframe = 0;
|
||||
|
||||
profile.snapshot = {};
|
||||
var fps = [];
|
||||
var frame_lead = 240;
|
||||
|
||||
|
||||
var classes = ["gameobject", "transform", "dsp_node", "texture", "font", "warp_gravity", "warp_damp", "sg_buffer", "datastream", "cpShape", "cpConstraint", "timer", "skin"];
|
||||
var get_snapshot = function()
|
||||
|
@ -361,11 +360,15 @@ var get_snapshot = function()
|
|||
}
|
||||
}
|
||||
|
||||
var fps = [];
|
||||
var frame_lead = 1;
|
||||
var fps_t = 0;
|
||||
profile.report_frame = function (t) {
|
||||
fps.push(t);
|
||||
if (fps.length > frame_lead) {
|
||||
if (profile.secs(profile.now() - fps_t) > frame_lead) {
|
||||
profile.snapshot.fps = Math.mean(fps);
|
||||
fps.length = 0;
|
||||
fps_t = profile.now();
|
||||
get_snapshot();
|
||||
}
|
||||
};
|
||||
|
|
|
@ -1130,6 +1130,7 @@ prosperon.process = function process() {
|
|||
profile.frame("update");
|
||||
prosperon.update(dt * game.timescale);
|
||||
update_emitters(dt * game.timescale);
|
||||
os.update_timers(dt * game.timescale);
|
||||
profile.endframe();
|
||||
if (sim.mode === "step") sim.pause();
|
||||
}
|
||||
|
|
|
@ -40,7 +40,6 @@
|
|||
#include "gui.h"
|
||||
#include "timer.h"
|
||||
#include <sys/resource.h>
|
||||
#include <malloc.h>
|
||||
|
||||
#if (defined(_WIN32) || defined(__WIN32__))
|
||||
#include <direct.h>
|
||||
|
@ -2666,7 +2665,10 @@ static const JSCFunctionListEntry js_texture_funcs[] = {
|
|||
MIST_FUNC_DEF(texture, inram, 0),
|
||||
};
|
||||
|
||||
JSC_GETSET(timer, remain, number)
|
||||
|
||||
static const JSCFunctionListEntry js_timer_funcs[] = {
|
||||
CGETSET_ADD(timer, remain),
|
||||
};
|
||||
|
||||
JSC_GETSET(font, linegap, number)
|
||||
|
@ -2828,7 +2830,8 @@ JSC_CCALL(os_memstate,
|
|||
)
|
||||
|
||||
JSC_CCALL(os_mallinfo,
|
||||
struct mallinfo jsmem = mallinfo();
|
||||
ret = JS_UNDEFINED;
|
||||
/*struct mallinfo jsmem = mallinfo();
|
||||
ret = JS_NewObject(js);
|
||||
JSJMEMRET(arena);
|
||||
JSJMEMRET(ordblks);
|
||||
|
@ -2838,7 +2841,7 @@ JSC_CCALL(os_mallinfo,
|
|||
JSJMEMRET(usmblks);
|
||||
JSJMEMRET(uordblks);
|
||||
JSJMEMRET(fordblks);
|
||||
JSJMEMRET(keepcost);
|
||||
JSJMEMRET(keepcost);*/
|
||||
)
|
||||
|
||||
JSC_CCALL(os_rusage,
|
||||
|
@ -3053,7 +3056,7 @@ JSC_CCALL(os_make_circle2d,
|
|||
return ret;
|
||||
)
|
||||
|
||||
JSC_CCALL(os_make_timer, return timer2js(timer_make()))
|
||||
JSC_CCALL(os_make_timer, return timer2js(timer_make(argv[0])))
|
||||
JSC_CCALL(os_update_timers, timer_update(js2number(argv[0])))
|
||||
|
||||
JSC_CCALL(os_obj_size,
|
||||
|
@ -3388,7 +3391,7 @@ static const JSCFunctionListEntry js_os_funcs[] = {
|
|||
MIST_FUNC_DEF(os, make_hemisphere, 2),
|
||||
MIST_FUNC_DEF(os, make_plane, 2),
|
||||
MIST_FUNC_DEF(os, make_video, 1),
|
||||
MIST_FUNC_DEF(os, make_timer, 0),
|
||||
MIST_FUNC_DEF(os, make_timer, 1),
|
||||
MIST_FUNC_DEF(os, update_timers, 1),
|
||||
MIST_FUNC_DEF(os, mem, 1),
|
||||
MIST_FUNC_DEF(os, mem_limit, 1),
|
||||
|
|
|
@ -52,15 +52,15 @@ extern JSValue cpShape2js(cpShape *s);
|
|||
return ret; \
|
||||
} \
|
||||
|
||||
#define JSC_DCALL(FN) JSValue js_##FN (JSContext *js, JSValue self, int argc, JSValue *argv) { FN(); }
|
||||
#define JSC_DCALL(FN) JSValue js_##FN (JSContext *js, JSValue self, int argc, JSValue *argv) { FN(); return JS_UNDEFINED; }
|
||||
|
||||
#define JSC_1CALL(FN) JSValue js_##FN (JSContext *js, JSValue self, int argc, JSValue *argv) { FN(js2number(argv[0])); }
|
||||
#define JSC_2CALL(FN) JSValue js_##FN (JSContext *js, JSValue self, int argc, JSValue *argv) { FN(js2number(argv[0]), js2number(argv[1])); }
|
||||
#define JSC_3CALL(FN) JSValue js_##FN (JSContext *js, JSValue self, int argc, JSValue *argv) { FN(js2number(argv[0]), js2number(argv[1]), js2number(argv[2])); }
|
||||
#define JSC_4CALL(FN) JSValue js_##FN (JSContext *js, JSValue self, int argc, JSValue *argv) { FN(js2number(argv[0]), js2number(argv[1]), js2number(argv[2]), js2number(argv[3])); }
|
||||
#define JSC_5CALL(FN) JSValue js_##FN (JSContext *js, JSValue self, int argc, JSValue *argv) { FN(js2number(argv[0]), js2number(argv[1]), js2number(argv[2]), js2number(argv[3]), js2number(argv[4])); }
|
||||
#define JSC_6CALL(FN) JSValue js_##FN (JSContext *js, JSValue self, int argc, JSValue *argv) { FN(js2number(argv[0]), js2number(argv[1]), js2number(argv[2]), js2number(argv[3]), js2number(argv[4]), js2number(argv[5])); }
|
||||
#define JSC_7CALL(FN) JSValue js_##FN (JSContext *js, JSValue self, int argc, JSValue *argv) { FN(js2number(argv[0]), js2number(argv[1]), js2number(argv[2]), js2number(argv[3]), js2number(argv[4]), js2number(argv[5]), js2number(argv[6])); }
|
||||
#define JSC_1CALL(FN) JSValue js_##FN (JSContext *js, JSValue self, int argc, JSValue *argv) { FN(js2number(argv[0])); return JS_UNDEFINED; }
|
||||
#define JSC_2CALL(FN) JSValue js_##FN (JSContext *js, JSValue self, int argc, JSValue *argv) { FN(js2number(argv[0]), js2number(argv[1])); return JS_UNDEFINED; }
|
||||
#define JSC_3CALL(FN) JSValue js_##FN (JSContext *js, JSValue self, int argc, JSValue *argv) { FN(js2number(argv[0]), js2number(argv[1]), js2number(argv[2])); return JS_UNDEFINED; }
|
||||
#define JSC_4CALL(FN) JSValue js_##FN (JSContext *js, JSValue self, int argc, JSValue *argv) { FN(js2number(argv[0]), js2number(argv[1]), js2number(argv[2]), js2number(argv[3])); return JS_UNDEFINED; }
|
||||
#define JSC_5CALL(FN) JSValue js_##FN (JSContext *js, JSValue self, int argc, JSValue *argv) { FN(js2number(argv[0]), js2number(argv[1]), js2number(argv[2]), js2number(argv[3]), js2number(argv[4])); return JS_UNDEFINED; }
|
||||
#define JSC_6CALL(FN) JSValue js_##FN (JSContext *js, JSValue self, int argc, JSValue *argv) { FN(js2number(argv[0]), js2number(argv[1]), js2number(argv[2]), js2number(argv[3]), js2number(argv[4]), js2number(argv[5])); return JS_UNDEFINED; }
|
||||
#define JSC_7CALL(FN) JSValue js_##FN (JSContext *js, JSValue self, int argc, JSValue *argv) { FN(js2number(argv[0]), js2number(argv[1]), js2number(argv[2]), js2number(argv[3]), js2number(argv[4]), js2number(argv[5]), js2number(argv[6])); return JS_UNDEFINED; }
|
||||
|
||||
#define GETSETPAIR(ID, ENTRY, TYPE, FN) \
|
||||
JSValue js_##ID##_set_##ENTRY (JS_SETSIG) { \
|
||||
|
|
|
@ -4,9 +4,10 @@
|
|||
|
||||
timer **timers;
|
||||
|
||||
timer *timer_make()
|
||||
timer *timer_make(JSValue fn)
|
||||
{
|
||||
timer *t = calloc(sizeof(*t),1);
|
||||
t->fn = JS_DupValue(js,fn);
|
||||
arrput(timers, t);
|
||||
return t;
|
||||
}
|
||||
|
@ -19,6 +20,8 @@ void timer_free(timer *t)
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
JS_FreeValue(js, t->fn);
|
||||
|
||||
free(t);
|
||||
}
|
||||
|
@ -26,11 +29,13 @@ void timer_free(timer *t)
|
|||
void timer_update(double dt)
|
||||
{
|
||||
for (int i = 0; i < arrlen(timers); i++) {
|
||||
if (timers[i]->remain <= 0) continue;
|
||||
timers[i]->remain -= dt;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void timer_stop(timer *t)
|
||||
{
|
||||
|
||||
}
|
|
@ -1,13 +1,14 @@
|
|||
#ifndef TIMER_H
|
||||
|
||||
#include "script.h"
|
||||
|
||||
typedef struct timer {
|
||||
double start;
|
||||
double remain;
|
||||
JSValue fn;
|
||||
} timer;
|
||||
|
||||
timer *timer_make();
|
||||
timer *timer_make(JSValue fn);
|
||||
void timer_free(timer *t);
|
||||
void timer_update(double dt);
|
||||
void timer_stop(timer *t);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue