diff options
author | BoredGuy <osome3717@gmail.com> | 2025-01-03 11:38:13 +0300 |
---|---|---|
committer | BoredGuy <osome3717@gmail.com> | 2025-01-03 11:38:13 +0300 |
commit | 5f7e0d8cf88d0adb9739e2cc7e26ba26243975f8 (patch) | |
tree | 099937e59239471f0f611602b5b7788dfde91814 | |
parent | 1d1554652888fa4f0cd12a51fda8d9bd54dbcee6 (diff) |
Minor optimization
Animations can now share a single texture
-rw-r--r-- | Week1-Pacman/src/animation.c | 6 | ||||
-rw-r--r-- | Week1-Pacman/src/animation.h | 1 | ||||
-rw-r--r-- | Week1-Pacman/src/main.c | 38 | ||||
-rw-r--r-- | Week1-Pacman/src/pacman.c | 50 | ||||
-rw-r--r-- | Week1-Pacman/src/pacman.h | 8 |
5 files changed, 69 insertions, 34 deletions
diff --git a/Week1-Pacman/src/animation.c b/Week1-Pacman/src/animation.c index 7e4bb57..50f5c6c 100644 --- a/Week1-Pacman/src/animation.c +++ b/Week1-Pacman/src/animation.c @@ -17,7 +17,11 @@ void init_animation(struct animation* animation, exit(1); } - animation->texture = IMG_LoadTexture(init->ren, init->spritesheet); + if(init->spritesheet_texture == NULL) { + animation->texture = IMG_LoadTexture(init->ren, init->spritesheet); + } else { + animation->texture = init->spritesheet_texture; + } if(animation->texture == NULL) { printf("Failed to load spritesheet %s, error: %s, exitting!\n", diff --git a/Week1-Pacman/src/animation.h b/Week1-Pacman/src/animation.h index 390e3f5..af55e14 100644 --- a/Week1-Pacman/src/animation.h +++ b/Week1-Pacman/src/animation.h @@ -6,6 +6,7 @@ #include "demo.h" struct animation_init { + SDL_Texture* spritesheet_texture; SDL_Renderer* ren; const char* spritesheet; int initial_frame_count; diff --git a/Week1-Pacman/src/main.c b/Week1-Pacman/src/main.c index 66e1278..6ae2fde 100644 --- a/Week1-Pacman/src/main.c +++ b/Week1-Pacman/src/main.c @@ -40,38 +40,12 @@ int main(int argc, char** argv) { keyboard = SDL_GetKeyboardState(NULL); - for(int i = 0; i < 4; i++) - init_animation(&pacman.animations[i], &(struct animation_init){ - .ren = demo.ren, - .spritesheet = "assets/Sprites/sprites.png", - - .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 = FACING_DOWN; - pacman.xpos = 100; - pacman.ypos = 200; + load_pacman_spritesheet(&demo); + init_pacman(&pacman, &(struct pacman_init) { + .initial_xpos = 100, + .initial_ypos = 200, + .initial_facing = FACING_DOWN + }); int then = SDL_GetTicks(); 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 <assert.h> +#include <SDL2/SDL_image.h> #include <SDL2/SDL.h> #include <stdbool.h> #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, diff --git a/Week1-Pacman/src/pacman.h b/Week1-Pacman/src/pacman.h index e2a3c4d..65137cd 100644 --- a/Week1-Pacman/src/pacman.h +++ b/Week1-Pacman/src/pacman.h @@ -13,6 +13,12 @@ enum facing { FACING_UP }; +struct pacman_init { + float initial_xpos; + float initial_ypos; + enum facing initial_facing; +}; + struct pacman { /* * Coolest bug I have ever seen in my life @@ -26,6 +32,8 @@ struct pacman { struct animation animations[4]; }; +void load_pacman_spritesheet(struct demo* demo); +void init_pacman(struct pacman* pacman, struct pacman_init* init); void handle_pacman_input(struct pacman* pacman, struct map* map); void update_pacman(struct pacman* pacman, float dt, struct map* map); void draw_pacman(struct demo* demo, const struct pacman* pacman); |