anim code

This commit is contained in:
KikooDX 2021-11-10 14:05:00 +01:00
parent b5b5c24dac
commit ff0c0e9764
8 changed files with 73 additions and 4 deletions

View File

@ -18,6 +18,7 @@ set(SOURCES
src/player.c
src/util.c
src/camera.c
src/anim.c
)
set(LEVELS

19
inc/anim.h Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include "vec.h"
#include <gint/display.h>
struct Anim {
bopti_image_t *texture;
struct Vec pos;
struct Vec frame_dim;
int frame_duration;
int frame;
int life_ini;
int life;
int loop;
};
struct Anim anim_new(bopti_image_t *, int x, int y, int frame_width,
int frame_duration, int loop);
void anim_update(struct Anim *);
void anim_draw(struct Anim *);

View File

@ -1,3 +1,3 @@
#pragma once
enum Tile { TILE_VOID, TILE_SOLID, TILE_OOB = TILE_SOLID };
enum Tile { TILE_VOID, TILE_SOLID, TILE_PLAYER, TILE_OOB = TILE_SOLID };

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.6 KiB

After

Width:  |  Height:  |  Size: 9.7 KiB

49
src/anim.c Normal file
View File

@ -0,0 +1,49 @@
#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;
return anim;
}
void
anim_update(struct Anim *a)
{
if (!a->life)
return;
a->life--;
if (a->life % a->frame_duration == 0)
a->frame++;
/* 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);
}
}

View File

@ -62,7 +62,7 @@ level_find(enum Tile seek)
const enum Tile tile = self.data[i];
if (tile == seek) {
return VEC(i % self.width * TILE_SIZE,
(int)(i / self.height) * TILE_SIZE);
i / self.width * TILE_SIZE);
}
}
return VEC(0, 0);

View File

@ -26,11 +26,12 @@ main(void)
timer_start(timer);
input_init();
level_load(&lvl_test);
player_init(level_find(TILE_VOID));
player_init(level_find(TILE_PLAYER));
camera_init(player_pos());
while (!input_pressed(K_EXIT)) {
int i;
draw();
for (i = 0; i < frameskip; i++) {
if (has_ticked > frameskip) {
frameskip = has_ticked;
@ -41,7 +42,6 @@ main(void)
has_ticked = 0;
update();
}
draw();
}
timer_stop(timer);