From 600426be8f2248ca847931e9182e62c6e8033232 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Wed, 18 Jan 2023 20:43:07 +0000 Subject: [PATCH] 2d animation restructure --- source/engine/2dphysics.c | 8 +++----- source/engine/ffi.c | 9 +++++---- source/engine/sprite.c | 9 ++++++++- source/engine/sprite.h | 3 ++- source/engine/texture.c | 4 +++- source/engine/texture.h | 11 +++++++---- 6 files changed, 28 insertions(+), 16 deletions(-) diff --git a/source/engine/2dphysics.c b/source/engine/2dphysics.c index 120f36d..c39b3e5 100644 --- a/source/engine/2dphysics.c +++ b/source/engine/2dphysics.c @@ -25,8 +25,6 @@ float dynamic_color[3] = {255/255, 70/255, 46/255}; float kinematic_color[3] = {255/255, 206/255,71/255}; float static_color[3] = {0.22f, 0.271f, 1.f}; -static struct color static_color = {56, 69, 255}; - void color2float(struct color color, float *fcolor) { fcolor[0] = (float)color.r/255; @@ -47,9 +45,9 @@ int cpshape_enabled(cpShape *c) { cpShapeFilter filter = cpShapeGetFilter(c); if (filter.categories == ~CP_ALL_CATEGORIES && filter.mask == ~CP_ALL_CATEGORIES) - return 1; + return 0; - return 0; + return 1; } float *shape_outline_color(cpShape *shape) @@ -70,7 +68,7 @@ float *shape_outline_color(cpShape *shape) float *shape_color(cpShape *shape) { - if (cpshape_enabled(shape)) return disabled_color; + if (!cpshape_enabled(shape)) return disabled_color; if (cpShapeGetSensor(shape)) return trigger_color; diff --git a/source/engine/ffi.c b/source/engine/ffi.c index 74f528f..d7f0a78 100644 --- a/source/engine/ffi.c +++ b/source/engine/ffi.c @@ -128,7 +128,7 @@ duk_ret_t duk_cmd(duk_context *duk) { break; case 12: - //anim2d_delete(duk_to_int(duk, 1)); + sprite_loadtex(id2sprite(duk_to_int(duk, 1)), duk_to_string(duk, 2)); break; case 13: @@ -168,7 +168,7 @@ duk_ret_t duk_cmd(duk_context *duk) { return 1; case 22: - shape_enabled(duk_to_pointer(duk, 1), duk_to_int(duk, 2)); + shape_enabled(duk_to_pointer(duk, 1), duk_to_boolean(duk, 2)); break; case 23: @@ -399,9 +399,12 @@ duk_ret_t duk_make_sprite(duk_context *duk) { return 1; } +/* Make anim from texture */ duk_ret_t duk_make_anim2d(duk_context *duk) { int go = duk_to_int(duk, 0); const char *path = duk_to_string(duk, 1); + int frames = duk_to_int(duk, 2); + int fps = duk_to_int(duk, 3); int sprite = make_sprite(go); struct sprite *sp = id2sprite(sprite); @@ -410,8 +413,6 @@ duk_ret_t duk_make_anim2d(duk_context *duk) { anim_load(&sp->anim, path); sp->tex = sp->anim.anim->tex; - YughInfo("Made an animation."); - duk_push_int(duk, sprite); return 1; } diff --git a/source/engine/sprite.c b/source/engine/sprite.c index 0cc14c7..45a591b 100644 --- a/source/engine/sprite.c +++ b/source/engine/sprite.c @@ -92,11 +92,13 @@ void sprite_draw_all() void sprite_loadtex(struct sprite *sprite, const char *path) { sprite->tex = texture_loadfromfile(path); + sprite_setframe(sprite, &ST_UNIT); } void sprite_settex(struct sprite *sprite, struct Texture *tex) { sprite->tex = tex; + sprite_setframe(sprite, &ST_UNIT); } static uint32_t VAO = 0; @@ -176,7 +178,7 @@ void sprite_draw(struct sprite *sprite) 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, tex_get_rect(sprite->tex)); + tex_draw(sprite->tex, pos, cpBodyGetAngle(go->body), size, sprite->pos, *sprite->frame); } } } @@ -190,6 +192,11 @@ void gui_draw_img(const char *img, float x, float y) { tex_draw(tex, pos, 0.f, size, offset, tex_get_rect(tex)); } +void sprite_setframe(struct sprite *sprite, struct glrect *frame) +{ + sprite->frame = frame; +} + void video_draw(struct datastream *stream, mfloat_t position[2], mfloat_t size[2], float rotate, mfloat_t color[3]) { shader_use(vid_shader); diff --git a/source/engine/sprite.h b/source/engine/sprite.h index 13660af..6b61d64 100644 --- a/source/engine/sprite.h +++ b/source/engine/sprite.h @@ -16,8 +16,8 @@ struct sprite { mfloat_t color[3]; int go; int id; - struct anim2d anim; struct Texture *tex; + struct glrect *frame; int next; int enabled; }; @@ -30,6 +30,7 @@ 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_settex(struct sprite *sprite, struct Texture *tex); +void sprite_setframe(struct sprite *sprite, struct glrect *frame); void sprite_initialize(); void sprite_draw(struct sprite *sprite); void video_draw(struct datastream *ds, mfloat_t pos[2], mfloat_t size[2], float rotate, mfloat_t color[3]); diff --git a/source/engine/texture.c b/source/engine/texture.c index b866c4e..7878ea8 100644 --- a/source/engine/texture.c +++ b/source/engine/texture.c @@ -9,6 +9,8 @@ #include "util.h" #include "parson.h" +struct glrect ST_UNIT = { 0.f, 1.f, 0.f, 1.f }; + static struct { char *key; struct Texture *value; @@ -176,7 +178,7 @@ void texanim_fromframes(struct TexAnim *anim, int frames) { if (anim->st_frames) free(anim->st_frames); - anim->st_frames = calloc(frames, sizeof(*anim->st_frames)); + arrsetlen(anim->st_frames, frames); float width = (float)1/frames; diff --git a/source/engine/texture.h b/source/engine/texture.h index 487bf53..6e14b67 100644 --- a/source/engine/texture.h +++ b/source/engine/texture.h @@ -19,7 +19,7 @@ struct glrect { float st_s_w(struct glrect st); float st_s_h(struct glrect st); -#define ST_UNIT (struct glrect) { 0.f, 1.f, 0.f, 1.f } +extern struct glrect ST_UNIT; /* Pixel U,V coordiantes */ struct uvrect { @@ -30,7 +30,7 @@ struct uvrect { }; /* Tracks a playing animation */ -/* Objects should keep this, and simple change what TexAnim they are pointing to */ +/* Objects should keep this, and just change what TexAnim they are pointing to */ struct anim2d { int frame; int playing; @@ -42,9 +42,9 @@ struct anim2d { /* Describes an animation on a particular texture */ struct TexAnim { + struct Texture *tex; struct glrect *st_frames; /* Dynamic array of frames of animation */ int ms; - struct Texture *tex; int loop; }; @@ -55,6 +55,7 @@ struct TextureOptions { int animation; }; +/* Represents an actual texture on the GPU */ struct Texture { unsigned int id; /* ID reference for the GPU memory location of the texture */ int width; @@ -72,8 +73,10 @@ void tex_bind(struct Texture *tex); // Bind to gl context char * tex_get_path(struct Texture *tex); // Get image path for texture -void anim_load(struct anim2d *anim, const char *path); /* Load and start new animation */ +struct anim2d *anim2d_from_tex(const char *path, int frames, int fps); void texanim_fromframes(struct TexAnim *anim, int frames); + +void anim_load(struct anim2d *anim, const char *path); /* Load and start new animation */ void anim_calc(struct anim2d *anim); void anim_play(struct anim2d *anim); void anim_setframe(struct anim2d *anim, int frame);