From: Lukas Jiriste Date: Thu, 18 Jan 2024 10:01:54 +0000 (+0100) Subject: Make it so move only calculates the new area X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=e29f5320d8e3a8da243366785b24849e83f0499a;p=42%2Ffract-ol.git Make it so move only calculates the new area 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. --- diff --git a/event_handling.c b/event_handling.c index b122286..4cbc294 100644 --- a/event_handling.c +++ b/event_handling.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); diff --git a/fractol.h b/fractol.h index 5436dea..1d89b7f 100644 --- a/fractol.h +++ b/fractol.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 06464e8..419b81a 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 ; }