Make it so move only calculates the new area
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 18 Jan 2024 10:01:54 +0000 (11:01 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 18 Jan 2024 10:01:54 +0000 (11:01 +0100)
Only the area that has not been displayed before the move needs to be
calculated. The area of the window that does not move outside the window
need not be recalculated. Implement this functionality.

event_handling.c
fractol.h
main.c

index b122286f678286a99878efc3e10a870d96692034..4cbc294536b1e339ae3958b69d1b93434b6dfb75 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/12/05 19:35:01 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/01/17 14:42:38 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/01/18 10:36:06 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -36,6 +36,8 @@ int   handle_key_press(int keycode, t_session *s)
                change_zoom(s, (t_vect2){.x = s->img.width / 2, .y = s->img.height / 2}, ZOOM_IN);
        else if (keycode == XK_KP_Subtract)
                change_zoom(s, (t_vect2){.x = s->img.width / 2, .y = s->img.height / 2}, ZOOM_OUT);
+       else if (keycode == XK_r)
+               erase_calced(&s->img.calced);
        if (keycode != XK_Escape)
                draw_fractal(s);
        return (0);
@@ -60,7 +62,7 @@ int   handle_mouse_press(int button, int x, int y, t_session *s)
  */
 int    no_event_handle(t_session *s)
 {
-       if (s->img.undersample > 1)
+       if (s && s->win && s->img.undersample > 1)
        {
                --s->img.undersample;
                draw_fractal(s);
index 5436deaabfbe6a3106eee58602770d6af8bdf9dd..1d89b7f38af882d47c611e4e1bb0a23b3c54255f 100644 (file)
--- a/fractol.h
+++ b/fractol.h
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/11/11 18:51:29 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/01/18 08:41:34 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/01/18 10:11:39 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -59,4 +59,6 @@ void  change_zoom(t_session *s, t_vect2 invariant, double d_zoom);
 void   move_view(t_session *s, float move_amount_right, float move_amount_up);
 int    close_win(t_session *s);
 
+void   erase_calced(t_mat *calced);
+
 #endif
diff --git a/main.c b/main.c
index 06464e817b2c75b706c9a3d9484460de36639b41..419b81a01b7312c00c337f6c70f7687a0b429833 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/01/18 08:46:27 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/01/18 10:44:15 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -26,6 +26,7 @@
 int    close_win(t_session *s)
 {
        mlx_destroy_window(s->mlx, s->win);
+       s->win = NULL;
        return (0);
 }
 
@@ -170,18 +171,18 @@ void      draw_fractal(t_session *s)
        mlx_put_image_to_window(s->mlx, s->win, s->img.img, 0, 0);
 }
 
-void   erase_calced(t_mat *mat)
+void   erase_calced(t_mat *calced)
 {
        size_t  i;
        size_t  j;
 
        i = 0;
-       while (i < mat->rows)
+       while (i < calced->rows)
        {
                j = 0;
-               while (j < mat->cols)
+               while (j < calced->cols)
                {
-                       *(bool *)ft_mat_access(mat, i, j) = false;
+                       *(bool *)ft_mat_access(calced, i, j) = false;
                        ++j;
                }
                ++i;
@@ -200,12 +201,51 @@ void      change_zoom(t_session *s, t_vect2 invariant, double d_zoom)
        return ;
 }
 
+void   move_img(t_img *img, int delta_x, int delta_y)
+{
+       int             x;
+       int             y;
+       t_color color;
+
+       x = 0;
+       if (delta_x < 0)
+               x = img->width - 1;
+       while (0 <= x && x < img->width)
+       {
+               y = 0;
+               if (delta_y < 0)
+                       y = img->height - 1;
+               while (0 <= y && y < img->height)
+               {
+                       if (0 <= x - delta_x && x - delta_x < img->width &&
+                                       0 <= y - delta_y && y - delta_y < img->height)
+                       {
+                               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);
+                       }
+                       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;
+                       y += 1 - 2 * (delta_y < 0);
+               }
+               x += 1 - 2 * (delta_x < 0);
+       }
+       return ;
+}
+
 // move_amount is ratio of move distance to length one can see
 void   move_view(t_session *s, float move_amount_right, float move_amount_up)
 {
-       s->view.window_coord.x += move_amount_right * s->img.height * s->view.pixel_size.x;
-       s->view.window_coord.y += move_amount_up * s->img.height * s->view.pixel_size.y;
-       erase_calced(&s->img.calced);
+       int     x_pix_change;
+       int     y_pix_change;
+
+       x_pix_change = round(move_amount_right * s->img.height);
+       y_pix_change = -round(move_amount_up * s->img.height);
+       s->view.window_coord.x += x_pix_change * s->view.pixel_size.x;
+       s->view.window_coord.y -= y_pix_change * s->view.pixel_size.y;
+       move_img(&s->img, x_pix_change, y_pix_change);
        s->img.undersample = s->img.undersample_max;
        return ;
 }
@@ -259,9 +299,9 @@ void        parse_args(int argc, char **argv, t_session *s)
 {
        if (argc == 0)
                free(argv);
-       s->img.width = 50;
-       s->img.height = 50;
-       s->img.undersample_max = 3;
+       s->img.width = 1000;
+       s->img.height = 1000;
+       s->img.undersample_max = 5;
        return ;
 }