crystal-tower/src/anim.c

77 lines
1.6 KiB
C
Raw Permalink Normal View History

2021-11-10 14:05:00 +01:00
#include "anim.h"
#include "camera.h"
2021-11-16 23:11:16 +01:00
#include "raygint/display.h"
2021-11-10 14:05:00 +01:00
#include "vec.h"
2021-11-17 14:51:05 +01:00
#include <stddef.h>
2021-11-10 14:05:00 +01:00
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);
2021-11-17 14:51:05 +01:00
#ifdef GINT
2021-11-10 14:05:00 +01:00
anim.frame_dim = VEC(frame_width, texture->height);
2021-11-17 15:14:03 +01:00
anim.life_ini = frame_duration * texture->width / frame_width;
2021-11-17 14:51:05 +01:00
#endif
#ifdef RAYLIB
anim.frame_dim = VEC(frame_width, frame_width);
anim.life_ini = frame_duration * anim.frame_dim.x * 8;
#endif
2021-11-10 14:05:00 +01:00
anim.frame_duration = frame_duration;
anim.frame = 0;
anim.life = anim.life_ini;
anim.loop = loop;
2021-11-10 20:44:53 +01:00
anim.callback = NULL;
2021-11-10 14:05:00 +01:00
return anim;
}
void
anim_update(struct Anim *a)
{
if (!a->life)
return;
a->life--;
if (a->life % a->frame_duration == 0)
a->frame++;
2021-11-10 20:44:53 +01:00
if (!a->life && a->callback != NULL)
a->callback(a);
2021-11-10 14:05:00 +01:00
/* loop */
if (!a->life && a->loop) {
a->frame = 0;
a->life = a->life_ini;
}
}
2021-11-17 14:51:05 +01:00
#ifdef GINT
2021-11-10 14:05:00 +01:00
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);
}
}
2021-11-17 14:51:05 +01:00
#endif
#ifdef RAYLIB
void
anim_draw(struct Anim *a)
{
if (a->life) {
const struct Vec off = camera_offset();
const int x1 = a->pos.x + off.x;
const int y1 = a->pos.y + off.y;
const int x2 = x1 + a->frame_dim.x - 1;
const int y2 = y1 + a->frame_dim.y - 1;
drect(x1, y1, x2, y2, C_BLUE);
}
}
#endif