From f6161d04b0987ae4a54aef8678d3bd03f4d5d197 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Tue, 14 Nov 2023 15:20:09 +0000 Subject: [PATCH] transform work --- Makefile | 2 +- scripts/entity.js | 34 +- source/engine/2dphysics.c | 49 +- source/engine/2dphysics.h | 4 +- source/engine/HandmadeMath.h | 921 +------------------------------- source/engine/debug/debugdraw.c | 59 +- source/engine/debug/debugdraw.h | 18 +- source/engine/font.c | 4 +- source/engine/gameobject.c | 10 +- source/engine/gameobject.h | 4 +- source/engine/input.c | 6 +- source/engine/input.h | 6 +- source/engine/jsffi.c | 132 +++-- source/engine/jsffi.h | 4 +- source/engine/render.c | 2 +- source/engine/script.c | 2 +- source/engine/texture.c | 6 +- source/engine/texture.h | 3 +- 18 files changed, 177 insertions(+), 1089 deletions(-) diff --git a/Makefile b/Makefile index 7e2873f..9b9c7dc 100755 --- a/Makefile +++ b/Makefile @@ -61,7 +61,7 @@ else endif endif -CFLAGS += -DHAVE_CEIL -DCP_USE_CGTYPES=0 -DHAVE_FLOOR -DHAVE_FMOD -DHAVE_LRINT -DHAVE_LRINTF $(includeflag) -MD $(WARNING_FLAGS) -I. -DVER=\"$(VER)\" -DINFO=\"$(INFO)\" +CFLAGS += -DHAVE_CEIL -DCP_USE_CGTYPES=0 -DCP_USE_DOUBLES=0 -DTINYSPLINE_FLOAT_PRECISION -DHAVE_FLOOR -DHAVE_FMOD -DHAVE_LRINT -DHAVE_LRINTF $(includeflag) -MD $(WARNING_FLAGS) -I. -DVER=\"$(VER)\" -DINFO=\"$(INFO)\" PKGCMD = tar --directory $(BIN) --exclude="./*.a" --exclude="./obj" -czf $(DISTDIR)/$(DIST) . ZIP = .tar.gz diff --git a/scripts/entity.js b/scripts/entity.js index b199085..59bd9d7 100644 --- a/scripts/entity.js +++ b/scripts/entity.js @@ -55,7 +55,12 @@ var gameobject = { get max_angularvelocity() { return Math.rad2deg(cmd(155, this.body)); }, set torque(x) { if (!(x >= 0 && x <= Infinity)) return; cmd(153, this.body, x); }, gscale() { return cmd(103,this.body); }, - sgscale(x) { cmd(36,this.body,x) }, + sgscale(x) { + if (typeof x === 'number') + cmd(36,this.body, [x,x]); + else + cmd(36,this.body,x) + }, get scale() { if (!this.level) return this.gscale(); return this.gscale()/this.level.gscale(); @@ -64,7 +69,9 @@ var gameobject = { if (this.level) x *= this.level.gscale(); var pct = x/this.gscale(); - cmd(36, this.body, x); + + this.sgscale(x); +// cmd(36, this.body, x); this.objects?.forEach(function(obj) { obj.sgscale(obj.gscale()*pct); @@ -72,29 +79,6 @@ var gameobject = { }); }, - get flipx() { return cmd(104,this.body); }, - set flipx(x) { - cmd(55, this.body, x); - return; - this.objects.forEach(function(obj) { - obj.flipx = !obj.flipx; - var rp = obj.pos; - obj.pos = [-rp.x, rp.y].add(this.worldpos()); - obj.angle = -obj.angle; - },this); - }, - - get flipy() { return cmd(105,this.body); }, - set flipy(x) { - cmd(56, this.body, x); - return; - this.objects.forEach(function(obj) { - var rp = obj.pos; - obj.pos = [rp.x, -rp.y].add(this.worldpos()); - obj.angle = -obj.angle; - },this); - }, - set pos(x) { this.set_worldpos(Vector.rotate(x.scale(this.level.gscale()),Math.deg2rad(this.level.worldangle())).add(this.level.worldpos())); }, diff --git a/source/engine/2dphysics.c b/source/engine/2dphysics.c index b3a6603..00fb3bd 100644 --- a/source/engine/2dphysics.c +++ b/source/engine/2dphysics.c @@ -83,7 +83,7 @@ void querylistbodies(cpBody *body, void *data) { } } -int *phys2d_query_box_points(cpVect pos, cpVect wh, cpVect *points, int n) { +int *phys2d_query_box_points(HMM_Vec2 pos, HMM_Vec2 wh, HMM_Vec2 *points, int n) { cpShape *box = cpBoxShapeNew(NULL, wh.x, wh.y, 0.f); cpTransform T = {0}; T.a = 1; @@ -97,7 +97,7 @@ int *phys2d_query_box_points(cpVect pos, cpVect wh, cpVect *points, int n) { if (qhits) arrfree(qhits); for (int i = 0; i < n; i++) { - if (cpBBContainsVect(bbox, points[i])) + if (cpBBContainsVect(bbox, points[i].cp)) arrpush(qhits, i); } @@ -106,7 +106,7 @@ int *phys2d_query_box_points(cpVect pos, cpVect wh, cpVect *points, int n) { return qhits; } -int *phys2d_query_box(cpVect pos, cpVect wh) { +int *phys2d_query_box(HMM_Vec2 pos, HMM_Vec2 wh) { cpShape *box = cpBoxShapeNew(NULL, wh.x, wh.y, 0.f); cpTransform T = {0}; T.a = 1; @@ -220,24 +220,21 @@ void phys2d_circledel(struct phys2d_circle *c) { phys2d_shape_del(&c->shape); } -HMM_Vec2 bodytransformpoint(cpBody *body, HMM_Vec2 offset) { +HMM_Vec2 bodytransformpoint(cpBody *body, cpVect offset) { HMM_Vec2 pos; - pos.cp = cpBodyGetPosition(body); - float d = sqrt(pow(offset.X, 2.f) + pow(offset.Y, 2.f)); - float a = atan2(offset.Y, offset.X) + cpBodyGetAngle(body); - pos.X += d * cos(a); - pos.Y += d * sin(a); - return pos; + pos.cp = offset; + struct gameobject *go = id2go(body2id(body)); + return go2world(go, pos); } void phys2d_dbgdrawcpcirc(cpCircleShape *c) { - HMM_Vec2 pos = bodytransformpoint(cpShapeGetBody(c), (HMM_Vec2)cpCircleShapeGetOffset(c)); + HMM_Vec2 pos = bodytransformpoint(cpShapeGetBody(c), cpCircleShapeGetOffset(c)); float radius = cpCircleShapeGetRadius(c); struct rgba color = shape_color(c); float seglen = cpShapeGetSensor(c) ? 5 : -1; - draw_circle(pos.cp, radius, 1, color, seglen); + draw_circle(pos, radius, 1, color, seglen); color.a = col_alpha; - draw_circle(pos.cp,radius,radius,color,-1); + draw_circle(pos,radius,radius,color,-1); } void phys2d_dbgdrawcircle(struct phys2d_circle *circle) { @@ -247,7 +244,7 @@ void phys2d_dbgdrawcircle(struct phys2d_circle *circle) { void phys2d_applycircle(struct phys2d_circle *circle) { struct gameobject *go = id2go(circle->shape.go); - float radius = circle->radius * HMM_LenSqrV2(go->scale.XY); + float radius = circle->radius * HMM_MAX(HMM_ABS(go->scale.X), HMM_ABS(go->scale.Y)); cpCircleShapeSetRadius(circle->shape.shape, radius); cpCircleShapeSetOffset(circle->shape.shape, HMM_MulV2(go->scale.XY, circle->offset).cp); @@ -304,9 +301,13 @@ void phys2d_applybox(struct phys2d_box *box) { void phys2d_dbgdrawbox(struct phys2d_box *box) { int n = cpPolyShapeGetCount(box->shape.shape); HMM_Vec2 points[n * 2]; + struct gameobject *go = shape2go(box->shape.shape); - for (int i = 0; i < n; i++) - points[i] = bodytransformpoint(cpShapeGetBody(box->shape.shape), cpPolyShapeGetVert(box->shape.shape, i)).cp; + for (int i = 0; i < n; i++) { + HMM_Vec2 p; + p.cp = cpPolyShapeGetVert(box->shape.shape, i); + points[i] = go2world(go, p); + } struct rgba c = shape_color(box->shape.shape); struct rgba cl = c; @@ -382,7 +383,7 @@ void phys2d_dbgdrawpoly(struct phys2d_poly *poly) { if (arrlen(poly->points) >= 3) { int n = cpPolyShapeGetCount(poly->shape.shape); - cpVect points[n]; + HMM_Vec2 points[n]; for (int i = 0; i < n; i++) points[i] = bodytransformpoint(cpShapeGetBody(poly->shape.shape), cpPolyShapeGetVert(poly->shape.shape, i)); @@ -513,7 +514,7 @@ void phys2d_dbgdrawedge(struct phys2d_edge *edge) { for (int i = 0; i < arrlen(edge->points); i++) { drawpoints[i] = goscale(go, edge->points[i]); - drawpoints[i] = bodytransformpoint(cpShapeGetBody(edge->shapes[0]), drawpoints[i]); + drawpoints[i] = bodytransformpoint(cpShapeGetBody(edge->shapes[0]), drawpoints[i].cp); } float seglen = cpShapeGetSensor(edge->shapes[0]) ? sensor_seg : 0; @@ -580,7 +581,7 @@ void flush_collide_cbs() { arrsetlen(begins,0); } -void duk_call_phys_cb(cpVect norm, struct callee c, int hit, cpArbiter *arb) { +void duk_call_phys_cb(HMM_Vec2 norm, struct callee c, int hit, cpArbiter *arb) { cpShape *shape1; cpShape *shape2; cpArbiterGetShapes(arb, &shape1, &shape2); @@ -589,8 +590,11 @@ void duk_call_phys_cb(cpVect norm, struct callee c, int hit, cpArbiter *arb) { JS_SetPropertyStr(js, obj, "normal", vec2js(norm)); JS_SetPropertyStr(js, obj, "hit", JS_NewInt32(js, hit)); JS_SetPropertyStr(js, obj, "sensor", JS_NewBool(js, cpShapeGetSensor(shape2))); - JS_SetPropertyStr(js, obj, "velocity", vec2js(cpArbiterGetSurfaceVelocity(arb))); - JS_SetPropertyStr(js, obj, "pos", vec2js(cpArbiterGetPointA(arb, 0))); + HMM_Vec2 srfv; + srfv.cp = cpArbiterGetSurfaceVelocity(arb); + JS_SetPropertyStr(js, obj, "velocity", vec2js(srfv)); + srfv.cp = cpArbiterGetPointA(arb,0); + JS_SetPropertyStr(js, obj, "pos", vec2js(srfv)); JS_SetPropertyStr(js,obj,"depth", num2js(cpArbiterGetDepth(arb,0))); JS_SetPropertyStr(js, obj, "id", JS_NewInt32(js,hit)); JS_SetPropertyStr(js,obj,"obj", JS_DupValue(js,id2go(hit)->ref)); @@ -619,7 +623,8 @@ static cpBool handle_collision(cpArbiter *arb, int type) { struct phys2d_shape *pshape1 = cpShapeGetUserData(shape1); struct phys2d_shape *pshape2 = cpShapeGetUserData(shape2); - cpVect norm1 = cpArbiterGetNormal(arb); + HMM_Vec2 norm1; + norm1.cp = cpArbiterGetNormal(arb); switch (type) { case CTYPE_BEGIN: diff --git a/source/engine/2dphysics.h b/source/engine/2dphysics.h index 5a61edf..84e359a 100644 --- a/source/engine/2dphysics.h +++ b/source/engine/2dphysics.h @@ -109,7 +109,7 @@ void phys2d_edge_set_enabled(struct phys2d_edge *edge, int enabled); void phys2d_init(); void phys2d_update(float deltaT); cpShape *phys2d_query_pos(cpVect pos); -int *phys2d_query_box(cpVect pos, cpVect wh); +int *phys2d_query_box(HMM_Vec2 pos, HMM_Vec2 wh); struct phys_cbs { struct callee begin; @@ -136,7 +136,7 @@ struct rgba shape_color_s(cpShape *shape); void shape_gui(struct phys2d_shape *shape); void phys2d_setup_handlers(int go); int *phys2d_query_shape(struct phys2d_shape *shape); -int *phys2d_query_box_points(cpVect pos, cpVect wh, cpVect *points, int n); +int *phys2d_query_box_points(HMM_Vec2 pos, HMM_Vec2 wh, HMM_Vec2 *points, int n); void flush_collide_cbs(); diff --git a/source/engine/HandmadeMath.h b/source/engine/HandmadeMath.h index d7b8f44..4fd8394 100644 --- a/source/engine/HandmadeMath.h +++ b/source/engine/HandmadeMath.h @@ -113,10 +113,6 @@ #endif /* not _MSC_VER */ #endif /* #ifndef HANDMADE_MATH_NO_SSE */ -#if (!defined(__cplusplus) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) -#define HANDMADE_MATH__USE_C11_GENERICS 1 -#endif - #ifdef HANDMADE_MATH__USE_SSE #include #endif @@ -125,18 +121,6 @@ #pragma warning(disable : 4201) #endif -#if defined(__GNUC__) || defined(__clang__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wfloat-equal" -#if (defined(__GNUC__) && (__GNUC__ == 4 && __GNUC_MINOR__ < 8)) || defined(__clang__) -#pragma GCC diagnostic ignored "-Wmissing-braces" -#endif -#ifdef __clang__ -#pragma GCC diagnostic ignored "-Wgnu-anonymous-struct" -#pragma GCC diagnostic ignored "-Wmissing-field-initializers" -#endif -#endif - #if defined(__GNUC__) || defined(__clang__) #define HMM_DEPRECATED(msg) __attribute__((deprecated(msg))) #elif defined(_MSC_VER) @@ -145,10 +129,6 @@ #define HMM_DEPRECATED(msg) #endif -#ifdef __cplusplus -extern "C" { -#endif - #if !defined(HANDMADE_MATH_USE_DEGREES) && !defined(HANDMADE_MATH_USE_TURNS) && !defined(HANDMADE_MATH_USE_RADIANS) #define HANDMADE_MATH_USE_RADIANS #endif @@ -215,6 +195,10 @@ typedef union HMM_Vec2 { float X, Y; }; + struct { + float x, y; + }; + struct { float U, V; @@ -234,14 +218,9 @@ typedef union HMM_Vec2 { cpVect cp; -#ifdef __cplusplus - inline float &operator[](const int &Index) { - return Elements[Index]; - } -#endif } HMM_Vec2; -const HMM_Vec2 v2zero = {0,0}; +static const HMM_Vec2 v2zero = {0,0}; typedef union HMM_Vec3 { struct @@ -285,14 +264,9 @@ typedef union HMM_Vec3 { float Elements[3]; -#ifdef __cplusplus - inline float &operator[](const int &Index) { - return Elements[Index]; - } -#endif } HMM_Vec3; -const HMM_Vec3 v3zero = {0,0,0}; +static const HMM_Vec3 v3zero = {0,0,0}; typedef union HMM_Vec4 { struct @@ -347,44 +321,24 @@ typedef union HMM_Vec4 { __m128 SSE; #endif -#ifdef __cplusplus - inline float &operator[](const int &Index) { - return Elements[Index]; - } -#endif } HMM_Vec4; typedef union HMM_Mat2 { float Elements[2][2]; HMM_Vec2 Columns[2]; -#ifdef __cplusplus - inline HMM_Vec2 &operator[](const int &Index) { - return Columns[Index]; - } -#endif } HMM_Mat2; typedef union HMM_Mat3 { float Elements[3][3]; HMM_Vec3 Columns[3]; -#ifdef __cplusplus - inline HMM_Vec3 &operator[](const int &Index) { - return Columns[Index]; - } -#endif } HMM_Mat3; typedef union HMM_Mat4 { float Elements[4][4]; HMM_Vec4 Columns[4]; -#ifdef __cplusplus - inline HMM_Vec4 &operator[](const int &Index) { - return Columns[Index]; - } -#endif } HMM_Mat4; typedef union HMM_Quat { @@ -822,6 +776,11 @@ static inline float HMM_DotV2(HMM_Vec2 Left, HMM_Vec2 Right) { return (Left.X * Right.X) + (Left.Y * Right.Y); } +static inline HMM_Vec2 HMM_ProjV2(HMM_Vec2 a, HMM_Vec2 b) +{ + return HMM_MulV2F(b, HMM_DotV2(a,b)/HMM_DotV2(b,b)); +} + static inline float HMM_DotV3(HMM_Vec3 Left, HMM_Vec3 Right) { return (Left.X * Right.X) + (Left.Y * Right.Y) + (Left.Z * Right.Z); } @@ -877,6 +836,10 @@ static inline float HMM_LenV2(HMM_Vec2 A) { return HMM_SqrtF(HMM_LenSqrV2(A)); } +static inline float HMM_DistV2(HMM_Vec2 a, HMM_Vec2 b) { + return HMM_LenV2(HMM_SubV2(a,b)); +} + static inline float HMM_LenV3(HMM_Vec3 A) { return HMM_SqrtF(HMM_LenSqrV3(A)); } @@ -886,6 +849,7 @@ static inline float HMM_LenV4(HMM_Vec4 A) { } static inline HMM_Vec2 HMM_NormV2(HMM_Vec2 A) { + // HMM_MulV2F(A, 1.0/HMM_LenV2(A)+FLOAT_MIN); return HMM_MulV2F(A, HMM_InvSqrtF(HMM_DotV2(A, A))); } @@ -2207,857 +2171,4 @@ static inline HMM_Quat HMM_QFromAxisAngle_LH(HMM_Vec3 Axis, float AngleOfRotatio return HMM_QFromAxisAngle_RH(Axis, -AngleOfRotation); } -#ifdef __cplusplus -} -#endif - -#ifdef __cplusplus - -static inline float HMM_Len(HMM_Vec2 A) { - return HMM_LenV2(A); -} - -static inline float HMM_Len(HMM_Vec3 A) { - return HMM_LenV3(A); -} - -static inline float HMM_Len(HMM_Vec4 A) { - return HMM_LenV4(A); -} - -static inline float HMM_LenSqr(HMM_Vec2 A) { - return HMM_LenSqrV2(A); -} - -static inline float HMM_LenSqr(HMM_Vec3 A) { - return HMM_LenSqrV3(A); -} - -static inline float HMM_LenSqr(HMM_Vec4 A) { - return HMM_LenSqrV4(A); -} - -static inline HMM_Vec2 HMM_Norm(HMM_Vec2 A) { - return HMM_NormV2(A); -} - -static inline HMM_Vec3 HMM_Norm(HMM_Vec3 A) { - return HMM_NormV3(A); -} - -static inline HMM_Vec4 HMM_Norm(HMM_Vec4 A) { - return HMM_NormV4(A); -} - -static inline HMM_Quat HMM_Norm(HMM_Quat A) { - return HMM_NormQ(A); -} - -static inline float HMM_Dot(HMM_Vec2 Left, HMM_Vec2 VecTwo) { - return HMM_DotV2(Left, VecTwo); -} - -static inline float HMM_Dot(HMM_Vec3 Left, HMM_Vec3 VecTwo) { - return HMM_DotV3(Left, VecTwo); -} - -static inline float HMM_Dot(HMM_Vec4 Left, HMM_Vec4 VecTwo) { - return HMM_DotV4(Left, VecTwo); -} - -static inline HMM_Vec2 HMM_Lerp(HMM_Vec2 Left, float Time, HMM_Vec2 Right) { - return HMM_LerpV2(Left, Time, Right); -} - -static inline HMM_Vec3 HMM_Lerp(HMM_Vec3 Left, float Time, HMM_Vec3 Right) { - return HMM_LerpV3(Left, Time, Right); -} - -static inline HMM_Vec4 HMM_Lerp(HMM_Vec4 Left, float Time, HMM_Vec4 Right) { - return HMM_LerpV4(Left, Time, Right); -} - -static inline HMM_Mat2 HMM_Transpose(HMM_Mat2 Matrix) { - return HMM_TransposeM2(Matrix); -} - -static inline HMM_Mat3 HMM_Transpose(HMM_Mat3 Matrix) { - return HMM_TransposeM3(Matrix); -} - -static inline HMM_Mat4 HMM_Transpose(HMM_Mat4 Matrix) { - return HMM_TransposeM4(Matrix); -} - -static inline float HMM_Determinant(HMM_Mat2 Matrix) { - return HMM_DeterminantM2(Matrix); -} - -static inline float HMM_Determinant(HMM_Mat3 Matrix) { - return HMM_DeterminantM3(Matrix); -} - -static inline float HMM_Determinant(HMM_Mat4 Matrix) { - return HMM_DeterminantM4(Matrix); -} - -static inline HMM_Mat2 HMM_InvGeneral(HMM_Mat2 Matrix) { - return HMM_InvGeneralM2(Matrix); -} - -static inline HMM_Mat3 HMM_InvGeneral(HMM_Mat3 Matrix) { - return HMM_InvGeneralM3(Matrix); -} - -static inline HMM_Mat4 HMM_InvGeneral(HMM_Mat4 Matrix) { - return HMM_InvGeneralM4(Matrix); -} - -static inline float HMM_Dot(HMM_Quat QuatOne, HMM_Quat QuatTwo) { - return HMM_DotQ(QuatOne, QuatTwo); -} - -static inline HMM_Vec2 HMM_Add(HMM_Vec2 Left, HMM_Vec2 Right) { - return HMM_AddV2(Left, Right); -} - -static inline HMM_Vec3 HMM_Add(HMM_Vec3 Left, HMM_Vec3 Right) { - return HMM_AddV3(Left, Right); -} - -static inline HMM_Vec4 HMM_Add(HMM_Vec4 Left, HMM_Vec4 Right) { - return HMM_AddV4(Left, Right); -} - -static inline HMM_Mat2 HMM_Add(HMM_Mat2 Left, HMM_Mat2 Right) { - return HMM_AddM2(Left, Right); -} - -static inline HMM_Mat3 HMM_Add(HMM_Mat3 Left, HMM_Mat3 Right) { - return HMM_AddM3(Left, Right); -} - -static inline HMM_Mat4 HMM_Add(HMM_Mat4 Left, HMM_Mat4 Right) { - return HMM_AddM4(Left, Right); -} - -static inline HMM_Quat HMM_Add(HMM_Quat Left, HMM_Quat Right) { - return HMM_AddQ(Left, Right); -} - -static inline HMM_Vec2 HMM_Sub(HMM_Vec2 Left, HMM_Vec2 Right) { - return HMM_SubV2(Left, Right); -} - -static inline HMM_Vec3 HMM_Sub(HMM_Vec3 Left, HMM_Vec3 Right) { - return HMM_SubV3(Left, Right); -} - -static inline HMM_Vec4 HMM_Sub(HMM_Vec4 Left, HMM_Vec4 Right) { - return HMM_SubV4(Left, Right); -} - -static inline HMM_Mat2 HMM_Sub(HMM_Mat2 Left, HMM_Mat2 Right) { - return HMM_SubM2(Left, Right); -} - -static inline HMM_Mat3 HMM_Sub(HMM_Mat3 Left, HMM_Mat3 Right) { - return HMM_SubM3(Left, Right); -} - -static inline HMM_Mat4 HMM_Sub(HMM_Mat4 Left, HMM_Mat4 Right) { - return HMM_SubM4(Left, Right); -} - -static inline HMM_Quat HMM_Sub(HMM_Quat Left, HMM_Quat Right) { - return HMM_SubQ(Left, Right); -} - -static inline HMM_Vec2 HMM_Mul(HMM_Vec2 Left, HMM_Vec2 Right) { - return HMM_MulV2(Left, Right); -} - -static inline HMM_Vec2 HMM_Mul(HMM_Vec2 Left, float Right) { - return HMM_MulV2F(Left, Right); -} - -static inline HMM_Vec3 HMM_Mul(HMM_Vec3 Left, HMM_Vec3 Right) { - return HMM_MulV3(Left, Right); -} - -static inline HMM_Vec3 HMM_Mul(HMM_Vec3 Left, float Right) { - return HMM_MulV3F(Left, Right); -} - -static inline HMM_Vec4 HMM_Mul(HMM_Vec4 Left, HMM_Vec4 Right) { - return HMM_MulV4(Left, Right); -} - -static inline HMM_Vec4 HMM_Mul(HMM_Vec4 Left, float Right) { - return HMM_MulV4F(Left, Right); -} - -static inline HMM_Mat2 HMM_Mul(HMM_Mat2 Left, HMM_Mat2 Right) { - return HMM_MulM2(Left, Right); -} - -static inline HMM_Mat3 HMM_Mul(HMM_Mat3 Left, HMM_Mat3 Right) { - return HMM_MulM3(Left, Right); -} - -static inline HMM_Mat4 HMM_Mul(HMM_Mat4 Left, HMM_Mat4 Right) { - return HMM_MulM4(Left, Right); -} - -static inline HMM_Mat2 HMM_Mul(HMM_Mat2 Left, float Right) { - return HMM_MulM2F(Left, Right); -} - -static inline HMM_Mat3 HMM_Mul(HMM_Mat3 Left, float Right) { - return HMM_MulM3F(Left, Right); -} - -static inline HMM_Mat4 HMM_Mul(HMM_Mat4 Left, float Right) { - return HMM_MulM4F(Left, Right); -} - -static inline HMM_Vec2 HMM_Mul(HMM_Mat2 Matrix, HMM_Vec2 Vector) { - return HMM_MulM2V2(Matrix, Vector); -} - -static inline HMM_Vec3 HMM_Mul(HMM_Mat3 Matrix, HMM_Vec3 Vector) { - return HMM_MulM3V3(Matrix, Vector); -} - -static inline HMM_Vec4 HMM_Mul(HMM_Mat4 Matrix, HMM_Vec4 Vector) { - return HMM_MulM4V4(Matrix, Vector); -} - -static inline HMM_Quat HMM_Mul(HMM_Quat Left, HMM_Quat Right) { - return HMM_MulQ(Left, Right); -} - -static inline HMM_Quat HMM_Mul(HMM_Quat Left, float Right) { - return HMM_MulQF(Left, Right); -} - -static inline HMM_Vec2 HMM_Div(HMM_Vec2 Left, HMM_Vec2 Right) { - return HMM_DivV2(Left, Right); -} - -static inline HMM_Vec2 HMM_Div(HMM_Vec2 Left, float Right) { - return HMM_DivV2F(Left, Right); -} - -static inline HMM_Vec3 HMM_Div(HMM_Vec3 Left, HMM_Vec3 Right) { - return HMM_DivV3(Left, Right); -} - -static inline HMM_Vec3 HMM_Div(HMM_Vec3 Left, float Right) { - return HMM_DivV3F(Left, Right); -} - -static inline HMM_Vec4 HMM_Div(HMM_Vec4 Left, HMM_Vec4 Right) { - return HMM_DivV4(Left, Right); -} - -static inline HMM_Vec4 HMM_Div(HMM_Vec4 Left, float Right) { - return HMM_DivV4F(Left, Right); -} - -static inline HMM_Mat2 HMM_Div(HMM_Mat2 Left, float Right) { - return HMM_DivM2F(Left, Right); -} - -static inline HMM_Mat3 HMM_Div(HMM_Mat3 Left, float Right) { - return HMM_DivM3F(Left, Right); -} - -static inline HMM_Mat4 HMM_Div(HMM_Mat4 Left, float Right) { - return HMM_DivM4F(Left, Right); -} - -static inline HMM_Quat HMM_Div(HMM_Quat Left, float Right) { - return HMM_DivQF(Left, Right); -} - -static inline HMM_Bool HMM_Eq(HMM_Vec2 Left, HMM_Vec2 Right) { - return HMM_EqV2(Left, Right); -} - -static inline HMM_Bool HMM_Eq(HMM_Vec3 Left, HMM_Vec3 Right) { - return HMM_EqV3(Left, Right); -} - -static inline HMM_Bool HMM_Eq(HMM_Vec4 Left, HMM_Vec4 Right) { - return HMM_EqV4(Left, Right); -} - -static inline HMM_Vec2 operator+(HMM_Vec2 Left, HMM_Vec2 Right) { - return HMM_AddV2(Left, Right); -} - -static inline HMM_Vec3 operator+(HMM_Vec3 Left, HMM_Vec3 Right) { - return HMM_AddV3(Left, Right); -} - -static inline HMM_Vec4 operator+(HMM_Vec4 Left, HMM_Vec4 Right) { - return HMM_AddV4(Left, Right); -} - -static inline HMM_Mat2 operator+(HMM_Mat2 Left, HMM_Mat2 Right) { - return HMM_AddM2(Left, Right); -} - -static inline HMM_Mat3 operator+(HMM_Mat3 Left, HMM_Mat3 Right) { - return HMM_AddM3(Left, Right); -} - -static inline HMM_Mat4 operator+(HMM_Mat4 Left, HMM_Mat4 Right) { - return HMM_AddM4(Left, Right); -} - -static inline HMM_Quat operator+(HMM_Quat Left, HMM_Quat Right) { - return HMM_AddQ(Left, Right); -} - -static inline HMM_Vec2 operator-(HMM_Vec2 Left, HMM_Vec2 Right) { - return HMM_SubV2(Left, Right); -} - -static inline HMM_Vec3 operator-(HMM_Vec3 Left, HMM_Vec3 Right) { - return HMM_SubV3(Left, Right); -} - -static inline HMM_Vec4 operator-(HMM_Vec4 Left, HMM_Vec4 Right) { - return HMM_SubV4(Left, Right); -} - -static inline HMM_Mat2 operator-(HMM_Mat2 Left, HMM_Mat2 Right) { - return HMM_SubM2(Left, Right); -} - -static inline HMM_Mat3 operator-(HMM_Mat3 Left, HMM_Mat3 Right) { - return HMM_SubM3(Left, Right); -} - -static inline HMM_Mat4 operator-(HMM_Mat4 Left, HMM_Mat4 Right) { - return HMM_SubM4(Left, Right); -} - -static inline HMM_Quat operator-(HMM_Quat Left, HMM_Quat Right) { - return HMM_SubQ(Left, Right); -} - -static inline HMM_Vec2 operator*(HMM_Vec2 Left, HMM_Vec2 Right) { - return HMM_MulV2(Left, Right); -} - -static inline HMM_Vec3 operator*(HMM_Vec3 Left, HMM_Vec3 Right) { - return HMM_MulV3(Left, Right); -} - -static inline HMM_Vec4 operator*(HMM_Vec4 Left, HMM_Vec4 Right) { - return HMM_MulV4(Left, Right); -} - -static inline HMM_Mat2 operator*(HMM_Mat2 Left, HMM_Mat2 Right) { - return HMM_MulM2(Left, Right); -} - -static inline HMM_Mat3 operator*(HMM_Mat3 Left, HMM_Mat3 Right) { - return HMM_MulM3(Left, Right); -} - -static inline HMM_Mat4 operator*(HMM_Mat4 Left, HMM_Mat4 Right) { - return HMM_MulM4(Left, Right); -} - -static inline HMM_Quat operator*(HMM_Quat Left, HMM_Quat Right) { - return HMM_MulQ(Left, Right); -} - -static inline HMM_Vec2 operator*(HMM_Vec2 Left, float Right) { - return HMM_MulV2F(Left, Right); -} - -static inline HMM_Vec3 operator*(HMM_Vec3 Left, float Right) { - return HMM_MulV3F(Left, Right); -} - -static inline HMM_Vec4 operator*(HMM_Vec4 Left, float Right) { - return HMM_MulV4F(Left, Right); -} - -static inline HMM_Mat2 operator*(HMM_Mat2 Left, float Right) { - return HMM_MulM2F(Left, Right); -} - -static inline HMM_Mat3 operator*(HMM_Mat3 Left, float Right) { - return HMM_MulM3F(Left, Right); -} - -static inline HMM_Mat4 operator*(HMM_Mat4 Left, float Right) { - return HMM_MulM4F(Left, Right); -} - -static inline HMM_Quat operator*(HMM_Quat Left, float Right) { - return HMM_MulQF(Left, Right); -} - -static inline HMM_Vec2 operator*(float Left, HMM_Vec2 Right) { - return HMM_MulV2F(Right, Left); -} - -static inline HMM_Vec3 operator*(float Left, HMM_Vec3 Right) { - return HMM_MulV3F(Right, Left); -} - -static inline HMM_Vec4 operator*(float Left, HMM_Vec4 Right) { - return HMM_MulV4F(Right, Left); -} - -static inline HMM_Mat2 operator*(float Left, HMM_Mat2 Right) { - return HMM_MulM2F(Right, Left); -} - -static inline HMM_Mat3 operator*(float Left, HMM_Mat3 Right) { - return HMM_MulM3F(Right, Left); -} - -static inline HMM_Mat4 operator*(float Left, HMM_Mat4 Right) { - return HMM_MulM4F(Right, Left); -} - -static inline HMM_Quat operator*(float Left, HMM_Quat Right) { - return HMM_MulQF(Right, Left); -} - -static inline HMM_Vec2 operator*(HMM_Mat2 Matrix, HMM_Vec2 Vector) { - return HMM_MulM2V2(Matrix, Vector); -} - -static inline HMM_Vec3 operator*(HMM_Mat3 Matrix, HMM_Vec3 Vector) { - return HMM_MulM3V3(Matrix, Vector); -} - -static inline HMM_Vec4 operator*(HMM_Mat4 Matrix, HMM_Vec4 Vector) { - return HMM_MulM4V4(Matrix, Vector); -} - -static inline HMM_Vec2 operator/(HMM_Vec2 Left, HMM_Vec2 Right) { - return HMM_DivV2(Left, Right); -} - -static inline HMM_Vec3 operator/(HMM_Vec3 Left, HMM_Vec3 Right) { - return HMM_DivV3(Left, Right); -} - -static inline HMM_Vec4 operator/(HMM_Vec4 Left, HMM_Vec4 Right) { - return HMM_DivV4(Left, Right); -} - -static inline HMM_Vec2 operator/(HMM_Vec2 Left, float Right) { - return HMM_DivV2F(Left, Right); -} - -static inline HMM_Vec3 operator/(HMM_Vec3 Left, float Right) { - return HMM_DivV3F(Left, Right); -} - -static inline HMM_Vec4 operator/(HMM_Vec4 Left, float Right) { - return HMM_DivV4F(Left, Right); -} - -static inline HMM_Mat4 operator/(HMM_Mat4 Left, float Right) { - return HMM_DivM4F(Left, Right); -} - -static inline HMM_Mat3 operator/(HMM_Mat3 Left, float Right) { - return HMM_DivM3F(Left, Right); -} - -static inline HMM_Mat2 operator/(HMM_Mat2 Left, float Right) { - return HMM_DivM2F(Left, Right); -} - -static inline HMM_Quat operator/(HMM_Quat Left, float Right) { - return HMM_DivQF(Left, Right); -} - -static inline HMM_Vec2 &operator+=(HMM_Vec2 &Left, HMM_Vec2 Right) { - return Left = Left + Right; -} - -static inline HMM_Vec3 &operator+=(HMM_Vec3 &Left, HMM_Vec3 Right) { - return Left = Left + Right; -} - -static inline HMM_Vec4 &operator+=(HMM_Vec4 &Left, HMM_Vec4 Right) { - return Left = Left + Right; -} - -static inline HMM_Mat2 &operator+=(HMM_Mat2 &Left, HMM_Mat2 Right) { - return Left = Left + Right; -} - -static inline HMM_Mat3 &operator+=(HMM_Mat3 &Left, HMM_Mat3 Right) { - return Left = Left + Right; -} - -static inline HMM_Mat4 &operator+=(HMM_Mat4 &Left, HMM_Mat4 Right) { - return Left = Left + Right; -} - -static inline HMM_Quat &operator+=(HMM_Quat &Left, HMM_Quat Right) { - return Left = Left + Right; -} - -static inline HMM_Vec2 &operator-=(HMM_Vec2 &Left, HMM_Vec2 Right) { - return Left = Left - Right; -} - -static inline HMM_Vec3 &operator-=(HMM_Vec3 &Left, HMM_Vec3 Right) { - return Left = Left - Right; -} - -static inline HMM_Vec4 &operator-=(HMM_Vec4 &Left, HMM_Vec4 Right) { - return Left = Left - Right; -} - -static inline HMM_Mat2 &operator-=(HMM_Mat2 &Left, HMM_Mat2 Right) { - return Left = Left - Right; -} - -static inline HMM_Mat3 &operator-=(HMM_Mat3 &Left, HMM_Mat3 Right) { - return Left = Left - Right; -} - -static inline HMM_Mat4 &operator-=(HMM_Mat4 &Left, HMM_Mat4 Right) { - return Left = Left - Right; -} - -static inline HMM_Quat &operator-=(HMM_Quat &Left, HMM_Quat Right) { - return Left = Left - Right; -} - -static inline HMM_Vec2 &operator*=(HMM_Vec2 &Left, HMM_Vec2 Right) { - return Left = Left * Right; -} - -static inline HMM_Vec3 &operator*=(HMM_Vec3 &Left, HMM_Vec3 Right) { - return Left = Left * Right; -} - -static inline HMM_Vec4 &operator*=(HMM_Vec4 &Left, HMM_Vec4 Right) { - return Left = Left * Right; -} - -static inline HMM_Vec2 &operator*=(HMM_Vec2 &Left, float Right) { - return Left = Left * Right; -} - -static inline HMM_Vec3 &operator*=(HMM_Vec3 &Left, float Right) { - return Left = Left * Right; -} - -static inline HMM_Vec4 &operator*=(HMM_Vec4 &Left, float Right) { - return Left = Left * Right; -} - -static inline HMM_Mat2 &operator*=(HMM_Mat2 &Left, float Right) { - return Left = Left * Right; -} - -static inline HMM_Mat3 &operator*=(HMM_Mat3 &Left, float Right) { - return Left = Left * Right; -} - -static inline HMM_Mat4 &operator*=(HMM_Mat4 &Left, float Right) { - return Left = Left * Right; -} - -static inline HMM_Quat &operator*=(HMM_Quat &Left, float Right) { - return Left = Left * Right; -} - -static inline HMM_Vec2 &operator/=(HMM_Vec2 &Left, HMM_Vec2 Right) { - return Left = Left / Right; -} - -static inline HMM_Vec3 &operator/=(HMM_Vec3 &Left, HMM_Vec3 Right) { - return Left = Left / Right; -} - -static inline HMM_Vec4 &operator/=(HMM_Vec4 &Left, HMM_Vec4 Right) { - return Left = Left / Right; -} - -static inline HMM_Vec2 &operator/=(HMM_Vec2 &Left, float Right) { - return Left = Left / Right; -} - -static inline HMM_Vec3 &operator/=(HMM_Vec3 &Left, float Right) { - return Left = Left / Right; -} - -static inline HMM_Vec4 &operator/=(HMM_Vec4 &Left, float Right) { - return Left = Left / Right; -} - -static inline HMM_Mat4 &operator/=(HMM_Mat4 &Left, float Right) { - return Left = Left / Right; -} - -static inline HMM_Quat &operator/=(HMM_Quat &Left, float Right) { - return Left = Left / Right; -} - -static inline HMM_Bool operator==(HMM_Vec2 Left, HMM_Vec2 Right) { - return HMM_EqV2(Left, Right); -} - -static inline HMM_Bool operator==(HMM_Vec3 Left, HMM_Vec3 Right) { - return HMM_EqV3(Left, Right); -} - -static inline HMM_Bool operator==(HMM_Vec4 Left, HMM_Vec4 Right) { - return HMM_EqV4(Left, Right); -} - -static inline HMM_Bool operator!=(HMM_Vec2 Left, HMM_Vec2 Right) { - return !HMM_EqV2(Left, Right); -} - -static inline HMM_Bool operator!=(HMM_Vec3 Left, HMM_Vec3 Right) { - return !HMM_EqV3(Left, Right); -} - -static inline HMM_Bool operator!=(HMM_Vec4 Left, HMM_Vec4 Right) { - return !HMM_EqV4(Left, Right); -} - -static inline HMM_Vec2 operator-(HMM_Vec2 In) { - - HMM_Vec2 Result; - Result.X = -In.X; - Result.Y = -In.Y; - - return Result; -} - -static inline HMM_Vec3 operator-(HMM_Vec3 In) { - - HMM_Vec3 Result; - Result.X = -In.X; - Result.Y = -In.Y; - Result.Z = -In.Z; - - return Result; -} - -static inline HMM_Vec4 operator-(HMM_Vec4 In) { - - HMM_Vec4 Result; -#if HANDMADE_MATH__USE_SSE - Result.SSE = _mm_xor_ps(In.SSE, _mm_set1_ps(-0.0f)); -#else - Result.X = -In.X; - Result.Y = -In.Y; - Result.Z = -In.Z; - Result.W = -In.W; -#endif - - return Result; -} - -#endif /* __cplusplus*/ - -#ifdef HANDMADE_MATH__USE_C11_GENERICS -#define HMM_Add(A, B) _Generic((A), \ - HMM_Vec2 \ - : HMM_AddV2, \ - HMM_Vec3 \ - : HMM_AddV3, \ - HMM_Vec4 \ - : HMM_AddV4, \ - HMM_Mat2 \ - : HMM_AddM2, \ - HMM_Mat3 \ - : HMM_AddM3, \ - HMM_Mat4 \ - : HMM_AddM4, \ - HMM_Quat \ - : HMM_AddQ)(A, B) - -#define HMM_Sub(A, B) _Generic((A), \ - HMM_Vec2 \ - : HMM_SubV2, \ - HMM_Vec3 \ - : HMM_SubV3, \ - HMM_Vec4 \ - : HMM_SubV4, \ - HMM_Mat2 \ - : HMM_SubM2, \ - HMM_Mat3 \ - : HMM_SubM3, \ - HMM_Mat4 \ - : HMM_SubM4, \ - HMM_Quat \ - : HMM_SubQ)(A, B) - -#define HMM_Mul(A, B) _Generic((B), \ - float \ - : _Generic((A), \ - HMM_Vec2 \ - : HMM_MulV2F, \ - HMM_Vec3 \ - : HMM_MulV3F, \ - HMM_Vec4 \ - : HMM_MulV4F, \ - HMM_Mat2 \ - : HMM_MulM2F, \ - HMM_Mat3 \ - : HMM_MulM3F, \ - HMM_Mat4 \ - : HMM_MulM4F, \ - HMM_Quat \ - : HMM_MulQF), \ - HMM_Mat2 \ - : HMM_MulM2, \ - HMM_Mat3 \ - : HMM_MulM3, \ - HMM_Mat4 \ - : HMM_MulM4, \ - HMM_Quat \ - : HMM_MulQ, \ - default \ - : _Generic((A), \ - HMM_Vec2 \ - : HMM_MulV2, \ - HMM_Vec3 \ - : HMM_MulV3, \ - HMM_Vec4 \ - : HMM_MulV4, \ - HMM_Mat2 \ - : HMM_MulM2V2, \ - HMM_Mat3 \ - : HMM_MulM3V3, \ - HMM_Mat4 \ - : HMM_MulM4V4))(A, B) - -#define HMM_Div(A, B) _Generic((B), \ - float \ - : _Generic((A), \ - HMM_Mat2 \ - : HMM_DivM2F, \ - HMM_Mat3 \ - : HMM_DivM3F, \ - HMM_Mat4 \ - : HMM_DivM4F, \ - HMM_Vec2 \ - : HMM_DivV2F, \ - HMM_Vec3 \ - : HMM_DivV3F, \ - HMM_Vec4 \ - : HMM_DivV4F, \ - HMM_Quat \ - : HMM_DivQF), \ - HMM_Mat2 \ - : HMM_DivM2, \ - HMM_Mat3 \ - : HMM_DivM3, \ - HMM_Mat4 \ - : HMM_DivM4, \ - HMM_Quat \ - : HMM_DivQ, \ - default \ - : _Generic((A), \ - HMM_Vec2 \ - : HMM_DivV2, \ - HMM_Vec3 \ - : HMM_DivV3, \ - HMM_Vec4 \ - : HMM_DivV4))(A, B) - -#define HMM_Len(A) _Generic((A), \ - HMM_Vec2 \ - : HMM_LenV2, \ - HMM_Vec3 \ - : HMM_LenV3, \ - HMM_Vec4 \ - : HMM_LenV4)(A) - -#define HMM_LenSqr(A) _Generic((A), \ - HMM_Vec2 \ - : HMM_LenSqrV2, \ - HMM_Vec3 \ - : HMM_LenSqrV3, \ - HMM_Vec4 \ - : HMM_LenSqrV4)(A) - -#define HMM_Norm(A) _Generic((A), \ - HMM_Vec2 \ - : HMM_NormV2, \ - HMM_Vec3 \ - : HMM_NormV3, \ - HMM_Vec4 \ - : HMM_NormV4)(A) - -#define HMM_Dot(A, B) _Generic((A), \ - HMM_Vec2 \ - : HMM_DotV2, \ - HMM_Vec3 \ - : HMM_DotV3, \ - HMM_Vec4 \ - : HMM_DotV4)(A, B) - -#define HMM_Lerp(A, T, B) _Generic((A), \ - float \ - : HMM_Lerp, \ - HMM_Vec2 \ - : HMM_LerpV2, \ - HMM_Vec3 \ - : HMM_LerpV3, \ - HMM_Vec4 \ - : HMM_LerpV4)(A, T, B) - -#define HMM_Eq(A, B) _Generic((A), \ - HMM_Vec2 \ - : HMM_EqV2, \ - HMM_Vec3 \ - : HMM_EqV3, \ - HMM_Vec4 \ - : HMM_EqV4)(A, B) - -#define HMM_Transpose(M) _Generic((M), \ - HMM_Mat2 \ - : HMM_TransposeM2, \ - HMM_Mat3 \ - : HMM_TransposeM3, \ - HMM_Mat4 \ - : HMM_TransposeM4)(M) - -#define HMM_Determinant(M) _Generic((M), \ - HMM_Mat2 \ - : HMM_DeterminantM2, \ - HMM_Mat3 \ - : HMM_DeterminantM3, \ - HMM_Mat4 \ - : HMM_DeterminantM4)(M) - -#define HMM_InvGeneral(M) _Generic((M), \ - HMM_Mat2 \ - : HMM_InvGeneralM2, \ - HMM_Mat3 \ - : HMM_InvGeneralM3, \ - HMM_Mat4 \ - : HMM_InvGeneralM4)(M) - -#endif - -#if defined(__GNUC__) || defined(__clang__) -#pragma GCC diagnostic pop -#endif - #endif /* HANDMADE_MATH_H */ diff --git a/source/engine/debug/debugdraw.c b/source/engine/debug/debugdraw.c index 9f94229..f890ac3 100644 --- a/source/engine/debug/debugdraw.c +++ b/source/engine/debug/debugdraw.c @@ -212,7 +212,6 @@ void debugdraw_init() }); csg = sg_make_shader(circle_shader_desc(sg_query_backend())); - circle_pipe = sg_make_pipeline(&(sg_pipeline_desc){ .shader = csg, .layout = { @@ -295,15 +294,15 @@ void debugdraw_init() }); } -void draw_line(cpVect *a_points, int n, struct rgba color, float seg_len, int closed, float seg_speed) +void draw_line(HMM_Vec2 *a_points, int n, struct rgba color, float seg_len, int closed, float seg_speed) { if (n < 2) return; - cpVect *points = a_points; + HMM_Vec2 *points = a_points; seg_speed = 1; if (closed) { n++; - points = malloc(sizeof(cpVect) * n); - memcpy(points, a_points, sizeof(cpVect)*(n-1)); + points = malloc(sizeof(HMM_Vec2) * n); + memcpy(points, a_points, sizeof(HMM_Vec2)*(n-1)); points[n-1] = points[0]; } @@ -321,7 +320,7 @@ void draw_line(cpVect *a_points, int n, struct rgba color, float seg_len, int cl v[0].dist = 0; for (int i = 1; i < n; i++) { - dist += cpvdist(points[i-1], points[i]); + dist += HMM_DistV2(points[i-1], points[i]); v[i].dist = dist; } @@ -352,9 +351,9 @@ void draw_line(cpVect *a_points, int n, struct rgba color, float seg_len, int cl if (closed) free(points); } -cpVect center_of_vects(cpVect *v, int n) +HMM_Vec2 center_of_vects(HMM_Vec2 *v, int n) { - cpVect c; + HMM_Vec2 c; for (int i = 0; i < n; i++) { c.x += v[i].x; c.y += v[i].y; @@ -365,15 +364,15 @@ cpVect center_of_vects(cpVect *v, int n) return c; } -float vecs2m(cpVect a, cpVect b) +float vecs2m(HMM_Vec2 a, HMM_Vec2 b) { return (b.y-a.y)/(b.x-a.x); } -cpVect *inflatepoints(cpVect *p, float d, int n) +HMM_Vec2 *inflatepoints(HMM_Vec2 *p, float d, int n) { if (d == 0) { - cpVect *ret = NULL; + HMM_Vec2 *ret = NULL; arraddn(ret,n); for (int i = 0; i < n; i++) ret[i] = p[i]; @@ -402,7 +401,7 @@ cpVect *inflatepoints(cpVect *p, float d, int n) .closed = 0, }); - cpVect *ret = NULL; + HMM_Vec2 *ret = NULL; arraddn(ret,mesh->num_vertices); for (int i = 0; i < mesh->num_vertices; i++) { ret[i].x = mesh->positions[i].x; @@ -411,16 +410,16 @@ cpVect *inflatepoints(cpVect *p, float d, int n) return ret; } -void draw_edge(cpVect *points, int n, struct rgba color, int thickness, int closed, int flags, struct rgba line_color, float line_seg) +void draw_edge(HMM_Vec2 *points, int n, struct rgba color, int thickness, int closed, int flags, struct rgba line_color, float line_seg) { -// static_assert(sizeof(cpVect) == 2*sizeof(float)); +// static_assert(sizeof(HMM_Vec2) == 2*sizeof(float)); if (thickness == 0) { draw_line(points,n,color,0,closed,0); } /* todo: should be dashed, and filled. use a texture. */ /* draw polygon outline */ - if (cpveql(points[0], points[n-1])) { + if (HMM_EqV2(points[0], points[n-1])) { closed = true; n--; } @@ -471,8 +470,8 @@ void draw_edge(cpVect *points, int n, struct rgba color, int thickness, int clos if (thickness == 1) { draw_line(points,n,line_color,line_seg, closed, 0); } else { - cpVect in_p[n]; - cpVect out_p[n]; + HMM_Vec2 in_p[n]; + HMM_Vec2 out_p[n]; for (int i = 0, v = 0; i < n*2+1; i+=2, v++) { in_p[v].x = vertices[i].pos.x; @@ -485,7 +484,7 @@ void draw_edge(cpVect *points, int n, struct rgba color, int thickness, int clos } if (!closed) { - cpVect p[n*2]; + HMM_Vec2 p[n*2]; for (int i = 0; i < n; i++) p[i] = in_p[i]; for (int i = n-1, v = n; i >= 0; i--,v++) @@ -500,7 +499,7 @@ void draw_edge(cpVect *points, int n, struct rgba color, int thickness, int clos } } -void draw_circle(cpVect pos, float radius, float pixels, struct rgba color, float seg) +void draw_circle(HMM_Vec2 pos, float radius, float pixels, struct rgba color, float seg) { struct circle_vertex cv; cv.pos.x = pos.x; @@ -513,12 +512,12 @@ void draw_circle(cpVect pos, float radius, float pixels, struct rgba color, floa circle_count++; } -void draw_box(struct cpVect c, struct cpVect wh, struct rgba color) +void draw_box(HMM_Vec2 c, HMM_Vec2 wh, struct rgba color) { float hw = wh.x / 2.f; float hh = wh.y / 2.f; - cpVect verts[4] = { + HMM_Vec2 verts[4] = { { .x = c.x-hw, .y = c.y-hh }, { .x = c.x+hw, .y = c.y-hh }, { .x = c.x+hw, .y = c.y+hh }, @@ -528,17 +527,17 @@ void draw_box(struct cpVect c, struct cpVect wh, struct rgba color) draw_poly(verts, 4, color); } -void draw_arrow(struct cpVect start, struct cpVect end, struct rgba color, int capsize) +void draw_arrow(HMM_Vec2 start, HMM_Vec2 end, struct rgba color, int capsize) { - cpVect points[2] = {start, end}; + HMM_Vec2 points[2] = {start, end}; draw_line(points, 2, color, 0, 0,0); draw_cppoint(end, capsize, color); } void draw_grid(float width, float span, struct rgba color) { - cpVect offset = cam_pos(); - offset = cpvmult(offset, 1/cam_zoom()); + HMM_Vec2 offset = (HMM_Vec2)cam_pos(); + offset = HMM_MulV2F(offset, 1/cam_zoom()); float ubo[4]; ubo[0] = offset.x; @@ -560,25 +559,25 @@ void draw_grid(float width, float span, struct rgba color) sg_draw(0,4,1); } -void draw_cppoint(struct cpVect point, float r, struct rgba color) +void draw_cppoint(HMM_Vec2 point, float r, struct rgba color) { struct point_vertex p = { .color = color, .radius = r }; - p.pos.x = point.x; - p.pos.y = point.y; + p.pos.x = point.X; + p.pos.y = point.Y; sg_append_buffer(point_bind.vertex_buffers[0], &(sg_range){.ptr = &p, .size = sizeof(struct point_vertex)}); point_c++; } -void draw_points(struct cpVect *points, int n, float size, struct rgba color) +void draw_points(HMM_Vec2 *points, int n, float size, struct rgba color) { for (int i = 0; i < n; i++) draw_cppoint(points[i], size, color); } -void draw_poly(cpVect *points, int n, struct rgba color) +void draw_poly(HMM_Vec2 *points, int n, struct rgba color) { /* Find polygon mesh */ int tric = n - 2; diff --git a/source/engine/debug/debugdraw.h b/source/engine/debug/debugdraw.h index bf02efd..8ff7d7a 100644 --- a/source/engine/debug/debugdraw.h +++ b/source/engine/debug/debugdraw.h @@ -6,17 +6,17 @@ struct rgba; void debugdraw_init(); -void draw_cppoint(struct cpVect point, float r, struct rgba color); -void draw_points(struct cpVect *points, int n, float size, struct rgba color); +void draw_cppoint(HMM_Vec2 point, float r, struct rgba color); +void draw_points(HMM_Vec2 *points, int n, float size, struct rgba color); -void draw_line(cpVect *points, int n, struct rgba color, float seg_len, int closed, float seg_speed); -void draw_arrow(struct cpVect start, struct cpVect end, struct rgba, int capsize); -void draw_edge(struct cpVect *points, int n, struct rgba color, int thickness, int closed, int flags, struct rgba line_color, float line_seg); +void draw_line(HMM_Vec2 *points, int n, struct rgba color, float seg_len, int closed, float seg_speed); +void draw_arrow(HMM_Vec2 start, HMM_Vec2 end, struct rgba, int capsize); +void draw_edge(HMM_Vec2 *points, int n, struct rgba color, int thickness, int closed, int flags, struct rgba line_color, float line_seg); /* pixels - how many pixels thick, segsize - dashed line seg len */ -void draw_circle(cpVect c, float radius, float pixels, struct rgba color, float seg); -void draw_box(cpVect c, cpVect wh, struct rgba color); -void draw_poly(cpVect *points, int n, struct rgba color); +void draw_circle(HMM_Vec2 c, float radius, float pixels, struct rgba color, float seg); +void draw_box(HMM_Vec2 c, HMM_Vec2 wh, struct rgba color); +void draw_poly(HMM_Vec2 *points, int n, struct rgba color); void draw_grid(float width, float span, struct rgba color); @@ -24,6 +24,6 @@ void debug_flush(HMM_Mat4 *view); void debug_newframe(); void debug_nextpass(); -cpVect *inflatepoints(cpVect *p, float d, int n); +HMM_Vec2 *inflatepoints(HMM_Vec2 *p, float d, int n); #endif diff --git a/source/engine/font.c b/source/engine/font.c index b3f1508..eb56174 100644 --- a/source/engine/font.c +++ b/source/engine/font.c @@ -188,7 +188,7 @@ void draw_underline_cursor(HMM_Vec2 pos, float scale, struct rgba color) void draw_char_box(struct Character c, HMM_Vec2 cursor, float scale, struct rgba color) { - cpVect wh; + HMM_Vec2 wh; color.a = 30; wh.x = c.Size[0] * scale + 2; @@ -196,7 +196,7 @@ void draw_char_box(struct Character c, HMM_Vec2 cursor, float scale, struct rgba cursor.X += c.Bearing[0] * scale + 1; cursor.Y -= (c.Bearing[1] * scale + 1); - cpVect b; + HMM_Vec2 b; b.x = cursor.X + wh.x/2; b.y = cursor.Y + wh.y/2; diff --git a/source/engine/gameobject.c b/source/engine/gameobject.c index 82752a8..cd38085 100644 --- a/source/engine/gameobject.c +++ b/source/engine/gameobject.c @@ -100,7 +100,7 @@ HMM_Vec2 world2go(struct gameobject *go, HMM_Vec2 pos) return HMM_MulM3V3(t_world2go(go), (HMM_Vec3){pos.X, pos.Y, 1.0}).XY; } -HMM_Vec2 goscale(struct gameobject *go, HMM_Vec2 pos); +HMM_Vec2 goscale(struct gameobject *go, HMM_Vec2 pos) { return HMM_MulV2(go->scale.XY, pos); } @@ -116,8 +116,8 @@ HMM_Mat3 t_world2go(struct gameobject *go) } -int pos2gameobject(cpVect pos) { - cpShape *hit = phys2d_query_pos(pos); +int pos2gameobject(HMM_Vec2 pos) { + cpShape *hit = phys2d_query_pos(pos.cp); if (hit) { return shape2gameobject(hit); @@ -126,7 +126,7 @@ int pos2gameobject(cpVect pos) { for (int i = 0; i < arrlen(gameobjects); i++) { if (!gameobjects[i].body) continue; cpVect gpos = cpBodyGetPosition(gameobjects[i].body); - float dist = cpvlength(cpvsub(gpos, pos)); + float dist = cpvlength(cpvsub(gpos, pos.cp)); if (dist <= 25) return i; } @@ -332,7 +332,7 @@ void gameobjects_cleanup() { } } -void gameobject_move(struct gameobject *go, cpVect vec) { +void gameobject_move(struct gameobject *go, HMM_Vec2 vec) { cpVect p = cpBodyGetPosition(go->body); p.x += vec.x; p.y += vec.y; diff --git a/source/engine/gameobject.h b/source/engine/gameobject.h index ac09b63..5008ae7 100644 --- a/source/engine/gameobject.h +++ b/source/engine/gameobject.h @@ -80,9 +80,9 @@ uint32_t go2mask(struct gameobject *go); void go_shape_apply(cpBody *body, cpShape *shape, struct gameobject *go); /* Tries a few methods to select a gameobject; if none is selected returns -1 */ -int pos2gameobject(cpVect pos); +int pos2gameobject(HMM_Vec2 pos); -void gameobject_move(struct gameobject *go, cpVect vec); +void gameobject_move(struct gameobject *go, HMM_Vec2 vec); void gameobject_rotate(struct gameobject *go, float as); void gameobject_setangle(struct gameobject *go, float angle); void gameobject_setpos(struct gameobject *go, cpVect vec); diff --git a/source/engine/input.c b/source/engine/input.c index 38ae4df..f478bb3 100644 --- a/source/engine/input.c +++ b/source/engine/input.c @@ -16,9 +16,9 @@ float deltaT = 0; static int mouse_states[3] = {INPUT_UP}; static int key_states[512] = {INPUT_UP}; -cpVect mousewheel = {0,0}; -cpVect mouse_pos = {0, 0}; -cpVect mouse_delta = {0, 0}; +HMM_Vec2 mousewheel = {0,0}; +HMM_Vec2 mouse_pos = {0, 0}; +HMM_Vec2 mouse_delta = {0, 0}; struct joystick { int id; diff --git a/source/engine/input.h b/source/engine/input.h index 082f819..9657675 100644 --- a/source/engine/input.h +++ b/source/engine/input.h @@ -8,9 +8,9 @@ #include #include -extern cpVect mousewheel; -extern cpVect mouse_pos; -extern cpVect mouse_delta; +extern HMM_Vec2 mousewheel; +extern HMM_Vec2 mouse_pos; +extern HMM_Vec2 mouse_delta; #define INPUT_DOWN 0 #define INPUT_UP 1 diff --git a/source/engine/jsffi.c b/source/engine/jsffi.c index d7076dd..32fe3ef 100644 --- a/source/engine/jsffi.c +++ b/source/engine/jsffi.c @@ -214,7 +214,7 @@ struct boundingbox js2bb(JSValue v) } -HMM_Vec2 js2hmmv2(JSValue v) +HMM_Vec2 js2vec2(JSValue v) { HMM_Vec2 v2; v2.X = js2number(js_getpropidx(v,0)); @@ -222,13 +222,6 @@ HMM_Vec2 js2hmmv2(JSValue v) return v2; } -cpVect js2vec2(JSValue v) { - cpVect vect; - vect.x = js2number(js_getpropidx(v,0)); - vect.y = js2number(js_getpropidx(v,1)); - return vect; -} - cpBitmask js2bitmask(JSValue v) { cpBitmask mask = 0; int len = js_arrlen(v); @@ -243,10 +236,10 @@ cpBitmask js2bitmask(JSValue v) { return mask; } -cpVect *cpvecarr = NULL; +HMM_Vec2 *cpvecarr = NULL; /* Does not need to be freed by returning; but not reentrant */ -cpVect *js2cpvec2arr(JSValue v) { +HMM_Vec2 *js2cpvec2arr(JSValue v) { if (cpvecarr) arrfree(cpvecarr); @@ -266,12 +259,12 @@ JSValue bitmask2js(cpBitmask mask) { return arr; } -void vec2float(cpVect v, float *f) { +void vec2float(HMM_Vec2 v, float *f) { f[0] = v.x; f[1] = v.y; } -JSValue vec2js(cpVect v) { +JSValue vec2js(HMM_Vec2 v) { JSValue array = JS_NewArray(js); js_setprop_num(array,0,JS_NewFloat64(js,v.x)); js_setprop_num(array,1,JS_NewFloat64(js,v.y)); @@ -280,11 +273,11 @@ JSValue vec2js(cpVect v) { JSValue v22js(HMM_Vec2 v) { - cpVect c = { v.X, v.Y }; + HMM_Vec2 c = { v.X, v.Y }; return vec2js(c); } -JSValue vecarr2js(cpVect *points, int n) { +JSValue vecarr2js(HMM_Vec2 *points, int n) { JSValue array = JS_NewArray(js); for (int i = 0; i < n; i++) js_setprop_num(array,i,vec2js(points[i])); @@ -294,7 +287,7 @@ JSValue vecarr2js(cpVect *points, int n) { JSValue duk_ui_text(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) { const unsigned char *s = JS_ToCString(js, argv[0]); - HMM_Vec2 pos = js2hmmv2(argv[1]); + HMM_Vec2 pos = js2vec2(argv[1]); float size = js2number(argv[2]); struct rgba c = js2color(argv[3]); @@ -330,7 +323,7 @@ int js_print_exception(JSValue v) JSValue duk_gui_img(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) { const char *img = JS_ToCString(js, argv[0]); - gui_draw_img(img, js2hmmv2(argv[1]), js2hmmv2(argv[2]), js2number(argv[3]), js2bool(argv[4]), js2hmmv2(argv[5]), 1.0, js2color(argv[6])); + gui_draw_img(img, js2vec2(argv[1]), js2vec2(argv[2]), js2number(argv[3]), js2bool(argv[4]), js2vec2(argv[5]), 1.0, js2color(argv[6])); JS_FreeCString(js, img); return JS_NULL; } @@ -366,14 +359,14 @@ JSValue bb2js(struct boundingbox bb) JSValue duk_spline_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) { - static_assert(sizeof(tsReal) * 2 == sizeof(cpVect)); + static_assert(sizeof(tsReal) * 2 == sizeof(HMM_Vec2)); tsBSpline spline; int degrees = js2int(argv[1]); int d = js2int(argv[2]); /* dimensions */ int type = js2int(argv[3]); - cpVect *points = js2cpvec2arr(argv[4]); + HMM_Vec2 *points = js2cpvec2arr(argv[4]); size_t nsamples = js2int(argv[5]); tsStatus status; @@ -387,7 +380,7 @@ JSValue duk_spline_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst if (status.code) YughCritical("Spline creation error %d: %s", status.code, status.message); - cpVect *samples = malloc(nsamples*sizeof(cpVect)); + HMM_Vec2 *samples = malloc(nsamples*sizeof(HMM_Vec2)); size_t rsamples; /* TODO: This does not work with Clang/GCC due to UB */ @@ -412,20 +405,20 @@ JSValue ints2js(int *ints) { return arr; } -int vec_between(cpVect p, cpVect a, cpVect b) { - cpVect n; +int vec_between(HMM_Vec2 p, HMM_Vec2 a, HMM_Vec2 b) { + HMM_Vec2 n; n.x = b.x - a.x; n.y = b.y - a.y; - n = cpvnormalize(n); + n = HMM_NormV2(n); - return (cpvdot(n, cpvsub(p, a)) > 0 && cpvdot(cpvneg(n), cpvsub(p, b)) > 0); + return HMM_DotV2(n, HMM_SubV2(p,a)) > 0 && HMM_DotV2(HMM_MulV2F(n, -1), HMM_SubV2(p,b)) > 0; } /* Determines between which two points in 'segs' point 'p' falls. 0 indicates 'p' comes before the first point. arrlen(segs) indicates it comes after the last point. */ -int point2segindex(cpVect p, cpVect *segs, double slop) { +int point2segindex(HMM_Vec2 p, HMM_Vec2 *segs, double slop) { float shortest = slop < 0 ? INFINITY : slop; int best = -1; @@ -444,10 +437,10 @@ int point2segindex(cpVect p, cpVect *segs, double slop) { shortest = dist; best = i + 1; } else { - if (i == 0 && cpvdist(p, segs[0]) < slop) { + if (i == 0 && HMM_DistV2(p,segs[0]) < slop) { shortest = dist; best = i; - } else if (i == arrlen(segs) - 2 && cpvdist(p, arrlast(segs)) < slop) { + } else if (i == arrlen(segs) - 2 && HMM_DistV2(p,arrlast(segs)) < slop) { shortest = dist; best = arrlen(segs); } @@ -455,12 +448,12 @@ int point2segindex(cpVect p, cpVect *segs, double slop) { } if (best == 1) { - cpVect n; + HMM_Vec2 n; n.x = segs[1].x - segs[0].x; n.y = segs[1].y - segs[0].y; - n = cpvnormalize(n); - if (cpvdot(n, cpvsub(p, segs[0])) < 0) { - if (cpvdist(p, segs[0]) >= slop) + n = HMM_NormV2(n); + if (HMM_DotV2(n, HMM_SubV2(p,segs[0])) < 0 ){ + if (HMM_DistV2(p, segs[0]) >= slop) best = -1; else best = 0; @@ -468,13 +461,13 @@ int point2segindex(cpVect p, cpVect *segs, double slop) { } if (best == arrlen(segs) - 1) { - cpVect n; + HMM_Vec2 n; n.x = segs[best - 1].x - segs[best].x; n.y = segs[best - 1].y - segs[best - 1].y; - n = cpvnormalize(n); + n = HMM_NormV2(n); - if (cpvdot(n, cpvsub(p, segs[best])) < 0) { - if (cpvdist(p, segs[best]) >= slop) + if (HMM_DotV2(n, HMM_SubV2(p, segs[best])) < 0) { + if (HMM_DistV2(p, segs[best]) >= slop) best = -1; else best = arrlen(segs); @@ -530,7 +523,7 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) break; case 8: - phys2d_set_gravity(js2vec2(argv[1])); + phys2d_set_gravity(js2vec2(argv[1]).cp); break; case 9: @@ -647,15 +640,14 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) break; case 36: - id2go(js2int(argv[1]))->scale = js2number(argv[2]); - id2go(js2int(argv[1]))->scale3 = HMM_V3i(js2number(argv[2])); + id2go(js2int(argv[1]))->scale.XY = js2vec2(argv[2]); gameobject_apply(id2go(js2int(argv[1]))); cpSpaceReindexShapesForBody(space, id2go(js2int(argv[1]))->body); break; case 37: if (!id2sprite(js2int(argv[1]))) break; - id2sprite(js2int(argv[1]))->pos = js2hmmv2(argv[2]); + id2sprite(js2int(argv[1]))->pos = js2vec2(argv[2]); break; case 38: @@ -733,19 +725,19 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) break; case 55: - js2go(argv[1])->flipx = JS_ToBool(js, argv[2]) ? -1 : 1; break; +// js2go(argv[1])->flipx = JS_ToBool(js, argv[2]) ? -1 : 1; case 56: - js2go(argv[1])->flipy = JS_ToBool(js, argv[2]) ? -1 : 1; +// js2go(argv[1])->flipy = JS_ToBool(js, argv[2]) ? -1 : 1; break; case 57: - ret = JS_NewBool(js, js2go(argv[1])->flipx == -1 ? 1 : 0); +// ret = JS_NewBool(js, js2go(argv[1])->flipx == -1 ? 1 : 0); break; case 58: - ret = JS_NewBool(js, js2go(argv[1])->flipy == -1 ? 1 : 0); +// ret = JS_NewBool(js, js2go(argv[1])->flipy == -1 ? 1 : 0); break; case 59: @@ -804,7 +796,7 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) break; case 72: - ret = vec2js(cpSpaceGetGravity(space)); + ret = vec2js((HMM_Vec2)cpSpaceGetGravity(space)); break; case 73: @@ -855,7 +847,7 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) break; case 85: - ret = vec2js(cpvproject(js2vec2(argv[1]), js2vec2(argv[2]))); + ret = vec2js(HMM_ProjV2(js2vec2(argv[1]), js2vec2(argv[2]))); break; case 86: @@ -934,15 +926,15 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) break; case 103: - ret = num2js(js2go(argv[1])->scale); + ret = num2js(js2go(argv[1])->scale.X); break; case 104: - ret = bool2js(js2go(argv[1])->flipx == -1 ? 1 : 0); +// ret = bool2js(js2go(argv[1])->flipx == -1 ? 1 : 0); break; case 105: - ret = bool2js(js2go(argv[1])->flipy == -1 ? 1 : 0); +// ret = bool2js(js2go(argv[1])->flipy == -1 ? 1 : 0); break; case 106: @@ -1070,11 +1062,11 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) break; case 136: - ret = v22js(world2screen(js2hmmv2(argv[1]))); + ret = v22js(world2screen(js2vec2(argv[1]))); break; case 137: - ret = v22js(screen2world(js2hmmv2(argv[1]))); + ret = v22js(screen2world(js2vec2(argv[1]))); break; case 138: @@ -1355,7 +1347,7 @@ JSValue duk_set_body(JSContext *js, JSValueConst this, int argc, JSValueConst *a break; case 2: - cpBodySetPosition(go->body, js2vec2(argv[2])); + cpBodySetPosition(go->body, js2vec2(argv[2]).cp); break; case 3: @@ -1363,15 +1355,15 @@ JSValue duk_set_body(JSContext *js, JSValueConst this, int argc, JSValueConst *a break; case 4: - cpBodyApplyImpulseAtWorldPoint(go->body, js2vec2(argv[2]), cpBodyGetPosition(go->body)); + cpBodyApplyImpulseAtWorldPoint(go->body, js2vec2(argv[2]).cp, cpBodyGetPosition(go->body)); return JS_NULL; case 5: - go->flipx = JS_ToBool(js, argv[2]); +// go->flipx = JS_ToBool(js, argv[2]); break; case 6: - go->flipy = JS_ToBool(js, argv[2]); +// go->flipy = JS_ToBool(js, argv[2]); break; case 7: @@ -1385,7 +1377,7 @@ JSValue duk_set_body(JSContext *js, JSValueConst this, int argc, JSValueConst *a return JS_NULL; case 9: - cpBodySetVelocity(go->body, js2vec2(argv[2])); + cpBodySetVelocity(go->body, js2vec2(argv[2]).cp); return JS_NULL; case 10: @@ -1397,14 +1389,14 @@ JSValue duk_set_body(JSContext *js, JSValueConst this, int argc, JSValueConst *a break; case 12: - cpBodyApplyForceAtWorldPoint(go->body, js2vec2(argv[2]), cpBodyGetPosition(go->body)); + cpBodyApplyForceAtWorldPoint(go->body, js2vec2(argv[2]).cp, cpBodyGetPosition(go->body)); return JS_NULL; case 13: cpBodySetMoment(go->body, js2number(argv[2])); return JS_NULL; case 14: - cpBodyApplyForceAtLocalPoint(go->body, js2vec2(argv[2]), js2vec2(argv[3])); + cpBodyApplyForceAtLocalPoint(go->body, js2vec2(argv[2]).cp, js2vec2(argv[3]).cp); return JS_NULL; } @@ -1425,13 +1417,13 @@ JSValue duk_q_body(JSContext *js, JSValueConst this, int argc, JSValueConst *arg return JS_NewInt64(js, cpBodyGetType(go->body)); case 1: - return vec2js(cpBodyGetPosition(go->body)); + return vec2js((HMM_Vec2)cpBodyGetPosition(go->body)); case 2: return JS_NewFloat64(js, cpBodyGetAngle(go->body)); case 3: - return vec2js(cpBodyGetVelocity(go->body)); + return vec2js((HMM_Vec2)cpBodyGetVelocity(go->body)); case 4: return JS_NewFloat64(js, cpBodyGetAngularVelocity(go->body)); @@ -1474,14 +1466,12 @@ JSValue duk_make_anim2d(JSContext *js, JSValueConst this, int argc, JSValueConst JSValue duk_make_box2d(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) { int go = js2int(argv[0]); - cpVect size = js2vec2(argv[1]); - cpVect offset = js2vec2(argv[2]); + HMM_Vec2 size = js2vec2(argv[1]); struct phys2d_box *box = Make2DBox(go); box->w = size.x; box->h = size.y; - box->offset[0] = offset.x; - box->offset[1] = offset.y; + box->offset = js2vec2(argv[2]); phys2d_applybox(box); @@ -1494,7 +1484,7 @@ JSValue duk_make_box2d(JSContext *js, JSValueConst this, int argc, JSValueConst JSValue duk_cmd_box2d(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) { int cmd = js2int(argv[0]); struct phys2d_box *box = js2ptr(argv[1]); - cpVect arg; + HMM_Vec2 arg; if (!box) return JS_NULL; @@ -1506,9 +1496,7 @@ JSValue duk_cmd_box2d(JSContext *js, JSValueConst this, int argc, JSValueConst * break; case 1: - arg = js2vec2(argv[2]); - box->offset[0] = arg.x; - box->offset[1] = arg.y; + box->offset = js2vec2(argv[2]); break; case 2: @@ -1596,12 +1584,12 @@ JSValue duk_make_edge2d(JSContext *js, JSValueConst this, int argc, JSValueConst struct phys2d_edge *edge = Make2DEdge(go); int n = js_arrlen(argv[1]); - cpVect points[n]; + HMM_Vec2 points[n]; for (int i = 0; i < n; i++) { points[i] = js2vec2(js_getpropidx(argv[1],i)); phys2d_edgeaddvert(edge); - phys2d_edge_setvert(edge, i, points[i]); + phys2d_edge_setvert(edge, i, points[i].cp); } JSValue edgeval = JS_NewObject(js); @@ -1634,10 +1622,10 @@ JSValue duk_cmd_edge2d(JSContext *js, JSValueConst this, int argc, JSValueConst } JSValue duk_inflate_cpv(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) { - cpVect *points = js2cpvec2arr(argv[0]); + HMM_Vec2 *points = js2cpvec2arr(argv[0]); int n = js2int(argv[1]); double d = js2number(argv[2]); - cpVect *infl = inflatepoints(points,d,n); + HMM_Vec2 *infl = inflatepoints(points,d,n); JSValue arr = vecarr2js(infl,arrlen(infl)); arrfree(infl); return arr; @@ -1653,7 +1641,7 @@ JSValue duk_anim(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) for (int i = 0; i < keyframes; i++) { struct keyframe k; - cpVect v = js2vec2(js_getpropidx( argv[1], i)); + HMM_Vec2 v = js2vec2(js_getpropidx( argv[1], i)); k.time = v.y; k.val = v.x; a = anim_add_keyframe(a, k); @@ -1680,7 +1668,7 @@ JSValue duk_make_timer(JSContext *js, JSValueConst this, int argc, JSValueConst JSValue duk_cmd_points(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) { int n = js_arrlen(argv[1]); - cpVect points[n]; + HMM_Vec2 points[n]; for (int i = 0; i < n; i++) points[i] = js2vec2(js_arridx(argv[1], i)); diff --git a/source/engine/jsffi.h b/source/engine/jsffi.h index 0742d29..8ed13c9 100644 --- a/source/engine/jsffi.h +++ b/source/engine/jsffi.h @@ -7,8 +7,8 @@ void ffi_load(); -JSValue vec2js(cpVect v); -cpVect js2vec2(JSValue v); +JSValue vec2js(HMM_Vec2 v); +HMM_Vec2 js2vec2(JSValue v); JSValue bitmask2js(cpBitmask mask); cpBitmask js2bitmask(JSValue v); diff --git a/source/engine/render.c b/source/engine/render.c index 16235bc..dc33c8a 100644 --- a/source/engine/render.c +++ b/source/engine/render.c @@ -219,7 +219,7 @@ void trace_make_pipeline(const sg_pipeline_desc *d, sg_pipeline result, void *da void trace_apply_pipeline(sg_pipeline pip, void *data) { -// YughInfo("Applying pipeline %d", pip); + YughInfo("Applying pipeline %d", pip); } void trace_fail_pipeline(sg_pipeline pip, void *data) diff --git a/source/engine/script.c b/source/engine/script.c index f19c5f1..d1e8a0b 100644 --- a/source/engine/script.c +++ b/source/engine/script.c @@ -261,7 +261,7 @@ void callee_int(struct callee c, int i) { JS_FreeValue(js, v); } -void callee_vec2(struct callee c, cpVect vec) { +void callee_vec2(struct callee c, HMM_Vec2 vec) { JSValue v = vec2js(vec); js_callee_exec(&c, 1, &v); JS_FreeValue(js, v); diff --git a/source/engine/texture.c b/source/engine/texture.c index 86aa362..6162dcb 100644 --- a/source/engine/texture.c +++ b/source/engine/texture.c @@ -357,9 +357,9 @@ struct glrect tex_get_rect(struct Texture *tex) { return ST_UNIT; } -cpVect tex_get_dimensions(struct Texture *tex) { - if (!tex) return cpvzero; - cpVect d; +HMM_Vec2 tex_get_dimensions(struct Texture *tex) { + if (!tex) return (HMM_Vec2){0,0}; + HMM_Vec2 d; d.x = tex->width; d.y = tex->height; return d; diff --git a/source/engine/texture.h b/source/engine/texture.h index 789d115..136e5b6 100644 --- a/source/engine/texture.h +++ b/source/engine/texture.h @@ -4,6 +4,7 @@ #include "timer.h" #include #include "sokol/sokol_gfx.h" +#include "HandmadeMath.h" #define TEX_SPEC 0 @@ -105,7 +106,7 @@ void anim_decr(struct anim2d *anim); int gif_nframes(const char *path); struct glrect tex_get_rect(struct Texture *tex); -cpVect tex_get_dimensions(struct Texture *tex); +HMM_Vec2 tex_get_dimensions(struct Texture *tex); struct glrect anim_get_rect(struct anim2d *anim); int anim_frames(struct TexAnim *a);