parsing ok

This commit is contained in:
hugogogo
2021-07-24 15:26:48 +02:00
parent 0bf59a09b9
commit 04ef5aab68
8 changed files with 68 additions and 94 deletions
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+1 -1
View File
@@ -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); void draw_color_pixel(t_fdf *fdf, int new_x, int new_y, int z);
int keypress(int keycode, t_fdf *fdf); int keypress(int keycode, t_fdf *fdf);
void position_state(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); int *new_coordinates(t_fdf *fdf, int i, int j);
void draw_lines(t_fdf *fdf, int *start, int *end); void draw_lines(t_fdf *fdf, int *start, int *end);
void draw_grid(t_fdf *fdf, int i, int j); void draw_grid(t_fdf *fdf, int i, int j);
+1 -1
View File
@@ -76,7 +76,7 @@ void draw_color_pixel(t_fdf *fdf, int new_x, int new_y, int z)
color = 0xffffff; color = 0xffffff;
z -= (z * fdf->zoom) / fdf->offset; z -= (z * fdf->zoom) / fdf->offset;
z /= fdf->altitude; z /= fdf->altitude;
if (z) if (fdf->max_z)
{ {
color = color ^ (((0xff / fdf->max_z) * z) << 8 ); color = color ^ (((0xff / fdf->max_z) * z) << 8 );
color = color ^ ((0xff / fdf->max_z) * z); color = color ^ ((0xff / fdf->max_z) * z);
+5 -20
View File
@@ -43,32 +43,17 @@ t_fdf *init_fdf(t_fdf *fdf)
int main(int ac, char **av) int main(int ac, char **av)
{ {
t_fdf *fdf; t_fdf *fdf;
int fd;
if (ac == 1) if (ac != 2)
{ return (0);
fdf = malloc(sizeof(t_fdf)); fdf = malloc(sizeof(t_fdf));
fdf->map = parse_map(fdf, av); fd = open(av[1], O_RDONLY);
fdf->map = parse_map(fdf, fd);
fdf = init_fdf(fdf); fdf = init_fdf(fdf);
mlx_hook(fdf->win_ptr, 2, 1L << 0, keypress, fdf); mlx_hook(fdf->win_ptr, 2, 1L << 0, keypress, fdf);
mlx_hook(fdf->win_ptr, 17, 1L << 17, shut_down, fdf); mlx_hook(fdf->win_ptr, 17, 1L << 17, shut_down, fdf);
mlx_loop(fdf->mlx_ptr); 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);
}
}
return (0); return (0);
} }
+63 -74
View File
@@ -1,84 +1,73 @@
#include "fdf.h" #include "fdf.h"
/* int **split_to_map(t_fdf *fdf, char *raw)
int maint(int ac, char **av)
{ {
int *fd; int i;
int i = 0; int j;
int j = 0; int z;
int ret;
char *line = NULL;
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)
{
if ((ret = ft_gnl(fd[i], &line)) > 0)
{
ft_printf(" [fd%i-%i] %s\n", fd[i], ret, line);
free(line);
j = 0;
}
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);
}
*/
int **parse_map(t_fdf *fdf, char **av)
{
// int fd;
int **map; int **map;
map = ft_calloc(fdf->map_height, sizeof(map));
i = -1;
z = 0;
while (++i < fdf->map_height)
{
map[i] = ft_calloc(fdf->map_width, sizeof(*map));
j = -1;
while (++j < fdf->map_width)
{
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];
}
}
fdf->max_z = z;
return (map);
}
int size_width(char *raw)
{
int i; int i;
int j; int j;
(void)av; i = 0;
map = ft_calloc(10, sizeof(map)); while (raw[i] && raw[i] != '!')
i = -1;
while (++i < 10)
{ {
map[i] = ft_calloc(15, sizeof(*map)); while (raw[i] == ' ')
j = -1; i++;
while(++j < 15) while (ft_isdigit(raw[i]))
map[i][j] = 0; i++;
j++;
} }
map[9][0] = 1; return (j);
map[9][1] = 2; }
map[9][2] = 3;
map[9][3] = 4; int **parse_map(t_fdf *fdf, int fd)
map[9][4] = 5; {
map[9][5] = 6; int height;
map[9][6] = 7; int ret;
map[9][7] = 8; char *line;
map[9][8] = 9; char *raw;
map[9][9] = 10;
map[9][10] = 2; height = 0;
map[9][11] = 5; ret = 1;
map[9][12] = 8; line = NULL;
map[9][13] = 3; raw = ft_strdup("");
map[9][14] = 6; while (ret > 0)
fdf->max_z = 10; {
fdf->map_width = j; ret = ft_gnl(fd, &line);
fdf->map_height = i; if (ret > 0)
return (map); {
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));
} }