From 8c3e8aa539bb3ef18954c82dab7f145800be1714 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Tue, 7 Nov 2023 18:45:52 +0000 Subject: [PATCH] timers fixed --- Makefile | 2 +- scripts/editor.js | 4 +--- scripts/engine.js | 13 ++----------- scripts/entity.js | 17 +++++++++++++++++ scripts/gui.js | 1 + scripts/physics.js | 4 ++++ source/engine/jsffi.c | 7 ++----- source/engine/script.c | 17 ++++++++++++++++- source/engine/script.h | 2 ++ source/engine/texture.c | 20 ++++++++++---------- source/engine/timer.c | 9 ++++++++- source/engine/timer.h | 2 ++ 12 files changed, 66 insertions(+), 32 deletions(-) diff --git a/Makefile b/Makefile index eb6bedd..f21ac55 100755 --- a/Makefile +++ b/Makefile @@ -252,7 +252,7 @@ crosswin: clean: @echo Cleaning project @rm -rf bin dist - @rm -f shaders/*.sglsl.h shaders/*.metal core.cdb jso cdb packer TAGS scripts/*.jso + @rm -f shaders/*.sglsl.h shaders/*.metal core.cdb jso cdb packer scripts/*.jso @make -C quickjs clean TAGINC != find . -name "*.[chj]" diff --git a/scripts/editor.js b/scripts/editor.js index 3cba454..3da3960 100644 --- a/scripts/editor.js +++ b/scripts/editor.js @@ -215,7 +215,6 @@ var editor = { Primum.clear(); load("config.js"); Game.play(); - Game.editor_mode(false); Player.players[0].uncontrol(this); Player.players[0].control(limited_editor); Register.unregister_obj(this); @@ -849,7 +848,6 @@ editor.inputs['M-p'].doc = "Do one time step, pausing if necessary."; editor.inputs['C-M-p'] = function() { if (!Game.playing()) { editor.start_play_ed(); - Game.editor_mode(false); } Log.warn(`Starting edited level ...`); }; @@ -1544,7 +1542,7 @@ var replpanel = Object.copy(inputpanel, { return [ Mum.text({str:log, anchor:[0,0], offset:[0,-300].sub(this.scrolloffset), selectable: true}), - Mum.text({str:this.value,color:Color.purple, offset:[0,-290], caret: this.caret}) + Mum.text({str:this.value,color:Color.green, offset:[0,-290], caret: this.caret}) ]; }, prevmark:-1, diff --git a/scripts/engine.js b/scripts/engine.js index 2a6b613..62bcc5e 100644 --- a/scripts/engine.js +++ b/scripts/engine.js @@ -239,6 +239,7 @@ load("scripts/gui.js"); var timer = { make(fn, secs,obj,loop,app) { + obj ??= globalThis; app ??= false; if (secs === 0) { fn.call(obj); @@ -246,17 +247,7 @@ var timer = { } var t = Object.create(this); - - t.callback = fn; - var guardfn = function() { - if (typeof t.callback === 'function') { - t.callback(); - if (!t.loop) t.kill(); - } - else - Log.warn("Timer trying to execute without a function."); - }; - t.id = make_timer(guardfn, secs, app); + t.id = make_timer(fn, secs, app, obj); t.loop = loop; t.pause(); diff --git a/scripts/entity.js b/scripts/entity.js index 3ca34fe..573e1dc 100644 --- a/scripts/entity.js +++ b/scripts/entity.js @@ -10,6 +10,13 @@ function grab_from_points(pos, points, slop) { return idx; }; +var actor = {}; +actor.spawn = function(ur, config){}; +actor.die = function(actor){}; +actor.kill = function(actor){}; +actor.delay = function(fn, seconds) {}; +actor.clock = function(fn){}; + var gameobject = { impl: { full_path() { @@ -34,6 +41,12 @@ var gameobject = { }; this.objects = {}; }, + + timers:[], + delay(fn, seconds) { + var t = timer.oneshot(fn, seconds, this, false); + return function() { t.kill(); }; + }, set max_velocity(x) { cmd(151, this.body, x); }, get max_velocity() { return cmd(152, this.body); }, @@ -286,6 +299,9 @@ var gameobject = { if (typeof obj.debug === 'function') Register.debug.register(obj.debug, obj); + if (typeof obj.gui === 'function') + Register.gui.register(obj.gui,obj); + obj.components.forEach(function(x) { if (typeof x.collide === 'function') register_collide(1, x.collide, x, obj.body, x.shape); @@ -638,6 +654,7 @@ gameobject.doc = { transform: `Return an object representing the transform state of this object.`, kill: `Remove this object from the world.`, level: "The entity this entity belongs to.", + delay: 'Run the given function after the given number of seconds has elapsed.', }; /* Default objects */ diff --git a/scripts/gui.js b/scripts/gui.js index d8d2e9a..d24bcfc 100644 --- a/scripts/gui.js +++ b/scripts/gui.js @@ -252,6 +252,7 @@ Mum.image = Mum.extend({ Mum.column = Mum.extend({ draw(cursor, cnt) { + cnt ??= Mum; if (this.hide) return; cursor = cursor.add(this.offset); this.max_width = cnt.width; diff --git a/scripts/physics.js b/scripts/physics.js index e384b97..0bb1163 100644 --- a/scripts/physics.js +++ b/scripts/physics.js @@ -37,6 +37,10 @@ var physics = { return cmd(86, box.pos, box.wh, points, points.length); }, + + shape_query(shape) { + return cmd(80,shape); + }, }; physics.doc = {}; diff --git a/source/engine/jsffi.c b/source/engine/jsffi.c index 48a13c8..74a23d1 100644 --- a/source/engine/jsffi.c +++ b/source/engine/jsffi.c @@ -1673,11 +1673,8 @@ JSValue duk_anim(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) JSValue duk_make_timer(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) { double secs = js2number(argv[1]); - struct callee *c = malloc(sizeof(*c)); - c->fn = JS_DupValue(js, argv[0]); - c->obj = globalThis; + struct callee *c = make_callee(argv[0], argv[3]); int id = timer_make(secs, call_callee, c, 1, js2bool(argv[2])); - return JS_NewInt64(js, id); } @@ -1749,7 +1746,7 @@ void ffi_load() { DUK_FUNC(make_edge2d, 3) DUK_FUNC(cmd_edge2d, 6) DUK_FUNC(make_model,2); - DUK_FUNC(make_timer, 3) + DUK_FUNC(make_timer, 4) DUK_FUNC(cmd_points, 5); diff --git a/source/engine/script.c b/source/engine/script.c index 949694e..f19c5f1 100644 --- a/source/engine/script.c +++ b/source/engine/script.c @@ -71,13 +71,13 @@ void script_startup() { void script_stop() { + timers_free(); send_signal("quit",0,NULL); for (int i = 0; i < shlen(jsstrs); i++) JS_FreeValue(js,jsstrs[i].value); JS_FreeContext(js); - JS_RunGC(rt); JS_FreeRuntime(rt); } @@ -271,6 +271,21 @@ void script_callee(struct callee c, int argc, JSValue *argv) { js_callee_exec(&c, argc, argv); } +struct callee *make_callee(JSValue fn, JSValue obj) +{ + struct callee *c = malloc(sizeof(*c)); + c->fn = JS_DupValue(js, fn); + c->obj = JS_DupValue(js, obj); + return c; +} + +void free_callee(struct callee *c) +{ + JS_FreeValue(js,c->fn); + JS_FreeValue(js,c->obj); + free(c); +} + void send_signal(const char *signal, int argc, JSValue *argv) { JSValue globalThis = JS_GetGlobalObject(js); diff --git a/source/engine/script.h b/source/engine/script.h index 266c4b2..739067f 100644 --- a/source/engine/script.h +++ b/source/engine/script.h @@ -27,6 +27,8 @@ time_t jso_file(const char *file); JSValue script_runfile(const char *file); void script_update(double dt); void script_draw(); +struct callee *make_callee(JSValue fn, JSValue obj); +void free_callee(struct callee *c); void duk_run_err(); void js_dump_stack(); diff --git a/source/engine/texture.c b/source/engine/texture.c index d71c7c5..86aa362 100644 --- a/source/engine/texture.c +++ b/source/engine/texture.c @@ -382,20 +382,20 @@ void anim_load(struct anim2d *anim, const char *path) { } void anim_play(struct anim2d *anim) { - if (anim->playing) - return; +// if (anim->playing) +// return; - if (anim->frame == anim_frames(anim->anim)) - anim->frame = 0; +// if (anim->frame == anim_frames(anim->anim)) +// anim->frame = 0; - anim->playing = 1; +// anim->playing = 1; - if (anim->timer == NULL) - anim->timer = id2timer(timer_make(1.f / anim->anim->ms, anim_incr, anim, 0, 0)); - else - timerr_settime(anim->timer, 1.f / anim->anim->ms); +// if (anim->timer == NULL) +// anim->timer = id2timer(timer_make(1.f / anim->anim->ms, anim_incr, anim, 0, 0)); +// else +// timerr_settime(anim->timer, 1.f / anim->anim->ms); - timer_start(anim->timer); +// timer_start(anim->timer); } void anim_stop(struct anim2d *anim) { diff --git a/source/engine/timer.c b/source/engine/timer.c index ba745dc..615416f 100644 --- a/source/engine/timer.c +++ b/source/engine/timer.c @@ -3,6 +3,7 @@ #include #include +#include "script.h" struct timer *timers; static int first = -1; @@ -76,7 +77,7 @@ void timer_start(struct timer *t) { void timer_remove(int id) { struct timer *t = id2timer(id); - if (t->owndata) free(t->data); + free_callee(t->data); t->next = first; t->on = 0; first = id; @@ -90,3 +91,9 @@ void timerr_settime(struct timer *t, double interval) { struct timer *id2timer(int id) { return &timers[id]; } + +void timers_free() +{ + for (int i = 0; i < arrlen(timers); i++) + free_callee(timers[i].data); +} diff --git a/source/engine/timer.h b/source/engine/timer.h index d9cf93d..0ba2de6 100644 --- a/source/engine/timer.h +++ b/source/engine/timer.h @@ -23,4 +23,6 @@ void timer_stop(struct timer *t); void timer_update(double dt, double scale); void timerr_settime(struct timer *t, double interval); +void timers_free(); + #endif