Add the ability to move with WASD and arrow keys
authorLukas Jiriste <ljiriste@student.42prague.com>
Tue, 16 Jan 2024 13:31:51 +0000 (14:31 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Tue, 16 Jan 2024 13:31:51 +0000 (14:31 +0100)
event_handling.c
fractol.h
main.c

index b4609d63ca14066f332c9efe617515f66c800f22..f568f0b9f5d2296eac625b61a795c563bf2557d3 100644 (file)
@@ -1,12 +1,12 @@
 /* ************************************************************************** */
 /*                                                                            */
 /*                                                        :::      ::::::::   */
-/*   hooked.c                                           :+:      :+:    :+:   */
+/*   event_handling.c                                   :+:      :+:    :+:   */
 /*                                                    +:+ +:+         +:+     */
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/12/05 19:35:01 by ljiriste          #+#    #+#             */
-/*   Updated: 2023/12/05 19:46:30 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/01/16 14:24:40 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "fractol.h"
 #include "vect2.h"
 
+#define MOVE_AMOUNT 0.1
+
 int    handle_key_press(int keycode, t_session *s)
 {
        if (keycode == XK_Escape)
                close_win(s);
+       else if (keycode == XK_Up || keycode == XK_w)
+               move_view(0, MOVE_AMOUNT, s);
+       else if (keycode == XK_Left || keycode == XK_a)
+               move_view(-MOVE_AMOUNT, 0, s);
+       else if (keycode == XK_Down || keycode == XK_s)
+               move_view(0, -MOVE_AMOUNT, s);
+       else if (keycode == XK_Right || keycode == XK_d)
+               move_view(MOVE_AMOUNT, 0, s);
+       if (keycode != XK_Escape && s->view.draw_whole)
+               draw_fractal(s);
        return (0);
 }
 
index ae0e8774efec8ecbf22b72605a171ce3fdc4c237..7524676230eebc6a70d476ae576f2bb547619d60 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: 2023/12/05 19:47:36 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/01/16 14:01:51 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -54,6 +54,7 @@ int   handle_mouse_press(int button, int x, int y, t_session *s);
 int    no_event_handle(t_session *s);
 int    draw_fractal(t_session *s);
 void   change_zoom(t_view *view, t_vect2 invariant, double d_zoom);
+void   move_view(float move_amount_right, float move_amount_up, t_session *s);
 int    close_win(t_session *s);
 
 #endif
diff --git a/main.c b/main.c
index ed5126956d67eb0bcbce85a220811e9e662cab77..4de99726358812917c48fbee6822520868ffdad6 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: 2023/12/05 19:51:07 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/01/16 14:31:25 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -130,6 +130,15 @@ void       change_zoom(t_view *view, t_vect2 invariant, double d_zoom)
        return ;
 }
 
+// move_amount is ratio of move distance to length one can see
+void   move_view(float move_amount_right, float move_amount_up, t_session *s)
+{
+       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;
+       s->view.draw_whole = 1;
+       return ;
+}
+
 void   init_view(t_session *s)
 {
        s->view.fractal = mandelbrot;