diff options
author | BoredGuy <osome3717@gmail.com> | 2024-12-25 16:10:31 +0300 |
---|---|---|
committer | BoredGuy <osome3717@gmail.com> | 2024-12-25 16:10:31 +0300 |
commit | 6f3fe42d5888e70fe195b6bbf32b7bdd4059415b (patch) | |
tree | 024b054c282167c5d093cf3671368bb71d238002 | |
parent | 7340dcda9151c50c84e283f102ea2f44dbf1443f (diff) |
Started work on collisions
-rw-r--r-- | Week1-Pacman/src/collision.c | 15 | ||||
-rw-r--r-- | Week1-Pacman/src/collision.h | 9 | ||||
-rw-r--r-- | Week1-Pacman/src/main.c | 17 | ||||
-rw-r--r-- | Week1-Pacman/src/map.c | 31 | ||||
-rw-r--r-- | Week1-Pacman/src/map.h | 3 |
5 files changed, 69 insertions, 6 deletions
diff --git a/Week1-Pacman/src/collision.c b/Week1-Pacman/src/collision.c new file mode 100644 index 0000000..268be95 --- /dev/null +++ b/Week1-Pacman/src/collision.c @@ -0,0 +1,15 @@ +#include "collision.h" + +bool is_colliding(SDL_Rect* a, SDL_Rect* b) { + bool collision_v, collision_h; + + collision_h = + (a->x <= b->x && a->x + a->w - 1 >= b->x) || + (b->x <= a->x && b->x + b->w - 1 >= a->x); + + collision_v = + (a->y <= b->y && a->y + a->h - 1 >= b->y) || + (b->y <= a->y && b->y + b->h - 1 >= a->y); + + return collision_v && collision_h; +} diff --git a/Week1-Pacman/src/collision.h b/Week1-Pacman/src/collision.h new file mode 100644 index 0000000..8b58dfa --- /dev/null +++ b/Week1-Pacman/src/collision.h @@ -0,0 +1,9 @@ +#ifndef COLLISION_H_ +#define COLLISION_H_ + +#include <stdbool.h> +#include <SDL2/SDL.h> + +bool is_colliding(SDL_Rect* a, SDL_Rect* b); + +#endif // COLLISION_H_ diff --git a/Week1-Pacman/src/main.c b/Week1-Pacman/src/main.c index 373d795..90e085b 100644 --- a/Week1-Pacman/src/main.c +++ b/Week1-Pacman/src/main.c @@ -4,11 +4,13 @@ #include "demo.h" #include "pacman.h" #include "map.h" +#include "collision.h" #define WINDOW_WIDTH 800 #define WINDOW_HEIGHT 600 int main() { + int counter = 0; SDL_Init(SDL_INIT_EVERYTHING); struct demo demo = {0}; @@ -84,6 +86,21 @@ int main() { draw_map(&demo, &map); draw_pacman(&demo, &pacman); SDL_RenderPresent(demo.ren); + + SDL_Rect r = { + .x = pacman.xpos, + .y = pacman.ypos, + .w = 40, + .h = 40 + }; + + for(int i = 0; i < map.collider_count; i++) { + if(is_colliding(&r, &map.colliders[i])) { + printf("%d: Collision!\n", counter); + counter++; + break; + } + } } SDL_DestroyRenderer(demo.ren); diff --git a/Week1-Pacman/src/map.c b/Week1-Pacman/src/map.c index 0610fd4..7df77af 100644 --- a/Week1-Pacman/src/map.c +++ b/Week1-Pacman/src/map.c @@ -8,14 +8,18 @@ SDL_Texture* map_texture = NULL; -void add_map_row(const char* string, int* row, int width); +void add_map_row(const char* string, struct map* map); void load_map(const char* filename, struct map* map) { - if(map->contents != NULL) { + if(map->contents != NULL || map->colliders != NULL) { printf("Reallocating non empty map, exitting!\n"); exit(1); } + map->collider_count = 0; + map->width = 0; + map->height = 0; + FILE* file = fopen(filename, "r"); if(file == NULL) { @@ -49,21 +53,36 @@ void load_map(const char* filename, struct map* map) { map->contents = realloc(map->contents, map->width * map->height * sizeof(int)); - add_map_row(buffer, - &map->contents[map->width * (map->height - 1)], - map->width); + add_map_row(buffer, map); } fclose(file); } -void add_map_row(const char* line, int* row, int width) { +void add_map_row(const char* line, struct map* map) { + int* row = &map->contents[map->width * (map->height - 1)]; + int width = map->width; + for(int i = 0; i < width; i++) { sscanf(line, "%d", &row[i]); int length = 1; int j = row[i]; + //If the current tile isn't empty add a collider here + if(row[i] != -1) { + map->colliders = + realloc(map->colliders, ++map->collider_count * sizeof(SDL_Rect)); + + map->colliders[map->collider_count - 1] = + (SDL_Rect) { + .x = i * 26, + .y = (map->height - 1) * 26, + .w = 26, + .h = 26 + }; + } + if(j < 0) { length++; j = -j; diff --git a/Week1-Pacman/src/map.h b/Week1-Pacman/src/map.h index 2161806..0721a94 100644 --- a/Week1-Pacman/src/map.h +++ b/Week1-Pacman/src/map.h @@ -8,6 +8,9 @@ struct map { int width; int height; + + SDL_Rect* colliders; + int collider_count; }; /* |