From 61467b9686c4ab3513ac5f0622e50e87060b2d3d Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Tue, 5 Dec 2023 19:52:17 +0100 Subject: [PATCH] Split event handling from main. Add color smoothing. Move event handling (and no_event_handle) functions to event_handling.c. Add smoothing to mandelbrot output. --- Makefile | 2 +- event_handling.c | 57 ++++++++++++++++++++++++++++++++++++++++ fractol.h | 8 +++++- main.c | 68 ++++++++---------------------------------------- 4 files changed, 76 insertions(+), 59 deletions(-) create mode 100644 event_handling.c diff --git a/Makefile b/Makefile index 0601b7c..4cfd9ba 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ MLX := $(MLXDIR)libmlx_Linux.a LFTDIR := Libft/ LFT := $(LFTDIR)libft.a -SRCS := main.c complex.c color.c +SRCS := main.c complex.c color.c event_handling.c OBJS := $(SRCS:%.c=%.o) all : $(NAME) diff --git a/event_handling.c b/event_handling.c new file mode 100644 index 0000000..b4609d6 --- /dev/null +++ b/event_handling.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hooked.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/05 19:35:01 by ljiriste #+# #+# */ +/* Updated: 2023/12/05 19:46:30 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include "fractol.h" +#include "vect2.h" + +int handle_key_press(int keycode, t_session *s) +{ + if (keycode == XK_Escape) + close_win(s); + return (0); +} + +int handle_mouse_press(int button, int x, int y, t_session *s) +{ + double delta_zoom; + + if (button == Button4) + delta_zoom = 0.5; + else if (button == Button5) + delta_zoom = 2; + if (button == Button4 || button == Button5) + { + change_zoom(&s->view, (t_vect2){.x = x, .y = y}, delta_zoom); + draw_fractal(s); + } + return (0); +} + +/* valgrind indicates an issue when the draw_fractal is in proccess + * while and ESC is pressed thus ending the program. + * + * Setting the condition to less than 1 basically turns this redrawing + * off. + */ +int no_event_handle(t_session *s) +{ + if (s->view.resolution < 1) + { + s->view.draw_whole = 0; + s->view.resolution *= 2; + draw_fractal(s); + } + return (0); +} diff --git a/fractol.h b/fractol.h index c1ddea7..ae0e877 100644 --- a/fractol.h +++ b/fractol.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/11 18:51:29 by ljiriste #+# #+# */ -/* Updated: 2023/11/11 18:51:32 by ljiriste ### ########.fr */ +/* Updated: 2023/12/05 19:47:36 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,5 +49,11 @@ struct s_session }; typedef struct s_session t_session; +int handle_key_press(int keycode, t_session *s); +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); +int close_win(t_session *s); #endif diff --git a/main.c b/main.c index 07a295c..ed51269 100644 --- a/main.c +++ b/main.c @@ -6,16 +6,16 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/27 14:29:26 by ljiriste #+# #+# */ -/* Updated: 2023/11/11 18:50:45 by ljiriste ### ########.fr */ +/* Updated: 2023/12/05 19:51:07 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ #include #include +#include #include #include #include -#include #include "libft.h" #include "fractol.h" #include "complex.h" @@ -68,33 +68,27 @@ void free_session(t_session *s) double mandelbrot(double x, double y, double resolution) { - double threshold; - const double min_threshold = 2.5; - double max_count; + const double threshold = 1000; + const double log2 = log(2); int count; t_complex c; t_complex z; c.r = x; c.i = y; - max_count = 100 * resolution; - threshold = 3; - if (threshold < min_threshold) - threshold = min_threshold; count = 0; z.r = 0; z.i = 0; - while (complex_norm(z) < threshold && count < max_count) + while (complex_norm(z) < threshold && count < 100 * resolution) { z = complex_add(complex_mul(z, z), c); ++count; } - if (count == max_count) + if (count == 100 * resolution) return (-1); - return (fmod(count / 100., 1.)); + return (fmod((count + 1 - log(log(complex_norm(z)) / log2) / log2) / 100., 1.)); } - int draw_fractal(t_session *s) { int x; @@ -131,51 +125,11 @@ void change_zoom(t_view *view, t_vect2 invariant, double d_zoom) view->pixel_size.y /= d_zoom; view->window_coord.x += view->pixel_size.x * invariant.x * (d_zoom - 1); view->window_coord.y -= view->pixel_size.y * invariant.y * (d_zoom - 1); - view->resolution = 1; + //view->resolution = 1; view->draw_whole = 1; return ; } -int handle_key_press(int keycode, t_session *s) -{ - if (keycode == XK_Escape) - close_win(s); - return (0); -} - -int handle_mouse_press(int button, int x, int y, t_session *s) -{ - double delta_zoom; - - if (button == Button4) - delta_zoom = 0.5; - else if (button == Button5) - delta_zoom = 2; - if (button == Button4 || button == Button5) - { - change_zoom(&s->view, (t_vect2){.x = x, .y = y}, delta_zoom); - draw_fractal(s); - } - return (0); -} - -/* valgrind indicates an issue when the draw_fractal is in proccess - * while and ESC is pressed thus ending the program. - * - * Setting the condition to less than 1 basically turns this redrawing - * off. - */ -int no_event_handle(t_session *s) -{ - if (s->view.resolution < 1) - { - s->view.draw_whole = 0; - s->view.resolution *= 2; - draw_fractal(s); - } - return (0); -} - void init_view(t_session *s) { s->view.fractal = mandelbrot; @@ -184,7 +138,7 @@ void init_view(t_session *s) s->view.pixel_size.y = 0.01; s->view.window_coord.x = -s->img.width / 2 * s->view.pixel_size.x; s->view.window_coord.y = s->img.height / 2 * s->view.pixel_size.y; - s->view.resolution = 1; + s->view.resolution = 10; s->view.draw_whole = 1; return ; } @@ -193,8 +147,8 @@ void parse_args(int argc, char **argv, t_session *s) { if (argc == 0) free(argv); - s->img.width = 200; - s->img.height = 200; + s->img.width = 500; + s->img.height = 500; init_view(s); return ; } -- 2.30.2