diff --git a/source/engine/2dphysics.c b/source/engine/2dphysics.c index 9b04785..10459f4 100644 --- a/source/engine/2dphysics.c +++ b/source/engine/2dphysics.c @@ -19,9 +19,14 @@ float phys2d_gravity = -50.f; void phys2d_init() { space = cpSpaceNew(); + phys2d_set_gravity(0, phys2d_gravity); cpSpaceSetGravity(space, cpv(0, phys2d_gravity)); } +void phys2d_set_gravity(float x, float y) { + cpSpaceSetGravity(space, cpv(x, y)); +} + void phys2d_update(float deltaT) { cpSpaceStep(space, deltaT); @@ -29,7 +34,7 @@ void phys2d_update(float deltaT) void phys2d_apply() { - cpSpaceSetGravity(space, cpv(0, phys2d_gravity)); + phys2d_set_gravity(0, phys2d_gravity); } void phys2d_shape_apply(struct phys2d_shape *shape) @@ -42,7 +47,6 @@ void init_phys2dshape(struct phys2d_shape *shape, struct gameobject *go) { shape->go = go; cpShapeSetCollisionType(shape->shape, go); - YughInfo("Added shape type %d", go); phys2d_shape_apply(shape); } @@ -58,7 +62,6 @@ struct phys2d_circle *Make2DCircle(struct gameobject *go) new->radius = 10.f; new->offset[0] = 0.f; new->offset[1] = 0.f; - phys2d_circleinit(new, go); return new; } @@ -100,7 +103,7 @@ struct phys2d_segment *Make2DSegment(struct gameobject *go) new->a[1] = 0.f; new->b[0] = 0.f; new->b[1] = 0.f; - phys2d_seginit(new, go); + return new; } @@ -134,8 +137,6 @@ struct phys2d_box *Make2DBox(struct gameobject *go) new->offset[0] = 0.f; new->offset[1] = 0.f; - phys2d_boxinit(new, go); - return new; } @@ -169,8 +170,6 @@ struct phys2d_poly *Make2DPoly(struct gameobject *go) new->points = NULL; new->radius = 0.f; - phys2d_polyinit(new, go); - return new; } @@ -220,8 +219,6 @@ struct phys2d_edge *Make2DEdge(struct gameobject *go) new->thickness = 0.f; new->shapes = malloc(sizeof(cpShape *)); - phys2d_edgeinit(new, go); - return new; } @@ -458,8 +455,6 @@ static cpBool s7_phys_cb_begin(cpArbiter *arb, cpSpace *space, void *data) { struct gameobject *go = data; script_call_sym(go->cbs->begin); - YughInfo("Gameobject %p began collision.", data); - cpBody *body1; cpBody *body2; cpArbiterGetBodies(arb, &body1, &body2); @@ -468,8 +463,6 @@ static cpBool s7_phys_cb_begin(cpArbiter *arb, cpSpace *space, void *data) { cpShape *shape2; cpArbiterGetShapes(arb, &shape1, &shape2); - YughInfo("Body %p began collision with body %p.", body1, body2); - YughInfo("Shape %p began collision with shape %p.", shape1, shape2); return 1; } @@ -493,7 +486,6 @@ void phys2d_add_handler_type(int cmd, struct gameobject *go, s7_pointer cb) { go->cbs = malloc(sizeof(*go->cbs)); handler->userData = go; - YughInfo("Making phys handler %d for type %d", cmd, go); switch (cmd) { case 0: diff --git a/source/engine/2dphysics.h b/source/engine/2dphysics.h index e1db0c3..184a89a 100644 --- a/source/engine/2dphysics.h +++ b/source/engine/2dphysics.h @@ -101,6 +101,7 @@ void phys2d_update(float deltaT); void phys2d_apply(); void phys2d_add_handler_type(int cmd, struct gameobject *go, s7_pointer cb); +void phys2d_set_gravity(float x, float y); void shape_gui(struct phys2d_shape *shape); diff --git a/source/engine/editor/editorstate.h b/source/engine/editor/editorstate.h index 261d504..bb23f7c 100644 --- a/source/engine/editor/editorstate.h +++ b/source/engine/editor/editorstate.h @@ -7,4 +7,6 @@ extern void (*asset_command)(char *asset); void print_file(char *file); + + #endif diff --git a/source/engine/gameobject.c b/source/engine/gameobject.c index 36b4686..60083ac 100644 --- a/source/engine/gameobject.c +++ b/source/engine/gameobject.c @@ -145,6 +145,7 @@ int gameobject_makefromprefab(char *path) void gameobject_init(struct gameobject *go, FILE * fprefab) { go->body = cpSpaceAddBody(space, cpBodyNew(go->mass, 1.f)); + cpBodySetType(go->body, go->bodytype); int comp_n; fread(&comp_n, sizeof(int), 1, fprefab); @@ -165,6 +166,8 @@ void gameobject_init(struct gameobject *go, FILE * fprefab) newc->init(newc->data, go); } + + } void gameobject_saveprefab(struct gameobject *go) diff --git a/source/engine/mrbffi.c b/source/engine/mrbffi.c index a8f7dfd..7a69042 100644 --- a/source/engine/mrbffi.c +++ b/source/engine/mrbffi.c @@ -12,6 +12,9 @@ #include "input.h" #include "gameobject.h" #include "openglrender.h" +#include "2dphysics.h" + +#include "yugine.h" #include "s7.h" @@ -176,6 +179,29 @@ s7_pointer s7_sys_cmd(s7_scheme *sc, s7_pointer args) { case 0: quit(); break; + + case 1: + sim_start(); + break; + + case 2: + sim_stop(); + break; + + case 3: + sim_pause(); + break; + + case 4: + sim_step(); + break; + + case 5: + return s7_make_boolean(sc, sim_playing()); + + case 6: + return s7_make_boolean(sc, sim_paused()); + } return args; @@ -291,6 +317,26 @@ s7_pointer s7_phys_cmd(s7_scheme *sc, s7_pointer args) { phys2d_add_handler_type(cmd, get_gameobject_from_id(go), env); } +/* Query physics bodies */ +s7_pointer s7_phys_q(s7_scheme *sc, s7_pointer args) { + struct gameobject * go = get_gameobject_from_id(s7_integer(s7_car(args))); + int q = s7_integer(s7_cadr(args)); + + switch(q) { + case 0: + return s7_make_integer(sc, cpBodyGetType(go->body)); + + } +} + +s7_pointer s7_phys_set(s7_scheme *sc, s7_pointer args) { + int cmd = s7_integer(s7_car(args)); + double x = s7_real(s7_cadr(args)); + double y = s7_real(s7_caddr(args)); + + phys2d_set_gravity(x, y); +} + #define S7_FUNC(NAME, ARGS) s7_define_function(s7, #NAME, s7_ ##NAME, ARGS, 0, 0, "") void ffi_load() { @@ -314,5 +360,7 @@ void ffi_load() { S7_FUNC(set_body, 3); S7_FUNC(set_body_pos, 4); S7_FUNC(phys_cmd, 3); + S7_FUNC(phys_q, 2); + S7_FUNC(phys_set, 3); } diff --git a/source/engine/yugine.c b/source/engine/yugine.c index 78998f2..6be8b25 100644 --- a/source/engine/yugine.c +++ b/source/engine/yugine.c @@ -10,6 +10,10 @@ #include #include +#include "editorstate.h" +#include "yugine.h" +#include "2dphysics.h" + #ifdef __linux__ #include @@ -31,6 +35,8 @@ double physMS = 1/120.f; double updateMS = 1/60.f; static int ed = 1; +static int sim_play = 0; +static double lastTick; void seghandle(int sig) { #ifdef __linux__ @@ -147,9 +153,7 @@ int main(int argc, char **args) { } openglInit(); - - double lastTick; - + sim_stop(); while (!want_quit()) { double elapsed = glfwGetTime() - lastTick; deltaT = elapsed; @@ -158,18 +162,17 @@ int main(int argc, char **args) { timer_update(lastTick); - call_updates(); - - renderlag += elapsed; - physlag += elapsed; - - if (physlag >= physMS) { - physlag -= physMS; - phys2d_update(physMS); - call_physics(); - } - + if (sim_play) { + physlag += elapsed; + call_updates(); + if (physlag >= physMS) { + physlag -= physMS; + phys2d_update(physMS); + call_physics(); + } + } + renderlag += elapsed; if (renderlag >= renderMS) { renderlag -= renderMS; window_renderall(); @@ -183,3 +186,29 @@ int main(int argc, char **args) { return 0; } + + + + +int sim_playing() { return sim_play; } +void sim_start() { + /* Save starting state of everything */ + sim_play = 1; +} + +void sim_pause() { + sim_play = 0; +} + +int sim_paused() { + return sim_play; +} + +void sim_stop() { + /* Revert starting state of everything from sim_start */ + sim_play = 0; +} + +void sim_step() { + +} \ No newline at end of file diff --git a/source/engine/yugine.h b/source/engine/yugine.h new file mode 100644 index 0000000..cf62ea2 --- /dev/null +++ b/source/engine/yugine.h @@ -0,0 +1,12 @@ +#ifndef YUGINE_H +#define YUGINE_H + +int sim_playing(); +int sim_paused(); +void sim_start(); +void sim_pause(); +void sim_stop(); +void sim_step(); + + +#endif \ No newline at end of file diff --git a/source/scripts/engine.scm b/source/scripts/engine.scm index 26545f8..52b64ca 100644 --- a/source/scripts/engine.scm +++ b/source/scripts/engine.scm @@ -90,8 +90,18 @@ (define (body_pos! body x y) (set_body_pos body 0 x y)) (define (body_move! body x y) (set_body_pos body 1 x y)) +(define (gravity! x y) (phys_set 0 x y)) + (define (b2i val) (if (eq? val #f) 0 1)) (define (dbg_draw_phys val) (settings_cmd 3 (b2i val))) +(define (sim_play) (sys_cmd 1)) +(define (sim_stop) (sys_cmd 2)) +(define (sim_pause) (sys_cmd 3)) +(define (sim_step) (sys_cmd 4)) +(define (sim_play?) (sys_cmd 5)) +(define (sim_pause?) (sys_cmd 6)) + +(define (bodytype? body) (phys_q body 0)) (define-macro (register-phys type body . expr) (let ((f (gensym))) @@ -101,8 +111,8 @@ ((collide) 0) ((separate) 3)) ,f)))) -(define (collide . expr) - (register-phys collide body expr)) +(define-macro (collide . expr) + `(register-phys collide body ,@expr)) (define-macro (separate . expr) `(register-phys separate body ,@expr))