Texture animations in editor

This commit is contained in:
John Alanbrook 2022-08-22 13:55:54 +00:00
parent dda8f1cc24
commit 0a76259a92
6 changed files with 99 additions and 44 deletions

View file

@ -790,26 +790,19 @@ void editor_asset_tex_gui(struct Texture *tex) {
if (old_frames != tex->anim.frames || old_ms != tex->anim.ms) if (old_frames != tex->anim.frames || old_ms != tex->anim.ms)
tex_anim_set(&tex_gui_anim); 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); nk_image(ctx, nk_subimage_id(tex->id, tex->width, tex->height, r));
ImVec2 uv1 = ImVec2(tex_gui_anim.uv.x + tex_gui_anim.uv.w, } else {
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_layout_row_static(ctx, tex->height*tex_scale, tex->width*tex_scale, 1); nk_layout_row_static(ctx, tex->height*tex_scale, tex->width*tex_scale, 1);
nk_image(ctx, nk_image_id(tex->id)); nk_image(ctx, nk_image_id(tex->id));
}
} }
void text_ed_cb(GLFWwindow *win, unsigned int codepoint) void text_ed_cb(GLFWwindow *win, unsigned int codepoint)

View file

@ -108,16 +108,12 @@ void tex_gpu_load(struct Texture *tex)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 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); anim_incr(tex_anim);
if (!tex_anim->tex->anim.loop if (!tex_anim->tex->anim.loop && tex_anim->frame == tex_anim->tex->anim.frames)
&& tex_anim->frame == tex_anim->tex->anim.frames)
anim_pause(tex_anim); anim_pause(tex_anim);
return interval;
} }
void anim_incr(struct TexAnimation *anim) void anim_incr(struct TexAnimation *anim)
@ -128,9 +124,7 @@ void anim_incr(struct TexAnimation *anim)
void anim_decr(struct TexAnimation *anim) void anim_decr(struct TexAnimation *anim)
{ {
anim->frame = anim->frame = (anim->frame + anim->tex->anim.frames - 1) % anim->tex->anim.frames;
(anim->frame + anim->tex->anim.frames -
1) % anim->tex->anim.frames;
tex_anim_calc_uv(anim); tex_anim_calc_uv(anim);
} }
@ -138,9 +132,7 @@ void tex_anim_set(struct TexAnimation *anim)
{ {
if (anim->playing) { if (anim->playing) {
timer_remove(anim->timer); timer_remove(anim->timer);
anim->timer = anim->timer = timer_make(1.f / anim->tex->anim.ms, tex_incr_anim, anim);
timer_make(floor(1000.f / anim->tex->anim.ms), &tex_incr_anim,
anim);
} }
@ -186,10 +178,13 @@ void anim_play(struct TexAnimation *anim)
anim->frame = 0; anim->frame = 0;
anim->playing = 1; anim->playing = 1;
timer_remove(anim->timer);
anim->timer = if (anim->timer == NULL)
timer_make(floor(1000.f / anim->tex->anim.ms), &tex_incr_anim, anim->timer = timer_make(1.f / anim->tex->anim.ms, tex_incr_anim, anim);
anim); else
timer_settime(anim->timer, 1.f/anim->tex->anim.ms);
timer_start(anim->timer);
} }
void anim_stop(struct TexAnimation *anim) void anim_stop(struct TexAnimation *anim)
@ -200,7 +195,7 @@ void anim_stop(struct TexAnimation *anim)
anim->playing = 0; anim->playing = 0;
anim->frame = 0; anim->frame = 0;
anim->pausetime = 0; anim->pausetime = 0;
timer_remove(anim->timer); timer_stop(anim->timer);
tex_anim_calc_uv(anim); tex_anim_calc_uv(anim);
} }
@ -210,7 +205,7 @@ void anim_pause(struct TexAnimation *anim)
return; return;
anim->playing = 0; anim->playing = 0;
timer_remove(anim->timer); timer_pause(anim->timer);
} }
void anim_fwd(struct TexAnimation *anim) void anim_fwd(struct TexAnimation *anim)

View file

@ -71,7 +71,7 @@ void anim_bkwd(struct TexAnimation *anim);
void anim_incr(struct TexAnimation *anim); void anim_incr(struct TexAnimation *anim);
void anim_decr(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_calc_uv(struct TexAnimation *anim);
void tex_anim_set(struct TexAnimation *anim); void tex_anim_set(struct TexAnimation *anim);

View file

@ -1,28 +1,83 @@
#include "timer.h" #include "timer.h"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <stdlib.h> #include <stdlib.h>
#include "vec.h"
static double time; static double time;
struct vec timers;
void timer_init() { void timer_init() {
time = glfwGetTime(); time = glfwGetTime();
timers = vec_init(sizeof(struct timer), 10);
} }
struct timer *timer_make(int interval, void *(*callback)(void *param), void *param) { void check_timer(struct timer *t)
struct timer *new = malloc(sizeof(*new)); {
new->fire_time = time + interval; 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) { 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) { void timer_start(struct timer *t) {
t->on = 1;
t->fire_time = time + t->remain_time;
} }
void timer_remove(struct timer *t) { void timer_remove(struct timer *t) {
free(t); 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;
}

View file

@ -3,12 +3,22 @@
struct timer { struct timer {
int timerid; int timerid;
int on;
double fire_time; double fire_time;
double interval;
int repeat;
double remain_time;
void (*cb)(void *data);
void *data;
}; };
void timer_init(); 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_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 #endif

View file

@ -63,6 +63,8 @@ int main(int argc, char **args) {
elapsed = glfwGetTime() - lastTick; elapsed = glfwGetTime() - lastTick;
lastTick = glfwGetTime(); lastTick = glfwGetTime();
timer_update(lastTick);
//renderlag += elapsed; //renderlag += elapsed;
//physlag += elapsed; //physlag += elapsed;