summaryrefslogtreecommitdiff
path: root/Week1-Pacman/src
diff options
context:
space:
mode:
authorBoredGuy <osome3717@gmail.com>2024-12-14 08:16:53 +0300
committerBoredGuy <osome3717@gmail.com>2024-12-14 08:16:53 +0300
commit36144cc98ed6d96cc4f28e73d8da3434774c5d7a (patch)
tree947de22d753cdba817bf3de7c48b97de8351c891 /Week1-Pacman/src
parent0979d3fef156da87b906c55621b49634dced28ba (diff)
Animation system updates
- implement draw_animation - add dpi scaling
Diffstat (limited to 'Week1-Pacman/src')
-rw-r--r--Week1-Pacman/src/animation.c21
-rw-r--r--Week1-Pacman/src/animation.h9
-rw-r--r--Week1-Pacman/src/pacman.c1
-rw-r--r--Week1-Pacman/src/pacman.h1
4 files changed, 29 insertions, 3 deletions
diff --git a/Week1-Pacman/src/animation.c b/Week1-Pacman/src/animation.c
index 40be24a..88fce7b 100644
--- a/Week1-Pacman/src/animation.c
+++ b/Week1-Pacman/src/animation.c
@@ -1,8 +1,12 @@
-#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "animation.h"
+#include <SDL2/SDL_image.h>
+
+SDL_Rect* current_frame(struct animation* animation) {
+ return &animation->frames[animation->current_frame];
+}
void init_animation(struct animation* animation,
struct animation_init* init) {
@@ -13,6 +17,13 @@ void init_animation(struct animation* animation,
exit(1);
}
+ animation->texture = IMG_LoadTexture(init->ren, init->spritesheet);
+
+ if(animation->texture == NULL) {
+ printf("Failed to load spritesheet %s, error: %s, exitting!\n",
+ init->spritesheet, IMG_GetError());
+ }
+
animation->num_frames = initial_frame_count;
int size = initial_frame_count * sizeof(float);
@@ -39,3 +50,11 @@ void update_animation(struct animation* animation, float dt) {
(frame_index + 1) % animation->num_frames;
}
}
+
+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);
+}
diff --git a/Week1-Pacman/src/animation.h b/Week1-Pacman/src/animation.h
index 795b6c7..f225142 100644
--- a/Week1-Pacman/src/animation.h
+++ b/Week1-Pacman/src/animation.h
@@ -1,10 +1,13 @@
#ifndef ANIMATION_H_
#define ANIMATION_H_
-#include <SDL2/SDL_rect.h>
+#include <SDL2/SDL.h>
#include <stdbool.h>
+#include "demo.h"
struct animation_init {
+ SDL_Renderer* ren;
+ const char* spritesheet;
int initial_frame_count;
float* initial_frame_times;
@@ -12,6 +15,7 @@ struct animation_init {
};
struct animation {
+ SDL_Texture* texture;
int num_frames;
int current_frame;
@@ -25,5 +29,8 @@ void init_animation(struct animation* animation,
struct animation_init* init);
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);
#endif // ANIMATION_H_
diff --git a/Week1-Pacman/src/pacman.c b/Week1-Pacman/src/pacman.c
index 6eef8f4..c0a9644 100644
--- a/Week1-Pacman/src/pacman.c
+++ b/Week1-Pacman/src/pacman.c
@@ -3,6 +3,7 @@
#include "pacman.h"
#define PACMAN_SPEED 40
+#define PACMAN_SIDE 20
void handle_pacman_input(SDL_Event* e, struct pacman* pacman) {
diff --git a/Week1-Pacman/src/pacman.h b/Week1-Pacman/src/pacman.h
index 567fc1d..b56e95d 100644
--- a/Week1-Pacman/src/pacman.h
+++ b/Week1-Pacman/src/pacman.h
@@ -15,7 +15,6 @@ struct pacman {
int xpos;
int ypos;
- SDL_Texture* pacman_texture;
enum facing facing;
float frame_time;