mirror of https://git.sr.ht/~kikoodx/crystal-tower
53 lines
1.1 KiB
C
53 lines
1.1 KiB
C
#include "anim.h"
|
|
#include "camera.h"
|
|
#include "vec.h"
|
|
#include <gint/display.h>
|
|
|
|
struct Anim
|
|
anim_new(bopti_image_t *texture, int x, int y, int frame_width,
|
|
int frame_duration, int loop)
|
|
{
|
|
struct Anim anim;
|
|
anim.texture = texture;
|
|
anim.pos = VEC(x, y);
|
|
anim.frame_dim = VEC(frame_width, texture->height);
|
|
anim.frame_duration = frame_duration;
|
|
anim.frame = 0;
|
|
anim.life_ini = frame_duration * texture->width / frame_width;
|
|
anim.life = anim.life_ini;
|
|
anim.loop = loop;
|
|
anim.callback = NULL;
|
|
return anim;
|
|
}
|
|
|
|
void
|
|
anim_update(struct Anim *a)
|
|
{
|
|
if (!a->life)
|
|
return;
|
|
a->life--;
|
|
if (a->life % a->frame_duration == 0)
|
|
a->frame++;
|
|
if (!a->life && a->callback != NULL)
|
|
a->callback(a);
|
|
/* loop */
|
|
if (!a->life && a->loop) {
|
|
a->frame = 0;
|
|
a->life = a->life_ini;
|
|
}
|
|
}
|
|
|
|
void
|
|
anim_draw(struct Anim *a)
|
|
{
|
|
if (a->life) {
|
|
const struct Vec off = camera_offset();
|
|
const int x = a->pos.x + off.x;
|
|
const int y = a->pos.y + off.y;
|
|
const int rx = a->frame * a->frame_dim.x;
|
|
const int ry = 0;
|
|
dsubimage(x, y, a->texture, rx, ry, a->frame_dim.x,
|
|
a->frame_dim.y, DIMAGE_NONE);
|
|
}
|
|
}
|