summaryrefslogtreecommitdiff
path: root/Week1-Pacman/src/animation.c
diff options
context:
space:
mode:
authorBoredGuy <osome3717@gmail.com>2024-12-13 16:22:39 +0300
committerBoredGuy <osome3717@gmail.com>2024-12-13 16:22:39 +0300
commitebc3c130ed4d8ee6c44fc902bc4ee955799d1ac5 (patch)
treefd42ad5e77178f3aa7dc533ed0dde475cff3818a /Week1-Pacman/src/animation.c
Initial Commit
Diffstat (limited to 'Week1-Pacman/src/animation.c')
-rw-r--r--Week1-Pacman/src/animation.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/Week1-Pacman/src/animation.c b/Week1-Pacman/src/animation.c
new file mode 100644
index 0000000..40be24a
--- /dev/null
+++ b/Week1-Pacman/src/animation.c
@@ -0,0 +1,41 @@
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "animation.h"
+
+void init_animation(struct animation* animation,
+ struct animation_init* init) {
+ int initial_frame_count = init->initial_frame_count;
+
+ if (initial_frame_count <= 0) {
+ fprintf(stderr, "All animations need to have at least 1 frame!\n");
+ exit(1);
+ }
+
+ animation->num_frames = initial_frame_count;
+
+ int size = initial_frame_count * sizeof(float);
+
+ animation->frame_times = malloc(size);
+ memcpy(animation->frame_times, init->initial_frame_times, size);
+
+ size = initial_frame_count * sizeof(SDL_Rect);
+
+ animation->frames = malloc(size);
+ memcpy(animation->frames, init->initial_frame_times, size);
+
+}
+
+void update_animation(struct animation* animation, float dt) {
+ int frame_index = animation->current_frame;
+ float current_frame_time = animation->frame_times[frame_index];
+
+ animation->current_time += dt;
+
+ if(animation->current_time >= current_frame_time) {
+ animation->current_time = 0;
+ animation->current_frame =
+ (frame_index + 1) % animation->num_frames;
+ }
+}