diff --git a/builds/draw.o b/builds/draw.o index 6c454ff..e550e70 100644 Binary files a/builds/draw.o and b/builds/draw.o differ diff --git a/builds/fdf.o b/builds/fdf.o index b40fb07..a435c20 100644 Binary files a/builds/fdf.o and b/builds/fdf.o differ diff --git a/builds/parse.o b/builds/parse.o index 02517c0..dce6a4b 100644 Binary files a/builds/parse.o and b/builds/parse.o differ diff --git a/fdf b/fdf index ef15727..fc06acb 100755 Binary files a/fdf and b/fdf differ diff --git a/includes/fdf.h b/includes/fdf.h index 23815ce..b6ca444 100644 --- a/includes/fdf.h +++ b/includes/fdf.h @@ -47,7 +47,7 @@ void draw_pixel(t_fdf *fdf, int x, int y, int color); void draw_color_pixel(t_fdf *fdf, int new_x, int new_y, int z); int keypress(int keycode, t_fdf *fdf); void position_state(t_fdf *fdf); -int **parse_map(t_fdf *fdf, char **av); +int **parse_map(t_fdf *fdf, int fd); int *new_coordinates(t_fdf *fdf, int i, int j); void draw_lines(t_fdf *fdf, int *start, int *end); void draw_grid(t_fdf *fdf, int i, int j); diff --git a/srcs/draw.c b/srcs/draw.c index 2cdc52a..4542026 100644 --- a/srcs/draw.c +++ b/srcs/draw.c @@ -76,7 +76,7 @@ void draw_color_pixel(t_fdf *fdf, int new_x, int new_y, int z) color = 0xffffff; z -= (z * fdf->zoom) / fdf->offset; z /= fdf->altitude; - if (z) + if (fdf->max_z) { color = color ^ (((0xff / fdf->max_z) * z) << 8 ); color = color ^ ((0xff / fdf->max_z) * z); diff --git a/srcs/fdf.c b/srcs/fdf.c index e3a093c..6713a71 100644 --- a/srcs/fdf.c +++ b/srcs/fdf.c @@ -43,32 +43,17 @@ t_fdf *init_fdf(t_fdf *fdf) int main(int ac, char **av) { t_fdf *fdf; + int fd; - if (ac == 1) - { - fdf = malloc(sizeof(t_fdf)); - fdf->map = parse_map(fdf, av); - fdf = init_fdf(fdf); - mlx_hook(fdf->win_ptr, 2, 1L << 0, keypress, fdf); - mlx_hook(fdf->win_ptr, 17, 1L << 17, shut_down, fdf); - mlx_loop(fdf->mlx_ptr); - } - if (ac == 2) - { - int fd; - int ret; - char *line; - - ret = 1; - line = NULL; - fd = open(av[1], O_RDONLY); - while (ret > 0) - { - ret = ft_gnl(fd, &line); - ft_printf("%s\n", line); - free(line); - } - } + if (ac != 2) + return (0); + fdf = malloc(sizeof(t_fdf)); + fd = open(av[1], O_RDONLY); + fdf->map = parse_map(fdf, fd); + fdf = init_fdf(fdf); + mlx_hook(fdf->win_ptr, 2, 1L << 0, keypress, fdf); + mlx_hook(fdf->win_ptr, 17, 1L << 17, shut_down, fdf); + mlx_loop(fdf->mlx_ptr); return (0); } diff --git a/srcs/parse.c b/srcs/parse.c index 69310ba..6dfc0f4 100644 --- a/srcs/parse.c +++ b/srcs/parse.c @@ -1,84 +1,73 @@ #include "fdf.h" -/* -int maint(int ac, char **av) +int **split_to_map(t_fdf *fdf, char *raw) { - int *fd; - int i = 0; - int j = 0; - int ret; - char *line = NULL; + int i; + int j; + int z; + int **map; - fd = (int *)ft_malloc(sizeof(int) * ac); - while (++i <= ac - 1) - fd[i - 1] = open(av[i], O_RDONLY); - i = 0; - while (j < ac - 1) + map = ft_calloc(fdf->map_height, sizeof(map)); + i = -1; + z = 0; + while (++i < fdf->map_height) { - if ((ret = ft_gnl(fd[i], &line)) > 0) + map[i] = ft_calloc(fdf->map_width, sizeof(*map)); + j = -1; + while (++j < fdf->map_width) { - ft_printf(" [fd%i-%i] %s\n", fd[i], ret, line); - free(line); - j = 0; + while (!ft_isdigit(*raw)) + raw++; + map[i][j] = ft_atoi(raw); + while (ft_isdigit(*raw)) + raw++; + if (map[i][j] > z) + z = map[i][j]; } - else if (ret == -1) - { - ft_printf("[fd%i-%i] *ERROR*\n", fd[i], ret); - free(line); - j++; - } - else if (*line != '\0') - ft_printf(" [fd%i-%i] %s\n", fd[i], ret, line); - else - { - ft_printf("[fd%i-%i] %s *FINI*\n", fd[i], ret, line); - free(line); - j++; - } - i++; - if (i >= ac - 1) - i = 0; } - free(fd); - //while (1); - return (0); + fdf->max_z = z; + return (map); } -*/ -int **parse_map(t_fdf *fdf, char **av) +int size_width(char *raw) { -// int fd; - int **map; int i; int j; - (void)av; - map = ft_calloc(10, sizeof(map)); - i = -1; - while (++i < 10) + i = 0; + while (raw[i] && raw[i] != '!') { - map[i] = ft_calloc(15, sizeof(*map)); - j = -1; - while(++j < 15) - map[i][j] = 0; + while (raw[i] == ' ') + i++; + while (ft_isdigit(raw[i])) + i++; + j++; } - map[9][0] = 1; - map[9][1] = 2; - map[9][2] = 3; - map[9][3] = 4; - map[9][4] = 5; - map[9][5] = 6; - map[9][6] = 7; - map[9][7] = 8; - map[9][8] = 9; - map[9][9] = 10; - map[9][10] = 2; - map[9][11] = 5; - map[9][12] = 8; - map[9][13] = 3; - map[9][14] = 6; - fdf->max_z = 10; - fdf->map_width = j; - fdf->map_height = i; - return (map); + return (j); +} + +int **parse_map(t_fdf *fdf, int fd) +{ + int height; + int ret; + char *line; + char *raw; + + height = 0; + ret = 1; + line = NULL; + raw = ft_strdup(""); + while (ret > 0) + { + ret = ft_gnl(fd, &line); + if (ret > 0) + { + height++; + raw = ft_strjoinfree(raw, line); + raw = ft_strjoinfree(raw, ft_strdup("!")); + } + } + fdf->map_height = height; + fdf->map_width = size_width(raw); + return (split_to_map(fdf, raw)); }