diff options
| -rw-r--r-- | include/so_long.h | 9 | ||||
| -rw-r--r-- | src/main.c | 8 | ||||
| -rw-r--r-- | 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 <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/08 14:14:02 by dkaiser #+# #+# */ -/* Updated: 2024/05/10 14:55:46 by dkaiser ### ########.fr */ +/* Updated: 2024/05/10 17:28:23 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,9 @@ # define SO_LONG_H # include "MLX42/MLX42.h" +# include "fcntl.h" # include "libft.h" # include "unistd.h" -# include "fcntl.h" # define PLAYER_MOVE_SPEED 250 @@ -62,7 +62,7 @@ typedef struct s_tilemap { t_ivector grid_size; t_ivector tile_size; - char **tiles; + char *tiles; } t_tilemap; typedef struct s_game @@ -71,8 +71,11 @@ typedef struct s_game void *window; t_player player; int input_direction; + t_tilemap map; } t_game; +int load_map_from_file(t_tilemap *tilemap, char *filename); + int init(t_game *game); void loop(void *params); int draw(t_game *game); @@ -6,16 +6,20 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/08 14:14:13 by dkaiser #+# #+# */ -/* Updated: 2024/05/10 10:53:37 by dkaiser ### ########.fr */ +/* Updated: 2024/05/10 17:30:38 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "so_long.h" -int main(void) +int main(int argc, char *argv[]) { t_game game; + if (argc != 2) + return (1); + if (load_map_from_file(&game.map, argv[1])) + return (1); init(&game); mlx_loop(game.mlx); } diff --git a/src/tilemap.c b/src/tilemap.c index c0c0524..45d9fa5 100644 --- a/src/tilemap.c +++ b/src/tilemap.c @@ -6,7 +6,7 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/10 14:47:01 by dkaiser #+# #+# */ -/* Updated: 2024/05/10 15:51:50 by dkaiser ### ########.fr */ +/* Updated: 2024/05/10 17:26:17 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,8 @@ #include "so_long.h" static t_ivector get_map_size_from_file(int fd); +static int load_tiles_from_file(t_tilemap *tilemap, int fd); +static int load_tiles_from_line(t_tilemap *tilemap, int l, char *line); static int get_line_len(char *line); int load_map_from_file(t_tilemap *tilemap, char *filename) @@ -27,6 +29,15 @@ int load_map_from_file(t_tilemap *tilemap, char *filename) close(fd); if (tilemap->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; |
