transform work

This commit is contained in:
John Alanbrook 2023-11-14 15:20:09 +00:00
parent 51438a5328
commit f6161d04b0
18 changed files with 177 additions and 1089 deletions

View file

@ -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

View file

@ -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()));
},

View file

@ -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:

View file

@ -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();

File diff suppressed because it is too large Load diff

View file

@ -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;

View file

@ -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

View file

@ -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;

View file

@ -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;

View file

@ -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);

View file

@ -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;

View file

@ -8,9 +8,9 @@
#include <chipmunk/chipmunk.h>
#include <stdint.h>
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

View file

@ -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));

View file

@ -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);

View file

@ -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)

View file

@ -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);

View file

@ -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;

View file

@ -4,6 +4,7 @@
#include "timer.h"
#include <chipmunk/chipmunk.h>
#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);