From: Lukas Jiriste Date: Tue, 16 Jan 2024 13:31:51 +0000 (+0100) Subject: Add the ability to move with WASD and arrow keys X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=575ccc041b01957649292a6f6356f82eef3336da;p=42%2Ffract-ol.git Add the ability to move with WASD and arrow keys --- diff --git a/event_handling.c b/event_handling.c index b4609d6..f568f0b 100644 --- a/event_handling.c +++ b/event_handling.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* hooked.c :+: :+: :+: */ +/* event_handling.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -16,10 +16,22 @@ #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); } diff --git a/fractol.h b/fractol.h index ae0e877..7524676 100644 --- a/fractol.h +++ b/fractol.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 ed51269..4de9972 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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;