From 6e794848a4b11092ae4d1c42f6fb377ad7a0b945 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Tue, 17 Jan 2023 21:09:14 +0000 Subject: [PATCH] Collider commands --- source/engine/2dphysics.c | 18 +++++++++--------- source/engine/2dphysics.h | 6 ++---- source/engine/ffi.c | 26 ++++++++++---------------- source/engine/sound/mix.c | 10 +++++++++- source/engine/sound/mix.h | 3 ++- 5 files changed, 32 insertions(+), 31 deletions(-) diff --git a/source/engine/2dphysics.c b/source/engine/2dphysics.c index 694da47..15f6d5b 100644 --- a/source/engine/2dphysics.c +++ b/source/engine/2dphysics.c @@ -468,19 +468,19 @@ void phys2d_dbgdrawedge(struct phys2d_edge *edge) } } -cpShape *id2shape(int id) +void shape_enabled(struct phys2d_shape *shape, int enabled) { - return NULL; + YughInfo("Setting shape %p to enabled? %d.", shape, enabled); + if (enabled) + cpSpaceAddShape(space, shape->shape); + else + cpSpaceRemoveShape(space, shape->shape); } -void shape_enable(struct phys2d_shape *shape) +void shape_set_sensor(struct phys2d_shape *shape, int sensor) { - cpSpaceAddShape(space, shape->shape); -} - -void shape_disable(struct phys2d_shape *shape) -{ - cpSpaceRemoveShape(space, shape->shape); + YughInfo("Setting shape %p to sensor? %d.", shape, sensor); + cpShapeSetSensor(shape->shape, sensor); } void phys2d_reindex_body(cpBody *body) { diff --git a/source/engine/2dphysics.h b/source/engine/2dphysics.h index 2460cf7..d07494f 100644 --- a/source/engine/2dphysics.h +++ b/source/engine/2dphysics.h @@ -97,10 +97,8 @@ void phys2d_add_handler_type(int cmd, int go, struct callee c); void register_collide(void *sym); void phys2d_set_gravity(cpVect v); -void shape_enable(struct phys2d_shape *shape); -void shape_disable(struct phys2d_shape *shape); - -cpShape *id2shape(int id); +void shape_enabled(struct phys2d_shape *shape, int enabled); +void shape_set_sensor(struct phys2d_shape *shape, int sensor); struct color { unsigned char r; diff --git a/source/engine/ffi.c b/source/engine/ffi.c index 7ddc927..cb836b4 100644 --- a/source/engine/ffi.c +++ b/source/engine/ffi.c @@ -153,30 +153,23 @@ duk_ret_t duk_cmd(duk_context *duk) { break; case 18: - cpShapeSetSensor(id2shape(duk_to_int(duk, 1)), duk_to_boolean(duk, 2)); + shape_set_sensor(duk_to_pointer(duk, 1), duk_to_boolean(duk, 2)); break; case 19: - if (id2shape(duk_to_int(duk, 1))) - duk_push_boolean(duk, cpShapeGetSensor(id2shape(duk_to_int(duk, 1)))); - else - duk_push_undefined(duk); - - return 1; + mix_master_vol(duk_to_number(duk, 1)); + break; case 20: sprite_enabled(duk_to_int(duk, 1), duk_to_boolean(duk, 2)); break; case 21: - duk_push_boolean(duk, id2sprite(duk_to_int(duk, 1))->enabled); - return 1; + break; case 22: - return 0; - - case 23: - return 0; + shape_enabled(duk_to_pointer(duk, 1), duk_to_int(duk, 2)); + break; } return 0; @@ -432,7 +425,7 @@ duk_ret_t duk_make_box2d(duk_context *duk) { phys2d_applybox(box); - duk_push_pointer(duk, box); + duk_push_pointer(duk, &box->shape); return 1; } @@ -458,7 +451,9 @@ duk_ret_t duk_make_circle2d(duk_context *duk) { phys2d_applycircle(circle); - return 0; + duk_push_pointer(duk, &circle->shape); + + return 1; } duk_ret_t duk_anim(duk_context *duk) { @@ -517,7 +512,6 @@ void ffi_load() DUK_FUNC(make_sprite, 3); DUK_FUNC(make_anim2d, 4); DUK_FUNC(make_box2d, 3); - DUK_FUNC(box2d_cmd, DUK_VARARGS); DUK_FUNC(make_circle2d, 3); DUK_FUNC(cmd, DUK_VARARGS); DUK_FUNC(register, 3); diff --git a/source/engine/sound/mix.c b/source/engine/sound/mix.c index 31cd821..afa8bb1 100644 --- a/source/engine/sound/mix.c +++ b/source/engine/sound/mix.c @@ -14,6 +14,14 @@ short mastermix[BUF_FRAMES*CHANNELS]; static int initted = 0; +static float master_volume = 1.f; + +void mix_master_vol(float v) { + if (v < 0.f) v = 0.f; + if (v > 100.f) v = 100.f; + master_volume = v / 100.f; +} + void mixer_init() { for (int i = 0; i < 256; i++) { bus[i].next = i+1; @@ -68,7 +76,7 @@ void bus_fill_buffers(short *master, int n) { int nextbus = bus[curbus].next; /* Save this in case busses get changed during fill */ dsp_run(bus[curbus].in, bus[curbus].buf, BUF_FRAMES); for (int i = 0; i < BUF_FRAMES*CHANNELS; i++) - master[i] += bus[curbus].buf[i]; + master[i] += bus[curbus].buf[i] * master_volume; curbus = nextbus; } diff --git a/source/engine/sound/mix.h b/source/engine/sound/mix.h index e746341..7913a85 100644 --- a/source/engine/sound/mix.h +++ b/source/engine/sound/mix.h @@ -23,7 +23,8 @@ void mixer_init(); struct bus *first_free_bus(struct dsp_filter in); void bus_fill_buffers(short *master, int n); - +/* Set volume between 0 and 100% */ +void mix_master_vol(float v); void bus_free(struct bus *bus);