From 0a76259a922ae05b290e7519ca1cf900c09e7eee Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Mon, 22 Aug 2022 13:55:54 +0000 Subject: [PATCH] Texture animations in editor --- source/engine/editor.c | 25 ++++++--------- source/engine/texture.c | 31 ++++++++---------- source/engine/texture.h | 2 +- source/engine/timer.c | 69 ++++++++++++++++++++++++++++++++++++----- source/engine/timer.h | 14 +++++++-- source/engine/yugine.c | 2 ++ 6 files changed, 99 insertions(+), 44 deletions(-) diff --git a/source/engine/editor.c b/source/engine/editor.c index 599cc84..c489b4a 100644 --- a/source/engine/editor.c +++ b/source/engine/editor.c @@ -790,26 +790,19 @@ 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); + 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; - ImVec2 uv0 = ImVec2(tex_gui_anim.uv.x, tex_gui_anim.uv.y); - ImVec2 uv1 = ImVec2(tex_gui_anim.uv.x + tex_gui_anim.uv.w, - tex_gui_anim.uv.y + tex_gui_anim.uv.h); - ImGui::Image((void *) (intptr_t) tex->id, - ImVec2(tex->width * tex_gui_anim.uv.w * tex_scale, - tex->height * tex_gui_anim.uv.h * tex_scale), - uv0, uv1); - } else { - ImGui::Image((void *) (intptr_t) tex->id, - ImVec2(tex->width * tex_scale, - tex->height * tex_scale)); - } - */ - + nk_image(ctx, nk_subimage_id(tex->id, tex->width, tex->height, r)); + } else { nk_layout_row_static(ctx, tex->height*tex_scale, tex->width*tex_scale, 1); nk_image(ctx, nk_image_id(tex->id)); + } } void text_ed_cb(GLFWwindow *win, unsigned int codepoint) diff --git a/source/engine/texture.c b/source/engine/texture.c index 8c1b46b..6a4411b 100644 --- a/source/engine/texture.c +++ b/source/engine/texture.c @@ -108,16 +108,12 @@ void tex_gpu_load(struct Texture *tex) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); } -unsigned int tex_incr_anim(unsigned int interval, struct TexAnimation *tex_anim) +void tex_incr_anim(struct TexAnimation *tex_anim) { anim_incr(tex_anim); - if (!tex_anim->tex->anim.loop - && tex_anim->frame == tex_anim->tex->anim.frames) + if (!tex_anim->tex->anim.loop && tex_anim->frame == tex_anim->tex->anim.frames) anim_pause(tex_anim); - - - return interval; } void anim_incr(struct TexAnimation *anim) @@ -128,9 +124,7 @@ void anim_incr(struct TexAnimation *anim) void anim_decr(struct TexAnimation *anim) { - anim->frame = - (anim->frame + anim->tex->anim.frames - - 1) % anim->tex->anim.frames; + anim->frame = (anim->frame + anim->tex->anim.frames - 1) % anim->tex->anim.frames; tex_anim_calc_uv(anim); } @@ -138,9 +132,7 @@ void tex_anim_set(struct TexAnimation *anim) { if (anim->playing) { timer_remove(anim->timer); - anim->timer = - timer_make(floor(1000.f / anim->tex->anim.ms), &tex_incr_anim, - anim); + anim->timer = timer_make(1.f / anim->tex->anim.ms, tex_incr_anim, anim); } @@ -186,10 +178,13 @@ void anim_play(struct TexAnimation *anim) anim->frame = 0; anim->playing = 1; - timer_remove(anim->timer); - anim->timer = - timer_make(floor(1000.f / anim->tex->anim.ms), &tex_incr_anim, - anim); + + if (anim->timer == NULL) + anim->timer = timer_make(1.f / anim->tex->anim.ms, tex_incr_anim, anim); + else + timer_settime(anim->timer, 1.f/anim->tex->anim.ms); + + timer_start(anim->timer); } void anim_stop(struct TexAnimation *anim) @@ -200,7 +195,7 @@ void anim_stop(struct TexAnimation *anim) anim->playing = 0; anim->frame = 0; anim->pausetime = 0; - timer_remove(anim->timer); + timer_stop(anim->timer); tex_anim_calc_uv(anim); } @@ -210,7 +205,7 @@ void anim_pause(struct TexAnimation *anim) return; anim->playing = 0; - timer_remove(anim->timer); + timer_pause(anim->timer); } void anim_fwd(struct TexAnimation *anim) diff --git a/source/engine/texture.h b/source/engine/texture.h index 038b85e..b55c78c 100644 --- a/source/engine/texture.h +++ b/source/engine/texture.h @@ -71,7 +71,7 @@ void anim_bkwd(struct TexAnimation *anim); void anim_incr(struct TexAnimation *anim); void anim_decr(struct TexAnimation *anim); -unsigned int tex_incr_anim(unsigned int interval, struct TexAnimation *tex_anim); +void tex_incr_anim(struct TexAnimation *tex_anim); void tex_anim_calc_uv(struct TexAnimation *anim); void tex_anim_set(struct TexAnimation *anim); diff --git a/source/engine/timer.c b/source/engine/timer.c index ddcd9f7..d583b22 100644 --- a/source/engine/timer.c +++ b/source/engine/timer.c @@ -1,28 +1,83 @@ #include "timer.h" #include #include +#include "vec.h" static double time; +struct vec timers; + void timer_init() { time = glfwGetTime(); + timers = vec_init(sizeof(struct timer), 10); } -struct timer *timer_make(int interval, void *(*callback)(void *param), void *param) { - struct timer *new = malloc(sizeof(*new)); - new->fire_time = time + interval; +void check_timer(struct timer *t) +{ + if (!t->on) + return; - return new; + if (time >= t->fire_time) { + t->cb(t->data); + + if (t->repeat) { + t->fire_time = time + t->interval; + return; + } + + t->on = 0; + return; + } +} + +void timer_update(double s) { + time = s; + + vec_walk(&timers, check_timer); +} + +struct timer *timer_make(double interval, void (*callback)(void *param), void *param) { + struct timer *new = calloc(sizeof(*new),1); + + new->remain_time = interval; + new->interval = interval; + new->cb = callback; + new->data = param; + new->repeat = 1; + + timer_start(new); + + struct timer *nn = vec_add(&timers, new); + free(new); + + nn->timerid = timers.len-1; + + return nn; } void timer_pause(struct timer *t) { + t->on = 0; + t->remain_time = t->fire_time - time; +} + +void timer_stop(struct timer *t) { + t->on = 0; + t->remain_time = t->interval; } void timer_start(struct timer *t) { - + t->on = 1; + t->fire_time = time + t->remain_time; } void timer_remove(struct timer *t) { - free(t); -} \ No newline at end of file + vec_delete(&timers, t->timerid); +} + +void timer_settime(struct timer *t, double interval) { + double elapsed = time - (t->fire_time - t->interval); + t->interval = interval; + t->remain_time = time + t->interval - elapsed; +} + diff --git a/source/engine/timer.h b/source/engine/timer.h index d6ea14b..dd807b9 100644 --- a/source/engine/timer.h +++ b/source/engine/timer.h @@ -3,12 +3,22 @@ struct timer { int timerid; + int on; double fire_time; + double interval; + int repeat; + double remain_time; + void (*cb)(void *data); + void *data; }; void timer_init(); -struct timer *timer_make(int interval, void *(*callback)(void *param), void *param); +struct timer *timer_make(double interval, void (*callback)(void *param), void *param); void timer_remove(struct timer *t); - +void timer_start(struct timer *t); +void timer_pause(struct timer *t); +void timer_stop(struct timer *t); +void timer_update(double s); +void timer_settime(struct timer *t, double interval); #endif \ No newline at end of file diff --git a/source/engine/yugine.c b/source/engine/yugine.c index 59522c2..76d4eac 100644 --- a/source/engine/yugine.c +++ b/source/engine/yugine.c @@ -63,6 +63,8 @@ int main(int argc, char **args) { elapsed = glfwGetTime() - lastTick; lastTick = glfwGetTime(); + timer_update(lastTick); + //renderlag += elapsed; //physlag += elapsed;