2d animation restructure

This commit is contained in:
John Alanbrook 2023-01-18 20:43:07 +00:00
parent 1a1884f61a
commit 600426be8f
6 changed files with 28 additions and 16 deletions

View file

@ -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 kinematic_color[3] = {255/255, 206/255,71/255};
float static_color[3] = {0.22f, 0.271f, 1.f}; 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) void color2float(struct color color, float *fcolor)
{ {
fcolor[0] = (float)color.r/255; fcolor[0] = (float)color.r/255;
@ -47,9 +45,9 @@ int cpshape_enabled(cpShape *c)
{ {
cpShapeFilter filter = cpShapeGetFilter(c); cpShapeFilter filter = cpShapeGetFilter(c);
if (filter.categories == ~CP_ALL_CATEGORIES && filter.mask == ~CP_ALL_CATEGORIES) 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) float *shape_outline_color(cpShape *shape)
@ -70,7 +68,7 @@ float *shape_outline_color(cpShape *shape)
float *shape_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; if (cpShapeGetSensor(shape)) return trigger_color;

View file

@ -128,7 +128,7 @@ duk_ret_t duk_cmd(duk_context *duk) {
break; break;
case 12: case 12:
//anim2d_delete(duk_to_int(duk, 1)); sprite_loadtex(id2sprite(duk_to_int(duk, 1)), duk_to_string(duk, 2));
break; break;
case 13: case 13:
@ -168,7 +168,7 @@ duk_ret_t duk_cmd(duk_context *duk) {
return 1; return 1;
case 22: 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; break;
case 23: case 23:
@ -399,9 +399,12 @@ duk_ret_t duk_make_sprite(duk_context *duk) {
return 1; return 1;
} }
/* 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); int go = duk_to_int(duk, 0);
const char *path = duk_to_string(duk, 1); 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); int sprite = make_sprite(go);
struct sprite *sp = id2sprite(sprite); struct sprite *sp = id2sprite(sprite);
@ -410,8 +413,6 @@ duk_ret_t duk_make_anim2d(duk_context *duk) {
anim_load(&sp->anim, path); anim_load(&sp->anim, path);
sp->tex = sp->anim.anim->tex; sp->tex = sp->anim.anim->tex;
YughInfo("Made an animation.");
duk_push_int(duk, sprite); duk_push_int(duk, sprite);
return 1; return 1;
} }

View file

@ -92,11 +92,13 @@ void sprite_draw_all()
void sprite_loadtex(struct sprite *sprite, const char *path) void sprite_loadtex(struct sprite *sprite, const char *path)
{ {
sprite->tex = texture_loadfromfile(path); sprite->tex = texture_loadfromfile(path);
sprite_setframe(sprite, &ST_UNIT);
} }
void sprite_settex(struct sprite *sprite, struct Texture *tex) void sprite_settex(struct sprite *sprite, struct Texture *tex)
{ {
sprite->tex = tex; sprite->tex = tex;
sprite_setframe(sprite, &ST_UNIT);
} }
static uint32_t VAO = 0; static uint32_t VAO = 0;
@ -176,7 +178,7 @@ void sprite_draw(struct sprite *sprite)
if (sprite->tex->opts.animation) { if (sprite->tex->opts.animation) {
tex_draw(sprite->tex, pos, cpBodyGetAngle(go->body), size, sprite->pos, anim_get_rect(&sprite->anim)); tex_draw(sprite->tex, pos, cpBodyGetAngle(go->body), size, sprite->pos, anim_get_rect(&sprite->anim));
} else { } 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)); 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]) void video_draw(struct datastream *stream, mfloat_t position[2], mfloat_t size[2], float rotate, mfloat_t color[3])
{ {
shader_use(vid_shader); shader_use(vid_shader);

View file

@ -16,8 +16,8 @@ struct sprite {
mfloat_t color[3]; mfloat_t color[3];
int go; int go;
int id; int id;
struct anim2d anim;
struct Texture *tex; struct Texture *tex;
struct glrect *frame;
int next; int next;
int enabled; 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_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_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);
void video_draw(struct datastream *ds, mfloat_t pos[2], mfloat_t size[2], float rotate, mfloat_t color[3]); void video_draw(struct datastream *ds, mfloat_t pos[2], mfloat_t size[2], float rotate, mfloat_t color[3]);

View file

@ -9,6 +9,8 @@
#include "util.h" #include "util.h"
#include "parson.h" #include "parson.h"
struct glrect ST_UNIT = { 0.f, 1.f, 0.f, 1.f };
static struct { static struct {
char *key; char *key;
struct Texture *value; struct Texture *value;
@ -176,7 +178,7 @@ void texanim_fromframes(struct TexAnim *anim, int frames)
{ {
if (anim->st_frames) free(anim->st_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; float width = (float)1/frames;

View file

@ -19,7 +19,7 @@ struct glrect {
float st_s_w(struct glrect st); float st_s_w(struct glrect st);
float st_s_h(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 */ /* Pixel U,V coordiantes */
struct uvrect { struct uvrect {
@ -30,7 +30,7 @@ struct uvrect {
}; };
/* Tracks a playing animation */ /* 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 { struct anim2d {
int frame; int frame;
int playing; int playing;
@ -42,9 +42,9 @@ struct anim2d {
/* Describes an animation on a particular texture */ /* Describes an animation on a particular texture */
struct TexAnim { struct TexAnim {
struct Texture *tex;
struct glrect *st_frames; /* Dynamic array of frames of animation */ struct glrect *st_frames; /* Dynamic array of frames of animation */
int ms; int ms;
struct Texture *tex;
int loop; int loop;
}; };
@ -55,6 +55,7 @@ struct TextureOptions {
int animation; int animation;
}; };
/* Represents an actual texture on the GPU */
struct Texture { struct Texture {
unsigned int id; /* ID reference for the GPU memory location of the texture */ unsigned int id; /* ID reference for the GPU memory location of the texture */
int width; 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 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 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_calc(struct anim2d *anim);
void anim_play(struct anim2d *anim); void anim_play(struct anim2d *anim);
void anim_setframe(struct anim2d *anim, int frame); void anim_setframe(struct anim2d *anim, int frame);