diff --git a/CMakeLists.txt b/CMakeLists.txt index 840f68a..b7f33cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ set(SOURCES src/player.c src/util.c src/camera.c + src/anim.c ) set(LEVELS diff --git a/inc/anim.h b/inc/anim.h new file mode 100644 index 0000000..d9da82b --- /dev/null +++ b/inc/anim.h @@ -0,0 +1,19 @@ +#pragma once +#include "vec.h" +#include + +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 *); diff --git a/inc/tile.h b/inc/tile.h index daf9df3..4519eb9 100644 --- a/inc/tile.h +++ b/inc/tile.h @@ -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 }; diff --git a/lvl/test.kble b/lvl/test.kble index 5bc4bcd..d09bd0c 100644 Binary files a/lvl/test.kble and b/lvl/test.kble differ diff --git a/res/tileset.png b/res/tileset.png index 9dfce00..47b7789 100644 Binary files a/res/tileset.png and b/res/tileset.png differ diff --git a/src/anim.c b/src/anim.c new file mode 100644 index 0000000..a824779 --- /dev/null +++ b/src/anim.c @@ -0,0 +1,49 @@ +#include "anim.h" +#include "camera.h" +#include "vec.h" +#include + +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); + } +} diff --git a/src/level.c b/src/level.c index 41511a7..9ea9294 100644 --- a/src/level.c +++ b/src/level.c @@ -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); diff --git a/src/main.c b/src/main.c index 0f0d94f..b265d0b 100644 --- a/src/main.c +++ b/src/main.c @@ -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);