Fix rectangular windows, add argument parsing
authorLukas Jiriste <ljiriste@student.42prague.com>
Wed, 10 Apr 2024 11:30:03 +0000 (13:30 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Wed, 10 Apr 2024 11:39:16 +0000 (13:39 +0200)
When height and width were not the same, the program would crash
because the access to a matrix had rows and columns switched.
 (image x axis corresponds with column number of matrix)
Add 4 options for argument parsing:
 height
 width
 (level of) detail
 (maximum) undersample

main.c

diff --git a/main.c b/main.c
index f4dad73dfff97f5604178a45909858015fafd30f..e50f47593ed156f32cc045787b8b8ff95aea116e 100644 (file)
--- a/main.c
+++ b/main.c
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/10/27 14:29:26 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/04/10 12:07:49 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/04/10 13:39:05 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -42,7 +42,7 @@ t_color       get_color(t_session *s, int x, int y)
 {
        double  palette_param;
 
-       if (!*(bool *)ft_mat_access(&s->img.calced, x, y))
+       if (!*(bool *)ft_mat_access(&s->img.calced, y, x))
        {
                palette_param = s->view.fractal(&s->set,
                                s->view.window_coord.x + s->view.pixel_size.x * x,
@@ -68,13 +68,13 @@ void        process_pixel_group(t_session *s, int x, int y)
                j = 0;
                while (j < s->img.undersample && y + j < s->img.height)
                {
-                       if (!*(bool *)ft_mat_access(&s->img.calced, x + i, y + j))
+                       if (!*(bool *)ft_mat_access(&s->img.calced, y + j, x + i))
                                ft_putpx_img(&s->img, x + i, y + j, color);
                        ++j;
                }
                ++i;
        }
-       *(bool *)ft_mat_access(&s->img.calced, x, y) = 1;
+       *(bool *)ft_mat_access(&s->img.calced, y, x) = 1;
        return ;
 }
 
@@ -148,12 +148,12 @@ void      move_img(t_img *img, int delta_x, int delta_y)
                        {
                                color = get_img_pixel_color(img, x, y);
                                ft_putpx_img(img, x - delta_x, y - delta_y, color);
-                               *(bool *)ft_mat_access(&img->calced, x - delta_x, y - delta_y) =
-                               *(bool *)ft_mat_access(&img->calced, x, y);
+                               *(bool *)ft_mat_access(&img->calced, y - delta_y, x - delta_x) =
+                               *(bool *)ft_mat_access(&img->calced, y, x);
                        }
                        if (!(0 <= x + delta_x && x + delta_x < img->width &&
                                        0 <= y + delta_y && y + delta_y < img->height))
-                               *(bool *)ft_mat_access(&img->calced, x, y) = false;
+                               *(bool *)ft_mat_access(&img->calced, y, x) = false;
                        y += 1 - 2 * (delta_y < 0);
                }
                x += 1 - 2 * (delta_x < 0);
@@ -231,20 +231,25 @@ void      set_default(t_session *s)
        return ;
 }
 
-int    process_arg(__attribute((unused)) char *arg, __attribute((unused)) t_session *s)
-{
-       return (0);
-}
-
 int    parse_args(int argc, char **argv, t_session *s)
 {
-       int     i;
+       int                     i;
 
+       if (argc % 2 == 0)
+               return (1);
        set_default(s);
        i = 1;
-       while (i < argc)
+       while (i + 1 < argc)
        {
-               if (process_arg(argv[i], s))
+               if (!ft_strcmp(argv[i], "-w") && ft_isint(argv[i + 1]))
+                       s->img.width = ft_atoi(argv[++i]);
+               else if (!ft_strcmp(argv[i], "-h") && ft_isint(argv[i + 1]))
+                       s->img.height = ft_atoi(argv[++i]);
+               else if (!ft_strcmp(argv[i], "-u") && ft_isint(argv[i + 1]))
+                       s->img.undersample_max = ft_atoi(argv[++i]);
+               else if (!ft_strcmp(argv[i], "-d") && ft_isint(argv[i + 1]))
+                       s->set.man.detail = ft_atoi(argv[++i]);
+               else
                        return (1);
                ++i;
        }