From 591f48c703bb99572f56081c496f08d8bf2baf61 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Sat, 27 May 2023 15:13:20 +0000 Subject: [PATCH] physics fixes --- Makefile | 5 +++ source/engine/2dphysics.c | 26 +++++++++++-- source/engine/2dphysics.h | 2 + source/engine/debug/debugdraw.c | 7 ++-- source/engine/debug/debugdraw.h | 4 +- source/engine/debug/log.c | 9 ++--- source/engine/debug/log.h | 4 ++ source/engine/ffi.c | 4 ++ source/engine/script.c | 2 +- source/engine/yugine.c | 25 +++++++++++-- source/scripts/debug.js | 7 ++++ source/scripts/engine.js | 66 ++++++++++++++++++++------------- 12 files changed, 117 insertions(+), 44 deletions(-) diff --git a/Makefile b/Makefile index 002e4bd..f00618b 100755 --- a/Makefile +++ b/Makefile @@ -13,6 +13,11 @@ QFLAGS := ifdef DBG QFLAGS += -O0 -g -DDBG INFO = dbg + + ifeq ($(CC),tcc) + QFLAGS += + endif + else QFLAGS += -O2 INFO = rel diff --git a/source/engine/2dphysics.c b/source/engine/2dphysics.c index 04e1b6d..d040c9e 100644 --- a/source/engine/2dphysics.c +++ b/source/engine/2dphysics.c @@ -173,6 +173,7 @@ void phys2d_set_gravity(cpVect v) { void phys2d_update(float deltaT) { cpSpaceStep(space, deltaT); + flush_collide_cbs(); } void init_phys2dshape(struct phys2d_shape *shape, int go, void *data) { @@ -262,9 +263,9 @@ void phys2d_dbgdrawcpcirc(cpCircleShape *c) { float radius = cpCircleShapeGetRadius(c); struct rgba color = shape_color(c); float seglen = cpShapeGetSensor(c) ? 5 : -1; - draw_circle(pos.x, pos.y, radius, 1, color, seglen); + draw_circle(pos, radius, 1, color, seglen); color.a = col_alpha; - draw_circle(pos.x,pos.y,radius,radius,color,-1); + draw_circle(pos,radius,radius,color,-1); } void phys2d_dbgdrawcircle(struct phys2d_circle *circle) { @@ -586,6 +587,21 @@ void phys2d_reindex_body(cpBody *body) { cpSpaceReindexShapesForBody(space, body); } +struct postphys_cb { + struct callee c; + JSValue send; +}; + +static struct postphys_cb begins[512]; +static uint32_t bptr; + +void flush_collide_cbs() { + for (int i = 0; i < bptr; i++) + script_callee(begins[i].c, 1, &begins[i].send); + + bptr = 0; +} + void duk_call_phys_cb(cpVect norm, struct callee c, int hit, cpArbiter *arb) { cpShape *shape1; cpShape *shape2; @@ -599,7 +615,11 @@ void duk_call_phys_cb(cpVect norm, struct callee c, int hit, cpArbiter *arb) { JS_SetPropertyStr(js, obj, "pos", vec2js(cpArbiterGetPointA(arb, 0))); JS_SetPropertyStr(js, obj, "id", JS_NewInt32(js,hit)); JS_SetPropertyStr(js,obj,"obj", JS_DupValue(js,id2go(hit)->ref)); - script_callee(c, 1, &obj); + + begins[bptr].c = c; + begins[bptr].send = obj; + bptr++; + return; } #define CTYPE_BEGIN 0 diff --git a/source/engine/2dphysics.h b/source/engine/2dphysics.h index 2223735..7eb33e5 100644 --- a/source/engine/2dphysics.h +++ b/source/engine/2dphysics.h @@ -138,6 +138,8 @@ void phys2d_setup_handlers(int go); int *phys2d_query_shape(struct phys2d_shape *shape); int *phys2d_query_box_points(cpVect pos, cpVect wh, cpVect *points, int n); +void flush_collide_cbs(); + void phys2d_reindex_body(cpBody *body); cpVect world2go(struct gameobject *go, cpVect worldpos); cpVect go2world(struct gameobject *go, cpVect gopos); diff --git a/source/engine/debug/debugdraw.c b/source/engine/debug/debugdraw.c index 4844403..7e3d401 100644 --- a/source/engine/debug/debugdraw.c +++ b/source/engine/debug/debugdraw.c @@ -70,7 +70,7 @@ static sg_bindings circle_bind; static sg_shader csg; static int circle_count = 0; struct circle_vertex { - float pos[2]; + cpVect pos; float radius; struct rgba color; float segsize; @@ -507,11 +507,10 @@ void draw_edge(cpVect *points, int n, struct rgba color, int thickness, int clos } } -void draw_circle(int x, int y, float radius, float pixels, struct rgba color, float seg) +void draw_circle(cpVect pos, float radius, float pixels, struct rgba color, float seg) { struct circle_vertex cv; - cv.pos[0] = x; - cv.pos[1] = y; + cv.pos = pos; cv.radius = radius; cv.color = color; cv.segsize = seg/radius; diff --git a/source/engine/debug/debugdraw.h b/source/engine/debug/debugdraw.h index 22e5bb7..ddcc23f 100644 --- a/source/engine/debug/debugdraw.h +++ b/source/engine/debug/debugdraw.h @@ -13,8 +13,8 @@ void draw_arrow(struct cpVect start, struct cpVect end, struct rgba, int capsize void draw_edge(struct cpVect *points, int n, struct rgba color, int thickness, int closed, int flags, struct rgba line_color, float line_seg); /* pixels - how many pixels thick, segsize - dashed line seg len */ -void draw_circle(int x, int y, float radius, float pixels, struct rgba color, float seg); -void draw_box(struct cpVect c, struct cpVect wh, struct rgba color); +void draw_circle(cpVect c, float radius, float pixels, struct rgba color, float seg); +void draw_box(cpVect c, cpVect wh, struct rgba color); void draw_poly(cpVect *points, int n, struct rgba color); void draw_grid(int width, int span, struct rgba color); diff --git a/source/engine/debug/log.c b/source/engine/debug/log.c index 4c49da6..1c0199e 100644 --- a/source/engine/debug/log.c +++ b/source/engine/debug/log.c @@ -19,9 +19,8 @@ int logLevel = 1; 3 critical */ -//char *logstr[] = { "INFO", "WARN", "\x1b[1;31mERROR\x1b[0m", "CRITICAL" }; char *logstr[] = { "info", "warn", "error", "critical" }; -char *catstr[] = {"engine", "script"}; +char *catstr[] = {"engine", "script", "render"}; FILE *logfile = NULL; @@ -48,12 +47,12 @@ void mYughLog(int category, int priority, int line, const char *file, const char va_end(args); char buffer[ERROR_BUFFER] = { '\0' }; - snprintf(buffer, ERROR_BUFFER, "%g | %s:%d: %s, %s: %s\n", ticks, file, line, logstr[priority], catstr[category], msgbuffer); + snprintf(buffer, ERROR_BUFFER, "%s:%d: %s, %s: %s\n", file, line, logstr[priority], catstr[category], msgbuffer); log_print(buffer); - if (category == 1 && priority >= 2) - js_stacktrace(); +// if (category != LOG_SCRIPT && priority >= 2) +// js_stacktrace(); } #endif } diff --git a/source/engine/debug/log.h b/source/engine/debug/log.h index a2c363a..83f758b 100644 --- a/source/engine/debug/log.h +++ b/source/engine/debug/log.h @@ -10,6 +10,10 @@ #define LOG_ERROR 2 #define LOG_CRITICAL 3 +#define LOG_ENGINE 0 +#define LOG_SCRIPT 1 +#define LOG_RENDER 2 + #define M_PI 3.14 extern char lastlog[]; diff --git a/source/engine/ffi.c b/source/engine/ffi.c index 94f22e9..b91e203 100644 --- a/source/engine/ffi.c +++ b/source/engine/ffi.c @@ -1017,6 +1017,10 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) case 114: return bool2js(js2sprite(argv[1])->enabled); + + case 115: + draw_circle(js2vec2(argv[1]), js2number(argv[2]), js2number(argv[2]), js2color(argv[3]), -1); + break; } if (str) diff --git a/source/engine/script.c b/source/engine/script.c index 270c607..633a71b 100644 --- a/source/engine/script.c +++ b/source/engine/script.c @@ -98,7 +98,7 @@ int js_print_exception(JSValue v) { const char *name = JS_ToCString(js, JS_GetPropertyStr(js, exception, "name")); const char *msg = JS_ToCString(js, JS_GetPropertyStr(js, exception, "message")); const char *stack = JS_ToCString(js, val); - YughWarn("%s :: %s\n%s", name, msg,stack); + YughLog(LOG_SCRIPT, LOG_ERROR, "%s :: %s\n%s", name, msg,stack); JS_FreeCString(js, name); JS_FreeCString(js, msg); diff --git a/source/engine/yugine.c b/source/engine/yugine.c index 25d4a99..44a8ad1 100644 --- a/source/engine/yugine.c +++ b/source/engine/yugine.c @@ -62,16 +62,33 @@ int fps; #define SIM_PAUSE 2 #define SIM_STEP 3 +#ifdef __TINYC__ +int backtrace(void **buffer, int size) { + extern uint64_t *__libc_stack_end; + uint64_t **p, *bp, *frame; + asm ("mov %%rbp, %0;" : "=r" (bp)); + p = (uint64_t**) bp; + int i = 0; + while (i < size) { + frame = p[0]; + if (frame < bp || frame > __libc_stack_end) { + return i; + } + buffer[i++] = p[1]; + p = (uint64_t**) frame; + } + return i; +} +#endif + void print_stacktrace() { void *ents[512]; - size_t size; - - size = backtrace(ents, 512); + size_t size = backtrace(ents, 512); YughCritical("====================BACKTRACE===================="); char **stackstr = backtrace_symbols(ents, size); - YughInfo("Stack size is %d.", size); + YughCritical("Stack size is %d.", size); for (int i = 0; i < size; i++) YughCritical(stackstr[i]); diff --git a/source/scripts/debug.js b/source/scripts/debug.js index aceaac3..9d86fc0 100644 --- a/source/scripts/debug.js +++ b/source/scripts/debug.js @@ -6,6 +6,13 @@ var Gizmos = { }, }; +var Shape = { + circle(pos, radius, color) { + + cmd(115, pos, radius, color); + }, +}; + var Debug = { draw_grid(width, span, color) { color = color ? color : Color.green; diff --git a/source/scripts/engine.js b/source/scripts/engine.js index de503b6..0661540 100644 --- a/source/scripts/engine.js +++ b/source/scripts/engine.js @@ -12,8 +12,6 @@ function load(file) { files[file] = modtime; } - - load("scripts/base.js"); var Log = { @@ -62,7 +60,6 @@ var Log = { }, stack(skip = 0) { - Log.warn("Printing stack"); var stack = (new Error()).stack; var n = stack.next('\n',0)+1; for (var i = 0; i < skip; i++) @@ -456,18 +453,28 @@ function state2str(state) { } var Register = { + inloop: false, + loopcbs: [], + finloop() { + this.loopcbs.forEach(x => x()); + this.loopcbs = []; + }, + + wraploop(loop) { + this.inloop = true; + loop(); + this.inloop = false; + this.finloop(); + }, + updates: [], update(dt) { - this.updates.forEach(x => x[0].call(x[1], dt)); + this.wraploop(() => this.updates.forEach(x => x[0].call(x[1], dt))); }, physupdates: [], - postphys_cbs: [], physupdate(dt) { - this.postphys_cbs.forEach(x => x()); - this.postphys_cbs = []; - - this.physupdates.forEach(x => x[0].call(x[1], dt)); + this.wraploop(() => this.physupdates.forEach(x => x[0].call(x[1], dt))); }, guis: [], @@ -520,6 +527,15 @@ var Register = { draw() { this.draws.forEach(x => x[0].call(x[1])); }, + + endofloop(fn) { + if (!this.inloop) + fn(); + else { + Log.warn("resgieted ..."); + this.loopcbs.push(fn); + } + }, }; Register.unregister_obj(null); @@ -1464,26 +1480,26 @@ var gameobject = { stop() {}, kill() { - Log.warn(`Killing ${this.toString()}`); - cmd(2, this.body); - - delete Game.objects[this.body]; + Register.endofloop(() => { + cmd(2, this.body); + delete Game.objects[this.body]; - if (this.level) - this.level.unregister(this); + if (this.level) + this.level.unregister(this); - this.uncontrol(); - this.instances.remove(this); - Register.unregister_obj(this); - Signal.clear_obj(this); + this.uncontrol(); + this.instances.remove(this); + Register.unregister_obj(this); + Signal.clear_obj(this); - this.body = -1; - for (var key in this.components) { - Register.unregister_obj(this.components[key]); - this.components[key].kill(); - } + this.body = -1; + for (var key in this.components) { + Register.unregister_obj(this.components[key]); + this.components[key].kill(); + } - this.stop(); + this.stop(); + }); }, get up() {