From d6797a7f2457ebc857bcac0a9b8c0446852adae3 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Fri, 13 Jan 2023 19:07:44 +0000 Subject: [PATCH] File reloading start --- source/engine/ffi.c | 38 +++++++++++++++++++++++++++----------- source/engine/gameobject.c | 18 ++++-------------- source/engine/gameobject.h | 2 +- source/engine/script.c | 13 ++++++++++++- source/engine/script.h | 1 + 5 files changed, 45 insertions(+), 27 deletions(-) diff --git a/source/engine/ffi.c b/source/engine/ffi.c index 9fa49fa..d0d8dce 100644 --- a/source/engine/ffi.c +++ b/source/engine/ffi.c @@ -60,7 +60,8 @@ duk_ret_t duk_cmd(duk_context *duk) { switch(cmd) { case 0: - script_dofile(duk_to_string(duk, 1)); + duk_push_int(duk, script_dofile(duk_to_string(duk, 1))); + return 1; break; case 1: @@ -104,7 +105,12 @@ duk_ret_t duk_cmd(duk_context *duk) { break; case 11: + duk_push_int(duk, file_mod_secs(duk_to_string(duk, 1))); + return 1; + break; + case 12: + anim2d_delete(duk_to_int(duk, 1)); break; } @@ -232,9 +238,11 @@ duk_ret_t duk_set_body(duk_context *duk) { int id = duk_to_int(duk, 1); struct gameobject *go = get_gameobject_from_id(id); + /* TODO: Possible that reindexing shapes only needs done for static shapes? */ switch (cmd) { case 0: gameobject_setangle(go, duk_to_number(duk, 2)); + cpSpaceReindexShapesForBody(space, go->body); break; case 1: @@ -243,6 +251,7 @@ duk_ret_t duk_set_body(duk_context *duk) { case 2: cpBodySetPosition(go->body, duk2vec2(duk, 2)); + cpSpaceReindexShapesForBody(space, go->body); break; case 3: @@ -265,6 +274,14 @@ duk_ret_t duk_set_body(duk_context *duk) { cpBodySetMass(go->body, duk_to_number(duk, 2)); break; + case 8: + cpBodySetAngularVelocity(go->body, duk_to_number(duk, 2)); + break; + + case 9: + cpBodySetVelocity(go->body, duk2vec2(duk, 2)); + break; + } return 0; @@ -305,16 +322,6 @@ duk_ret_t duk_q_body(duk_context *duk) { return 0; } -duk_ret_t duk_sprite(duk_context *duk) { - int cmd = duk_to_int(duk, 0); - int id = duk_to_int(duk, 1); - - switch (cmd) { - case 0: break; - - } -} - duk_ret_t duk_make_sprite(duk_context *duk) { int go = duk_to_int(duk, 0); const char *path = duk_to_string(duk, 1); @@ -330,6 +337,14 @@ duk_ret_t duk_make_sprite(duk_context *duk) { return 1; } +duk_ret_t duk_make_anim2d(duk_context *duk) { + int go = duk_to_int(duk, 0); + const char *path = duk_to_string(duk, 1); + cpVect pos = duk2vec2(duk, 2); + + +} + duk_ret_t duk_make_box2d(duk_context *duk) { int go = duk_to_int(duk, 0); cpVect size = duk2vec2(duk, 1); @@ -417,6 +432,7 @@ void ffi_load() DUK_FUNC(win_make, 3); DUK_FUNC(make_sprite, 3); + DUK_FUNC(make_anim2d, 3); DUK_FUNC(make_box2d, 3); DUK_FUNC(make_circle2d, 3); DUK_FUNC(cmd, 2); diff --git a/source/engine/gameobject.c b/source/engine/gameobject.c index dc4f1fe..1689da3 100644 --- a/source/engine/gameobject.c +++ b/source/engine/gameobject.c @@ -91,6 +91,7 @@ void rm_body_shapes(cpBody *body, cpShape *shape, void *data) { struct phys2d_shape *s = cpShapeGetUserData(shape); free(s->data); cpSpaceRemoveShape(space, shape); + cpShapeFree(shape); } /* Really more of a "mark for deletion" ... */ @@ -101,31 +102,20 @@ void gameobject_delete(int id) } void gameobject_clean(int id) { - if (id < 0) { - YughError("Tried to clean ID %d.", id); - return; - } struct gameobject *go = id2go(id); cpBodyEachShape(go->body, rm_body_shapes, NULL); cpSpaceRemoveBody(space, go->body); + cpBodyFree(go->body); go->body = NULL; } -static int last_cleaned = -1; - void gameobjects_cleanup() { - if (first < 0) { - last_cleaned = first; - return; - } - int clean = first; - while (clean != last_cleaned) { + + while (clean > 0 && id2go(clean)->body) { gameobject_clean(clean); clean = id2go(clean)->next; } - - last_cleaned = first; } void gameobject_save(struct gameobject *go, FILE * file) diff --git a/source/engine/gameobject.h b/source/engine/gameobject.h index 7e306c1..5ff8264 100644 --- a/source/engine/gameobject.h +++ b/source/engine/gameobject.h @@ -32,7 +32,7 @@ struct gameobject { float e; /* elasticity */ int flipx; /* 1 or -1 */ int flipy; - cpBody *body; + cpBody *body; /* NULL if this object is dead */ int id; struct phys_cbs cbs; }; diff --git a/source/engine/script.c b/source/engine/script.c index 804a797..c4c9cf8 100644 --- a/source/engine/script.c +++ b/source/engine/script.c @@ -10,6 +10,10 @@ #include "stb_ds.h" +#include "time.h" +#include "sys/stat.h" +#include "sys/types.h" + duk_context *duk = NULL; duk_idx_t vect2duk(cpVect v) { @@ -47,6 +51,12 @@ void script_run(const char *script) { duk_eval_string(duk, script); } +time_t file_mod_secs(const char *file) { + struct stat attr; + stat(file, &attr); + return attr.st_mtime; +} + int script_dofile(const char *file) { const char *script = slurp_text(file); if (!script) { @@ -62,7 +72,8 @@ int script_dofile(const char *file) { duk_pop(duk); - return 0; + + return file_mod_secs(file); } /* Call the "update" function in the master game script */ diff --git a/source/engine/script.h b/source/engine/script.h index 411e646..4a444c7 100644 --- a/source/engine/script.h +++ b/source/engine/script.h @@ -23,6 +23,7 @@ void script_call_sym(void *sym); void script_call_sym_args(void *sym, void *args); int script_has_sym(void *sym); void script_eval_w_env(const char *s, void *env); +time_t file_mod_secs(const char *file); void register_update(struct callee c); void call_updates(double dt);