From 9303fb63cfb21bb9169b55ab041383c1fcc0dbf4 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Fri, 20 Jan 2023 00:30:23 +0000 Subject: [PATCH] prefab syncing --- source/engine/2dphysics.c | 7 +++-- source/engine/2dphysics.h | 2 +- source/engine/ffi.c | 51 ++++++++++++++++++++++++++++++------ source/engine/openglrender.c | 3 +++ source/engine/sprite.c | 2 ++ 5 files changed, 52 insertions(+), 13 deletions(-) diff --git a/source/engine/2dphysics.c b/source/engine/2dphysics.c index 5988e57..5c607d2 100644 --- a/source/engine/2dphysics.c +++ b/source/engine/2dphysics.c @@ -119,8 +119,7 @@ struct phys2d_circle *Make2DCircle(int go) struct phys2d_circle *new = malloc(sizeof(struct phys2d_circle)); new->radius = 10.f; - new->offset[0] = 0.f; - new->offset[1] = 0.f; + new->offset = cpvzero; new->shape.shape = cpSpaceAddShape(space, cpCircleShapeNew(id2go(go)->body, new->radius, cpvzero)); new->shape.debugdraw = phys2d_dbgdrawcircle; @@ -137,7 +136,7 @@ void phys2d_circledel(struct phys2d_circle *c) void circle_gui(struct phys2d_circle *circle) { nuke_property_float("Radius", 1.f, &circle->radius, 10000.f, 1.f, 1.f); - nuke_property_float2("Offset", 0.f, circle->offset, 1.f, 0.01f, 0.01f); + //nuke_property_float2("Offset", 0.f, circle->offset, 1.f, 0.01f, 0.01f); phys2d_applycircle(circle); } @@ -366,7 +365,7 @@ void phys2d_applycircle(struct phys2d_circle *circle) float radius = circle->radius * go->scale; float s = go->scale; - cpVect offset = { circle->offset[0] * s, circle->offset[1] * s }; + cpVect offset = { circle->offset.x * s, circle->offset.y * s }; cpCircleShapeSetRadius(circle->shape.shape, radius); cpCircleShapeSetOffset(circle->shape.shape, offset); diff --git a/source/engine/2dphysics.h b/source/engine/2dphysics.h index 92c9fce..d23f068 100644 --- a/source/engine/2dphysics.h +++ b/source/engine/2dphysics.h @@ -25,7 +25,7 @@ struct phys2d_shape { struct phys2d_circle { float radius; - float offset[2]; + cpVect offset; struct phys2d_shape shape; }; diff --git a/source/engine/ffi.c b/source/engine/ffi.c index 0a98714..0064dac 100644 --- a/source/engine/ffi.c +++ b/source/engine/ffi.c @@ -45,6 +45,12 @@ cpVect duk2vec2(duk_context *duk, int p) { return pos; } +void vec2float(cpVect v, float *f) +{ + f[0] = v.x; + f[1] = v.y; +} + duk_ret_t duk_gui_text(duk_context *duk) { const char *s = duk_to_string(duk, 0); cpVect pos = duk2vec2(duk, 1); @@ -80,7 +86,7 @@ duk_ret_t duk_nuke(duk_context *duk) case 2: editnum = duk_to_number(duk, 2); - nuke_property_float(duk_to_string(duk, 1), 0.f, &editnum, 100.f, 0.01f, 0.01f); + nuke_property_float(duk_to_string(duk, 1), duk_to_number(duk, 3), &editnum, duk_to_number(duk, 4), duk_to_number(duk, 5), duk_to_number(duk, 5)); duk_push_number(duk, editnum); return 1; @@ -261,6 +267,15 @@ duk_ret_t duk_cmd(duk_context *duk) { case 35: ((struct timer*)duk_to_pointer(duk, 1))->repeat = duk_to_boolean(duk, 2); return 0; + + case 36: + id2go(duk_to_int(duk, 1))->scale = duk_to_number(duk, 2); + return 0; + + case 37: + if (!id2sprite(duk_to_int(duk, 1))) return 0; + vec2float(duk2vec2(duk, 2), id2sprite(duk_to_int(duk, 1))->pos); + break; } return 0; @@ -515,7 +530,11 @@ duk_ret_t duk_make_box2d(duk_context *duk) { phys2d_applybox(box); - duk_push_pointer(duk, &box->shape); + int idx = duk_push_object(duk); + duk_push_pointer(duk, &box->shape); + duk_put_prop_string(duk, idx, "id"); + duk_push_pointer(duk, box); + duk_put_prop_string(duk, idx, "shape"); return 1; } @@ -524,19 +543,33 @@ duk_ret_t duk_cmd_box2d(duk_context *duk) { int cmd = duk_to_int(duk, 0); struct phys2d_box *box = duk_to_pointer(duk, 1); + cpVect arg = duk2vec2(duk, 2); + if (!box) return 0; + + switch(cmd) { + case 0: + box->w = arg.x; + box->h = arg.y; + break; + + case 1: + box->offset[0] = arg.x; + box->offset[1] = arg.y; + break; + } + + phys2d_applybox(box); return 0; } duk_ret_t duk_make_circle2d(duk_context *duk) { int go = duk_to_int(duk, 0); double radius = duk_to_number(duk, 1); - cpVect offset = duk2vec2(duk, 2); struct phys2d_circle *circle = Make2DCircle(go); circle->radius = radius; - circle->offset[0] = offset.x; - circle->offset[1] = offset.y; + circle->offset = duk2vec2(duk, 2); phys2d_applycircle(circle); @@ -559,12 +592,14 @@ duk_ret_t duk_cmd_circle2d(duk_context *duk) switch(cmd) { case 0: circle->radius = duk_to_number(duk, 2); - phys2d_applycircle(circle); - return 0; + break; + case 1: + circle->offset = duk2vec2(duk, 2); + break; } - + phys2d_applycircle(circle); return 0; } diff --git a/source/engine/openglrender.c b/source/engine/openglrender.c index 3eae7fb..2efcff5 100644 --- a/source/engine/openglrender.c +++ b/source/engine/openglrender.c @@ -124,7 +124,10 @@ void add_zoom(float val) { zoom = val; } void openglRender(struct window *window) { + glCullFace(GL_BACK); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //////////// 2D projection mfloat_t projection[16] = { 0.f }; cpVect pos = cam_pos(); diff --git a/source/engine/sprite.c b/source/engine/sprite.c index 385e197..2571452 100644 --- a/source/engine/sprite.c +++ b/source/engine/sprite.c @@ -58,6 +58,7 @@ void sprite_enabled(int id, int e) } struct sprite *id2sprite(int id) { + if (id < 0) return NULL; return &sprites[id]; } @@ -180,6 +181,7 @@ void sprite_draw(struct sprite *sprite) void sprite_setanim(struct sprite *sprite, struct TexAnim *anim, int frame) { + if (!sprite) return; sprite->tex = anim->tex; sprite->frame = &anim->st_frames[frame]; }