FlappyBat/src/animation.c

93 lines
1.7 KiB
C

#include <gint/display.h>
#include <gint/defs/util.h>
#include "animation.h"
#include "engine.h"
struct sheet
{
bopti_image_t *img;
int frame_w;
int frame_h;
};
extern bopti_image_t img_spritesheet;
struct sheet const anim_player = {
.img = &img_spritesheet,
.frame_w = 64,
.frame_h = 64,
};
extern bopti_image_t img_wind;
struct sheet const anim_wind = {
.img = &img_wind,
.frame_w = 48,
.frame_h = 224,
};
static struct anim_frame anim_frame(struct sheet const *sheet, int col,int row)
{
struct anim_frame f = {
.source = sheet->img,
.left = sheet->frame_w * col,
.top = sheet->frame_h * row,
.w = sheet->frame_w,
.h = sheet->frame_h,
};
return f;
}
int anim_player_idle(struct anim_data *data, int init)
{
if(init)
{
data->function = anim_player_idle;
data->frame = 0;
}
data->img = anim_frame(&anim_player, 0, 0);
return 0;
}
void dframe(int x, int y, struct anim_frame const frame)
{
dsubimage(x, y, frame.source, frame.left, frame.top, frame.w, frame.h, DIMAGE_NONE);
}
int anim_player_jump(struct anim_data *data, int init){
if(init)
{
data->function = anim_player_jump;
data->frame = 0;
data->duration = 30;
}
else
{
if(data->frame >= 9)
{
return anim_player_idle(data, 0);
}
data->frame = data->frame + 1;
data->duration += 30;
}
data->img = anim_frame(&anim_player, data->frame, 0);
return 1;
}
int anim_wind_idle(struct anim_data *data, int init){
if(init)
{
data->function = anim_wind_idle;
data->frame = 0;
data->duration = 100;
}
else
{
data->frame = (data->frame + 1) % 12;
data->duration += 100;
}
data->img = anim_frame(&anim_wind, data->frame, 0);
return 0;
}