From 81e3dc680b3895a8542d633d21b3698d4d41eaad Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Fri, 10 May 2024 17:39:41 +0200 Subject: [PATCH] Add tilemap loading --- include/so_long.h | 9 ++++++--- src/main.c | 8 ++++++-- src/tilemap.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/include/so_long.h b/include/so_long.h index ce481ef..2333368 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -6,7 +6,7 @@ /* By: dkaiser grid_size.x < 0 || tilemap->grid_size.y < 0) return (1); + fd = open(filename, O_RDONLY); + if (fd < 0) + return (1); + if (load_tiles_from_file(tilemap, fd)) + { + close(fd); + return (1); + } + close(fd); return (0); } @@ -56,6 +67,44 @@ static t_ivector get_map_size_from_file(int fd) return (result); } +static int load_tiles_from_file(t_tilemap *tilemap, int fd) +{ + int l; + char *next_line; + + tilemap->tiles = malloc(tilemap->grid_size.x * tilemap->grid_size.y + * sizeof(char)); + if (!tilemap->tiles) + return (1); + l = 0; + next_line = get_next_line(fd); + while (next_line) + { + if (load_tiles_from_line(tilemap, l, next_line)) + return (1); + next_line = get_next_line(fd); + l++; + } + return (0); +} + +static int load_tiles_from_line(t_tilemap *tilemap, int l, char *line) +{ + int x; + + x = 0; + while (line[x] && line[x] != '\n') + { + if (line[x] == EMPTY || line[x] == WALL || line[x] == COLLECTIBLE + || line[x] == PLAYER_START || line[x] == EXIT) + tilemap->tiles[l * tilemap->grid_size.x + x] = line[x]; + else + return (1); + line++; + } + return (0); +} + static int get_line_len(char *line) { int len; -- 2.47.2