summaryrefslogtreecommitdiff
path: root/Week1-Pacman/src
diff options
context:
space:
mode:
Diffstat (limited to 'Week1-Pacman/src')
-rw-r--r--Week1-Pacman/src/animation.c41
-rw-r--r--Week1-Pacman/src/animation.h29
-rw-r--r--Week1-Pacman/src/main.c6
-rw-r--r--Week1-Pacman/src/pacman.c44
-rw-r--r--Week1-Pacman/src/pacman.h30
5 files changed, 150 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;
+ }
+}
diff --git a/Week1-Pacman/src/animation.h b/Week1-Pacman/src/animation.h
new file mode 100644
index 0000000..795b6c7
--- /dev/null
+++ b/Week1-Pacman/src/animation.h
@@ -0,0 +1,29 @@
+#ifndef ANIMATION_H_
+#define ANIMATION_H_
+
+#include <SDL2/SDL_rect.h>
+#include <stdbool.h>
+
+struct animation_init {
+ int initial_frame_count;
+
+ float* initial_frame_times;
+ SDL_Rect* initial_frames;
+};
+
+struct animation {
+ int num_frames;
+
+ int current_frame;
+ float current_time;
+
+ float* frame_times;
+ SDL_Rect* frames;
+};
+
+void init_animation(struct animation* animation,
+ struct animation_init* init);
+void update_animation(struct animation* animation, float dt);
+void free_animation(struct animation* animation);
+
+#endif // ANIMATION_H_
diff --git a/Week1-Pacman/src/main.c b/Week1-Pacman/src/main.c
new file mode 100644
index 0000000..e1f447f
--- /dev/null
+++ b/Week1-Pacman/src/main.c
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+int main() {
+
+ return 0;
+}
diff --git a/Week1-Pacman/src/pacman.c b/Week1-Pacman/src/pacman.c
new file mode 100644
index 0000000..6eef8f4
--- /dev/null
+++ b/Week1-Pacman/src/pacman.c
@@ -0,0 +1,44 @@
+#include <SDL2/SDL.h>
+#include <stdbool.h>
+#include "pacman.h"
+
+#define PACMAN_SPEED 40
+
+void handle_pacman_input(SDL_Event* e, struct pacman* pacman) {
+
+ if(e->type == SDL_KEYDOWN) {
+ switch(e->key.keysym.sym) {
+ case SDLK_UP:
+ pacman->facing = FACING_UP;
+ break;
+ case SDLK_DOWN:
+ pacman->facing = FACING_DOWN;
+ break;
+ case SDLK_LEFT:
+ pacman->facing = FACING_LEFT;
+ break;
+ case SDLK_RIGHT:
+ pacman->facing = FACING_RIGHT;
+ break;
+ }
+ }
+
+}
+
+void update_pacman(struct pacman* pacman, float dt) {
+
+ if(pacman->facing == FACING_UP) {
+ pacman->ypos -= PACMAN_SPEED * dt;
+ } else if(pacman->facing == FACING_DOWN) {
+ pacman->ypos += PACMAN_SPEED * dt;
+ } else if(pacman->facing == FACING_LEFT) {
+ pacman->xpos -= PACMAN_SPEED * dt;
+ } else {
+ pacman->xpos += PACMAN_SPEED * dt;
+ }
+
+}
+
+void draw_pacman(SDL_Renderer* ren, const struct pacman* pacman) {
+
+}
diff --git a/Week1-Pacman/src/pacman.h b/Week1-Pacman/src/pacman.h
new file mode 100644
index 0000000..567fc1d
--- /dev/null
+++ b/Week1-Pacman/src/pacman.h
@@ -0,0 +1,30 @@
+#ifndef PACMAN_H_
+#define PACMAN_H_
+
+#include <SDL2/SDL.h>
+#include "animation.h"
+
+enum facing {
+ FACING_UP,
+ FACING_DOWN,
+ FACING_LEFT,
+ FACING_RIGHT
+};
+
+struct pacman {
+ int xpos;
+ int ypos;
+
+ SDL_Texture* pacman_texture;
+ enum facing facing;
+ float frame_time;
+
+ /* One for each face */
+ struct animation animations[4];
+};
+
+void handle_pacman_input(SDL_Event* e, struct pacman* pacman);
+void update_pacman(struct pacman* pacman, float dt);
+void draw_pacman(SDL_Renderer* ren, const struct pacman* pacman);
+
+#endif // PACMAN_H_