From ebc3c130ed4d8ee6c44fc902bc4ee955799d1ac5 Mon Sep 17 00:00:00 2001 From: BoredGuy Date: Fri, 13 Dec 2024 16:22:39 +0300 Subject: Initial Commit --- Week1-Pacman/Makefile | 18 ++++++++++++++++++ Week1-Pacman/src/animation.c | 41 +++++++++++++++++++++++++++++++++++++++++ Week1-Pacman/src/animation.h | 29 +++++++++++++++++++++++++++++ Week1-Pacman/src/main.c | 6 ++++++ Week1-Pacman/src/pacman.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ Week1-Pacman/src/pacman.h | 30 ++++++++++++++++++++++++++++++ 6 files changed, 168 insertions(+) create mode 100644 Week1-Pacman/Makefile create mode 100644 Week1-Pacman/src/animation.c create mode 100644 Week1-Pacman/src/animation.h create mode 100644 Week1-Pacman/src/main.c create mode 100644 Week1-Pacman/src/pacman.c create mode 100644 Week1-Pacman/src/pacman.h (limited to 'Week1-Pacman') diff --git a/Week1-Pacman/Makefile b/Week1-Pacman/Makefile new file mode 100644 index 0000000..26567a9 --- /dev/null +++ b/Week1-Pacman/Makefile @@ -0,0 +1,18 @@ +## +# Project Title +# +# @file +# @version 0.1 + +CC=gcc +CFLAGS=-Wall -lSDL2 +HEADERS=$(wildcard src/*.h) +CFILES=$(wildcard src/*.c) + +build/pacman: $(HEADERS) $(CFILES) + $(CC) $(CFILES) -o build/pacman $(CFLAGS) + +run: build/pacman + build/pacman + +# end 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 +#include +#include +#include +#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 +#include + +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 + +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 +#include +#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 +#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_ -- cgit v1.2.3