diff options
author | BoredGuy <osome3717@gmail.com> | 2024-12-14 08:16:53 +0300 |
---|---|---|
committer | BoredGuy <osome3717@gmail.com> | 2024-12-14 08:16:53 +0300 |
commit | 36144cc98ed6d96cc4f28e73d8da3434774c5d7a (patch) | |
tree | 947de22d753cdba817bf3de7c48b97de8351c891 /Week1-Pacman | |
parent | 0979d3fef156da87b906c55621b49634dced28ba (diff) |
Animation system updates
- implement draw_animation
- add dpi scaling
Diffstat (limited to 'Week1-Pacman')
-rw-r--r-- | Week1-Pacman/Makefile | 2 | ||||
-rw-r--r-- | Week1-Pacman/src/animation.c | 21 | ||||
-rw-r--r-- | Week1-Pacman/src/animation.h | 9 | ||||
-rw-r--r-- | Week1-Pacman/src/pacman.c | 1 | ||||
-rw-r--r-- | Week1-Pacman/src/pacman.h | 1 |
5 files changed, 30 insertions, 4 deletions
diff --git a/Week1-Pacman/Makefile b/Week1-Pacman/Makefile index c0caff7..f7404b6 100644 --- a/Week1-Pacman/Makefile +++ b/Week1-Pacman/Makefile @@ -5,7 +5,7 @@ # @version 0.1 CC=gcc -CFLAGS=-Wall -lSDL2 +CFLAGS=-Wall -lSDL2 -lSDL2_image HEADERS=$(wildcard src/*.h) CFILES=$(wildcard src/*.c) 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; |