diff --git a/inc/fdf.h b/inc/fdf.h index aa399d5..a428bf3 100644 --- a/inc/fdf.h +++ b/inc/fdf.h @@ -11,7 +11,8 @@ #include "libft.h" #include "mlx.h" -#include // warning!! +#include +#include typedef struct s_point { @@ -33,6 +34,6 @@ typedef struct s_swap int swap_coord; } t_swap; -void line(t_point p1, t_point p2, t_mlx m); +void line(t_point *p1, t_point *p2, t_mlx *m); #endif diff --git a/libft/Makefile b/libft/Makefile index 2a11e58..0f73c35 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -78,7 +78,8 @@ SRC = ft_memset.c \ ft_lstadd_back.c \ \ ft_realloc.c \ - ft_read_file.c + ft_read_file.c \ + get_next_line.c OBJ = $(SRC:.c=.o) INC = -I ./includes/ diff --git a/libft/ft_read_file.c b/libft/ft_read_file.c index 2674bb4..1f8f83f 100644 --- a/libft/ft_read_file.c +++ b/libft/ft_read_file.c @@ -16,11 +16,11 @@ char *read_file(const int fd) { long readed; long old_size; - char buf[BUF_S + 1]; + char buf[BUFF_SIZE + 1]; char *string; - string = ft_memalloc(sizeof(char) * BUF_S); - while ((readed = read(fd, buf, BUF_S))) + string = ft_memalloc(sizeof(char) * BUFF_SIZE); + while ((readed = read(fd, buf, BUFF_SIZE))) { buf[readed] = '\0'; old_size = ft_strlen(string); diff --git a/libft/get_next_line.c b/libft/get_next_line.c new file mode 100644 index 0000000..2f23cbf --- /dev/null +++ b/libft/get_next_line.c @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/04 15:59:21 by gtertysh #+# #+# */ +/* Updated: 2016/12/06 19:45:44 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static t_fd *new_node(int fd) +{ + t_fd *new; + + new = NULL; + if ((new = (t_fd *)malloc(sizeof(t_fd)))) + { + new->fd = fd; + new->n = NULL; + new->next = NULL; + new->t = 0; + } + return (new); +} + +static t_fd *find_fd(int fd, t_fd **cur_fd) +{ + if (!*cur_fd) + return ((*cur_fd = new_node(fd))); + while ((*cur_fd)) + { + if ((*cur_fd)->fd == fd) + return (*cur_fd); + cur_fd = &((*cur_fd)->next); + } + return ((*cur_fd = new_node(fd))); +} + +int get_next_line(const int fd, char **line) +{ + static t_fd *head_fd; + t_fd *c; + + if (fd < 0 || !line) + return (-1); + *line = ft_strnew(0); + c = find_fd(fd, &head_fd); + c->readed = 0; + if (c->n) { + if ((c->t = strchr(c->n, '\n')) && + (*line = ft_strjoin(*line, ft_strsub(c->n, 0, (c->t - c->n)))) && + (c->n = ++(c->t))) + { + return (1); + } + else if ((*line = ft_strjoin(*line, c->n))) + { + c->n = 0; + } + } + while ((c->readed = read(c->fd, c->b, BUFF_SIZE))) + { + c->b[c->readed] = 0; + if ((c->n = strchr(c->b, '\n')) && + (*line = ft_strjoin(*line, ft_strsub(c->b, 0, (c->n++ - c->b))))) + break; + *line = ft_strjoin(*line, c->b); + } + if (!c->readed && !ft_strlen(*line)) + return (0); + return (1); +} \ No newline at end of file diff --git a/libft/includes/libft.h b/libft/includes/libft.h index 0f062e7..1df67c1 100644 --- a/libft/includes/libft.h +++ b/libft/includes/libft.h @@ -17,7 +17,17 @@ # include # include -# define BUF_S 1024 +# define BUFF_SIZE 100 + +typedef struct s_fd +{ + int fd; + char b[BUFF_SIZE + 1]; + char *n; + struct s_fd *next; + char *t; + int readed; +} t_fd; typedef struct s_list { @@ -97,5 +107,6 @@ void *ft_realloc(void *old, unsigned int new_size, unsigned int old_size); char *ft_read_file(const int fd); +int get_next_line(const int fd, char **line); #endif diff --git a/src/main.c b/src/main.c index a01677c..7a5931e 100644 --- a/src/main.c +++ b/src/main.c @@ -11,12 +11,12 @@ void swap_x_y(t_point *p1, t_point *p2) { int temp; - temp = (*p1).x; - (*p1).x = (*p1).y; - (*p1).y = temp; - temp = (*p2).x; - (*p2).x = (*p2).y; - (*p2).y = temp; + temp = p1->x; + p1->x = p1->y; + p1->y = temp; + temp = p2->x; + p2->x = p2->y; + p2->y = temp; } void swap_points(t_point *p1, t_point *p2) @@ -29,168 +29,181 @@ void swap_points(t_point *p1, t_point *p2) } -void draw_line(t_point p1, t_point p2, t_mlx m, t_swap s) +void draw_line(t_point *p1, t_point *p2, t_mlx *m, t_swap *s) { - int dx = abs(p2.x - p1.x); - int dy = abs(p2.y - p1.y); + int dx = abs(p2->x - p1->x); + int dy = abs(p2->y - p1->y); int derr = 2 * dy - dx; - while (p1.x < p2.x) + while (p1->x < p2->x) { - if (s.swap_coord) - mlx_pixel_put(m.mlx, m.win, p1.y, p1.x, p1.color); + if (s->swap_coord) + mlx_pixel_put(m->mlx, m->win, p1->y, p1->x, p1->color); else - mlx_pixel_put(m.mlx, m.win, p1.x, p1.y, p1.color); + mlx_pixel_put(m->mlx, m->win, p1->x, p1->y, p1->color); if (derr > 0) { - p1.y += s.swap_x; + p1->y += s->swap_x; derr -= dx; } - p1.x++; + (p1->x)++; derr += dy; } } -void test_line(t_mlx mlx) -{ - t_point p1; - t_point p2; - - p1.x = 0; - p1.y = 0; - p2.x = 500; - p1.color = 0xC100E5; - p2.color = 0xC100E5; - for (p2.y = 0; p2.y <= 500; p2.y += 4) - { - line(p1, p2, mlx); - } - for (p2.x = 500; p2.x >= 0; p2.x -= 4) - { - line(p1, p2, mlx); - } - p1.x = 500; - p1.y = 0; - p2.x = 0; - p1.color = 0x1EBF00; - p2.color = 0x1EBF00; - for (p2.y = 0; p2.y <= 500; p2.y += 4) - { - line(p1, p2, mlx); - } - for (p2.x = 0; p2.x <= 500; p2.x += 4) - { - line(p1, p2, mlx); - } - p1.x = 500; - p1.y = 500; - p2.x = 500; - p2.y = 0; - p1.color = 0xFF000D; - p2.color = 0xFF000D; - for (p2.x = 500; p2.x >= 0; p2.x -= 4) - { - line(p1, p2, mlx); - } - for (p2.y = 0; p2.y <= 500; p2.y += 4) - { - line(p1, p2, mlx); - } - p1.x = 0; - p1.y = 500; - p2.x = 500; - p2.y = 500; - p1.color = 0xFAEB19; - p2.color = 0xFAEB19; - for (p2.y = 500; p2.y >= 0; p2.y -= 4) - { - line(p1, p2, mlx); - } - for (p2.x = 500; p2.x >= 0; p2.x -= 4) - { - line(p1, p2, mlx); - } - mlx_clear_window(mlx.mlx, mlx.win); - p1.x = 0; - p1.y = 0; - p2.y = 500; - p1.color = 0xC100E5; - p2.color = 0xC100E5; - for (p2.x = 0; p2.x <= 500; p2.x += 1) - { - line(p1, p2, mlx); - p1.x += 1; - } - mlx_clear_window(mlx.mlx, mlx.win); - p1.x = 0; - p1.y = 0; - p2.x = 500; - p1.color = 0x1EBF00; - p2.color = 0x1EBF00; - for (p2.y = 0; p2.y <= 500; p2.y += 1) - { - line(p1, p2, mlx); - p1.y += 1; - } - mlx_clear_window(mlx.mlx, mlx.win); - p1.x = 500; - p1.y = 0; - p2.x = 500; - p1.color = 0xFF000D; - p2.color = 0xFF000D; - for (; p2.x >= 0; p2.x -= 1) - { - line(p1, p2, mlx); - p1.x -= 1; - } - mlx_clear_window(mlx.mlx, mlx.win); - p1.x = 0; - p1.y = 500; - p2.x = 500; - p1.color = 0xFAEB19; - p2.color = 0xFAEB19; - for (p2.y = 500; p2.y >= 0; p2.y -= 1) - { - line(p1, p2, mlx); - p1.y -= 1; - } -} - -void line(t_point p1, t_point p2, t_mlx m) +void line(t_point *p1, t_point *p2, t_mlx *m) { int dx; int dy; t_swap swap; - dx = abs(p2.x - p1.x); - dy = abs(p2.y - p1.y); + dx = abs(p2->x - p1->x); + dy = abs(p2->y - p1->y); swap.swap_coord = 0; swap.swap_x = 1; if (dy > dx) { - swap_x_y(&p1, &p2); + swap_x_y(p1, p2); swap.swap_coord= 1; } - if (p1.x > p2.x) + if (p1->x > p2->x) { - swap_points(&p1, &p2); + swap_points(p1, p2); } - if (p1.y > p2.y) + if (p1->y > p2->y) { swap.swap_x = -1; } - draw_line(p1, p2, m, swap); + draw_line(p1, p2, m, &swap); } +/* Don't work with struct pointers! */ + +//void test_line(t_mlx mlx) +//{ +// t_point p1; +// t_point p2; +// +// p1.x = 0; +// p1.y = 0; +// p2.x = 500; +// p1.color = 0xC100E5; +// p2.color = 0xC100E5; +// for (p2.y = 0; p2.y <= 500; p2.y += 4) +// { +// line(&p1, &p2, &mlx); +// } +// for (p2.x = 500; p2.x >= 0; p2.x -= 4) +// { +// line(&p1, &p2, &mlx); +// } +// p1.x = 500; +// p1.y = 0; +// p2.x = 0; +// p1.color = 0x1EBF00; +// p2.color = 0x1EBF00; +// for (p2.y = 0; p2.y <= 500; p2.y += 4) +// { +// line(&p1, &p2, &mlx); +// } +// for (p2.x = 0; p2.x <= 500; p2.x += 4) +// { +// line(&p1, &p2, &mlx); +// } +// p1.x = 500; +// p1.y = 500; +// p2.x = 500; +// p2.y = 0; +// p1.color = 0xFF000D; +// p2.color = 0xFF000D; +// for (p2.x = 500; p2.x >= 0; p2.x -= 4) +// { +// line(&p1, &p2, &mlx); +// } +// for (p2.y = 0; p2.y <= 500; p2.y += 4) +// { +// line(&p1, &p2, &mlx); +// } +// p1.x = 0; +// p1.y = 500; +// p2.x = 500; +// p2.y = 500; +// p1.color = 0xFAEB19; +// p2.color = 0xFAEB19; +// for (p2.y = 500; p2.y >= 0; p2.y -= 4) +// { +// line(&p1, &p2, &mlx); +// } +// for (p2.x = 500; p2.x >= 0; p2.x -= 4) +// { +// line(&p1, &p2, &mlx); +// } +// mlx_clear_window(mlx.mlx, mlx.win); +// p1.x = 0; +// p1.y = 0; +// p2.y = 500; +// p1.color = 0xC100E5; +// p2.color = 0xC100E5; +// for (p2.x = 0; p2.x <= 500; p2.x += 1) +// { +// line(&p1, &p2, &mlx); +// p1.x += 1; +// } +// mlx_clear_window(mlx.mlx, mlx.win); +// p1.x = 0; +// p1.y = 0; +// p2.x = 500; +// p1.color = 0x1EBF00; +// p2.color = 0x1EBF00; +// for (p2.y = 0; p2.y <= 500; p2.y += 1) +// { +// line(&p1, &p2, &mlx); +// p1.y += 1; +// } +// mlx_clear_window(mlx.mlx, mlx.win); +// p1.x = 500; +// p1.y = 0; +// p2.x = 500; +// p1.color = 0xFF000D; +// p2.color = 0xFF000D; +// for (; p2.x >= 0; p2.x -= 1) +// { +// line(&p1, &p2, &mlx); +// p1.x -= 1; +// } +// mlx_clear_window(mlx.mlx, mlx.win); +// p1.x = 0; +// p1.y = 500; +// p2.x = 500; +// p1.color = 0xFAEB19; +// p2.color = 0xFAEB19; +// for (p2.y = 500; p2.y >= 0; p2.y -= 1) +// { +// line(&p1, &p2, &mlx); +// p1.y -= 1; +// } +//} + + int main(int argc, char **argv) { - t_mlx m; + t_mlx m; + int fd; + char *line; - argc = argc + 1; - argv = argv; + if (argc != 2) + { + return (0); + } + fd = open(argv[1], O_RDONLY); + while (get_next_line(fd, &line)) + { + ft_putstr(line); + ft_putstr("\n"); + } m.mlx = mlx_init(); m.win = mlx_new_window(m.mlx, 500, 500, "fdf"); - //line(p1, p2, m); - test_line(m); + //line(&p1, &p2, &m); + //test_line(m); mlx_loop(m.mlx); return (0); }