File reloading start

This commit is contained in:
John Alanbrook 2023-01-13 19:07:44 +00:00
parent d2cbc61164
commit d6797a7f24
5 changed files with 45 additions and 27 deletions

View file

@ -60,7 +60,8 @@ duk_ret_t duk_cmd(duk_context *duk) {
switch(cmd) { switch(cmd) {
case 0: case 0:
script_dofile(duk_to_string(duk, 1)); duk_push_int(duk, script_dofile(duk_to_string(duk, 1)));
return 1;
break; break;
case 1: case 1:
@ -104,7 +105,12 @@ duk_ret_t duk_cmd(duk_context *duk) {
break; break;
case 11: 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; break;
} }
@ -232,9 +238,11 @@ duk_ret_t duk_set_body(duk_context *duk) {
int id = duk_to_int(duk, 1); int id = duk_to_int(duk, 1);
struct gameobject *go = get_gameobject_from_id(id); struct gameobject *go = get_gameobject_from_id(id);
/* TODO: Possible that reindexing shapes only needs done for static shapes? */
switch (cmd) { switch (cmd) {
case 0: case 0:
gameobject_setangle(go, duk_to_number(duk, 2)); gameobject_setangle(go, duk_to_number(duk, 2));
cpSpaceReindexShapesForBody(space, go->body);
break; break;
case 1: case 1:
@ -243,6 +251,7 @@ duk_ret_t duk_set_body(duk_context *duk) {
case 2: case 2:
cpBodySetPosition(go->body, duk2vec2(duk, 2)); cpBodySetPosition(go->body, duk2vec2(duk, 2));
cpSpaceReindexShapesForBody(space, go->body);
break; break;
case 3: case 3:
@ -265,6 +274,14 @@ duk_ret_t duk_set_body(duk_context *duk) {
cpBodySetMass(go->body, duk_to_number(duk, 2)); cpBodySetMass(go->body, duk_to_number(duk, 2));
break; break;
case 8:
cpBodySetAngularVelocity(go->body, duk_to_number(duk, 2));
break;
case 9:
cpBodySetVelocity(go->body, duk2vec2(duk, 2));
break;
} }
return 0; return 0;
@ -305,16 +322,6 @@ duk_ret_t duk_q_body(duk_context *duk) {
return 0; 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) { duk_ret_t duk_make_sprite(duk_context *duk) {
int go = duk_to_int(duk, 0); int go = duk_to_int(duk, 0);
const char *path = duk_to_string(duk, 1); const char *path = duk_to_string(duk, 1);
@ -330,6 +337,14 @@ duk_ret_t duk_make_sprite(duk_context *duk) {
return 1; 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) { duk_ret_t duk_make_box2d(duk_context *duk) {
int go = duk_to_int(duk, 0); int go = duk_to_int(duk, 0);
cpVect size = duk2vec2(duk, 1); cpVect size = duk2vec2(duk, 1);
@ -417,6 +432,7 @@ void ffi_load()
DUK_FUNC(win_make, 3); DUK_FUNC(win_make, 3);
DUK_FUNC(make_sprite, 3); DUK_FUNC(make_sprite, 3);
DUK_FUNC(make_anim2d, 3);
DUK_FUNC(make_box2d, 3); DUK_FUNC(make_box2d, 3);
DUK_FUNC(make_circle2d, 3); DUK_FUNC(make_circle2d, 3);
DUK_FUNC(cmd, 2); DUK_FUNC(cmd, 2);

View file

@ -91,6 +91,7 @@ void rm_body_shapes(cpBody *body, cpShape *shape, void *data) {
struct phys2d_shape *s = cpShapeGetUserData(shape); struct phys2d_shape *s = cpShapeGetUserData(shape);
free(s->data); free(s->data);
cpSpaceRemoveShape(space, shape); cpSpaceRemoveShape(space, shape);
cpShapeFree(shape);
} }
/* Really more of a "mark for deletion" ... */ /* Really more of a "mark for deletion" ... */
@ -101,31 +102,20 @@ void gameobject_delete(int id)
} }
void gameobject_clean(int id) { void gameobject_clean(int id) {
if (id < 0) {
YughError("Tried to clean ID %d.", id);
return;
}
struct gameobject *go = id2go(id); struct gameobject *go = id2go(id);
cpBodyEachShape(go->body, rm_body_shapes, NULL); cpBodyEachShape(go->body, rm_body_shapes, NULL);
cpSpaceRemoveBody(space, go->body); cpSpaceRemoveBody(space, go->body);
cpBodyFree(go->body);
go->body = NULL; go->body = NULL;
} }
static int last_cleaned = -1;
void gameobjects_cleanup() { void gameobjects_cleanup() {
if (first < 0) {
last_cleaned = first;
return;
}
int clean = first; int clean = first;
while (clean != last_cleaned) {
while (clean > 0 && id2go(clean)->body) {
gameobject_clean(clean); gameobject_clean(clean);
clean = id2go(clean)->next; clean = id2go(clean)->next;
} }
last_cleaned = first;
} }
void gameobject_save(struct gameobject *go, FILE * file) void gameobject_save(struct gameobject *go, FILE * file)

View file

@ -32,7 +32,7 @@ struct gameobject {
float e; /* elasticity */ float e; /* elasticity */
int flipx; /* 1 or -1 */ int flipx; /* 1 or -1 */
int flipy; int flipy;
cpBody *body; cpBody *body; /* NULL if this object is dead */
int id; int id;
struct phys_cbs cbs; struct phys_cbs cbs;
}; };

View file

@ -10,6 +10,10 @@
#include "stb_ds.h" #include "stb_ds.h"
#include "time.h"
#include "sys/stat.h"
#include "sys/types.h"
duk_context *duk = NULL; duk_context *duk = NULL;
duk_idx_t vect2duk(cpVect v) { duk_idx_t vect2duk(cpVect v) {
@ -47,6 +51,12 @@ void script_run(const char *script) {
duk_eval_string(duk, 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) { int script_dofile(const char *file) {
const char *script = slurp_text(file); const char *script = slurp_text(file);
if (!script) { if (!script) {
@ -62,7 +72,8 @@ int script_dofile(const char *file) {
duk_pop(duk); duk_pop(duk);
return 0;
return file_mod_secs(file);
} }
/* Call the "update" function in the master game script */ /* Call the "update" function in the master game script */

View file

@ -23,6 +23,7 @@ void script_call_sym(void *sym);
void script_call_sym_args(void *sym, void *args); void script_call_sym_args(void *sym, void *args);
int script_has_sym(void *sym); int script_has_sym(void *sym);
void script_eval_w_env(const char *s, void *env); 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 register_update(struct callee c);
void call_updates(double dt); void call_updates(double dt);