summaryrefslogtreecommitdiff
path: root/Week1-Pacman/src
diff options
context:
space:
mode:
Diffstat (limited to 'Week1-Pacman/src')
-rw-r--r--Week1-Pacman/src/animation.c20
-rw-r--r--Week1-Pacman/src/animation.h6
-rw-r--r--Week1-Pacman/src/demo.c50
-rw-r--r--Week1-Pacman/src/demo.h11
-rw-r--r--Week1-Pacman/src/main.c80
-rw-r--r--Week1-Pacman/src/pacman.c18
-rw-r--r--Week1-Pacman/src/pacman.h15
7 files changed, 174 insertions, 26 deletions
diff --git a/Week1-Pacman/src/animation.c b/Week1-Pacman/src/animation.c
index 88fce7b..0747ba1 100644
--- a/Week1-Pacman/src/animation.c
+++ b/Week1-Pacman/src/animation.c
@@ -4,7 +4,7 @@
#include "animation.h"
#include <SDL2/SDL_image.h>
-SDL_Rect* current_frame(struct animation* animation) {
+const SDL_Rect* current_frame(const struct animation* animation) {
return &animation->frames[animation->current_frame];
}
@@ -22,6 +22,7 @@ void init_animation(struct animation* animation,
if(animation->texture == NULL) {
printf("Failed to load spritesheet %s, error: %s, exitting!\n",
init->spritesheet, IMG_GetError());
+ exit(1);
}
animation->num_frames = initial_frame_count;
@@ -34,8 +35,9 @@ void init_animation(struct animation* animation,
size = initial_frame_count * sizeof(SDL_Rect);
animation->frames = malloc(size);
- memcpy(animation->frames, init->initial_frame_times, size);
+ memcpy(animation->frames, init->initial_frames, size);
+ animation->angle = init->initial_angle;
}
void update_animation(struct animation* animation, float dt) {
@@ -52,9 +54,13 @@ void update_animation(struct animation* animation, float dt) {
}
void draw_animation(struct demo* demo,
- struct animation* animation,
- SDL_Rect* d_rect) {
- SDL_Rect* s_rect = current_frame(animation);
-
- demo_rendercopy(demo, animation->texture, s_rect, d_rect);
+ const struct animation* animation,
+ const SDL_Rect* d_rect) {
+ const SDL_Rect* s_rect = current_frame(animation);
+
+ demo_rendercopy_ex(demo, animation->texture,
+ s_rect, d_rect,
+ animation->angle,
+ NULL,
+ SDL_FLIP_NONE);
}
diff --git a/Week1-Pacman/src/animation.h b/Week1-Pacman/src/animation.h
index f225142..e6c717a 100644
--- a/Week1-Pacman/src/animation.h
+++ b/Week1-Pacman/src/animation.h
@@ -10,6 +10,7 @@ struct animation_init {
const char* spritesheet;
int initial_frame_count;
+ float initial_angle;
float* initial_frame_times;
SDL_Rect* initial_frames;
};
@@ -17,6 +18,7 @@ struct animation_init {
struct animation {
SDL_Texture* texture;
int num_frames;
+ float angle;
int current_frame;
float current_time;
@@ -30,7 +32,7 @@ void init_animation(struct animation* animation,
void update_animation(struct animation* animation, float dt);
void free_animation(struct animation* animation);
void draw_animation(struct demo* demo,
- struct animation* animation,
- SDL_Rect* d_rect);
+ const struct animation* animation,
+ const SDL_Rect* d_rect);
#endif // ANIMATION_H_
diff --git a/Week1-Pacman/src/demo.c b/Week1-Pacman/src/demo.c
index 5ab6c39..85ee2a3 100644
--- a/Week1-Pacman/src/demo.c
+++ b/Week1-Pacman/src/demo.c
@@ -8,14 +8,14 @@ void update_demo(struct demo* demo) {
SDL_GetWindowSize(demo->win, &width, &height);
SDL_GetRendererOutputSize(demo->ren, &pixel_width, &pixel_height);
- demo->display_scale_x = pixel_width / width;
- demo->display_scale_y = pixel_height / height;
+ demo->display_scale_x = (float)pixel_width / width;
+ demo->display_scale_y = (float)pixel_height / height;
}
void demo_rendercopy(struct demo* demo,
SDL_Texture* texture,
- SDL_Rect* s_rect,
- SDL_Rect* d_rect) {
+ const SDL_Rect* s_rect,
+ const SDL_Rect* d_rect) {
const float scale_x = demo->display_scale_x;
const float scale_y = demo->display_scale_y;
@@ -43,5 +43,45 @@ void demo_rendercopy(struct demo* demo,
//Handle edge case where s_rect or d_rect are NULL
SDL_RenderCopy(demo->ren, texture,
s_rect ? &new_srect : NULL,
- d_rect ?&new_drect : NULL);
+ d_rect ? &new_drect : NULL);
+}
+
+void demo_rendercopy_ex(struct demo* demo,
+ SDL_Texture* texture,
+ const SDL_Rect* s_rect,
+ const SDL_Rect* d_rect,
+ const double angle,
+ const SDL_Point* center,
+ const SDL_RendererFlip flip) {
+ const float scale_x = demo->display_scale_x;
+ const float scale_y = demo->display_scale_y;
+
+ SDL_Rect new_srect;
+ SDL_Rect new_drect;
+
+ if(s_rect) {
+ new_srect = (SDL_Rect){
+ .x = s_rect->x * scale_x,
+ .y = s_rect->y * scale_y,
+ .w = s_rect->w * scale_x,
+ .h = s_rect->h * scale_y
+ };
+ }
+
+ if(d_rect) {
+ new_drect = (SDL_Rect){
+ .x = d_rect->x * scale_x,
+ .y = d_rect->y * scale_y,
+ .w = d_rect->w * scale_x,
+ .h = d_rect->h * scale_y
+ };
+ }
+
+ //Handle edge case where s_rect or d_rect are NULL
+ SDL_RenderCopyEx(demo->ren, texture,
+ s_rect ? &new_srect : NULL,
+ d_rect ? &new_drect : NULL,
+ angle,
+ center,
+ flip);
}
diff --git a/Week1-Pacman/src/demo.h b/Week1-Pacman/src/demo.h
index b276027..427a577 100644
--- a/Week1-Pacman/src/demo.h
+++ b/Week1-Pacman/src/demo.h
@@ -18,7 +18,14 @@ struct demo {
void update_demo(struct demo* demo);
void demo_rendercopy(struct demo* demo,
SDL_Texture* texture,
- SDL_Rect* s_rect,
- SDL_Rect* d_rect);
+ const SDL_Rect* s_rect,
+ const SDL_Rect* d_rect);
+void demo_rendercopy_ex(struct demo* demo,
+ SDL_Texture* texture,
+ const SDL_Rect* s_rect,
+ const SDL_Rect* d_rect,
+ const double angle,
+ const SDL_Point* center,
+ const SDL_RendererFlip flip);
#endif // DEMO_H_
diff --git a/Week1-Pacman/src/main.c b/Week1-Pacman/src/main.c
index e1f447f..895c359 100644
--- a/Week1-Pacman/src/main.c
+++ b/Week1-Pacman/src/main.c
@@ -1,6 +1,86 @@
+#include <SDL2/SDL.h>
#include <stdio.h>
+#include <stdbool.h>
+#include "demo.h"
+#include "pacman.h"
+
+#define WINDOW_WIDTH 800
+#define WINDOW_HEIGHT 600
int main() {
+ SDL_Init(SDL_INIT_EVERYTHING);
+ struct demo demo = {0};
+
+ demo.win = SDL_CreateWindow("Pacman",
+ SDL_WINDOWPOS_CENTERED,
+ SDL_WINDOWPOS_CENTERED,
+ WINDOW_WIDTH, WINDOW_HEIGHT,
+ SDL_WINDOW_SHOWN |
+ SDL_WINDOW_ALLOW_HIGHDPI);
+ demo.ren = SDL_CreateRenderer(demo.win,
+ -1,
+ SDL_RENDERER_PRESENTVSYNC |
+ SDL_RENDERER_ACCELERATED);
+ bool running = true;
+ SDL_Event e;
+
+ struct pacman pacman = {0};
+ for(int i = 0; i < 4; i++)
+ init_animation(&pacman.animations[i], &(struct animation_init){
+ .ren = demo.ren,
+ .spritesheet = "assets/sprites.png",
+
+ .initial_angle = 90.0 * i,
+ .initial_frame_count = 3,
+ .initial_frame_times = (float[]){0.10, 0.10, 0.10},
+ .initial_frames = (SDL_Rect[]) {
+ {
+ .x = 0,
+ .y = 190,
+ .w = 120,
+ .h = 130
+ },
+ {
+ .x = 160,
+ .y = 190,
+ .w = 120,
+ .h = 130
+ },
+ {
+ .x = 310,
+ .y = 190,
+ .w = 130,
+ .h = 130
+ }
+ }
+ });
+ pacman.facing = FACING_DOWN;
+
+ int then = SDL_GetTicks();
+ while(running) {
+ while(SDL_PollEvent(&e)) {
+ if(e.type == SDL_QUIT) {
+ running = false;
+ }
+
+ handle_pacman_input(&e, &pacman);
+ }
+
+ int now = SDL_GetTicks();
+ float dt = (float)(now - then) / 1000;
+ then = now;
+
+ SDL_RenderClear(demo.ren);
+ update_pacman(&pacman, dt);
+ update_demo(&demo);
+ draw_pacman(&demo, &pacman);
+ SDL_RenderPresent(demo.ren);
+ }
+
+ SDL_DestroyRenderer(demo.ren);
+ SDL_DestroyWindow(demo.win);
+ SDL_Quit();
return 0;
+
}
diff --git a/Week1-Pacman/src/pacman.c b/Week1-Pacman/src/pacman.c
index c0a9644..f3a4769 100644
--- a/Week1-Pacman/src/pacman.c
+++ b/Week1-Pacman/src/pacman.c
@@ -2,8 +2,8 @@
#include <stdbool.h>
#include "pacman.h"
-#define PACMAN_SPEED 40
-#define PACMAN_SIDE 20
+#define PACMAN_SPEED 261
+#define PACMAN_SIDE 40
void handle_pacman_input(SDL_Event* e, struct pacman* pacman) {
@@ -23,7 +23,6 @@ void handle_pacman_input(SDL_Event* e, struct pacman* pacman) {
break;
}
}
-
}
void update_pacman(struct pacman* pacman, float dt) {
@@ -38,8 +37,19 @@ void update_pacman(struct pacman* pacman, float dt) {
pacman->xpos += PACMAN_SPEED * dt;
}
+ update_animation(&pacman->animations[pacman->facing], dt);
}
-void draw_pacman(SDL_Renderer* ren, const struct pacman* pacman) {
+void draw_pacman(struct demo* demo, const struct pacman* pacman) {
+ const struct animation* current_animation =
+ &pacman->animations[pacman->facing];
+
+ SDL_Rect d_rect = {
+ .x = pacman->xpos,
+ .y = pacman->ypos,
+ .w = PACMAN_SIDE,
+ .h = PACMAN_SIDE
+ };
+ draw_animation(demo, current_animation, &d_rect);
}
diff --git a/Week1-Pacman/src/pacman.h b/Week1-Pacman/src/pacman.h
index b56e95d..e1610af 100644
--- a/Week1-Pacman/src/pacman.h
+++ b/Week1-Pacman/src/pacman.h
@@ -3,20 +3,23 @@
#include <SDL2/SDL.h>
#include "animation.h"
+#include "demo.h"
enum facing {
- FACING_UP,
+ FACING_RIGHT,
FACING_DOWN,
FACING_LEFT,
- FACING_RIGHT
+ FACING_UP
};
struct pacman {
- int xpos;
- int ypos;
+ /*
+ * Coolest bug I have ever seen in my life
+ */
+ float xpos;
+ float ypos;
enum facing facing;
- float frame_time;
/* One for each face */
struct animation animations[4];
@@ -24,6 +27,6 @@ struct pacman {
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);
+void draw_pacman(struct demo* demo, const struct pacman* pacman);
#endif // PACMAN_H_