diff --git a/Makefile b/Makefile index 86bc46d..d2053d6 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,8 @@ LIBS = $(_LIBS:lib%.a=%) SRCS = fdf.c \ draw.c \ parse.c \ - modifs.c + modifs.c \ + keypress.c ODIR = ./builds OBJS = $(SRCS:%.c=$(ODIR)/%.o) @@ -55,8 +56,9 @@ $(ODIR)/%.o: %.c debug: CFLAGS += -fsanitize=address debug: clean $(NAME) -leaks: clean $(NAME) - valgrind --leak-check=full --show-leak-kinds=all ./$(NAME) +leaks: $(NAME) + valgrind --leak-check=full --show-leak-kinds=all ./$(NAME) maps/42_color.fdf +# valgrind --leak-check=full --leak-resolution=low --show-reachable=yes ./$(NAME) maps/42_color.fdf clean: /bin/rm -f $(OBJS) diff --git a/builds/draw.o b/builds/draw.o new file mode 100644 index 0000000..f8d9556 Binary files /dev/null and b/builds/draw.o differ diff --git a/builds/fdf.o b/builds/fdf.o new file mode 100644 index 0000000..73908f5 Binary files /dev/null and b/builds/fdf.o differ diff --git a/builds/keypress.o b/builds/keypress.o new file mode 100644 index 0000000..5171fd1 Binary files /dev/null and b/builds/keypress.o differ diff --git a/builds/modifs.o b/builds/modifs.o new file mode 100644 index 0000000..57264be Binary files /dev/null and b/builds/modifs.o differ diff --git a/builds/parse.o b/builds/parse.o new file mode 100644 index 0000000..0d3d5d4 Binary files /dev/null and b/builds/parse.o differ diff --git a/fdf b/fdf new file mode 100755 index 0000000..2e6ef0d Binary files /dev/null and b/fdf differ diff --git a/includes/fdf.h b/includes/fdf.h index 8c3d79c..82bf42b 100644 --- a/includes/fdf.h +++ b/includes/fdf.h @@ -16,7 +16,6 @@ typedef struct s_fdf char *img_addr; int **map; int offset; - int margin; int min_z; int max_z; int z_amplitude; @@ -41,13 +40,16 @@ typedef struct s_fdf int img_endian; } t_fdf; -t_fdf *init_fdf(t_fdf *fdf); +void init_fdf(t_fdf *fdf); +void init_offset(t_fdf *fdf); +void init_server(t_fdf *fdf); int print_keycode(int keycode); int shut_down(t_fdf *fdf); void draw_image(t_fdf *fdf); 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 keypress_more(int keycode, t_fdf *fdf); void position_state(t_fdf *fdf); int **parse_map(t_fdf *fdf, int fd); int *new_coordinates(t_fdf *fdf, int i, int j); diff --git a/libft b/libft index 60ebf29..1f234c4 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 60ebf2933397d917c0ce970657b4fabe0e6c1f81 +Subproject commit 1f234c4e589cc9e723bfef66cca7c923821f5484 diff --git a/srcs/fdf.c b/srcs/fdf.c index b43cb89..41ff305 100644 --- a/srcs/fdf.c +++ b/srcs/fdf.c @@ -2,43 +2,79 @@ int shut_down(t_fdf *fdf) { - while (fdf->map_height--) - free(fdf->map[fdf->map_height]); - free(fdf->map); - mlx_destroy_image(fdf->mlx_ptr, fdf->img_ptr); - mlx_destroy_window(fdf->mlx_ptr, fdf->win_ptr); - mlx_destroy_display(fdf->mlx_ptr); - free(fdf); + if (fdf->map) + { + while (fdf->map_height-- && fdf->map[fdf->map_height]) + free(fdf->map[fdf->map_height]); + free(fdf->map); + } + if (fdf->img_ptr) + mlx_destroy_image(fdf->mlx_ptr, fdf->img_ptr); + if (fdf->win_ptr) + mlx_destroy_window(fdf->mlx_ptr, fdf->win_ptr); + if (fdf->mlx_ptr) + mlx_destroy_display(fdf->mlx_ptr); + if (fdf) + free(fdf); exit(0); return (0); } -t_fdf *init_fdf(t_fdf *fdf) +void init_server(t_fdf *fdf) { - fdf->offset = 50; - fdf->margin = 50; + fdf->mlx_ptr = mlx_init(); + if (!fdf->mlx_ptr) + shut_down(fdf); + fdf->win_ptr = mlx_new_window(fdf->mlx_ptr, fdf->win_size_x, + fdf->win_size_y, "test"); + if (!fdf->win_ptr) + shut_down(fdf); + fdf->img_ptr = mlx_new_image(fdf->mlx_ptr, fdf->img_size_x, + fdf->img_size_y); + if (!fdf->img_ptr) + shut_down(fdf); + fdf->img_addr = mlx_get_data_addr(fdf->img_ptr, &(fdf->img_bpp), + &(fdf->img_sizel), &(fdf->img_endian)); + if (!fdf->img_addr) + shut_down(fdf); +} + +void init_offset(t_fdf *fdf) +{ + int diagonal; + int x; + + x = (fdf->map_width) * (fdf->map_width); + x += (fdf->map_height) * (fdf->map_height); + fdf->offset = 1; + diagonal = 1; + while (diagonal < fdf->win_size_x) + { + diagonal = x * (fdf->offset * fdf->offset); + diagonal = ft_sqrt(diagonal); + fdf->offset++; + } + fdf->offset--; +} + +void init_fdf(t_fdf *fdf) +{ + fdf->win_size_x = 700; + fdf->win_size_y = 700; + fdf->img_size_x = fdf->win_size_x; + fdf->img_size_y = fdf->win_size_y; + init_offset(fdf); fdf->altitude = 3; fdf->z_amplitude = fdf->max_z - fdf->min_z; fdf->map_size_x = (fdf->map_width - 1) * fdf->offset + 1; fdf->map_size_y = (fdf->map_height - 1) * fdf->offset + 1; - fdf->win_size_x = fdf->map_size_x + 2 * fdf->margin; - fdf->win_size_y = fdf->map_size_y + 2 * fdf->margin; - fdf->img_size_x = fdf->win_size_x; - fdf->img_size_y = fdf->win_size_y; - fdf->rot_x = -30; - fdf->rot_y = -60; - fdf->mov_x = 0; - fdf->mov_y = 0; - fdf->zoom = -5; - fdf->mlx_ptr = mlx_init(); - fdf->win_ptr = mlx_new_window(fdf->mlx_ptr, fdf->win_size_x, - fdf->win_size_y, "test"); - fdf->img_ptr = mlx_new_image(fdf->mlx_ptr, fdf->img_size_x, - fdf->img_size_y); - fdf->img_addr = mlx_get_data_addr(fdf->img_ptr, &(fdf->img_bpp), - &(fdf->img_sizel), &(fdf->img_endian)); + fdf->rot_x = -45; + fdf->rot_y = -35; + fdf->mov_x = (fdf->win_size_x - fdf->map_size_x) / 2; + fdf->mov_y = (fdf->win_size_y - fdf->map_size_y) / 2; + fdf->zoom = 0; + init_server(fdf); draw_image(fdf); - return (fdf); } int main(int ac, char **av) @@ -50,8 +86,10 @@ int main(int ac, char **av) return (0); fdf = malloc(sizeof(t_fdf)); fd = open(av[1], O_RDONLY); + if (!fd || !fdf) + shut_down(fdf); fdf->map = parse_map(fdf, fd); - fdf = init_fdf(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); diff --git a/srcs/keypress.c b/srcs/keypress.c new file mode 100644 index 0000000..e439b38 --- /dev/null +++ b/srcs/keypress.c @@ -0,0 +1,60 @@ +#include "fdf.h" + +/* +** U -> up wiew +** I -> isometric view +** F -> fit to window +** C -> center +*/ +void keypress_more(int keycode, t_fdf *fdf) +{ + if (keycode == ESCAPE) + shut_down(fdf); + if (keycode == U) + { + fdf->rot_x = 0; + fdf->rot_y = 0; + } + else if (keycode == I) + { + fdf->rot_x = -30; + fdf->rot_y = -60; + } +} + +/* +** Q -> move left +** D -> move right +** Z -> move up +** S -> move down +** A -> zoom +** W -> unzoom +*/ +int keypress(int keycode, t_fdf *fdf) +{ + if (keycode == LEFT) + fdf->rot_x += 1; + else if (keycode == RIGHT) + fdf->rot_x -= 1; + else if (keycode == UP) + fdf->rot_y += 1; + else if (keycode == DOWN) + fdf->rot_y -= 1; + else if (keycode == Q) + fdf->mov_x -= 6; + else if (keycode == D) + fdf->mov_x += 6; + else if (keycode == Z) + fdf->mov_y -= 6; + else if (keycode == S) + fdf->mov_y += 6; + else if (keycode == A) + fdf->zoom += 6; + else if (keycode == W) + fdf->zoom -= 6; + else + keypress_more(keycode, fdf); + draw_image(fdf); + return (0); +} + diff --git a/srcs/modifs.c b/srcs/modifs.c index d706e42..427a295 100644 --- a/srcs/modifs.c +++ b/srcs/modifs.c @@ -1,34 +1,5 @@ #include "fdf.h" -// add "print_keycode(keycode);" at begining to print keycode -int keypress(int keycode, t_fdf *fdf) -{ - if (keycode == ESCAPE) - shut_down(fdf); - else if (keycode == LEFT) - (fdf->rot_x) += 1; - else if (keycode == RIGHT) - (fdf->rot_x) -= 1; - else if (keycode == UP) - (fdf->rot_y) += 1; - else if (keycode == DOWN) - (fdf->rot_y) -= 1; - else if (keycode == Q) - (fdf->mov_x) -= 6; - else if (keycode == D) - (fdf->mov_x) += 6; - else if (keycode == Z) - (fdf->mov_y) -= 6; - else if (keycode == S) - (fdf->mov_y) += 6; - else if (keycode == A) - (fdf->zoom) += 6; - else if (keycode == W) - (fdf->zoom) -= 6; - draw_image(fdf); - return (0); -} - /* ** int[0] = x, int[1] = y, int[2] = z ** quick explanation : @@ -39,7 +10,7 @@ int keypress(int keycode, t_fdf *fdf) ** z changes according to zoom : ** z += (z * fdf->zoom) / fdf->offset; ** deplacement : -** point[0] += fdf->margin + fdf->mov_x; +** point[0] += fdf->mov_x; ** center zoom : ** point[0] -= ((fdf->map_size_x / 2) / fdf->offset) * fdf->zoom; ** center after 3d rotation : @@ -62,8 +33,8 @@ int *new_coordinates(t_fdf *fdf, int i, int j) point[0] = x * cos(fdf->rad_x) + y * sin(fdf->rad_x); point[1] = y * cos(fdf->rad_x) - x * sin(fdf->rad_x); point[1] = point[1] * cos(fdf->rad_y) - -z * sin(fdf->rad_y); - point[0] += fdf->margin + fdf->mov_x; - point[1] += fdf->margin + fdf->mov_y; + point[0] += fdf->mov_x; + point[1] += fdf->mov_y; point[0] -= ((fdf->map_size_x / 2) / fdf->offset) * fdf->zoom; point[1] -= ((fdf->map_size_y / 2) / fdf->offset) * fdf->zoom; point[0] += (fdf->map_size_x + fdf->zoom * fdf->map_width) / 2; @@ -72,13 +43,6 @@ int *new_coordinates(t_fdf *fdf, int i, int j) return (point); } -int print_keycode(int keycode) -{ - ft_putnbr(keycode); - ft_putchar(' '); - return (0); -} - void position_state(t_fdf *fdf) { char *position; @@ -101,3 +65,12 @@ void position_state(t_fdf *fdf) mlx_string_put(fdf->mlx_ptr, fdf->win_ptr, x, y, 0xffffff, position); free(position); } + +/* +** int print_keycode(int keycode) +** { +** ft_putnbr(keycode); +** ft_putchar(' '); +** return (0); +** } +*/ diff --git a/srcs/parse.c b/srcs/parse.c index dde9447..41d2aec 100644 --- a/srcs/parse.c +++ b/srcs/parse.c @@ -15,10 +15,14 @@ int **split_to_map(t_fdf *fdf, char *raw) int **map; map = ft_calloc(fdf->map_height, sizeof(map)); + if (!map) + shut_down(fdf); i = -1; while (++i < fdf->map_height) { map[i] = ft_calloc(fdf->map_width, sizeof(*map)); + if (!map[i]) + shut_down(fdf); j = -1; while (++j < fdf->map_width) {