diff options
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | include/fractol.h | 19 | ||||
| -rw-r--r-- | src/event.c | 22 | ||||
| -rw-r--r-- | src/helper.c | 18 | ||||
| -rw-r--r-- | src/render.c | 59 | ||||
| -rw-r--r-- | src/state.c | 9 |
6 files changed, 80 insertions, 51 deletions
@@ -6,7 +6,7 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/02/24 09:19:49 by cacharle #+# #+# # -# Updated: 2020/02/25 15:11:09 by cacharle ### ########.fr # +# Updated: 2020/02/25 16:00:56 by cacharle ### ########.fr # # # # **************************************************************************** # @@ -27,7 +27,7 @@ CCFLAGS = -I$(LIBFT_DIR)/include -I$(MINILIBX_DIR) -I$(INCLUDE_DIR) \ -Wall -Wextra #-Werror LDFLAGS = -L$(LIBFT_DIR) -lft \ -L$(MINILIBX_DIR) -lmlx \ - -framework OpenGL -framework AppKit -lm + -framework OpenGL -framework AppKit -lm -lpthread NAME = fractol SRC_FILES = main.c \ diff --git a/include/fractol.h b/include/fractol.h index d031214..05172b5 100644 --- a/include/fractol.h +++ b/include/fractol.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/24 09:28:16 by cacharle #+# #+# */ -/* Updated: 2020/02/25 15:29:33 by cacharle ### ########.fr */ +/* Updated: 2020/02/25 16:21:56 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,13 +18,15 @@ # include <stdbool.h> # include <fcntl.h> # include <math.h> +# include <pthread.h> + # include "mlx.h" # include "libft.h" # include "libft_math.h" # define WINDOW_TITLE "fractol" -# define FRACTOL_RESOLUTION_LOW +# define FRACTOL_RESOLUTION_MEDIUM # ifdef FRACTOL_RESOLUTION_HIGH # define WINDOW_WIDTH 1600 @@ -45,6 +47,12 @@ # define MLXK_DOWN 125 # define MLXK_LEFT 123 # define MLXK_RIGHT 124 +# define MLXK_H 4 +# define MLXK_J 38 +# define MLXK_K 40 +# define MLXK_L 37 +# define MLXK_D 2 +# define MLXK_F 3 # define MLXK_PLUS 24 # define MLXK_MINUS 27 @@ -54,7 +62,7 @@ # define MLX_LITTLE_ENDIAN 0 # define MLX_BIG_ENDIAN 1 -# define PALETTE_SIZE 1024 +# define ZOOM_SPEED 1.2 typedef union { @@ -109,12 +117,14 @@ typedef struct s_state t_complex plane; t_complex c; int iterations; + int offsets[WINDOW_HEIGHT]; } t_state; typedef struct { t_state *state; int offset; + t_complex z; } t_render_routine_arg; /* @@ -153,7 +163,8 @@ int burningship(t_state *state, t_complex z); ** helper.c */ -void h_offset_to_complex(t_state *state, t_complex *z, int offset); +void h_zoom_in(t_state *state); +void h_zoom_out(t_state *state); /* ** color.c diff --git a/src/event.c b/src/event.c index 42aaa93..1c6753e 100644 --- a/src/event.c +++ b/src/event.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/24 09:52:35 by cacharle #+# #+# */ -/* Updated: 2020/02/25 15:24:31 by cacharle ### ########.fr */ +/* Updated: 2020/02/25 16:20:49 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,16 +23,16 @@ int event_quit(t_state *state) int event_keydown(int key, t_state *state) { - /* printf("%d\n", key); */ + printf("%d\n", key); if (key == MLXK_ESC) state->running = false; - else if (key == MLXK_UP) + else if (key == MLXK_UP || key == MLXK_K) state->center.i -= state->plane.i * MOVE_SPEED; - else if (key == MLXK_DOWN) + else if (key == MLXK_DOWN || key == MLXK_J) state->center.i += state->plane.i * MOVE_SPEED; - else if (key == MLXK_LEFT) + else if (key == MLXK_LEFT || key == MLXK_H) state->center.r -= state->plane.r * MOVE_SPEED; - else if (key == MLXK_RIGHT) + else if (key == MLXK_RIGHT || key == MLXK_L) state->center.r += state->plane.r * MOVE_SPEED; else if (key == MLXK_PLUS) { @@ -46,6 +46,10 @@ int event_keydown(int key, t_state *state) state->iterations = 1; state_update_palette(state); } + else if (key == MLXK_F) + h_zoom_in(state); + else if (key == MLXK_D) + h_zoom_out(state); else return (0); state->updated = false; @@ -59,16 +63,14 @@ int event_mouse(int button, int x, int y, t_state *state) if (button == MLX_MOUSE_SCROLL_UP) { - state->plane.r /= ZOOM_SPEED; - state->plane.i /= ZOOM_SPEED; + h_zoom_in(state); /* state->center.r += MOVE_SPEED * (double)(x - WINDOW_WIDTH / 2) / WINDOW_WIDTH; */ /* state->center.i += MOVE_SPEED * (double)(y - WINDOW_HEIGHT / 2) / WINDOW_HEIGHT; */ } else if (button == MLX_MOUSE_SCROLL_DOWN) { - state->plane.r *= ZOOM_SPEED; - state->plane.i *= ZOOM_SPEED; + h_zoom_out(state); /* state->center.r -= MOVE_SPEED * (double)(x - WINDOW_WIDTH / 2) / WINDOW_WIDTH; */ /* state->center.i -= MOVE_SPEED * (double)(y - WINDOW_HEIGHT / 2) / WINDOW_HEIGHT; */ } diff --git a/src/helper.c b/src/helper.c index 8f03bed..fb55a8b 100644 --- a/src/helper.c +++ b/src/helper.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/24 11:54:57 by cacharle #+# #+# */ -/* Updated: 2020/02/24 14:14:43 by cacharle ### ########.fr */ +/* Updated: 2020/02/25 16:21:13 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,3 +22,19 @@ /* z->a = y / state->window.height * state->plane.a; */ /* z->b = x / state->window.width * state->plane.b; */ /* } */ + +void h_zoom_in(t_state *state) +{ + state->plane.r /= ZOOM_SPEED; + state->plane.i /= ZOOM_SPEED; + /* state->iterations += 3; */ + /* state_update_palette(state); */ +} + +void h_zoom_out(t_state *state) +{ + state->plane.r *= ZOOM_SPEED; + state->plane.i *= ZOOM_SPEED; + /* state->iterations -= 3; */ + /* state_update_palette(state); */ +} diff --git a/src/render.c b/src/render.c index a217125..2045a3a 100644 --- a/src/render.c +++ b/src/render.c @@ -6,47 +6,54 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/24 09:54:59 by cacharle #+# #+# */ -/* Updated: 2020/02/25 15:30:07 by cacharle ### ########.fr */ +/* Updated: 2020/02/25 16:13:40 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "fractol.h" -/* static void *st_render_routine(t_state *state) */ -/* { */ -/* return (NULL); */ -/* } */ +static void *st_render_routine(void *void_arg) +{ + int j; + t_complex z; + t_state *state; + int offset; + t_render_routine_arg *arg; + + arg = void_arg; + state = arg->state; + z = arg->z; + offset = arg->offset; + j = -1; + while (++j < WINDOW_WIDTH) + { + z.r = ((double)j / (double)WINDOW_WIDTH) * state->plane.r - (state->plane.r / 2.0) + state->center.r; + ((t_color*)state->window.data)[offset] = state->palette[state->func(state, z)]; + offset++; + } + return (NULL); +} static void st_render_fractal(t_state *state) { - int offset; - int i; - int j; - t_color color; - t_complex z; - /* pthread_t threads[WINDOW_HEIGHT */ - int tmp; + int i; + pthread_t threads[WINDOW_HEIGHT]; + t_render_routine_arg routine_args[WINDOW_HEIGHT]; - color.hexcode = 0xffffff; - offset = 0; i = -1; while (++i < WINDOW_HEIGHT) { - j = -1; - while (++j < WINDOW_WIDTH) + routine_args[i].state = state; + routine_args[i].offset = i * WINDOW_WIDTH; + routine_args[i].z.i = ((double)i / (double)WINDOW_HEIGHT) * state->plane.i - (state->plane.i / 2.0) + state->center.i; + if (pthread_create(threads + i, NULL, st_render_routine, routine_args + i) < 0) { - z.r = ((double)j / (double)WINDOW_WIDTH) * state->plane.r - (state->plane.r / 2.0) + state->center.r; - z.i = ((double)i / (double)WINDOW_HEIGHT) * state->plane.i - (state->plane.i / 2.0) + state->center.i; - tmp = state->func(state, z); - /* if (tmp > state->iterations) */ - /* ((t_color*)state->window.data)[offset].hexcode = 0x000000; */ - /* else */ - /* for (int i = 0; i < state->iterations; i++) */ - /* printf("%d %d %d\n", state->palette[tmp].rgb.r, state->palette[tmp].rgb.g, state->palette[tmp].rgb.b); */ - ((t_color*)state->window.data)[offset] = state->palette[tmp]; - offset++; + state->running = false; + break; } } + while (i-- > 0) + pthread_join(threads[i], NULL); } int render_update(t_state *state) diff --git a/src/state.c b/src/state.c index 1e276f7..f079b6c 100644 --- a/src/state.c +++ b/src/state.c @@ -6,17 +6,12 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/24 09:58:01 by cacharle #+# #+# */ -/* Updated: 2020/02/25 15:34:51 by cacharle ### ########.fr */ +/* Updated: 2020/02/25 15:57:01 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "fractol.h" -#define PALETTE_START 0x000022 -#define PALETTE_END 0xd62f2f -/* #define PALETTE_START 0x000000 */ -/* #define PALETTE_END 0xffffff */ - void state_update_palette(t_state *state) { int i; @@ -37,8 +32,6 @@ void state_update_palette(t_state *state) state->palette[i] = color_hsl_to_rgb(hsl); } state->palette[i].hexcode = 0x0; - /* for (int i = 0; i < state->iterations; i++) */ - /* printf("%d %d %d\n", state->palette[i].rgb.r, state->palette[i].rgb.g, state->palette[i].rgb.b); */ } static int st_state_dispatch_func(t_state *state, char *fractal_name) |
