From 5558fd3d3d094e96b05d1e1cb1f349d316be3fb2 Mon Sep 17 00:00:00 2001 From: whaffman Date: Mon, 9 Dec 2024 14:55:44 +0100 Subject: [PATCH] fdf struct and draw_hook and key_hook rotations --- Makefile | 2 +- inc/fdf.h | 17 ++++++++-- src/fdf.c | 83 ++++++++++++++++++++----------------------------- src/fdf_hooks.c | 49 +++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 53 deletions(-) create mode 100644 src/fdf_hooks.c diff --git a/Makefile b/Makefile index f48547a..a0291d9 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ MLX42 = $(MLX42_PATH)/build/libmlx42.a OBJ_PATH = obj VPATH = src -SOURCES = fdf.c fdf_rotations.c fdf_draw.c +SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c")) OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) diff --git a/inc/fdf.h b/inc/fdf.h index 2eb18f2..0afd6a9 100644 --- a/inc/fdf.h +++ b/inc/fdf.h @@ -22,6 +22,9 @@ # include "libft.h" # include "MLX42.h" +# define SUCCESS 1 +# define FAILURE 0 + # define WRONG_LINE_LENGTH "Error: wrong line length. \ Expected %d, got %d in line nr %d\n" # define MALLOC_ERROR "Error: malloc failed\n" @@ -34,8 +37,8 @@ # define SQRT2 1.41421356237309514547 # define SQRT6 2.44948974278317788134 -# define WIDTH 1024 -# define HEIGHT 640 +# define WIDTH 2000 +# define HEIGHT 1500 typedef struct s_point_3d { @@ -79,7 +82,7 @@ typedef struct s_fdf int get_map_sizes(char *filename, t_map *map); void free_line_and_split(char **line, char ***split); int read_map(char *filename, t_map *map); -t_map *parse_map(char *filename); +int parse_map(char *filename, t_fdf *fdf); void print_map(t_map *map); void rotate_x(t_point_3d **points, double angle, int size); void rotate_y(t_point_3d **points, double angle, int size); @@ -87,5 +90,13 @@ void rotate_z(t_point_3d **points, double angle, int size); bool fdf_put_pixel(mlx_image_t *img, t_point_2d point); void fdf_draw_line(mlx_image_t *img, t_point_2d start, t_point_2d end); +int fdf_hooks(t_fdf *fdf); +void draw_hook(void *param); +void key_hook(mlx_key_data_t keydata, void *param); +void fdf_set_background(mlx_image_t *img, int color); +void fdf_project_isometric(t_map *map); +double deg2rad(double deg); + + #endif diff --git a/src/fdf.c b/src/fdf.c index 2f6826a..4ac995c 100644 --- a/src/fdf.c +++ b/src/fdf.c @@ -111,26 +111,27 @@ void print_map(t_map *map) } } -t_map *parse_map(char *filename) +int parse_map(char *filename, t_fdf *fdf) { t_map *map; map = malloc(sizeof(t_map)); if (!map) - return (ft_printf(MALLOC_ERROR), NULL); + return (ft_printf(MALLOC_ERROR), FAILURE); if (!get_map_sizes(filename, map)) - return (free(map), NULL); + return (free(map), FAILURE); map->points = malloc(map->width * map->height * sizeof(t_point_3d)); if (!map->points) - return (free(map), ft_printf(MALLOC_ERROR), NULL); + return (free(map), ft_printf(MALLOC_ERROR), FAILURE); if (!read_map(filename, map)) - return (free(map->points), free(map), NULL); - return (map); + return (free(map->points), free(map), FAILURE); + fdf->map = map; + return (SUCCESS); } void fdf_set_background(mlx_image_t *img, int color) { - int x; - int y; + int x; + int y; y = 0; while (y < (int) img->height) @@ -147,10 +148,10 @@ void fdf_set_background(mlx_image_t *img, int color) void fdf_project_isometric(t_map *map) { - int i; - double x; - double y; - const int scale = fmax(map->height, map->width) * SQRT2; + int i; + double x; + double y; + const int scale = fmax(map->height, map->width) * SQRT2; i = 0; map->projected = malloc(map->width * map->height * sizeof(t_point_2d)); @@ -158,9 +159,6 @@ void fdf_project_isometric(t_map *map) return ; while (i < map->width * map->height) { - // x = (map->points[i].x - map->points[i].y) * cos(0.523599); - // y = -map->points[i].z + (map->points[i].x + map->points[i].y) * sin(0.523599); - x = (map->points[i].x - map->points[i].y) / SQRT2; y = (map->points[i].x + map->points[i].y - 2 * map->points[i].z) / SQRT6; map->projected[i].x = x * WIDTH / scale + WIDTH / 2; @@ -170,54 +168,41 @@ void fdf_project_isometric(t_map *map) } } -int draw_map(t_map *map) -{ - mlx_t* mlx; - mlx_image_t* img; - int i; - - mlx = mlx_init(WIDTH, HEIGHT, "FdF", false); - if (!mlx) - return (EXIT_FAILURE); - img = mlx_new_image(mlx, mlx->width, mlx->height); - i = 0; - if (!img || (mlx_image_to_window(mlx, img, 0, 0) < 0)) - return (EXIT_FAILURE); - fdf_set_background(img, 0x000000FF); - fdf_project_isometric(map); - while (i < map->width * map->height) - { - fdf_put_pixel(img, map->projected[i]); - if (i % map->width != map->width - 1) - fdf_draw_line(img, map->projected[i], map->projected[i + 1]); - if (i / map->width != map->height - 1) - fdf_draw_line(img, map->projected[i], map->projected[i + map->width]); - i++; - } - mlx_loop(mlx); - mlx_terminate(mlx); - return (EXIT_SUCCESS); -} double deg2rad(double deg) { return (deg / 180.0 * M_PI); } + + + +int init_mlx(t_fdf *fdf) +{ + fdf->mlx = mlx_init(WIDTH, HEIGHT, "FdF", false); + if (!fdf->mlx) + return (FAILURE); + fdf->img = mlx_new_image(fdf->mlx, fdf->mlx->width, fdf->mlx->height); + if (!fdf->img || (mlx_image_to_window(fdf->mlx, fdf->img, 0, 0) < 0)) + return (FAILURE); + fdf_hooks(fdf); + mlx_loop(fdf->mlx); + mlx_terminate(fdf->mlx); + return (SUCCESS); +} + int main(int argc, char *argv[]) { - t_map *map; + t_fdf fdf; + if (argc != 2) { ft_printf(USAGE_ERROR, argv[0]); return (EXIT_FAILURE); } - map = parse_map(argv[1]); - if (!map) + if (!parse_map(argv[1], &fdf)) return (EXIT_FAILURE); - // rotate_z(&map->points, deg2rad(30) , map->width * map->height); - //rotate_x(&map->points, deg2rad(70), map->width * map->height); - draw_map(map); + init_mlx(&fdf); return (EXIT_SUCCESS); } diff --git a/src/fdf_hooks.c b/src/fdf_hooks.c new file mode 100644 index 0000000..dc9f730 --- /dev/null +++ b/src/fdf_hooks.c @@ -0,0 +1,49 @@ +#include "fdf.h" +#include "MLX42.h" + +int fdf_hooks(t_fdf *fdf) +{ + mlx_loop_hook(fdf->mlx, draw_hook, fdf); + mlx_key_hook(fdf->mlx, key_hook, fdf); + return (1); +} + +void draw_hook(void *param) +{ + t_fdf *fdf; + int i; + + fdf = (t_fdf *)param; + fdf_set_background(fdf->img, 0x000000FF); + fdf_project_isometric(fdf->map); + i = 0; + while (i < fdf->map->width * fdf->map->height) + { + fdf_put_pixel(fdf->img, fdf->map->projected[i]); + if (i % fdf->map->width != fdf->map->width - 1) + fdf_draw_line(fdf->img, fdf->map->projected[i], fdf->map->projected[i + 1]); + if (i / fdf->map->width != fdf->map->height - 1) + fdf_draw_line(fdf->img, fdf->map->projected[i], fdf->map->projected[i + fdf->map->width]); + i++; + } +} +void key_hook(mlx_key_data_t keydata, void *param) +{ + t_fdf *fdf; + + fdf = (t_fdf *)param; + if (keydata.key == MLX_KEY_ESCAPE) + { + mlx_close_window(((t_fdf *)param)->mlx); + exit(0); + } + if (keydata.key == MLX_KEY_LEFT && keydata.action == MLX_PRESS) + rotate_z(&fdf->map->points, deg2rad(-5), fdf->map->width * fdf->map->height); + if (keydata.key == MLX_KEY_RIGHT && keydata.action == MLX_PRESS) + rotate_z(&fdf->map->points, deg2rad(5), fdf->map->width * fdf->map->height); + if (keydata.key == MLX_KEY_UP && keydata.action == MLX_PRESS) + rotate_x(&fdf->map->points, deg2rad(-5), fdf->map->width * fdf->map->height); + if (keydata.key == MLX_KEY_DOWN && keydata.action == MLX_PRESS) + rotate_x(&fdf->map->points, deg2rad(5), fdf->map->width * fdf->map->height); + +}