diff --git a/Makefile b/Makefile index c6ea504..b9e74fb 100644 --- a/Makefile +++ b/Makefile @@ -58,7 +58,7 @@ $(MLX42_DIR): git submodule add https://github.com/codam-coding-college/MLX42.git $(MLX42_PATH); $(LIBFT): $(LIBFT_PATH) - $(MAKE) -sC $(LIBFT_PATH) + $(MAKE) -C $(LIBFT_PATH) $(LIBFT_PATH): git submodule add git@duinvoetje.nl:willem/libft.git $(LIBFT_PATH) @@ -72,6 +72,7 @@ $(OBJ_PATH)/%.o: %.c $(LIBFT) $(MLX42) | $(OBJ_PATH) clean: $(RM) $(OBJECTS) $(OBJ_PATH) $(MAKE) -C $(LIBFT_PATH) clean + $(MAKE) -C $(MLX42_PATH)/build clean fclean: clean $(RM) $(NAME) diff --git a/inc/fdf.h b/inc/fdf.h index 719053f..c6743ef 100644 --- a/inc/fdf.h +++ b/inc/fdf.h @@ -134,11 +134,12 @@ void set_map_point(t_fdf *fdf, int x, int y, char **str); mlx_image_t *draw_menu(t_fdf *fdf); int get_z_color(double z, int z_max); void close_hook(void *param); -int get_gradient_color(double z, int z_max); -void project_parallel(t_fdf *fdf); -void project(t_fdf *fdf); -int get_pixel_color(t_fdf *fdf, t_point_3d point); -int check_filename(char *filename); +int get_gradient_color(double z, int z_max); +void project_parallel(t_fdf *fdf); +void project(t_fdf *fdf); +int check_filename(char *filename); +void get_pixel_color(t_fdf *fdf, t_point_2d *point); +void reset_fdf(t_fdf *fdf); diff --git a/src/fdf_color.c b/src/fdf_color.c index bd52127..5784abd 100644 --- a/src/fdf_color.c +++ b/src/fdf_color.c @@ -37,6 +37,17 @@ int parse_color_string(char *str) return ((color << 8) + 0xFF); } +void get_pixel_color(t_fdf *fdf, t_point_2d *point) +{ + if (fdf->colormode == COLOR_MODE_DEFAULT) + point->color = point->color; + else if (fdf->colormode == COLOR_MODE_Z) + point->color = get_z_color(point->orig_z, fdf->map->z_max); + else if (fdf->colormode == COLOR_MODE_GRADIENT) + point->color = get_gradient_color(point->orig_z, fdf->map->z_max); + +} + int interpolate_color(t_point_2d start, t_point_2d end, t_point_2d current) { int dcur; @@ -79,7 +90,7 @@ int get_z_color(double z, int z_max) int get_gradient_color(double z, int z_max) { - const int colors[] = {0x0058fbff, 0x007efeff, 0x008fd7ff, 0x0098a3f, + const int colors[] = {0x0058fbff, 0x007efeff, 0x008fd7ff, 0x0098a3ff, 0x009c7aff, 0x1fa46bff, 0x5eb13dff, 0x83c052ff, 0xa5cf69ff, 0xc3de81ff, 0xeeffffff, 0xffffffff,0xffffffff}; diff --git a/src/fdf_draw.c b/src/fdf_draw.c index 8ae5b58..30c2882 100644 --- a/src/fdf_draw.c +++ b/src/fdf_draw.c @@ -13,9 +13,6 @@ bool fdf_put_pixel(t_fdf *fdf, t_point_2d point) { - int color; - - color = 0xFFFFFFFF; if (point.x < 0 || point.x >= (int) fdf->img->width || point.y < 0 || point.y >= (int) fdf->img->height) return (false); diff --git a/src/fdf_hooks.c b/src/fdf_hooks.c index f530be9..8c76e3f 100644 --- a/src/fdf_hooks.c +++ b/src/fdf_hooks.c @@ -101,9 +101,14 @@ mlx_image_t *draw_menu(t_fdf *fdf) void close_hook(void *param) { - mlx_close_window(((t_fdf *)param)->mlx); - mlx_terminate(((t_fdf *)param)->mlx); - clean_fdf((t_fdf *)param); + t_fdf *fdf; + + fdf = (t_fdf *)param; + mlx_close_window(fdf->mlx); + mlx_delete_image(fdf->mlx, fdf->img); + mlx_delete_image(fdf->mlx, fdf->menu); + mlx_terminate(fdf->mlx); + clean_fdf(fdf); exit(0); } @@ -152,5 +157,7 @@ void key_hook(mlx_key_data_t keydata, void *param) fdf->colormode = (fdf->colormode + 1) % 3; if (keydata.key == MLX_KEY_P && keydata.action == MLX_PRESS) fdf->projection = (fdf->projection + 1) % 3; + if (keydata.key == MLX_KEY_SPACE && keydata.action == MLX_PRESS) + reset_fdf(fdf); } diff --git a/src/init_mlx.c b/src/init_mlx.c index d56665a..f257fcc 100644 --- a/src/init_mlx.c +++ b/src/init_mlx.c @@ -15,7 +15,7 @@ int init_mlx(t_fdf *fdf) { mlx_t *mlx; - + mlx = mlx_init(WIDTH, HEIGHT, "FdF", true); if (!mlx) handle_error(fdf, "Error: failed to initialise MLX"); diff --git a/src/initialise_fdf.c b/src/initialise_fdf.c index 2945f12..c1729d7 100644 --- a/src/initialise_fdf.c +++ b/src/initialise_fdf.c @@ -26,23 +26,37 @@ t_fdf *initialise_fdf(void) fdf->map = map; fdf->mlx = NULL; fdf->img = NULL; - fdf->angle_x = 0; - fdf->angle_y = 0; - fdf->angle_z = 0; - fdf->zoom = 0; - fdf->animate_z = 0; - fdf->offset_x = WIDTH / 2; - fdf->offset_y = HEIGHT / 2; - fdf->z_scale = 0.1; + fdf->last_width = WIDTH; fdf->last_height = HEIGHT; - fdf->colormode = COLOR_MODE_DEFAULT; - fdf->projection = PROJECTION_ISOMETRIC; fdf->map->orig = NULL; fdf->map->rot = NULL; fdf->map->proj = NULL; fdf->map->width = 0; fdf->map->height = 0; - fdf->map->z_max = 0; + fdf->map->z_max = 0; + + reset_fdf(fdf); return (fdf); } + +void reset_fdf(t_fdf *fdf) +{ + fdf->offset_x = WIDTH / 2; + fdf->offset_y = HEIGHT / 2; + if (fdf->mlx) + { + fdf->offset_x = fdf->mlx->width / 2; + fdf->offset_y = fdf->mlx->height / 2; + } + + + fdf->angle_x = 0; + fdf->angle_y = 0; + fdf->angle_z = 0; + fdf->zoom = 0; + fdf->z_scale = 0.1; + fdf->animate_z = 0; + fdf->colormode = COLOR_MODE_DEFAULT; + fdf->projection = PROJECTION_ISOMETRIC; +} diff --git a/src/project_isometric.c b/src/project_isometric.c index 8152848..a1e4ef4 100644 --- a/src/project_isometric.c +++ b/src/project_isometric.c @@ -29,7 +29,8 @@ void project_trimetric(t_fdf *fdf) fdf->map->proj[i].x = x * fdf->zoom + fdf->offset_x; fdf->map->proj[i].y = y * fdf->zoom + fdf->offset_y; fdf->map->proj[i].orig_z = fdf->map->orig[i].z; - fdf->map->proj[i].color = get_pixel_color(fdf, fdf->map->orig[i]); + fdf->map->proj[i].color = fdf->map->orig[i].color; + get_pixel_color(fdf, &fdf->map->proj[i]); i++; } } @@ -47,7 +48,8 @@ void project_parallel(t_fdf *fdf) fdf->map->proj[i].x = fdf->map->rot[i].x * fdf->zoom + fdf->offset_x; fdf->map->proj[i].y = fdf->map->rot[i].y * fdf->zoom + fdf->offset_y; fdf->map->proj[i].orig_z = fdf->map->orig[i].z; - fdf->map->proj[i].color = get_pixel_color(fdf, fdf->map->orig[i]); + fdf->map->proj[i].color = fdf->map->orig[i].color; + get_pixel_color(fdf, &fdf->map->proj[i]); i++; } } @@ -70,7 +72,8 @@ void project_isometric(t_fdf *fdf) fdf->map->proj[i].x = x * fdf->zoom + fdf->offset_x; fdf->map->proj[i].y = y * fdf->zoom + fdf->offset_y; fdf->map->proj[i].orig_z = fdf->map->orig[i].z; - fdf->map->proj[i].color = get_pixel_color(fdf, fdf->map->orig[i]); + fdf->map->proj[i].color = fdf->map->orig[i].color; + get_pixel_color(fdf, &fdf->map->proj[i]); i++; } }