timer deaths
This commit is contained in:
parent
ac3ce97b80
commit
0826197b91
|
@ -42,6 +42,11 @@ struct gameobject *duk2go(duk_context *duk, int p)
|
|||
return id2go(duk_to_int(duk, p));
|
||||
}
|
||||
|
||||
struct timer *duk2timer(duk_context *duk, int p)
|
||||
{
|
||||
return id2timer(duk_to_int(duk, p));
|
||||
}
|
||||
|
||||
struct color duk2color(duk_context *duk, int p)
|
||||
{
|
||||
struct color color;
|
||||
|
@ -56,6 +61,22 @@ struct color duk2color(duk_context *duk, int p)
|
|||
return color;
|
||||
}
|
||||
|
||||
float duk_get_prop_number(duk_context *duk, int p, const char *str)
|
||||
{
|
||||
duk_get_prop_string(duk,p,str);
|
||||
return duk_to_number(duk,-1);
|
||||
}
|
||||
|
||||
struct glrect duk_to_glrect(duk_context *duk, int p)
|
||||
{
|
||||
struct glrect rect;
|
||||
rect.s0 = duk_get_prop_number(duk,p,"s0");
|
||||
rect.s1 = duk_get_prop_number(duk,p,"s1");
|
||||
rect.t0 = duk_get_prop_number(duk,p,"t0");
|
||||
rect.t1 = duk_get_prop_number(duk,p,"t1");
|
||||
return rect;
|
||||
}
|
||||
|
||||
cpVect duk2vec2(duk_context *duk, int p)
|
||||
{
|
||||
cpVect pos;
|
||||
|
@ -525,7 +546,7 @@ duk_ret_t duk_cmd(duk_context *duk) {
|
|||
break;
|
||||
|
||||
case 12:
|
||||
sprite_loadtex(id2sprite(duk_to_int(duk, 1)), duk_to_string(duk, 2));
|
||||
sprite_loadtex(id2sprite(duk_to_int(duk, 1)), duk_to_string(duk, 2), duk_to_glrect(duk, 3));
|
||||
break;
|
||||
|
||||
case 13:
|
||||
|
@ -573,27 +594,27 @@ duk_ret_t duk_cmd(duk_context *duk) {
|
|||
return 1;
|
||||
|
||||
case 24:
|
||||
timer_pause(id2timer(duk_to_int(duk, 1)));
|
||||
timer_pause(duk2timer(duk,1));
|
||||
break;
|
||||
|
||||
case 25:
|
||||
timer_stop(id2timer(duk_to_int(duk, 1)));
|
||||
timer_stop(duk2timer(duk,1));
|
||||
break;
|
||||
|
||||
case 26:
|
||||
timer_start(id2timer(duk_to_int(duk, 1)));
|
||||
timer_start(duk2timer(duk,1));
|
||||
break;
|
||||
|
||||
case 27:
|
||||
timer_remove(id2timer(duk_to_int(duk, 1)));
|
||||
timer_remove(duk2timer(duk,1));
|
||||
break;
|
||||
|
||||
case 28:
|
||||
timerr_settime(id2timer(duk_to_int(duk, 1)), duk_to_number(duk, 2));
|
||||
timerr_settime(duk2timer(duk,1), duk_to_number(duk, 2));
|
||||
break;
|
||||
|
||||
case 29:
|
||||
duk_push_number(duk, id2timer(duk_to_int(duk, 1))->interval);
|
||||
duk_push_number(duk, duk2timer(duk,1)->interval);
|
||||
return 1;
|
||||
|
||||
case 30:
|
||||
|
@ -605,19 +626,19 @@ duk_ret_t duk_cmd(duk_context *duk) {
|
|||
break;
|
||||
|
||||
case 32:
|
||||
duk_push_number(duk, id2timer(duk_to_int(duk, 1))->remain_time);
|
||||
duk_push_number(duk, duk2timer(duk,1)->remain_time);
|
||||
return 1;
|
||||
|
||||
case 33:
|
||||
duk_push_boolean(duk, ((struct timer*)duk_to_pointer(duk, 1))->on);
|
||||
duk_push_boolean(duk, duk2timer(duk, 1)->on);
|
||||
return 1;
|
||||
|
||||
case 34:
|
||||
duk_push_boolean(duk, ((struct timer*)duk_to_pointer(duk, 1))->repeat);
|
||||
duk_push_boolean(duk, duk2timer(duk,1)->repeat);
|
||||
return 1;
|
||||
|
||||
case 35:
|
||||
((struct timer*)duk_to_pointer(duk, 1))->repeat = duk_to_boolean(duk, 2);
|
||||
duk2timer(duk,1)->repeat = duk_to_boolean(duk, 2);
|
||||
return 0;
|
||||
|
||||
case 36:
|
||||
|
@ -1035,7 +1056,7 @@ duk_ret_t duk_make_sprite(duk_context *duk) {
|
|||
cpVect pos = duk2vec2(duk, 2);
|
||||
int sprite = make_sprite(go);
|
||||
struct sprite *sp = id2sprite(sprite);
|
||||
sprite_loadtex(sp, path);
|
||||
sprite_loadtex(sp, path, ST_UNIT);
|
||||
sp->pos[0] = pos.x;
|
||||
sp->pos[1] = pos.y;
|
||||
|
||||
|
|
|
@ -74,7 +74,7 @@ void sprite_io(struct sprite *sprite, FILE *f, int read)
|
|||
if (path[i] == '\0') break;
|
||||
}
|
||||
fread(sprite, sizeof(*sprite), 1, f);
|
||||
sprite_loadtex(sprite, path);
|
||||
sprite_loadtex(sprite, path, ST_UNIT);
|
||||
} else {
|
||||
fputs(tex_get_path(sprite->tex), f);
|
||||
fputc('\0', f);
|
||||
|
@ -98,10 +98,10 @@ void sprite_draw_all()
|
|||
sprite_draw(layers[i][j]);
|
||||
}
|
||||
|
||||
void sprite_loadtex(struct sprite *sprite, const char *path)
|
||||
void sprite_loadtex(struct sprite *sprite, const char *path, struct glrect frame)
|
||||
{
|
||||
sprite->tex = texture_loadfromfile(path);
|
||||
sprite_setframe(sprite, &ST_UNIT);
|
||||
sprite_setframe(sprite, &frame);
|
||||
}
|
||||
|
||||
void sprite_settex(struct sprite *sprite, struct Texture *tex)
|
||||
|
@ -175,7 +175,7 @@ void sprite_draw(struct sprite *sprite)
|
|||
cpVect cpos = cpBodyGetPosition(go->body);
|
||||
float pos[2] = {cpos.x, cpos.y};
|
||||
float size[2] = { sprite->size[0] * go->scale * go->flipx, sprite->size[1] * go->scale * go->flipy };
|
||||
tex_draw(sprite->tex, pos, cpBodyGetAngle(go->body), size, sprite->pos, *sprite->frame);
|
||||
tex_draw(sprite->tex, pos, cpBodyGetAngle(go->body), size, sprite->pos, sprite->frame);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -183,7 +183,7 @@ void sprite_setanim(struct sprite *sprite, struct TexAnim *anim, int frame)
|
|||
{
|
||||
if (!sprite) return;
|
||||
sprite->tex = anim->tex;
|
||||
sprite->frame = &anim->st_frames[frame];
|
||||
sprite->frame = anim->st_frames[frame];
|
||||
}
|
||||
|
||||
void gui_draw_img(const char *img, float x, float y) {
|
||||
|
@ -197,7 +197,7 @@ void gui_draw_img(const char *img, float x, float y) {
|
|||
|
||||
void sprite_setframe(struct sprite *sprite, struct glrect *frame)
|
||||
{
|
||||
sprite->frame = frame;
|
||||
sprite->frame = *frame;
|
||||
}
|
||||
|
||||
void video_draw(struct datastream *stream, mfloat_t position[2], mfloat_t size[2], float rotate, mfloat_t color[3])
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include "mathc.h"
|
||||
#include "texture.h"
|
||||
|
||||
|
||||
struct datastream;
|
||||
struct gameobject;
|
||||
|
||||
|
@ -17,7 +18,7 @@ struct sprite {
|
|||
int go;
|
||||
int id;
|
||||
struct Texture *tex;
|
||||
struct glrect *frame;
|
||||
struct glrect frame;
|
||||
int next;
|
||||
int enabled;
|
||||
int layer;
|
||||
|
@ -28,7 +29,7 @@ struct sprite *id2sprite(int id);
|
|||
void sprite_delete(int id);
|
||||
void sprite_enabled(int id, int e);
|
||||
void sprite_io(struct sprite *sprite, FILE *f, int read);
|
||||
void sprite_loadtex(struct sprite *sprite, const char *path);
|
||||
void sprite_loadtex(struct sprite *sprite, const char *path, struct glrect rect);
|
||||
void sprite_settex(struct sprite *sprite, struct Texture *tex);
|
||||
void sprite_setanim(struct sprite *sprite, struct TexAnim *anim, int frame);
|
||||
void sprite_setframe(struct sprite *sprite, struct glrect *frame);
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#include <stb_ds.h>
|
||||
|
||||
struct timer *timers;
|
||||
static int first = -1;
|
||||
|
||||
void check_timer(struct timer *t, double dt)
|
||||
{
|
||||
|
@ -39,11 +40,18 @@ struct timer *timer_make(double interval, void (*callback)(void *param), void *p
|
|||
new.repeat = 1;
|
||||
new.timerid = arrlen(timers);
|
||||
new.owndata = own;
|
||||
|
||||
timer_start(&new);
|
||||
arrput(timers, new);
|
||||
|
||||
return &arrlast(timers);
|
||||
|
||||
if (first <0) {
|
||||
timer_start(&new);
|
||||
arrput(timers, new);
|
||||
return &arrlast(timers);
|
||||
} else {
|
||||
int retid = first;
|
||||
first = id2timer(first)->next;
|
||||
*id2timer(retid) = new;
|
||||
timer_start(id2timer(retid));
|
||||
return id2timer(retid);
|
||||
}
|
||||
}
|
||||
|
||||
void timer_pause(struct timer *t) {
|
||||
|
@ -54,21 +62,19 @@ void timer_pause(struct timer *t) {
|
|||
|
||||
void timer_stop(struct timer *t) {
|
||||
if (!t->on) return;
|
||||
|
||||
t->on = 0;
|
||||
t->remain_time = t->interval;
|
||||
}
|
||||
|
||||
void timer_start(struct timer *t) {
|
||||
if (t->on) return;
|
||||
|
||||
t->on = 1;
|
||||
}
|
||||
|
||||
void timer_remove(struct timer *t) {
|
||||
int i = t->timerid;
|
||||
if (t->owndata) free(t->data);
|
||||
arrdelswap(timers, i);
|
||||
timers[i].timerid =
|
||||
timers[i].timerid = i;
|
||||
}
|
||||
|
||||
|
@ -80,4 +86,4 @@ void timerr_settime(struct timer *t, double interval) {
|
|||
struct timer *id2timer(int id)
|
||||
{
|
||||
return &timers[id];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,12 +4,13 @@
|
|||
struct timer {
|
||||
int timerid;
|
||||
int on;
|
||||
double interval; // Time of timer
|
||||
double interval; // Time of timer
|
||||
int repeat;
|
||||
double remain_time; // How much time until the timer executes
|
||||
void (*cb)(void *data);
|
||||
void *data;
|
||||
int owndata;
|
||||
int next;
|
||||
};
|
||||
|
||||
struct timer *timer_make(double interval, void (*callback)(void *param), void *param, int own);
|
||||
|
|
Loading…
Reference in a new issue