From 38da3627e741bda3fd365644553c68ccbc4f3166 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Fri, 27 Jan 2023 18:06:16 +0000 Subject: [PATCH] splines --- Makefile | 2 +- source/engine/2dphysics.c | 6 ++++ source/engine/ffi.c | 57 ++++++++++++++++++++++++++++++++++++++ source/engine/tinyspline.h | 2 +- 4 files changed, 65 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index fc33db01..c368c6e1 100755 --- a/Makefile +++ b/Makefile @@ -73,7 +73,7 @@ SEM = 0.0.1 COM != git rev-parse --short HEAD VER = $(SEM)-$(COM) -COMPILER_FLAGS = $(includeflag) $(QFLAGS) -MD $(WARNING_FLAGS) -DCP_USE_DOUBLES=0 -DVER=\"$(VER)\" -DINFO=\"$(INFO)\" -c $< -o $@ +COMPILER_FLAGS = $(includeflag) $(QFLAGS) -MD $(WARNING_FLAGS) -DCP_USE_DOUBLES=0 -DTINYSPLINE_FLOAT_PRECISION -DVER=\"$(VER)\" -DINFO=\"$(INFO)\" -c $< -o $@ LIBPATH = -L$(BIN) diff --git a/source/engine/2dphysics.c b/source/engine/2dphysics.c index e728139b..ff3516f6 100644 --- a/source/engine/2dphysics.c +++ b/source/engine/2dphysics.c @@ -4,6 +4,7 @@ #include #include "mathc.h" #include "nuke.h" +#include "debug.h" #include "debugdraw.h" #include "gameobject.h" @@ -12,6 +13,8 @@ #include "stb_ds.h" #include +#include "tinyspline.h" + #include "script.h" #include "log.h" @@ -106,6 +109,9 @@ void init_phys2dshape(struct phys2d_shape *shape, int go, void *data) cpShapeSetCollisionType(shape->shape, go); cpShapeSetUserData(shape->shape, shape); phys2d_shape_apply(shape); + + + } void phys2d_shape_del(struct phys2d_shape *shape) diff --git a/source/engine/ffi.c b/source/engine/ffi.c index 519ee6cd..c283f7f2 100644 --- a/source/engine/ffi.c +++ b/source/engine/ffi.c @@ -20,11 +20,13 @@ #include "sound.h" #include "music.h" #include "level.h" +#include "tinyspline.h" void duk_dump_stack(duk_context *duk) { duk_push_context_dump(duk); YughInfo("DUK STACK\n%s", duk_to_string(duk, -1)); + duk_pop(duk); } struct color duk2color(duk_context *duk, int p) @@ -47,9 +49,13 @@ cpVect duk2vec2(duk_context *duk, int p) { duk_get_prop_index(duk, p, 0); pos.x = duk_to_number(duk, -1); + duk_pop(duk); + duk_get_prop_index(duk, p, 1); pos.y = duk_to_number(duk, -1); + duk_pop(duk); + return pos; } @@ -126,6 +132,56 @@ duk_ret_t duk_win_make(duk_context *duk) { return 1; } +duk_ret_t duk_spline_cmd(duk_context *duk) +{ + tsBSpline spline; + + int n = duk_get_length(duk, 4); + int d = duk_to_int(duk, 2); + cpVect points[n*d]; + + ts_bspline_new(n, d, duk_to_int(duk, 1), duk_to_int(duk, 3), &spline, NULL); + + + + for (int i = 0; i < n; i++) { + duk_get_prop_index(duk, 4, i); + + points[i] = duk2vec2(duk, -1); + + duk_pop(duk); + } + + ts_bspline_set_control_points(&spline, points, NULL); + + + int nsamples = duk_to_int(duk, 5); + cpVect samples[nsamples]; + int rsamples; + ts_bspline_sample(&spline, nsamples, &samples, &rsamples, NULL); + + int arridx = duk_push_array(duk); + + duk_require_stack(duk, nsamples*3); + + for (int i = 0; i < nsamples; i++) { + int pidx = duk_push_array(duk); + + + duk_push_number(duk, samples[i].x); + duk_put_prop_index(duk, pidx, 0); + duk_push_number(duk, samples[i].y); + duk_put_prop_index(duk, pidx, 1); + duk_put_prop_index(duk, arridx, i); + } + + free(samples); + ts_bspline_free(&spline); + + +return 1; +} + duk_ret_t duk_cmd(duk_context *duk) { int cmd = duk_to_int(duk, 0); @@ -730,6 +786,7 @@ void ffi_load() DUK_FUNC(make_sprite, 3); DUK_FUNC(make_anim2d, 3); + DUK_FUNC(spline_cmd, 6); DUK_FUNC(make_box2d, 3); DUK_FUNC(cmd_box2d, DUK_VARARGS); DUK_FUNC(make_circle2d, 3); diff --git a/source/engine/tinyspline.h b/source/engine/tinyspline.h index 4452b340..44c94ea2 100644 --- a/source/engine/tinyspline.h +++ b/source/engine/tinyspline.h @@ -21,7 +21,7 @@ #elif defined(SWIG) #define TS_DEPRECATED #else -#warning "WARNING: TS_DEPRECATED is not supported by the compiler" +#warning "WARNING: TS_DEPRECATED is not supported by compiler" #define TS_DEPRECATED #endif /*! @} */