diff options
Diffstat (limited to 'Week1-Pacman/src/map.c')
-rw-r--r-- | Week1-Pacman/src/map.c | 31 |
1 files changed, 25 insertions, 6 deletions
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; |