From 68e9533b91734a9c11dea1a84ddbf5c9584629cb Mon Sep 17 00:00:00 2001 From: whaffman Date: Fri, 13 Dec 2024 17:18:20 +0100 Subject: [PATCH] fixed some norm errors --- MLX42.supp | 48 +++++++++++++++++++++++++++++++++++++++++ inc/fdf.h | 14 ++++++------ scratch.txt | 8 ------- src/clean_fdf.c | 2 +- src/fdf_color.c | 28 ++++++++++-------------- src/fdf_draw.c | 18 ++++++---------- src/fdf_hooks.c | 5 +++-- src/fdf_rotations.c | 12 +++++------ src/get_map_sizes.c | 2 +- src/get_z_max.c | 4 ++-- src/initialise_fdf.c | 5 ++--- src/parse_map.c | 12 ++++++----- src/project_isometric.c | 8 ++++--- src/read_map.c | 17 ++++++++------- 14 files changed, 108 insertions(+), 75 deletions(-) create mode 100644 MLX42.supp delete mode 100644 scratch.txt diff --git a/MLX42.supp b/MLX42.supp new file mode 100644 index 0000000..06bb237 --- /dev/null +++ b/MLX42.supp @@ -0,0 +1,48 @@ +{ + + Memcheck:Leak + ... + obj:/usr/lib/x86_64-linux-gnu/libnvidia* +} + +{ + + Memcheck:Leak + ... + obj:/usr/lib/x86_64-linux-gnu/libdbus* +} + +{ + + Memcheck:Leak + ... + obj:/usr/lib/x86_64-linux-gnu/libglfw* +} + +{ + + Memcheck:Leak + ... + obj:/usr/lib/x86_64-linux-gnu/libX11* +} + +{ + + Memcheck:Leak + ... + obj:/usr/lib/x86_64-linux-gnu/dri* +} + +{ + + Memcheck:Leak + ... + obj:/usr/lib/x86_64-linux-gnu/libLLVM* +} + +{ + + Memcheck:Leak + ... + fun:_dl_open +} diff --git a/inc/fdf.h b/inc/fdf.h index d615dc0..f6eebf9 100644 --- a/inc/fdf.h +++ b/inc/fdf.h @@ -26,8 +26,7 @@ # define SUCCESS 1 # define FAILURE 0 -# define WRONG_LINE_LENGTH "Error: wrong line length. \ - Expected %d, got %d in line nr %d\n" +# 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" @@ -59,7 +58,7 @@ typedef struct s_point_2d typedef struct s_map { - t_point_3d *original; + t_point_3d *orig; t_point_3d *rot; t_point_2d *proj; int width; @@ -84,7 +83,7 @@ typedef struct s_fdf double z_scale; } t_fdf; -int interpolate_color(t_point_2d start, t_point_2d end, t_point_2d current); +int interpolate_color(t_point_2d start, t_point_2d end, t_point_2d current); int get_color(char *str); int get_map_sizes(char *filename, t_map *map); void free_line_and_split(char **line, char ***split); @@ -92,7 +91,7 @@ int read_map(char *filename, t_fdf *fdf); int parse_map(char *filename, t_fdf *fdf); void print_map(t_map *map); void copy_map(t_fdf *fdf); -void fdf_apply_rotation(t_fdf *fdf); +void fdf_apply_rotation(t_fdf *fdf); void handle_error(t_fdf *fdf, char *error); void rotate_x(t_point_3d **points, double angle, int size); @@ -102,7 +101,7 @@ 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 resize_hook(int width, int height, void *param); +void resize_hook(int width, int height, void *param); void draw_hook(void *param); void key_hook(mlx_key_data_t keydata, void *param); @@ -112,6 +111,7 @@ double deg2rad(double deg); t_fdf *initialise_fdf(void); bool clean_fdf(t_fdf *fdf); void get_z_max(t_fdf *fdf); -int init_mlx(t_fdf *fdf); +int init_mlx(t_fdf *fdf); +void set_map_point(t_fdf *fdf, int x, int y, char **str); #endif diff --git a/scratch.txt b/scratch.txt deleted file mode 100644 index 62616c5..0000000 --- a/scratch.txt +++ /dev/null @@ -1,8 +0,0 @@ - - while (i < alle punten) - { - x' = (x - y) / SQRT2; - y' = (x + y - 2 * z) / SQRT6; - i++; - } -} diff --git a/src/clean_fdf.c b/src/clean_fdf.c index bb85937..3920881 100644 --- a/src/clean_fdf.c +++ b/src/clean_fdf.c @@ -16,7 +16,7 @@ bool clean_fdf(t_fdf *fdf) { if (fdf->map) { - free(fdf->map->original); + free(fdf->map->orig); free(fdf->map->rot); free(fdf->map->proj); free(fdf->map); diff --git a/src/fdf_color.c b/src/fdf_color.c index 8a187d8..5a72e06 100644 --- a/src/fdf_color.c +++ b/src/fdf_color.c @@ -16,30 +16,24 @@ int get_color(char *str) { int color; int i; + char *color_str; color = 0; - i = 0; - if (str[i] && str[i] == '-') - i++; - while (str[i] && ft_isdigit(str[i])) - i++; - i++; - if (str[i] == '0' && str[i + 1] == 'x') - i += 2; - while (str[i]) + i = 1; + color_str = ft_strchr(str, ','); + if (!color_str || *++color_str != '0' || *++color_str != 'x') + return (0xFFFFFFFF); + while (color_str[i]) { - if (ft_isdigit(str[i])) - color = color * 16 + str[i] - '0'; - else if (str[i] >= 'a' && str[i] <= 'f') - color = color * 16 + str[i] - 'a' + 10; - else if (str[i] >= 'A' && str[i] <= 'F') - color = color * 16 + str[i] - 'A' + 10; + if (ft_isdigit(color_str[i])) + color = color * 16 + color_str[i] - '0'; + else if (ft_tolower(color_str[i]) >= 'a' + && ft_tolower(color_str[i]) <= 'f') + color = color * 16 + ft_tolower(color_str[i]) - 'a' + 10; else break ; i++; } - if (color == 0) - return (0xFFFFFFFF); return ((color << 8) + 0xFF); } diff --git a/src/fdf_draw.c b/src/fdf_draw.c index 7a47972..1bd4b56 100644 --- a/src/fdf_draw.c +++ b/src/fdf_draw.c @@ -28,13 +28,10 @@ void fdf_draw_line_low(mlx_image_t *img, t_point_2d start, t_point_2d end) t_point_2d delta_point; delta_point.x = end.x - start.x; - delta_point.y = end.y - start.y; + delta_point.y = abs(end.y - start.y); yi = 1; - if (delta_point.y < 0) - { + if (end.y - start.y < 0) yi = -1; - delta_point.y = -delta_point.y; - } delta = 2 * delta_point.y - delta_point.x; current = start; while (current.x <= end.x) @@ -58,14 +55,11 @@ void fdf_draw_line_high(mlx_image_t *img, t_point_2d start, t_point_2d end) t_point_2d current; t_point_2d delta_point; - delta_point.x = end.x - start.x; + delta_point.x = abs(end.x - start.x); delta_point.y = end.y - start.y; xi = 1; - if (delta_point.x < 0) - { + if (end.x - start.x < 0) xi = -1; - delta_point.x = -delta_point.x; - } delta = 2 * delta_point.x - delta_point.y; current = start; while (current.y <= end.y) @@ -85,9 +79,9 @@ void fdf_draw_line_high(mlx_image_t *img, t_point_2d start, t_point_2d end) void fdf_draw_line(mlx_image_t *img, t_point_2d start, t_point_2d end) { if ((start.x < 0 || start.x >= (int) img->width - || start.y < 0 || start.y >= (int) img->height) + || start.y < 0 || start.y >= (int) img->height) && (end.x < 0 || end.x >= (int) img->width - || end.y < 0 || end.y >= (int) img->height)) + || end.y < 0 || end.y >= (int) img->height)) return ; if (abs(end.y - start.y) < abs(end.x - start.x)) { diff --git a/src/fdf_hooks.c b/src/fdf_hooks.c index 6537aa3..7c031f1 100644 --- a/src/fdf_hooks.c +++ b/src/fdf_hooks.c @@ -20,6 +20,7 @@ int fdf_hooks(t_fdf *fdf) mlx_resize_hook(fdf->mlx, resize_hook, fdf); return (1); } + void resize_hook(int width, int height, void *param) { t_fdf *fdf; @@ -31,7 +32,7 @@ void resize_hook(int width, int height, void *param) 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)) exit(1); - fdf->offset_x += (fdf->mlx->width - fdf->last_width ) / 2 ; + fdf->offset_x += (fdf->mlx->width - fdf->last_width) / 2 ; fdf->offset_y += (fdf->mlx->height - fdf->last_height) / 2; fdf->zoom = 0; fdf->last_height = fdf->mlx->height; @@ -43,7 +44,6 @@ void draw_hook(void *param) t_fdf *fdf; int i; - fdf = (t_fdf *)param; if (fdf->last_width != fdf->mlx->width || fdf->last_height != fdf->mlx->height) @@ -67,6 +67,7 @@ void draw_hook(void *param) i++; } } + void key_hook(mlx_key_data_t keydata, void *param) { t_fdf *fdf; diff --git a/src/fdf_rotations.c b/src/fdf_rotations.c index 62a8777..683df8b 100644 --- a/src/fdf_rotations.c +++ b/src/fdf_rotations.c @@ -27,10 +27,10 @@ void copy_map(t_fdf *fdf) i = 0; while (i < fdf->map->width * fdf->map->height) { - fdf->map->rot[i].x = fdf->map->original[i].x; - fdf->map->rot[i].y = fdf->map->original[i].y; - fdf->map->rot[i].z = fdf->map->original[i].z * fdf->z_scale; - fdf->map->rot[i].color = fdf->map->original[i].color; + fdf->map->rot[i].x = fdf->map->orig[i].x; + fdf->map->rot[i].y = fdf->map->orig[i].y; + fdf->map->rot[i].z = fdf->map->orig[i].z * fdf->z_scale; + fdf->map->rot[i].color = fdf->map->orig[i].color; i++; } } @@ -52,7 +52,7 @@ void rotate_x(t_point_3d **points, double angle, int size) void rotate_y(t_point_3d **points, double angle, int size) { - int i; + int i; double previous_x; i = 0; @@ -67,7 +67,7 @@ void rotate_y(t_point_3d **points, double angle, int size) void rotate_z(t_point_3d **points, double angle, int size) { - int i; + int i; double previous_x; double previous_y; diff --git a/src/get_map_sizes.c b/src/get_map_sizes.c index 9c10dde..b7d70a8 100644 --- a/src/get_map_sizes.c +++ b/src/get_map_sizes.c @@ -29,7 +29,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, map->width, width, map->height), 0); + return (ft_printf(WRONG_LINE_LENGTH), 0); else if (map->width == 0) map->width = width; map->height++; diff --git a/src/get_z_max.c b/src/get_z_max.c index e3b4ff7..0143a48 100644 --- a/src/get_z_max.c +++ b/src/get_z_max.c @@ -20,8 +20,8 @@ void get_z_max(t_fdf *fdf) fdf->map->z_max = 0; while (i < fdf->map->width * fdf->map->height) { - if (fdf->map->original[i].z > fdf->map->z_max) - fdf->map->z_max = fdf->map->original[i].z; + if (fdf->map->orig[i].z > fdf->map->z_max) + fdf->map->z_max = fdf->map->orig[i].z; i++; } } diff --git a/src/initialise_fdf.c b/src/initialise_fdf.c index 3332ab6..d3fea6f 100644 --- a/src/initialise_fdf.c +++ b/src/initialise_fdf.c @@ -30,18 +30,17 @@ t_fdf *initialise_fdf(void) 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->map->original = NULL; + 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; - - return (fdf); } diff --git a/src/parse_map.c b/src/parse_map.c index 665aea3..ce2580c 100644 --- a/src/parse_map.c +++ b/src/parse_map.c @@ -14,15 +14,17 @@ int parse_map(char *filename, t_fdf *fdf) { + int map_size; + if (!get_map_sizes(filename, fdf->map)) handle_error(fdf, "Error: failed to get map sizes"); - fdf->map->original = malloc(fdf->map->width * fdf->map->height * sizeof(t_point_3d)); - fdf->map->rot = malloc(fdf->map->width * fdf->map->height * sizeof(t_point_3d)); - fdf->map->proj = malloc(fdf->map->width * fdf->map->height * sizeof(t_point_2d)); - if (!fdf->map->original || !fdf->map->rot || !fdf->map->proj) + map_size = fdf->map->width * fdf->map->height; + fdf->map->orig = malloc(map_size * sizeof(t_point_3d)); + fdf->map->rot = malloc(map_size * sizeof(t_point_3d)); + fdf->map->proj = malloc(map_size * sizeof(t_point_2d)); + if (!fdf->map->orig || !fdf->map->rot || !fdf->map->proj) handle_error(fdf, MALLOC_ERROR); if (!read_map(filename, fdf)) handle_error(fdf, "Error: failed to read map"); - return (SUCCESS); } diff --git a/src/project_isometric.c b/src/project_isometric.c index fff263d..ad5ed0d 100644 --- a/src/project_isometric.c +++ b/src/project_isometric.c @@ -19,12 +19,14 @@ void project_isometric(t_fdf *fdf) double y; i = 0; - if(fdf->zoom == 0) - fdf->zoom = fmin(fdf->mlx->width / fdf->map->width, fdf->mlx->height / fdf->map->height) / SQRT2; + if (fdf->zoom == 0) + fdf->zoom = fmin(fdf->mlx->width / fdf->map->width, + fdf->mlx->height / fdf->map->height) / SQRT2; while (i < fdf->map->width * fdf->map->height) { x = (fdf->map->rot[i].x - fdf->map->rot[i].y) / SQRT2; - y = (fdf->map->rot[i].x + fdf->map->rot[i].y - 2 * fdf->map->rot[i].z) / SQRT6; + y = (fdf->map->rot[i].x + fdf->map->rot[i].y + - 2 * fdf->map->rot[i].z) / SQRT6; 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].color = fdf->map->rot[i].color; diff --git a/src/read_map.c b/src/read_map.c index 0fd67c2..8ba8582 100644 --- a/src/read_map.c +++ b/src/read_map.c @@ -20,7 +20,6 @@ int read_map(char *filename, t_fdf *fdf) int x; char **split; - fd = open(filename, O_RDONLY); if (fd < 0) handle_error(NULL, FILE_ERROR); @@ -33,16 +32,18 @@ int read_map(char *filename, t_fdf *fdf) break ; split = ft_split(line, ' '); while (split[x]) - { - fdf->map->original[y * fdf->map->width + x].x = x - fdf->map->width / 2; - fdf->map->original[y * fdf->map->width + x].y = fdf->map->height / 2 - y; - fdf->map->original[y * fdf->map->width + x].z = ft_atoi(split[x]); - fdf->map->original[y * fdf->map->width + x].color = get_color(split[x]); - x++; - } + set_map_point(fdf, x++, y, split); free_line_and_split(&line, &split); y--; } close(fd); return (1); } + +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].z = ft_atoi(str[x]); + fdf->map->orig[y * fdf->map->width + x].color = get_color(str[x]); +}