Fix centering and movement for small windows
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 18 Apr 2024 07:57:14 +0000 (09:57 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 18 Apr 2024 07:57:14 +0000 (09:57 +0200)
Centering was calculated based on default values hence it was wrong for
different sizes of window. Solved by moving the calculation after
argument parsing.
Movement was impossible for small windows, as the calculated movement
was smaller than a pixel which got rounded to 0. Solved by assigning
signum of movement when 0 is calculated.

main.c

diff --git a/main.c b/main.c
index e5134e9efc301f7ed9f25ab228237d74837cbf91..9f5071e6602e3a64ecb3528694d3ff204d694f50 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 14:03:23 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/04/18 09:54:31 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -168,7 +168,11 @@ void       move_view(t_session *s, float move_amount_right, float move_amount_up)
        int     y_pix_change;
 
        x_pix_change = round(move_amount_right * s->img.height);
+       if (x_pix_change == 0)
+               x_pix_change = (move_amount_right > 0) - (move_amount_right < 0);
        y_pix_change = -round(move_amount_up * s->img.height);
+       if (y_pix_change == 0)
+               y_pix_change = -((move_amount_up > 0) - (move_amount_up < 0));
        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);
@@ -227,7 +231,6 @@ void        set_default(t_session *s)
        s->img.width = 1000;
        s->img.height = 1000;
        s->img.undersample_max = 5;
-       init_view(s);
        return ;
 }
 
@@ -265,6 +268,7 @@ int parse_args(int argc, char **argv, t_session *s)
                        return (1);
                ++i;
        }
+       init_view(s);
        return (0);
 }