From eadf3524ba1a51eb53d87d9ab26962204cc8ed05 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Fri, 30 Dec 2022 19:31:06 +0000 Subject: [PATCH] UV and ST --- source/engine/editor/editor.c | 10 ++++----- source/engine/font.h | 10 ++------- source/engine/sprite.c | 30 ++++++++++++++----------- source/engine/sprite.h | 1 - source/engine/texture.c | 42 +++++++++++++---------------------- source/engine/texture.h | 30 ++++++++++++++++++------- 6 files changed, 62 insertions(+), 61 deletions(-) diff --git a/source/engine/editor/editor.c b/source/engine/editor/editor.c index 522782c..60261b8 100644 --- a/source/engine/editor/editor.c +++ b/source/engine/editor/editor.c @@ -869,12 +869,12 @@ void editor_asset_tex_gui(struct Texture *tex) { if (old_frames != tex->anim.frames || old_ms != tex->anim.ms) tex_anim_set(&tex_gui_anim); - nk_layout_row_static(ctx, tex->height*tex_scale*tex_gui_anim.uv.h, tex->width*tex_scale*tex_gui_anim.uv.w, 1); + nk_layout_row_static(ctx, tex->height*tex_scale*(tex_gui_anim.st.t1-tex_gui_anim.st.t0), tex->width*tex_scale*(tex_gui_anim.st.s1 - tex_gui_anim.st.s0), 1); struct nk_rect r; - r.x = tex_gui_anim.uv.x*tex->width; - r.y = tex_gui_anim.uv.y*tex->height; - r.w = tex_gui_anim.uv.w*tex->width; - r.h = tex_gui_anim.uv.h*tex->height; + r.x = tex_gui_anim.st.s0*tex->width; + r.y = tex_gui_anim.st.t0*tex->height; + r.w = st_s_w(tex_gui_anim.st)*tex->width; + r.h = st_s_h(tex_gui_anim.st)*tex->height; nk_image(ctx, nk_subimage_id(tex->id, tex->width, tex->height, r)); } else { diff --git a/source/engine/font.h b/source/engine/font.h index e7d9dd0..cdf13b4 100644 --- a/source/engine/font.h +++ b/source/engine/font.h @@ -2,17 +2,11 @@ #define FONT_H #include "mathc.h" +#include "texture.h" struct shader; struct window; -struct glrect { - float s0; - float s1; - float t0; - float t1; -}; - /// Holds all state information relevant to a character as loaded using FreeType struct Character { mfloat_t Size[2]; // Size of glyph @@ -28,7 +22,7 @@ struct sFont { uint32_t texID; }; -struct glrect runit = { 0.f, 1.f, 0.f, 1.f }; + void font_init(struct shader *s); void font_frame(struct window *w); diff --git a/source/engine/sprite.c b/source/engine/sprite.c index 1191124..330445f 100644 --- a/source/engine/sprite.c +++ b/source/engine/sprite.c @@ -1,6 +1,5 @@ #include "sprite.h" - #include "timer.h" #include "render.h" #include "openglrender.h" @@ -87,16 +86,6 @@ void sprite_loadtex(struct sprite *sprite, const char *path) sprite->tex = texture_loadfromfile(path); } -void sprite_loadanim(struct sprite *sprite, const char *path, struct Anim2D anim) -{ - sprite->tex = texture_loadfromfile(path); - sprite->anim = anim; - sprite->anim.timer = timer_make(sprite->anim.ms, &incrementAnimFrame, sprite); -/* - sprite->tex = texture_loadanimfromfile(sprite->tex, path, sprite->anim.frames, sprite->anim.dimensions); -*/ -} - void sprite_settex(struct sprite *sprite, struct Texture *tex) { sprite->tex = tex; @@ -122,7 +111,13 @@ void sprite_initialize() glEnableVertexAttribArray(0); } +struct glrect sprite_get_rect(struct sprite *sprite) { + if (sprite->tex->opts.animation) { + } else { + return tex_get_rect(sprite->tex); + } +} void tex_draw(struct Texture *tex, float pos[2], float angle, float size[2], float offset[2], struct glrect r) { mfloat_t model[16] = { 0.f }; @@ -173,9 +168,18 @@ void sprite_draw(struct sprite *sprite) cpVect cpos = cpBodyGetPosition(sprite->go->body); float pos[2] = {cpos.x, cpos.y}; - float size[2] = { sprite->size[0] * sprite->go->scale, sprite->size[1] * sprite->go->scale }; + if (sprite->tex->opts.animation) { + float size[2]; + struct Anim2D *a = &sprite->anim; + a->frames = sprite->tex->anim.frames; + size[0] = sprite->tex->anim.dimensions[0]; + size[1] = sprite->tex->anim.dimensions[1]; + tex_draw(sprite->tex, pos, cpBodyGetAngle(sprite->go->body), size, sprite->pos, tex_get_rect(sprite->tex)); + } else { + float size[2] = { sprite->size[0] * sprite->go->scale, sprite->size[1] * sprite->go->scale }; - tex_draw(sprite->tex, pos, cpBodyGetAngle(sprite->go->body), size, sprite->pos, tex_get_rect(sprite->tex)); + tex_draw(sprite->tex, pos, cpBodyGetAngle(sprite->go->body), size, sprite->pos, tex_get_rect(sprite->tex)); + } } } diff --git a/source/engine/sprite.h b/source/engine/sprite.h index a25320c..0a597fc 100644 --- a/source/engine/sprite.h +++ b/source/engine/sprite.h @@ -36,7 +36,6 @@ void sprite_delete(struct sprite *sprite); void sprite_init(struct sprite *sprite, struct gameobject *go); void sprite_io(struct sprite *sprite, FILE *f, int read); void sprite_loadtex(struct sprite *sprite, const char *path); -void sprite_loadanim(struct sprite *sprite, const char *path, struct Anim2D anim); void sprite_settex(struct sprite *sprite, struct Texture *tex); void sprite_initialize(); void sprite_draw(struct sprite *sprite); diff --git a/source/engine/texture.c b/source/engine/texture.c index 98cd53d..15ed840 100644 --- a/source/engine/texture.c +++ b/source/engine/texture.c @@ -80,8 +80,6 @@ struct Texture *texture_loadfromfile(const char *path) return new; } - - void tex_pull(struct Texture *tex) { if (tex->data != NULL) @@ -184,35 +182,19 @@ void tex_gpu_free(struct Texture *tex) void tex_anim_calc_uv(struct TexAnimation *anim) { - struct Rect uv; - uv.w = 1.f / anim->tex->anim.frames; - uv.h = 1.f; - uv.y = 0.f; - uv.x = uv.w * (anim->frame); + struct glrect st; + float w = 1.f / anim->tex->anim.frames; + st.s0 = w * (anim->frame); + st.s1 = st.s0 + w; + st.t0 = 0.f; + st.t1 = 1.f; - anim->uv = uv; + anim->st = st; } - - struct glrect tex_get_rect(struct Texture *tex) { - if (tex->opts.animation) { - /* - tex_anim_calc_uv(tex->anim); - struct glrect ret; - - ret.s0 = tex->anim.uv.x; - ret.s1 = tex->anim.uv.x + tex->anim.uv.w; - ret.t0 = tex->anim.uv.y; - ret.t1 = tex->anim.uv.y + tex->anim.uv.h; -*/ - return runit; - } else { - return runit; - } - - return runit; + return ST_UNIT; } void tex_bind(struct Texture *tex) @@ -271,4 +253,12 @@ void anim_bkwd(struct TexAnimation *anim) anim_decr(anim); } +float st_s_w(struct glrect st) +{ + return (st.s1 - st.s0); +} +float st_s_h(struct glrect st) +{ + return (st.t1 - st.t0); +} \ No newline at end of file diff --git a/source/engine/texture.h b/source/engine/texture.h index 8a441db..b57f73f 100644 --- a/source/engine/texture.h +++ b/source/engine/texture.h @@ -2,30 +2,44 @@ #define TEXTURE_H #include "timer.h" -#include "font.h" #define TEX_SPEC 0 #define TEX_NORM 1 #define TEX_HEIGHT 2 #define TEX_DIFF 3 - -struct Rect { - float x; - float y; - float w; - float h; +/* Normalized S,T coordinates for rendering */ +struct glrect { + float s0; + float s1; + float t0; + float t1; }; +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 } + +/* Pixel U,V coordiantes */ +struct uvrect { + int u0; + int u1; + int v0; + int v1; +}; + +/* Tracks a playing animation */ struct TexAnimation { int frame; int playing; int pausetime; struct timer *timer; - struct Rect uv; + struct glrect st; /* Current ST coordinates for active frame */ struct Texture *tex; }; +/* Describes an animation on a particular texture */ struct TexAnim { int frames; int dimensions[2];