From 5f7e0d8cf88d0adb9739e2cc7e26ba26243975f8 Mon Sep 17 00:00:00 2001 From: BoredGuy Date: Fri, 3 Jan 2025 11:38:13 +0300 Subject: Minor optimization Animations can now share a single texture --- Week1-Pacman/src/pacman.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) (limited to 'Week1-Pacman/src/pacman.c') diff --git a/Week1-Pacman/src/pacman.c b/Week1-Pacman/src/pacman.c index 6b189ae..0ec6670 100644 --- a/Week1-Pacman/src/pacman.c +++ b/Week1-Pacman/src/pacman.c @@ -1,3 +1,5 @@ +#include +#include #include #include #include "pacman.h" @@ -9,11 +11,57 @@ extern const Uint8* keyboard; +SDL_Texture* pacman_texture; + SDL_Rect* get_colliding_tile_raw(SDL_Rect*, struct map* map); SDL_Rect* get_colliding_tile(struct pacman* pacman, struct map* map); - void rect_step_one_pixel(SDL_Rect* r, enum facing); +void load_pacman_spritesheet(struct demo* demo) { + if(!pacman_texture) { + pacman_texture = + IMG_LoadTexture(demo->ren, "assets/Sprites/sprites.png"); + + assert(pacman_texture); + } +} + +void init_pacman(struct pacman* pacman, struct pacman_init* init) { + + for(int i = 0; i < 4; i++) + init_animation(&pacman->animations[i], &(struct animation_init){ + .spritesheet_texture = pacman_texture, + + .initial_angle = 90.0 * i, + .initial_frame_count = 3, + .initial_frame_times = (float[]){0.05, 0.05, 0.05}, + .initial_frames = (SDL_Rect[]) { + { + .x = 0, + .y = 285, + .w = 180, + .h = 195 + }, + { + .x = 240, + .y = 285, + .w = 180, + .h = 195 + }, + { + .x = 465, + .y = 285, + .w = 195, + .h = 195 + } + } + }); + + pacman->facing = init->initial_facing; + pacman->xpos = init->initial_xpos; + pacman->ypos = init->initial_ypos; +} + void handle_pacman_input(struct pacman* pacman, struct map* map) { SDL_Rect r = { .x = pacman->xpos, -- cgit v1.2.3