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) {
|
actor.delay = function (fn, seconds) {
|
||||||
var timers = this.timers;
|
var timers = this.timers;
|
||||||
|
|
||||||
var stop = function () {
|
var stop = function () {
|
||||||
timers.remove(stop);
|
timers.remove(stop);
|
||||||
|
stop.timer = undefined;
|
||||||
stop = undefined;
|
stop = undefined;
|
||||||
rm();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function execute() {
|
function execute() {
|
||||||
|
@ -151,15 +152,9 @@ actor.delay = function (fn, seconds) {
|
||||||
stop.pct = function () {
|
stop.pct = function () {
|
||||||
return 1 - stop.remain / stop.seconds;
|
return 1 - stop.remain / stop.seconds;
|
||||||
};
|
};
|
||||||
|
|
||||||
function update(dt) {
|
stop.timer = os.make_timer(execute);
|
||||||
profile.frame("timer");
|
stop.timer.remain = seconds;
|
||||||
stop.remain -= dt;
|
|
||||||
if (stop.remain <= 0) execute();
|
|
||||||
profile.endframe();
|
|
||||||
}
|
|
||||||
|
|
||||||
var rm = Register.appupdate.register(update);
|
|
||||||
|
|
||||||
timers.push(stop);
|
timers.push(stop);
|
||||||
return 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 = {
|
Number.roman = {
|
||||||
M: 1000,
|
M: 1000,
|
||||||
D: 500,
|
D: 500,
|
||||||
|
|
|
@ -75,34 +75,24 @@ var entity = {
|
||||||
var stop = function () {
|
var stop = function () {
|
||||||
timers.remove(stop);
|
timers.remove(stop);
|
||||||
execute = undefined;
|
execute = undefined;
|
||||||
|
stop.timer = undefined;
|
||||||
stop = undefined;
|
stop = undefined;
|
||||||
rm?.();
|
|
||||||
rm = undefined;
|
|
||||||
update = undefined;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function execute() {
|
function execute() {
|
||||||
fn();
|
fn();
|
||||||
stop?.();
|
stop?.();
|
||||||
}
|
}
|
||||||
|
|
||||||
stop.remain = seconds;
|
stop.remain = seconds;
|
||||||
stop.seconds = seconds;
|
stop.seconds = seconds;
|
||||||
stop.pct = function () {
|
stop.pct = function () {
|
||||||
return 1 - stop.remain / stop.seconds;
|
return 1 - stop.remain / stop.seconds;
|
||||||
};
|
};
|
||||||
|
|
||||||
function update(dt) {
|
stop.timer = os.make_timer(execute);
|
||||||
profile.frame("timer");
|
stop.timer.remain = seconds;
|
||||||
if (stop) {
|
|
||||||
// TODO: This seems broken
|
|
||||||
stop.remain -= dt;
|
|
||||||
if (stop.remain <= 0) execute();
|
|
||||||
}
|
|
||||||
profile.endframe();
|
|
||||||
}
|
|
||||||
|
|
||||||
var rm = Register.update.register(update);
|
|
||||||
timers.push(stop);
|
timers.push(stop);
|
||||||
return stop;
|
return stop;
|
||||||
},
|
},
|
||||||
|
@ -247,6 +237,7 @@ var entity = {
|
||||||
delete ent.objects;
|
delete ent.objects;
|
||||||
ent.objects = {};
|
ent.objects = {};
|
||||||
for (var i in o) {
|
for (var i in o) {
|
||||||
|
console.info(`creating ${i} on ${ent.toString()}`);
|
||||||
var newur = o[i].ur;
|
var newur = o[i].ur;
|
||||||
delete o[i].ur;
|
delete o[i].ur;
|
||||||
var n = ent.spawn(ur[newur], o[i]);
|
var n = ent.spawn(ur[newur], o[i]);
|
||||||
|
@ -430,7 +421,7 @@ var entity = {
|
||||||
|
|
||||||
rename_obj(name, newname) {
|
rename_obj(name, newname) {
|
||||||
if (!this.objects[name]) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
if (name === newname) {
|
if (name === newname) {
|
||||||
|
|
|
@ -331,8 +331,7 @@ profile.data = {};
|
||||||
profile.curframe = 0;
|
profile.curframe = 0;
|
||||||
|
|
||||||
profile.snapshot = {};
|
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 classes = ["gameobject", "transform", "dsp_node", "texture", "font", "warp_gravity", "warp_damp", "sg_buffer", "datastream", "cpShape", "cpConstraint", "timer", "skin"];
|
||||||
var get_snapshot = function()
|
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) {
|
profile.report_frame = function (t) {
|
||||||
fps.push(t);
|
fps.push(t);
|
||||||
if (fps.length > frame_lead) {
|
if (profile.secs(profile.now() - fps_t) > frame_lead) {
|
||||||
profile.snapshot.fps = Math.mean(fps);
|
profile.snapshot.fps = Math.mean(fps);
|
||||||
fps.length = 0;
|
fps.length = 0;
|
||||||
|
fps_t = profile.now();
|
||||||
get_snapshot();
|
get_snapshot();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -1130,6 +1130,7 @@ prosperon.process = function process() {
|
||||||
profile.frame("update");
|
profile.frame("update");
|
||||||
prosperon.update(dt * game.timescale);
|
prosperon.update(dt * game.timescale);
|
||||||
update_emitters(dt * game.timescale);
|
update_emitters(dt * game.timescale);
|
||||||
|
os.update_timers(dt * game.timescale);
|
||||||
profile.endframe();
|
profile.endframe();
|
||||||
if (sim.mode === "step") sim.pause();
|
if (sim.mode === "step") sim.pause();
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,6 @@
|
||||||
#include "gui.h"
|
#include "gui.h"
|
||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
#include <sys/resource.h>
|
#include <sys/resource.h>
|
||||||
#include <malloc.h>
|
|
||||||
|
|
||||||
#if (defined(_WIN32) || defined(__WIN32__))
|
#if (defined(_WIN32) || defined(__WIN32__))
|
||||||
#include <direct.h>
|
#include <direct.h>
|
||||||
|
@ -2666,7 +2665,10 @@ static const JSCFunctionListEntry js_texture_funcs[] = {
|
||||||
MIST_FUNC_DEF(texture, inram, 0),
|
MIST_FUNC_DEF(texture, inram, 0),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
JSC_GETSET(timer, remain, number)
|
||||||
|
|
||||||
static const JSCFunctionListEntry js_timer_funcs[] = {
|
static const JSCFunctionListEntry js_timer_funcs[] = {
|
||||||
|
CGETSET_ADD(timer, remain),
|
||||||
};
|
};
|
||||||
|
|
||||||
JSC_GETSET(font, linegap, number)
|
JSC_GETSET(font, linegap, number)
|
||||||
|
@ -2828,7 +2830,8 @@ JSC_CCALL(os_memstate,
|
||||||
)
|
)
|
||||||
|
|
||||||
JSC_CCALL(os_mallinfo,
|
JSC_CCALL(os_mallinfo,
|
||||||
struct mallinfo jsmem = mallinfo();
|
ret = JS_UNDEFINED;
|
||||||
|
/*struct mallinfo jsmem = mallinfo();
|
||||||
ret = JS_NewObject(js);
|
ret = JS_NewObject(js);
|
||||||
JSJMEMRET(arena);
|
JSJMEMRET(arena);
|
||||||
JSJMEMRET(ordblks);
|
JSJMEMRET(ordblks);
|
||||||
|
@ -2838,7 +2841,7 @@ JSC_CCALL(os_mallinfo,
|
||||||
JSJMEMRET(usmblks);
|
JSJMEMRET(usmblks);
|
||||||
JSJMEMRET(uordblks);
|
JSJMEMRET(uordblks);
|
||||||
JSJMEMRET(fordblks);
|
JSJMEMRET(fordblks);
|
||||||
JSJMEMRET(keepcost);
|
JSJMEMRET(keepcost);*/
|
||||||
)
|
)
|
||||||
|
|
||||||
JSC_CCALL(os_rusage,
|
JSC_CCALL(os_rusage,
|
||||||
|
@ -3053,7 +3056,7 @@ JSC_CCALL(os_make_circle2d,
|
||||||
return ret;
|
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_update_timers, timer_update(js2number(argv[0])))
|
||||||
|
|
||||||
JSC_CCALL(os_obj_size,
|
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_hemisphere, 2),
|
||||||
MIST_FUNC_DEF(os, make_plane, 2),
|
MIST_FUNC_DEF(os, make_plane, 2),
|
||||||
MIST_FUNC_DEF(os, make_video, 1),
|
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, update_timers, 1),
|
||||||
MIST_FUNC_DEF(os, mem, 1),
|
MIST_FUNC_DEF(os, mem, 1),
|
||||||
MIST_FUNC_DEF(os, mem_limit, 1),
|
MIST_FUNC_DEF(os, mem_limit, 1),
|
||||||
|
|
|
@ -52,15 +52,15 @@ extern JSValue cpShape2js(cpShape *s);
|
||||||
return ret; \
|
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_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])); }
|
#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])); }
|
#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])); }
|
#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])); }
|
#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])); }
|
#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])); }
|
#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) \
|
#define GETSETPAIR(ID, ENTRY, TYPE, FN) \
|
||||||
JSValue js_##ID##_set_##ENTRY (JS_SETSIG) { \
|
JSValue js_##ID##_set_##ENTRY (JS_SETSIG) { \
|
||||||
|
|
|
@ -4,9 +4,10 @@
|
||||||
|
|
||||||
timer **timers;
|
timer **timers;
|
||||||
|
|
||||||
timer *timer_make()
|
timer *timer_make(JSValue fn)
|
||||||
{
|
{
|
||||||
timer *t = calloc(sizeof(*t),1);
|
timer *t = calloc(sizeof(*t),1);
|
||||||
|
t->fn = JS_DupValue(js,fn);
|
||||||
arrput(timers, t);
|
arrput(timers, t);
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
@ -19,6 +20,8 @@ void timer_free(timer *t)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JS_FreeValue(js, t->fn);
|
||||||
|
|
||||||
free(t);
|
free(t);
|
||||||
}
|
}
|
||||||
|
@ -26,11 +29,13 @@ void timer_free(timer *t)
|
||||||
void timer_update(double dt)
|
void timer_update(double dt)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < arrlen(timers); i++) {
|
for (int i = 0; i < arrlen(timers); i++) {
|
||||||
|
if (timers[i]->remain <= 0) continue;
|
||||||
timers[i]->remain -= dt;
|
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
|
#ifndef TIMER_H
|
||||||
|
|
||||||
|
#include "script.h"
|
||||||
|
|
||||||
typedef struct timer {
|
typedef struct timer {
|
||||||
double start;
|
|
||||||
double remain;
|
double remain;
|
||||||
|
JSValue fn;
|
||||||
} timer;
|
} timer;
|
||||||
|
|
||||||
timer *timer_make();
|
timer *timer_make(JSValue fn);
|
||||||
void timer_free(timer *t);
|
void timer_free(timer *t);
|
||||||
void timer_update(double dt);
|
void timer_update(double dt);
|
||||||
void timer_stop(timer *t);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue