-
Notifications
You must be signed in to change notification settings - Fork 0
/
fractol.c
130 lines (122 loc) · 3.65 KB
/
fractol.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fractol.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bbellatr <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/24 16:56:19 by bbellatr #+# #+# */
/* Updated: 2022/01/24 16:56:22 by bbellatr ### ########.fr */
/* */
/* ************************************************************************** */
#include "fractol.h"
void res_params(t_params *params)
{
params->image.length = WIN_HEIGHT;
params->image.width = WIN_WIDTH;
params->max_iteration = 100;
if (params->argc == 4)
{
params->julia_k.re = ft_double(params->argv[2]);
params->julia_k.im = ft_double(params->argv[3]);
}
else
{
params->julia_k.re = -0.8;
params->julia_k.im = 0.0;
}
params->min.im = -2;
params->min.re = -2;
params->max.im = 2;
params->max.re = 2;
params->color_shift = 0;
}
static int (*get_formula(char *name)) (int x, int y, t_params *params)
{
size_t i;
int (*formula)(int x, int y, t_params *vars);
static t_formula formulas[3];
formulas[0].name = "mandelbrot";
formulas[0].formula = &mandelbrot;
formulas[1].name = "julia";
formulas[1].formula = &julia;
formulas[2].name = "burning_ship";
formulas[2].formula = &burning_ship;
i = 0;
formula = NULL;
while (i < (sizeof(formulas) / sizeof(t_formula)))
{
if (!ft_strncmp(name, formulas[i].name, ft_strlen(formulas[i].name)))
formula = formulas[i].formula;
i++;
}
return (formula);
}
void init_image(t_params *params)
{
params->mlx_data.img = mlx_new_image(params->mlx_data.mlx,
params->image.length, params->image.width);
params->mlx_data.addr = mlx_get_data_addr(params->mlx_data.img,
¶ms->mlx_data.bits_per_pixel,
¶ms->mlx_data.line_length,
¶ms->mlx_data.endian);
params->mlx_data.win = mlx_new_window(params->mlx_data.mlx,
params->image.length,
params->image.width,
params->argv[1]);
mlx_loop_hook(params->mlx_data.mlx, &draw_fractal, params);
mlx_hook(params->mlx_data.win, BUTTON_PRESS,
1L << 2, scroll_handler, params);
mlx_hook(params->mlx_data.win, KEY_PRESS,
1L << 0, press_key, params);
mlx_hook(params->mlx_data.win, DESTROY_NOTIFY,
1L << 17, end_p, params);
mlx_hook(params->mlx_data.win, MOTION_NOTIFY,
1L << 6, pointer_handler, params);
mlx_loop(params->mlx_data.mlx);
}
int init_params(t_params *params, char **argv)
{
params->argv = argv;
params->mlx_data.win = NULL;
params->mlx_data.img = NULL;
params->mlx_data.addr = NULL;
params->formula = get_formula(argv[1]);
if (params->formula == NULL)
return (0);
if (params->argc == 4)
{
params->julia_k.re = ft_double(params->argv[2]);
params->julia_k.im = ft_double(params->argv[3]);
if (params->julia_k.im > 1.0 || params->julia_k.im < -1.0
|| params->julia_k.re > 1.0 || params->julia_k.re < -1.0)
return (0);
}
res_params(params);
params->mlx_data.mlx = mlx_init();
return (1);
}
int main(int argc, char **argv)
{
t_params params;
params.argc = argc;
if (argc != 2 && argc != 4)
{
u_tip();
exit(EXIT_FAILURE);
}
if (argc == 4)
{
if (!check(argv[2]) || !check(argv[3]))
{
u_tip();
exit(EXIT_FAILURE);
}
}
if (!init_params(¶ms, argv))
{
u_tip();
end_p(¶ms);
}
init_image(¶ms);
}