Extended timer functions

This commit is contained in:
John Alanbrook 2023-01-19 16:44:29 +00:00
parent 695c102ce3
commit 63eb212dcd
8 changed files with 64 additions and 27 deletions

View file

@ -227,6 +227,22 @@ void box_gui(struct phys2d_box *box)
phys2d_applybox(box);
}
void phys2d_applybox(struct phys2d_box *box)
{
float s = id2go(box->shape.go)->scale;
cpTransform T = { 0 };
T.a = s;
T.d = s;
T.tx = box->offset[0] * s;
T.ty = box->offset[1] * s;
float hh = box->h / 2.f;
float hw = box->w / 2.f;
cpVect verts[4] =
{ { -hw, -hh }, { hw, -hh }, { hw, hh }, { -hw, hh } };
cpPolyShapeSetVerts(box->shape.shape, 4, verts, T);
cpPolyShapeSetRadius(box->shape.shape, box->r);
}
/************** POLYGON ************/
struct phys2d_poly *Make2DPoly(int go)
@ -366,21 +382,7 @@ void phys2d_applyseg(struct phys2d_segment *seg)
cpSegmentShapeSetRadius(seg->shape.shape, seg->thickness * s);
}
void phys2d_applybox(struct phys2d_box *box)
{
float s = id2go(box->shape.go)->scale;
cpTransform T = { 0 };
T.a = s;
T.d = s;
T.tx = box->offset[0] * s;
T.ty = box->offset[1] * s;
float hh = box->h / 2.f;
float hw = box->w / 2.f;
cpVect verts[4] =
{ { -hw, -hh }, { hw, -hh }, { hw, hh }, { -hw, hh } };
cpPolyShapeSetVerts(box->shape.shape, 4, verts, T);
cpPolyShapeSetRadius(box->shape.shape, box->r);
}
void phys2d_applypoly(struct phys2d_poly *poly)
{

View file

@ -202,6 +202,26 @@ duk_ret_t duk_cmd(duk_context *duk) {
case 30:
sprite_setanim(id2sprite(duk_to_int(duk, 1)), duk_to_pointer(duk, 2), duk_to_int(duk, 3));
return 0;
case 31:
free(duk_to_pointer(duk, 1));
break;
case 32:
duk_push_number(duk, id2timer(duk_to_int(duk, 1))->remain_time);
return 1;
case 33:
duk_push_boolean(duk, ((struct timer*)duk_to_pointer(duk, 1))->on);
return 1;
case 34:
duk_push_boolean(duk, ((struct timer*)duk_to_pointer(duk, 1))->repeat);
return 1;
case 35:
((struct timer*)duk_to_pointer(duk, 1))->repeat = duk_to_boolean(duk, 2);
return 0;
}
return 0;
@ -457,6 +477,14 @@ duk_ret_t duk_make_box2d(duk_context *duk) {
return 1;
}
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);
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);
@ -474,6 +502,11 @@ duk_ret_t duk_make_circle2d(duk_context *duk) {
return 1;
}
duk_ret_t duk_cmd_circle2d(duk_context *duk)
{
return 0;
}
/* These are anims for controlling properties on an object */
duk_ret_t duk_anim(duk_context *duk) {
void *prop = duk_get_heapptr(duk, 0);
@ -509,7 +542,7 @@ duk_ret_t duk_make_timer(duk_context *duk) {
struct callee *c = malloc(sizeof(*c));
c->fn = sym;
c->obj = obj;
struct timer *timer = timer_make(secs, call_callee, c);
struct timer *timer = timer_make(secs, call_callee, c, 1);
duk_push_int(duk, timer->timerid);
return 1;
@ -530,7 +563,9 @@ void ffi_load()
DUK_FUNC(make_sprite, 3);
DUK_FUNC(make_anim2d, 3);
DUK_FUNC(make_box2d, 3);
DUK_FUNC(cmd_box2d, DUK_VARARGS);
DUK_FUNC(make_circle2d, 3);
DUK_FUNC(cmd_circle2d, DUK_VARARGS);
DUK_FUNC(make_timer, 3);
DUK_FUNC(cmd, DUK_VARARGS);

View file

@ -348,11 +348,11 @@ void body_draw_shapes_dbg(cpBody *body, cpShape *shape, void *data) {
void gameobject_draw_debugs() {
for (int i = 0; i < arrlen(gameobjects); i++) {
if (!gameobjects[i].body) continue;
cpVect pos = cpBodyGetPosition(gameobjects[i].body);
float color[3] = {0.76f, 0.38f, 1.f};
draw_point(pos.x, pos.y, 3.f, color);
if (!gameobjects[i].body) continue;
cpBodyEachShape(gameobjects[i].body, body_draw_shapes_dbg, NULL);
}

View file

@ -132,11 +132,11 @@ void tex_draw(struct Texture *tex, float pos[2], float angle, float size[2], flo
memcpy(s_model, UNITMAT4, sizeof(UNITMAT4));
mfloat_t t_scale[2] = { tex->width * st_s_w(r), tex->height * st_s_h(r) };
mfloat_t t_offset[2] = { offset[0] * t_scale[0], offset[1] * t_scale[1] };
mfloat_t t_offset[2] = { offset[0] * t_scale[0] * size[0], offset[1] * t_scale[1] * size[1]};
mat4_translate_vec2(model, t_offset);
mat4_scale_vec2(model, t_scale);
mat4_scale_vec2(model, t_scale);
mat4_rotation_z(r_model, angle);
mat4_multiply(model, r_model, model);

View file

@ -246,7 +246,7 @@ void anim_play(struct anim2d *anim)
anim->playing = 1;
if (anim->timer == NULL)
anim->timer = timer_make(1.f / anim->anim->ms, anim_incr, anim);
anim->timer = timer_make(1.f / anim->anim->ms, anim_incr, anim, 0);
else
timerr_settime(anim->timer, 1.f/anim->anim->ms);

View file

@ -30,7 +30,7 @@ void timer_update(double dt) {
check_timer(&timers[i], dt);
}
struct timer *timer_make(double interval, void (*callback)(void *param), void *param) {
struct timer *timer_make(double interval, void (*callback)(void *param), void *param, int own) {
struct timer new;
new.remain_time = interval;
new.interval = interval;
@ -38,6 +38,7 @@ struct timer *timer_make(double interval, void (*callback)(void *param), void *p
new.data = param;
new.repeat = 1;
new.timerid = arrlen(timers);
new.owndata = own;
timer_start(&new);
arrput(timers, new);
@ -66,7 +67,7 @@ void timer_start(struct timer *t) {
void timer_remove(struct timer *t) {
int i = t->timerid;
free(t->data);
if (t->owndata) free(t->data);
arrdelswap(timers, i);
timers[i].timerid = i;
}

View file

@ -9,10 +9,10 @@ struct timer {
double remain_time; // How much time until the timer executes
void (*cb)(void *data);
void *data;
int ownd
int owndata;
};
struct timer *timer_make(double interval, void (*callback)(void *param), void *param);
struct timer *timer_make(double interval, void (*callback)(void *param), void *param, int own);
struct timer *id2timer(int id);
void timer_remove(struct timer *t);
void timer_start(struct timer *t);

View file

@ -179,9 +179,8 @@ int main(int argc, char **args) {
framems[framei++] = elapsed;
if (framei == FPSBUF) framei = 0;
timer_update(elapsed);
if (sim_play) {
timer_update(elapsed);
physlag += elapsed;
call_updates(elapsed * timescale);