summaryrefslogtreecommitdiff
path: root/sprite-animator.c
diff options
context:
space:
mode:
Diffstat (limited to 'sprite-animator.c')
-rw-r--r--sprite-animator.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/sprite-animator.c b/sprite-animator.c
new file mode 100644
index 0000000..b77d225
--- /dev/null
+++ b/sprite-animator.c
@@ -0,0 +1,47 @@
+#include "sprite-animator.h"
+
+void update_frame(animation_player_t *anim){
+ if (anim->frame_speed == 0){
+ return;
+ }
+
+ anim->frame_counter++;
+ if (anim->frame_counter >= (FPS / anim->frame_speed)){
+ anim->frame_counter = 0;
+
+ anim->frame_current = anim->frame_current + anim->index_increment;
+ if (anim->frame_current >= anim->index_end) {
+ anim->frame_current = anim->index_start + anim->index_offset;
+ }
+
+ anim->frame_rec.x = (anim->frame_current % 8) * anim->frame_width;
+ anim->frame_rec.y = (anim->frame_current / 8) * anim->frame_height;
+ }
+}
+
+void draw_frame(animation_player_t *anim, Vector2 pos, Color tint){
+ DrawTextureRec(*anim->sheet, anim->frame_rec, pos, tint);
+}
+
+animation_player_t make_animation_player
+(Texture2D *texture, int row, int col, int start, int end, int inc, int offset){
+ float w = texture->width / col;
+ float h = texture->height / row;
+ return (animation_player_t){
+ .sheet = texture,
+ .frame_counter = 0,
+ .frame_speed = 5,
+ .frame_width = w,
+ .frame_height = h,
+ .index_start = start,
+ .index_end = end,
+ .index_increment = inc,
+ .index_offset = offset,
+ .frame_current = start,
+ .frame_rec = (Rectangle){ 0, 0, w, h }
+ };
+}
+
+void draw_frame_box(animation_player_t *anim, Vector2 pos){
+ DrawRectangleLines(pos.x, pos.y, anim->frame_width, anim->frame_height, SKYBLUE);
+}