From 92fb7e8687cb44e5dfff1b7bc0f5c2d2b4952036 Mon Sep 17 00:00:00 2001 From: whaffman Date: Fri, 20 Dec 2024 16:33:25 +0100 Subject: [PATCH] finalizing --- inc/fdf.h | 81 +++++++++++++++++------------------ src/draw/get_gradient_color.c | 6 ++- src/draw/get_pixel_color.c | 10 ++++- src/draw/get_z_color.c | 8 ++-- src/draw/prepare_draw.c | 14 +++++- src/draw/put_pixel.c | 5 +-- src/fdf.c | 3 +- src/hooks/fdf_hooks.c | 3 +- src/parse/get_map_sizes.c | 8 ++-- src/parse/get_z_max.c | 27 ------------ src/parse/parse_map.c | 7 ++- src/parse/set_map_point.c | 4 +- src/util/clean_fdf.c | 17 +++++--- src/util/handle_error.c | 9 +++- src/util/init_mlx.c | 3 +- src/util/initialise_fdf.c | 3 +- 16 files changed, 103 insertions(+), 105 deletions(-) delete mode 100644 src/parse/get_z_max.c diff --git a/inc/fdf.h b/inc/fdf.h index 56b37e4..d3d907e 100644 --- a/inc/fdf.h +++ b/inc/fdf.h @@ -16,20 +16,16 @@ # include # include # include -# include # include # include # include "libft.h" # include "MLX42.h" # include -# define SUCCESS 1 -# define FAILURE 0 - -# define WRONG_LINE_LENGTH "Error: wrong line length.\n" -# define MALLOC_ERROR "Error: malloc failed\n" -# define FILE_ERROR "Error: could not open file %s\n" -# define USAGE_ERROR "Usage: %s \n" +# define WRONG_LINE_LENGTH "Error: wrong line length." +# define MALLOC_ERROR "Error: malloc failed" +# define FILE_ERROR "Error: could not open file for reading" +# define USAGE_ERROR "Usage: ./fdf " # ifndef M_PI # define M_PI 3.14159265358979323846 @@ -79,6 +75,7 @@ typedef struct s_map int width; int height; int z_max; + int z_min; } t_map; typedef struct s_fdf @@ -101,48 +98,48 @@ typedef struct s_fdf t_projection projection; } t_fdf; -void free_line_and_split(char **line, char ***split); -int get_map_sizes(char *filename, t_map *map); +int get_map_sizes(char *filename, t_fdf *fdf); int load_map_from_file(char *filename, t_fdf *fdf); -int parse_map(char *filename, t_fdf *fdf); +void parse_map(char *filename, t_fdf *fdf); void set_map_point(t_fdf *fdf, int x, int y, char **str); int parse_color_string(char *str); -void get_z_max(t_fdf *fdf); -void handle_error(t_fdf *fdf, char *error); +void get_z_min_max(t_fdf *fdf); -int fdf_hooks(t_fdf *fdf); +void fdf_hooks(t_fdf *fdf); void resize_hook(int width, int height, void *param); void draw_hook(void *param); -void key_hook(mlx_key_data_t keydata, void *param); - -mlx_image_t *draw_menu(t_fdf *fdf); -void apply_rotation(t_fdf *fdf); -int get_z_color(double z, int z_max); -void rotate_x(t_point_3d **points, double angle, int size); -void rotate_y(t_point_3d **points, double angle, int size); -void rotate_z(t_point_3d **points, double angle, int size); -void project_isometric(t_fdf *fdf); -int interpolate_color(t_point_2d start, - t_point_2d end, t_point_2d current); -void draw_line(t_fdf *fdf, t_point_2d start, - t_point_2d end); -bool put_pixel(t_fdf *fdf, t_point_2d point); -void fdf_set_background(mlx_image_t *img, int color); -double deg2rad(double deg); -t_fdf *initialise_fdf(void); -bool clean_fdf(t_fdf *fdf); -int init_mlx(t_fdf *fdf); void close_hook(void *param); -int get_gradient_color(double z, int z_max); -void project_parallel(t_fdf *fdf); -void project_trimetric(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); -void prepare_draw(t_fdf *fdf); +void key_hook(mlx_key_data_t keydata, void *param); void key_hook_options(mlx_key_data_t keydata, t_fdf *fdf); void key_hook_transform(mlx_key_data_t keydata, t_fdf *fdf); +mlx_image_t *draw_menu(t_fdf *fdf); +void apply_rotation(t_fdf *fdf); +void rotate_x(t_point_3d **points, double angle, int size); +void rotate_y(t_point_3d **points, double angle, int size); +void rotate_z(t_point_3d **points, double angle, int size); +void project(t_fdf *fdf); +void project_isometric(t_fdf *fdf); +void project_parallel(t_fdf *fdf); +void project_trimetric(t_fdf *fdf); +void get_pixel_color(t_fdf *fdf, t_point_2d *point); +int get_z_color(double z, int z_min, int z_max); +int get_gradient_color(double z, int z_min, int z_max); +int interpolate_color(t_point_2d start, + t_point_2d end, t_point_2d current); +void prepare_draw(t_fdf *fdf); +void draw_line(t_fdf *fdf, t_point_2d start, + t_point_2d end); +void put_pixel(t_fdf *fdf, t_point_2d point); +void fdf_set_background(mlx_image_t *img, int color); + +t_fdf *initialise_fdf(void); +void free_line_and_split(char **line, char ***split); +void init_mlx(t_fdf *fdf); +void clean_fdf(t_fdf *fdf); +int check_filename(char *filename); +void reset_fdf(t_fdf *fdf); +double deg2rad(double deg); +void handle_error(t_fdf *fdf, char *error); + #endif diff --git a/src/draw/get_gradient_color.c b/src/draw/get_gradient_color.c index 51cf4de..201d718 100644 --- a/src/draw/get_gradient_color.c +++ b/src/draw/get_gradient_color.c @@ -10,11 +10,13 @@ /* */ /* ************************************************************************** */ -int get_gradient_color(double z, int z_max) +int get_gradient_color(double z, int z_min, int z_max) { const int colors[] = {0x0058fbff, 0x007efeff, 0x008fd7ff, 0x0098a3ff, 0x009c7aff, 0x1fa46bff, 0x5eb13dff, 0x83c052ff, 0xa5cf69ff, 0xc3de81ff, 0xeeffffff, 0xffffffff, 0xffffffff}; - return (colors[(int)(z * 12 / z_max)]); + if (z < z_min || z_max == z_min) + return (0xffffffff); + return (colors[(int)((z - z_min) * 12 / (z_max - z_min))]); } diff --git a/src/draw/get_pixel_color.c b/src/draw/get_pixel_color.c index 4a147aa..f1d80b0 100644 --- a/src/draw/get_pixel_color.c +++ b/src/draw/get_pixel_color.c @@ -17,7 +17,13 @@ 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); + { + point->color = get_z_color(point->orig_z, + fdf->map->z_min, fdf->map->z_max); + } else if (fdf->colormode == COLOR_MODE_GRADIENT) - point->color = get_gradient_color(point->orig_z, fdf->map->z_max); + { + point->color = get_gradient_color(point->orig_z, + fdf->map->z_min, fdf->map->z_max); + } } diff --git a/src/draw/get_z_color.c b/src/draw/get_z_color.c index d1a1e66..7a13fa6 100644 --- a/src/draw/get_z_color.c +++ b/src/draw/get_z_color.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -int get_z_color(double z, int z_max) +int get_z_color(double z, int z_min, int z_max) { int red; int green; @@ -19,11 +19,11 @@ int get_z_color(double z, int z_max) red = 0; green = 0; blue = 0; - if (z < 0) - blue = 127; + if (z < 0 || z_max == z_min) + blue = 200; else { - red = 255 * z / z_max; + red = 255 * (z - z_min) / (z_max - z_min); green = 255 - red; } return (red << 24 | green << 16 | blue << 8 | 0x00FF); diff --git a/src/draw/prepare_draw.c b/src/draw/prepare_draw.c index 4fbd486..4884255 100644 --- a/src/draw/prepare_draw.c +++ b/src/draw/prepare_draw.c @@ -1,6 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* prepare_draw.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/20 15:18:36 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 15:18:36 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + #include "fdf.h" -void prepare_draw(t_fdf *fdf) +void prepare_draw(t_fdf *fdf) { fdf_set_background(fdf->img, 0x000000FF); if (fdf->animate_z) diff --git a/src/draw/put_pixel.c b/src/draw/put_pixel.c index ed28028..a0367e9 100644 --- a/src/draw/put_pixel.c +++ b/src/draw/put_pixel.c @@ -12,11 +12,10 @@ #include "fdf.h" -bool put_pixel(t_fdf *fdf, t_point_2d point) +void put_pixel(t_fdf *fdf, t_point_2d point) { if (point.x < 0 || point.x >= (int) fdf->img->width || point.y < 0 || point.y >= (int) fdf->img->height) - return (false); + return ; mlx_put_pixel(fdf->img, point.x, point.y, point.color); - return (true); } diff --git a/src/fdf.c b/src/fdf.c index a01e304..6801d20 100644 --- a/src/fdf.c +++ b/src/fdf.c @@ -21,8 +21,7 @@ int main(int argc, char *argv[]) handle_error(fdf, "Usage: ./fdf "); if (!check_filename(argv[1])) handle_error(fdf, "Error: wrong file extension"); - if (!parse_map(argv[1], fdf)) - handle_error(fdf, "Error: failed to parse map"); + parse_map(argv[1], fdf); init_mlx(fdf); fdf->menu = draw_menu(fdf); fdf->menu->enabled = false; diff --git a/src/hooks/fdf_hooks.c b/src/hooks/fdf_hooks.c index 13da142..e0d316c 100644 --- a/src/hooks/fdf_hooks.c +++ b/src/hooks/fdf_hooks.c @@ -13,11 +13,10 @@ #include "fdf.h" #include "MLX42.h" -int fdf_hooks(t_fdf *fdf) +void fdf_hooks(t_fdf *fdf) { mlx_loop_hook(fdf->mlx, draw_hook, fdf); mlx_key_hook(fdf->mlx, key_hook, fdf); mlx_close_hook(fdf->mlx, close_hook, fdf); mlx_resize_hook(fdf->mlx, resize_hook, fdf); - return (1); } diff --git a/src/parse/get_map_sizes.c b/src/parse/get_map_sizes.c index f38bb95..ba4337b 100644 --- a/src/parse/get_map_sizes.c +++ b/src/parse/get_map_sizes.c @@ -12,14 +12,16 @@ #include "fdf.h" -int get_map_sizes(char *filename, t_map *map) +int get_map_sizes(char *filename, t_fdf *fdf) { const int fd = open(filename, O_RDONLY); char *line; int width; + t_map *map; + map = fdf->map; if (fd < 0) - return (ft_printf(FILE_ERROR, filename), 0); + handle_error(fdf, FILE_ERROR); width = 0; while (true) { @@ -29,7 +31,7 @@ int get_map_sizes(char *filename, t_map *map) width = ft_count_words(line, ' '); free(line); if (map->width != 0 && map->width != width) - return (ft_printf(WRONG_LINE_LENGTH), 0); + handle_error(fdf, WRONG_LINE_LENGTH); else if (map->width == 0) map->width = width; map->height++; diff --git a/src/parse/get_z_max.c b/src/parse/get_z_max.c deleted file mode 100644 index d69900d..0000000 --- a/src/parse/get_z_max.c +++ /dev/null @@ -1,27 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: o_ :::::: ::: */ -/* get_z_max.c :+: / :+::+: :+: */ -/* +:+ > +:++:+ +:+ */ -/* By: whaffman +#+ +:+ +#++#++:++#++ */ -/* +#+ +#+#+ +#++#+ +#+ \o/ */ -/* Created: 2024/12/13 15:23:15 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/12/20 11:21:33 by whaffman ### ### ### ### / \ */ -/* */ -/* ************************************************************************** */ - -#include "fdf.h" - -void get_z_max(t_fdf *fdf) -{ - int i; - - i = 0; - fdf->map->z_max = 0; - while (i < fdf->map->width * fdf->map->height) - { - if (fdf->map->orig[i].z > fdf->map->z_max) - fdf->map->z_max = fdf->map->orig[i].z; - i++; - } -} diff --git a/src/parse/parse_map.c b/src/parse/parse_map.c index c00201a..0c3aaee 100644 --- a/src/parse/parse_map.c +++ b/src/parse/parse_map.c @@ -12,11 +12,11 @@ #include "fdf.h" -int parse_map(char *filename, t_fdf *fdf) +void parse_map(char *filename, t_fdf *fdf) { int map_size; - if (!get_map_sizes(filename, fdf->map)) + if (!get_map_sizes(filename, fdf)) handle_error(fdf, "Error: failed to get map sizes"); map_size = fdf->map->width * fdf->map->height; fdf->map->orig = malloc(map_size * sizeof(t_point_3d)); @@ -26,6 +26,5 @@ int parse_map(char *filename, t_fdf *fdf) handle_error(fdf, MALLOC_ERROR); if (!load_map_from_file(filename, fdf)) handle_error(fdf, "Error: failed to read map"); - get_z_max(fdf); - return (SUCCESS); + get_z_min_max(fdf); } diff --git a/src/parse/set_map_point.c b/src/parse/set_map_point.c index adb0a93..7115429 100644 --- a/src/parse/set_map_point.c +++ b/src/parse/set_map_point.c @@ -14,8 +14,8 @@ void set_map_point(t_fdf *fdf, int x, int y, char **str) { - fdf->map->orig[y * fdf->map->width + x].x = x - fdf->map->width / 2; - fdf->map->orig[y * fdf->map->width + x].y = fdf->map->height / 2 - y; + fdf->map->orig[y * fdf->map->width + x].x = x - fdf->map->width / 2.0 + 1; + fdf->map->orig[y * fdf->map->width + x].y = fdf->map->height / 2.0 - y; fdf->map->orig[y * fdf->map->width + x].z = ft_atoi(str[x]); fdf->map->orig[y * fdf->map->width + x].color = parse_color_string(str[x]); } diff --git a/src/util/clean_fdf.c b/src/util/clean_fdf.c index 0acf6ba..2170ca2 100644 --- a/src/util/clean_fdf.c +++ b/src/util/clean_fdf.c @@ -12,15 +12,18 @@ #include "fdf.h" -bool clean_fdf(t_fdf *fdf) +void clean_fdf(t_fdf *fdf) { - if (fdf->map) + if (fdf && fdf->map) { - free(fdf->map->orig); - free(fdf->map->rot); - free(fdf->map->proj); + if (fdf->map->orig) + free(fdf->map->orig); + if (fdf->map->rot) + free(fdf->map->rot); + if (fdf->map->proj) + free(fdf->map->proj); free(fdf->map); } - free(fdf); - return (true); + if (fdf) + free(fdf); } diff --git a/src/util/handle_error.c b/src/util/handle_error.c index 2ebfa54..18dd9e0 100644 --- a/src/util/handle_error.c +++ b/src/util/handle_error.c @@ -16,9 +16,16 @@ void handle_error(t_fdf *fdf, char *error) { if (errno) perror(error); + else + ft_putendl_fd(error, 2); if (mlx_errno) ft_putendl_fd(mlx_strerror(mlx_errno), 2); - ft_putendl_fd(error, 2); + if (fdf && fdf->img) + mlx_delete_image(fdf->mlx, fdf->img); + if (fdf && fdf->menu) + mlx_delete_image(fdf->mlx, fdf->menu); + if (fdf && fdf->mlx) + mlx_terminate(fdf->mlx); clean_fdf(fdf); exit(1); } diff --git a/src/util/init_mlx.c b/src/util/init_mlx.c index f67cb96..9507d42 100644 --- a/src/util/init_mlx.c +++ b/src/util/init_mlx.c @@ -12,7 +12,7 @@ #include "fdf.h" -int init_mlx(t_fdf *fdf) +void init_mlx(t_fdf *fdf) { mlx_t *mlx; @@ -24,5 +24,4 @@ int init_mlx(t_fdf *fdf) if (!fdf->img || (mlx_image_to_window(fdf->mlx, fdf->img, 0, 0) < 0)) handle_error(fdf, "Error: failed to create image"); fdf_hooks(fdf); - return (SUCCESS); } diff --git a/src/util/initialise_fdf.c b/src/util/initialise_fdf.c index dd5d0cf..beca5a0 100644 --- a/src/util/initialise_fdf.c +++ b/src/util/initialise_fdf.c @@ -21,9 +21,9 @@ t_fdf *initialise_fdf(void) if (!fdf) handle_error(fdf, MALLOC_ERROR); map = malloc(sizeof(t_map)); + fdf->map = map; if (!map) handle_error(fdf, MALLOC_ERROR); - fdf->map = map; fdf->mlx = NULL; fdf->img = NULL; fdf->last_width = WIDTH; @@ -34,6 +34,7 @@ t_fdf *initialise_fdf(void) fdf->map->width = 0; fdf->map->height = 0; fdf->map->z_max = 0; + fdf->map->z_min = 0; reset_fdf(fdf); return (fdf); }