From: Dominik Kaiser Date: Fri, 10 May 2024 13:53:31 +0000 (+0200) Subject: Add tilemap.c and read map size X-Git-Url: https://git.dkaiser.de/?a=commitdiff_plain;h=69765e686473bbd920221eda728b2cb83a6e516d;p=42%2Fso_long.git Add tilemap.c and read map size --- diff --git a/Makefile b/Makefile index 6f4e2c8..763c897 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ HEADERS = -Iinclude -Ilibft -IMLX42/include LIBS = -Llibft -lft -lm -LMLX42/build -lmlx42 -ldl -lglfw -pthread VPATH := src -SRC = main.c init.c loop.c input.c draw.c +SRC = main.c init.c loop.c input.c draw.c tilemap.c OBJ_DIR := obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/include/so_long.h b/include/so_long.h index d66e355..ce481ef 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -6,16 +6,28 @@ /* By: dkaiser grid_size = get_map_size_from_file(fd); + close(fd); + if (tilemap->grid_size.x < 0 || tilemap->grid_size.y < 0) + return (1); + return (0); +} + +static t_ivector get_map_size_from_file(int fd) +{ + t_ivector result; + char *next_line; + + result.x = 0; + result.y = 0; + next_line = get_next_line(fd); + while (next_line) + { + if (!result.x) + result.x = get_line_len(next_line); + else if (result.x != get_line_len(next_line)) + { + free(next_line); + result.x = -1; + result.y = -1; + return (result); + } + result.y++; + free(next_line); + next_line = get_next_line(fd); + } + return (result); +} + +static int get_line_len(char *line) +{ + int len; + + len = 0; + while (line[len] && line[len] != '\n') + len++; + return (len); +}