2D sprite animations; timers
This commit is contained in:
parent
600426be8f
commit
695c102ce3
|
@ -174,6 +174,34 @@ duk_ret_t duk_cmd(duk_context *duk) {
|
||||||
case 23:
|
case 23:
|
||||||
duk_push_boolean(duk, shape_is_enabled(duk_to_pointer(duk, 1)));
|
duk_push_boolean(duk, shape_is_enabled(duk_to_pointer(duk, 1)));
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
|
case 24:
|
||||||
|
timer_pause(id2timer(duk_to_int(duk, 1)));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 25:
|
||||||
|
timer_stop(id2timer(duk_to_int(duk, 1)));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 26:
|
||||||
|
timer_start(id2timer(duk_to_int(duk, 1)));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 27:
|
||||||
|
timer_remove(id2timer(duk_to_int(duk, 1)));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 28:
|
||||||
|
timerr_settime(id2timer(duk_to_int(duk, 1)), duk_to_number(duk, 2));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 29:
|
||||||
|
duk_push_number(duk, id2timer(duk_to_int(duk, 1))->interval);
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
case 30:
|
||||||
|
sprite_setanim(id2sprite(duk_to_int(duk, 1)), duk_to_pointer(duk, 2), duk_to_int(duk, 3));
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -401,19 +429,13 @@ duk_ret_t duk_make_sprite(duk_context *duk) {
|
||||||
|
|
||||||
/* Make anim from texture */
|
/* Make anim from texture */
|
||||||
duk_ret_t duk_make_anim2d(duk_context *duk) {
|
duk_ret_t duk_make_anim2d(duk_context *duk) {
|
||||||
int go = duk_to_int(duk, 0);
|
const char *path = duk_to_string(duk, 0);
|
||||||
const char *path = duk_to_string(duk, 1);
|
int frames = duk_to_int(duk, 1);
|
||||||
int frames = duk_to_int(duk, 2);
|
int fps = duk_to_int(duk, 2);
|
||||||
int fps = duk_to_int(duk, 3);
|
|
||||||
|
|
||||||
int sprite = make_sprite(go);
|
struct TexAnim *anim = anim2d_from_tex(path, frames, fps);
|
||||||
struct sprite *sp = id2sprite(sprite);
|
|
||||||
sp->pos[0] = -0.5f;
|
|
||||||
sp->pos[1] = -0.5f;
|
|
||||||
anim_load(&sp->anim, path);
|
|
||||||
sp->tex = sp->anim.anim->tex;
|
|
||||||
|
|
||||||
duk_push_int(duk, sprite);
|
duk_push_pointer(duk, anim);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -435,15 +457,6 @@ duk_ret_t duk_make_box2d(duk_context *duk) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
duk_ret_t duk_box2d_cmd(duk_context *duk) {
|
|
||||||
int cmd = duk_to_int(duk, 0);
|
|
||||||
struct phys2d_box *box = duk_to_pointer(duk, 1);
|
|
||||||
|
|
||||||
YughInfo("Issuing command %d with box %p.", cmd, box);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
duk_ret_t duk_make_circle2d(duk_context *duk) {
|
duk_ret_t duk_make_circle2d(duk_context *duk) {
|
||||||
int go = duk_to_int(duk, 0);
|
int go = duk_to_int(duk, 0);
|
||||||
double radius = duk_to_number(duk, 1);
|
double radius = duk_to_number(duk, 1);
|
||||||
|
@ -461,6 +474,7 @@ duk_ret_t duk_make_circle2d(duk_context *duk) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* These are anims for controlling properties on an object */
|
||||||
duk_ret_t duk_anim(duk_context *duk) {
|
duk_ret_t duk_anim(duk_context *duk) {
|
||||||
void *prop = duk_get_heapptr(duk, 0);
|
void *prop = duk_get_heapptr(duk, 0);
|
||||||
int keyframes = duk_get_length(duk, 1);
|
int keyframes = duk_get_length(duk, 1);
|
||||||
|
@ -488,18 +502,17 @@ duk_ret_t duk_anim(duk_context *duk) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
duk_ret_t duk_make_timer(duk_context *duk) {
|
||||||
|
void *sym = duk_get_heapptr(duk, 0);
|
||||||
|
double secs = duk_to_number(duk, 1);
|
||||||
|
void *obj = duk_get_heapptr(duk, 2);
|
||||||
|
struct callee *c = malloc(sizeof(*c));
|
||||||
|
c->fn = sym;
|
||||||
|
c->obj = obj;
|
||||||
|
struct timer *timer = timer_make(secs, call_callee, c);
|
||||||
|
|
||||||
|
duk_push_int(duk, timer->timerid);
|
||||||
duk_ret_t duk_anim_cmd(duk_context *duk) {
|
return 1;
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
duk_ret_t duk_timer(duk_context *duk) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
duk_ret_t duk_timer_cmd(duk_context *duk) {
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define DUK_FUNC(NAME, ARGS) duk_push_c_function(duk, duk_##NAME, ARGS); duk_put_global_string(duk, #NAME);
|
#define DUK_FUNC(NAME, ARGS) duk_push_c_function(duk, duk_##NAME, ARGS); duk_put_global_string(duk, #NAME);
|
||||||
|
@ -515,9 +528,11 @@ void ffi_load()
|
||||||
DUK_FUNC(win_make, 3);
|
DUK_FUNC(win_make, 3);
|
||||||
|
|
||||||
DUK_FUNC(make_sprite, 3);
|
DUK_FUNC(make_sprite, 3);
|
||||||
DUK_FUNC(make_anim2d, 4);
|
DUK_FUNC(make_anim2d, 3);
|
||||||
DUK_FUNC(make_box2d, 3);
|
DUK_FUNC(make_box2d, 3);
|
||||||
DUK_FUNC(make_circle2d, 3);
|
DUK_FUNC(make_circle2d, 3);
|
||||||
|
DUK_FUNC(make_timer, 3);
|
||||||
|
|
||||||
DUK_FUNC(cmd, DUK_VARARGS);
|
DUK_FUNC(cmd, DUK_VARARGS);
|
||||||
DUK_FUNC(register, 3);
|
DUK_FUNC(register, 3);
|
||||||
DUK_FUNC(register_collide, 4);
|
DUK_FUNC(register_collide, 4);
|
||||||
|
@ -525,9 +540,7 @@ void ffi_load()
|
||||||
DUK_FUNC(gui_text, 3);
|
DUK_FUNC(gui_text, 3);
|
||||||
DUK_FUNC(gui_img, 2);
|
DUK_FUNC(gui_img, 2);
|
||||||
|
|
||||||
DUK_FUNC(timer, 2);
|
|
||||||
DUK_FUNC(timer_cmd, 2);
|
|
||||||
|
|
||||||
DUK_FUNC(anim, 2);
|
DUK_FUNC(anim, 2);
|
||||||
DUK_FUNC(anim_cmd, 3);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -156,9 +156,9 @@ void register_physics(struct callee c) {
|
||||||
arrput(physics, c);
|
arrput(physics, c);
|
||||||
}
|
}
|
||||||
|
|
||||||
void call_callee(struct callee c) {
|
void call_callee(struct callee *c) {
|
||||||
duk_push_heapptr(duk, c.fn);
|
duk_push_heapptr(duk, c->fn);
|
||||||
duk_push_heapptr(duk, c.obj);
|
duk_push_heapptr(duk, c->obj);
|
||||||
|
|
||||||
if (duk_pcall_method(duk, 0))
|
if (duk_pcall_method(duk, 0))
|
||||||
duk_run_err();
|
duk_run_err();
|
||||||
|
@ -185,7 +185,7 @@ void call_updates(double dt) {
|
||||||
|
|
||||||
void call_gui() {
|
void call_gui() {
|
||||||
for (int i = 0; i < arrlen(guis); i++)
|
for (int i = 0; i < arrlen(guis); i++)
|
||||||
call_callee(guis[i]);
|
call_callee(&guis[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void call_physics(double dt) {
|
void call_physics(double dt) {
|
||||||
|
|
|
@ -21,6 +21,7 @@ void script_editor();
|
||||||
void script_call(const char *f);
|
void script_call(const char *f);
|
||||||
void script_call_sym(void *sym);
|
void script_call_sym(void *sym);
|
||||||
void script_call_sym_args(void *sym, void *args);
|
void script_call_sym_args(void *sym, void *args);
|
||||||
|
void call_callee(struct callee *c);
|
||||||
int script_has_sym(void *sym);
|
int script_has_sym(void *sym);
|
||||||
void script_eval_w_env(const char *s, void *env);
|
void script_eval_w_env(const char *s, void *env);
|
||||||
time_t file_mod_secs(const char *file);
|
time_t file_mod_secs(const char *file);
|
||||||
|
|
|
@ -174,13 +174,14 @@ void sprite_draw(struct sprite *sprite)
|
||||||
cpVect cpos = cpBodyGetPosition(go->body);
|
cpVect cpos = cpBodyGetPosition(go->body);
|
||||||
float pos[2] = {cpos.x, cpos.y};
|
float pos[2] = {cpos.x, cpos.y};
|
||||||
float size[2] = { sprite->size[0] * go->scale * go->flipx, sprite->size[1] * go->scale * go->flipy };
|
float size[2] = { sprite->size[0] * go->scale * go->flipx, sprite->size[1] * go->scale * go->flipy };
|
||||||
|
|
||||||
if (sprite->tex->opts.animation) {
|
|
||||||
tex_draw(sprite->tex, pos, cpBodyGetAngle(go->body), size, sprite->pos, anim_get_rect(&sprite->anim));
|
|
||||||
} else {
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void sprite_setanim(struct sprite *sprite, struct TexAnim *anim, int frame)
|
||||||
|
{
|
||||||
|
sprite->tex = anim->tex;
|
||||||
|
sprite->frame = &anim->st_frames[frame];
|
||||||
}
|
}
|
||||||
|
|
||||||
void gui_draw_img(const char *img, float x, float y) {
|
void gui_draw_img(const char *img, float x, float y) {
|
||||||
|
|
|
@ -30,6 +30,7 @@ void sprite_enabled(int id, int e);
|
||||||
void sprite_io(struct sprite *sprite, FILE *f, int read);
|
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);
|
||||||
void sprite_settex(struct sprite *sprite, struct Texture *tex);
|
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);
|
void sprite_setframe(struct sprite *sprite, struct glrect *frame);
|
||||||
void sprite_initialize();
|
void sprite_initialize();
|
||||||
void sprite_draw(struct sprite *sprite);
|
void sprite_draw(struct sprite *sprite);
|
||||||
|
|
|
@ -174,6 +174,16 @@ void anim_setframe(struct anim2d *anim, int frame)
|
||||||
anim_calc(anim);
|
anim_calc(anim);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct TexAnim *anim2d_from_tex(const char *path, int frames, int fps)
|
||||||
|
{
|
||||||
|
struct TexAnim *anim = malloc(sizeof(*anim));
|
||||||
|
anim->tex = texture_loadfromfile(path);
|
||||||
|
texanim_fromframes(anim, frames);
|
||||||
|
anim->ms = (float)1/fps;
|
||||||
|
|
||||||
|
return anim;
|
||||||
|
}
|
||||||
|
|
||||||
void texanim_fromframes(struct TexAnim *anim, int frames)
|
void texanim_fromframes(struct TexAnim *anim, int frames)
|
||||||
{
|
{
|
||||||
if (anim->st_frames) free(anim->st_frames);
|
if (anim->st_frames) free(anim->st_frames);
|
||||||
|
|
|
@ -73,7 +73,7 @@ void tex_bind(struct Texture *tex); // Bind to gl context
|
||||||
|
|
||||||
char * tex_get_path(struct Texture *tex); // Get image path for texture
|
char * tex_get_path(struct Texture *tex); // Get image path for texture
|
||||||
|
|
||||||
struct anim2d *anim2d_from_tex(const char *path, int frames, int fps);
|
struct TexAnim *anim2d_from_tex(const char *path, int frames, int fps);
|
||||||
void texanim_fromframes(struct TexAnim *anim, int frames);
|
void texanim_fromframes(struct TexAnim *anim, int frames);
|
||||||
|
|
||||||
void anim_load(struct anim2d *anim, const char *path); /* Load and start new animation */
|
void anim_load(struct anim2d *anim, const char *path); /* Load and start new animation */
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include "log.h"
|
||||||
|
|
||||||
#include <stb_ds.h>
|
#include <stb_ds.h>
|
||||||
|
|
||||||
|
@ -14,7 +15,6 @@ void check_timer(struct timer *t, double dt)
|
||||||
|
|
||||||
if (t->remain_time <= 0) {
|
if (t->remain_time <= 0) {
|
||||||
t->cb(t->data);
|
t->cb(t->data);
|
||||||
|
|
||||||
if (t->repeat) {
|
if (t->repeat) {
|
||||||
t->remain_time = t->interval;
|
t->remain_time = t->interval;
|
||||||
return;
|
return;
|
||||||
|
@ -66,6 +66,7 @@ void timer_start(struct timer *t) {
|
||||||
|
|
||||||
void timer_remove(struct timer *t) {
|
void timer_remove(struct timer *t) {
|
||||||
int i = t->timerid;
|
int i = t->timerid;
|
||||||
|
free(t->data);
|
||||||
arrdelswap(timers, i);
|
arrdelswap(timers, i);
|
||||||
timers[i].timerid = i;
|
timers[i].timerid = i;
|
||||||
}
|
}
|
||||||
|
@ -74,3 +75,8 @@ void timerr_settime(struct timer *t, double interval) {
|
||||||
t->remain_time += (interval - t->interval);
|
t->remain_time += (interval - t->interval);
|
||||||
t->interval = interval;
|
t->interval = interval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct timer *id2timer(int id)
|
||||||
|
{
|
||||||
|
return &timers[id];
|
||||||
|
}
|
|
@ -9,9 +9,11 @@ struct timer {
|
||||||
double remain_time; // How much time until the timer executes
|
double remain_time; // How much time until the timer executes
|
||||||
void (*cb)(void *data);
|
void (*cb)(void *data);
|
||||||
void *data;
|
void *data;
|
||||||
|
int ownd
|
||||||
};
|
};
|
||||||
|
|
||||||
struct timer *timer_make(double interval, void (*callback)(void *param), void *param);
|
struct timer *timer_make(double interval, void (*callback)(void *param), void *param);
|
||||||
|
struct timer *id2timer(int id);
|
||||||
void timer_remove(struct timer *t);
|
void timer_remove(struct timer *t);
|
||||||
void timer_start(struct timer *t);
|
void timer_start(struct timer *t);
|
||||||
void timer_pause(struct timer *t);
|
void timer_pause(struct timer *t);
|
||||||
|
|
Loading…
Reference in a new issue