From d9f6dd5e567645f122b0e4affebd1aca812e32c7 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Mon, 11 Dec 2023 22:59:59 +0000 Subject: [PATCH] fix compilation bugs; add bignum --- quickjs/Makefile | 2 +- scripts/engine.js | 1 + source/engine/2dphysics.c | 24 +++++++----------------- source/engine/3d/model.c | 2 +- source/engine/3d/model.h | 2 +- source/engine/gameobject.c | 4 ++-- source/engine/jsffi.c | 22 ++-------------------- source/engine/sound/sound.c | 2 +- source/engine/sprite.c | 4 ++-- source/engine/sprite.h | 2 +- source/engine/transform.c | 2 +- 11 files changed, 20 insertions(+), 47 deletions(-) diff --git a/quickjs/Makefile b/quickjs/Makefile index edad6d5..bd95f53 100644 --- a/quickjs/Makefile +++ b/quickjs/Makefile @@ -52,7 +52,7 @@ prefix=/usr/local # use address sanitizer #CONFIG_ASAN=y # include the code for BigInt/BigFloat/BigDecimal and math mode -#CONFIG_BIGNUM=y +CONFIG_BIGNUM=y OBJDIR=.obj diff --git a/scripts/engine.js b/scripts/engine.js index b454e79..15c0971 100644 --- a/scripts/engine.js +++ b/scripts/engine.js @@ -1,3 +1,4 @@ +"use math"; var files = {}; function load(file) { var modtime = cmd(0, file); diff --git a/source/engine/2dphysics.c b/source/engine/2dphysics.c index 906bcf9..bd8d791 100644 --- a/source/engine/2dphysics.c +++ b/source/engine/2dphysics.c @@ -57,18 +57,9 @@ cpShape *phys2d_query_pos(cpVect pos) { return find; } -int sort_ids(int *a, int *b) +gameobject **clean_ids(gameobject **ids) { - if (*a == *b) return 0; - if (*a < *b) return -1; - return 1; -} - -gameobject *clean_ids(int *ids) -{ - qsort(ids, sizeof(*ids), arrlen(ids), sort_ids); - - int curid = -1; + gameobject *curid = NULL; for (int i = arrlen(ids)-1; i >= 0; i--) if (ids[i] == curid) arrdelswap(ids, i); @@ -78,7 +69,7 @@ gameobject *clean_ids(int *ids) return ids; } -void querylist(cpShape *shape, cpContactPointSet *points, int *ids) { arrput(ids,shape2go(shape)); } +void querylist(cpShape *shape, cpContactPointSet *points, gameobject **ids) { arrput(ids,shape2go(shape)); } typedef struct querybox { cpBB bb; @@ -184,7 +175,7 @@ void init_phys2dshape(struct phys2d_shape *shape, gameobject *go, void *data) { shape->go = go; shape->data = data; go_shape_apply(go->body, shape->shape, go); - cpShapeSetCollisionType(shape->shape, go); + cpShapeSetCollisionType(shape->shape, (int)go); cpShapeSetUserData(shape->shape, shape); } @@ -566,7 +557,7 @@ void duk_call_phys_cb(HMM_Vec2 norm, struct callee c, gameobject *hit, cpArbiter JSValue obj = JS_NewObject(js); JS_SetPropertyStr(js, obj, "normal", vec2js(norm)); - JS_SetPropertyStr(js, obj, "hit", JS_NewInt32(js, hit)); + JS_SetPropertyStr(js, obj, "hit", hit->ref); JS_SetPropertyStr(js, obj, "sensor", JS_NewBool(js, cpShapeGetSensor(shape2))); HMM_Vec2 srfv; srfv.cp = cpArbiterGetSurfaceVelocity(arb); @@ -574,7 +565,6 @@ void duk_call_phys_cb(HMM_Vec2 norm, struct callee c, gameobject *hit, cpArbiter // srfv.cp = cpArbiterGetPointA(arb,0); // JS_SetPropertyStr(js, obj, "pos", vec2js(srfv)); // JS_SetPropertyStr(js,obj,"depth", num2js(cpArbiterGetDepth(arb,0))); - JS_SetPropertyStr(js, obj, "id", JS_NewInt32(js,hit)); JS_SetPropertyStr(js,obj,"obj", JS_DupValue(js,hit->ref)); struct postphys_cb cb; @@ -631,14 +621,14 @@ static cpBool script_phys_cb_separate(cpArbiter *arb, cpSpace *space, void *data } void phys2d_rm_go_handlers(gameobject *go) { - cpCollisionHandler *handler = cpSpaceAddWildcardHandler(space, go); + cpCollisionHandler *handler = cpSpaceAddWildcardHandler(space, (int)go); handler->userData = NULL; handler->beginFunc = NULL; handler->separateFunc = NULL; } void phys2d_setup_handlers(gameobject *go) { - cpCollisionHandler *handler = cpSpaceAddWildcardHandler(space, go); + cpCollisionHandler *handler = cpSpaceAddWildcardHandler(space, (int)go); handler->userData = go; handler->beginFunc = script_phys_cb_begin; handler->separateFunc = script_phys_cb_separate; diff --git a/source/engine/3d/model.c b/source/engine/3d/model.c index 31c3b5a..bda9919 100644 --- a/source/engine/3d/model.c +++ b/source/engine/3d/model.c @@ -378,7 +378,7 @@ void draw_model(struct model *model, HMM_Mat4 amodel) { } } -struct drawmodel *make_drawmodel(int go) +struct drawmodel *make_drawmodel(gameobject *go) { struct drawmodel *dm = malloc(sizeof(struct drawmodel)); dm->model = NULL; diff --git a/source/engine/3d/model.h b/source/engine/3d/model.h index 1eaa605..ecd0310 100644 --- a/source/engine/3d/model.h +++ b/source/engine/3d/model.h @@ -55,7 +55,7 @@ void model_init(); void draw_model(struct model *model, HMM_Mat4 amodel); void draw_models(struct model *model, struct shader *shader); -struct drawmodel *make_drawmodel(int go); +struct drawmodel *make_drawmodel(gameobject *go); void draw_drawmodel(struct drawmodel *dm); void free_drawmodel(struct drawmodel *dm); diff --git a/source/engine/gameobject.c b/source/engine/gameobject.c index 461b727..4a1187a 100644 --- a/source/engine/gameobject.c +++ b/source/engine/gameobject.c @@ -88,10 +88,10 @@ unsigned int editor_cat = 1<<31; void go_shape_apply(cpBody *body, cpShape *shape, gameobject *go) { cpShapeSetFriction(shape, go->f); cpShapeSetElasticity(shape, go->e); - cpShapeSetCollisionType(shape, go); + cpShapeSetCollisionType(shape, (int)go); cpShapeFilter filter; - filter.group = go; + filter.group = (int)go; filter.categories = 1<layer | editor_cat; // filter.mask = CP_ALL_CATEGORIES; filter.mask = category_masks[go->layer] | editor_cat; diff --git a/source/engine/jsffi.c b/source/engine/jsffi.c index 223f081..04a086a 100644 --- a/source/engine/jsffi.c +++ b/source/engine/jsffi.c @@ -74,23 +74,6 @@ void js_setprop_str(JSValue obj, const char *prop, JSValue v) JS_SetPropertyStr(js, obj, prop, v); } -JSValue unix2time(time_t unix) -{ - struct tm *t = localtime(&unix); - JSValue o = JS_NewObject(js); - js_setprop_str(o, "sec", JS_NewFloat64(js, t->tm_sec)); - js_setprop_str(o, "min", JS_NewFloat64(js, t->tm_min)); - js_setprop_str(o, "hour", JS_NewFloat64(js, t->tm_hour)); - js_setprop_str(o, "day", JS_NewFloat64(js, t->tm_mday)); - js_setprop_str(o, "mon", JS_NewFloat64(js, t->tm_mon)); - js_setprop_str(o, "year", JS_NewFloat64(js, t->tm_year+1900)); - js_setprop_str(o, "weekday", JS_NewFloat64(js, t->tm_wday)); - js_setprop_str(o, "yday", JS_NewFloat64(js, t->tm_yday)); - js_setprop_str(o, "dst", JS_NewBool(js, t->tm_isdst)); - js_setprop_str(o, "unix", JS_NewFloat64(js, unix)); - return o; -} - JSValue jstzone() { time_t t = time(NULL); @@ -524,7 +507,7 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) break; case 2: - gameobject_free(js2int(argv[1])); +// gameobject_free(js2gameobject(argv[1])); break; case 3: @@ -1344,7 +1327,6 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) dag_set(js2gameobject(argv[1]), js2gameobject(argv[2])); break; case 209: - ret = unix2time(js2number(argv[1])); break; case 210: ret = jscurtime(); @@ -1495,7 +1477,7 @@ JSValue duk_sys_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *ar return JS_NewBool(js, sim_paused()); case 7: - return JS_NewInt64(js, MakeGameobject()); + return gameobject2js(MakeGameobject()); case 8: return JS_NewInt64(js, frame_fps()); diff --git a/source/engine/sound/sound.c b/source/engine/sound/sound.c index 378de1b..d859554 100644 --- a/source/engine/sound/sound.c +++ b/source/engine/sound/sound.c @@ -270,7 +270,7 @@ void free_source(struct sound *s) free(s); } -static long *src_cb(struct sound *s, float **data) +static long src_cb(struct sound *s, float **data) { long needed = BUF_FRAMES/s->timescale; *data = s->data->data+s->frame; diff --git a/source/engine/sprite.c b/source/engine/sprite.c index a3844e5..74d2891 100644 --- a/source/engine/sprite.c +++ b/source/engine/sprite.c @@ -52,7 +52,7 @@ struct slice9_vert { struct rgba color; }; -int make_sprite(int go) { +int make_sprite(gameobject *go) { struct sprite sprite = { .t = t2d_unit, .color = color_white, @@ -70,7 +70,7 @@ int make_sprite(int go) { void sprite_delete(int id) { struct sprite *sp = id2sprite(id); - sp->go = -1; + sp->go = NULL; sp->enabled = 0; freelist_kill(sprites,id); } diff --git a/source/engine/sprite.h b/source/engine/sprite.h index aeb894b..8412ccd 100644 --- a/source/engine/sprite.h +++ b/source/engine/sprite.h @@ -21,7 +21,7 @@ struct sprite { int next; }; -int make_sprite(int go); +int make_sprite(gameobject *go); struct sprite *id2sprite(int id); void sprite_delete(int id); void sprite_enabled(int id, int e); diff --git a/source/engine/transform.c b/source/engine/transform.c index 1a15ed4..e6fa00e 100644 --- a/source/engine/transform.c +++ b/source/engine/transform.c @@ -14,7 +14,7 @@ HMM_Vec3 trans_down(const transform3d *trans) { return HMM_QVRot(vDOWN, trans->r HMM_Vec3 trans_right(const transform3d *trans) { return HMM_QVRot(vRIGHT, trans->rotation); } HMM_Vec3 trans_left(const transform3d *trans) { return HMM_QVRot(vLEFT, trans->rotation); } -HMM_Vec2 mat_t_pos(HMM_Mat3 m, HMM_Vec2 pos) { return HMM_MulM3V3(m, (HMM_Vec3){pos.x, pos.y, 0}).XY; } +HMM_Vec2 mat_t_pos(HMM_Mat3 m, HMM_Vec2 pos) { return HMM_MulM3V3(m, (HMM_Vec3){pos.x, pos.y, 0}).xy; } HMM_Vec2 mat_t_dir(HMM_Mat3 m, HMM_Vec2 dir) {