-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.c
104 lines (98 loc) · 2.96 KB
/
init.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rtavabil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/15 13:36:58 by rtavabil #+# #+# */
/* Updated: 2024/01/26 17:30:08 by rtavabil ### ########.fr */
/* */
/* ************************************************************************** */
#include "fractol.h"
int handle_key_man(int keysym, t_fractal *fractal)
{
if (keysym == XK_Escape)
{
mlx_destroy_image(fractal->mlx, fractal->img.img);
mlx_destroy_window(fractal->mlx, fractal->mlx_win);
mlx_destroy_display(fractal->mlx);
free(fractal->mlx);
exit(1);
}
if (keysym == XK_Up)
fractal->shift_y -= 0.2 * fractal->zoom;
if (keysym == XK_Down)
fractal->shift_y += 0.2 * fractal->zoom;
if (keysym == XK_Right)
fractal->shift_x -= 0.2 * fractal->zoom;
if (keysym == XK_Left)
fractal->shift_x += 0.2 * fractal->zoom;
render_man(fractal);
return (0);
}
int handle_mouse_man(int button, int x, int y, t_fractal *fractal)
{
if (button == 4)
{
fractal->zoom *= 1.1;
set_zoom(1.1, x, y, fractal);
}
else if (button == 5)
{
fractal->zoom /= 1.1;
set_zoom(0.9, x, y, fractal);
}
render_man(fractal);
return (x * y);
}
void set_values_man(t_fractal *fractal)
{
fractal->escape = ESCAPE;
fractal->iterations = ITERATIONS;
fractal->shift_x = 0.0;
fractal->shift_y = 0.0;
fractal->zoom = 1.0;
fractal->limit.max_x = 2;
fractal->limit.min_x = -2;
fractal->limit.max_y = -2;
fractal->limit.min_y = 2;
}
void init_events_man(t_fractal *fractal)
{
set_values_man(fractal);
mlx_mouse_hook(fractal->mlx_win, handle_mouse_man, fractal);
mlx_key_hook(fractal->mlx_win, handle_key_man, fractal);
mlx_hook(fractal->mlx_win,
DestroyNotify,
StructureNotifyMask,
handle_close_x,
fractal);
}
void init_man(t_fractal *fractal)
{
fractal->mlx = mlx_init();
if (!fractal->mlx)
init_error();
fractal->mlx_win = mlx_new_window(fractal->mlx, WIDTH, HEIGHT,
fractal->title);
if (!fractal->mlx_win)
{
mlx_destroy_display(fractal->mlx);
free(fractal->mlx);
init_error();
}
fractal->img.img = mlx_new_image(fractal->mlx, WIDTH, HEIGHT);
if (!fractal->img.img)
{
mlx_destroy_window(fractal->mlx, fractal->mlx_win);
mlx_destroy_display(fractal->mlx);
free(fractal->mlx);
init_error();
}
fractal->img.addr = mlx_get_data_addr(fractal->img.img,
&(fractal->img.bits_per_pixel),
&(fractal->img.line_length),
&(fractal->img.endian));
init_events_man(fractal);
}