-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
135 lines (124 loc) · 3.79 KB
/
main.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
131
132
133
134
135
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mabbas <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/08 20:43:36 by mabbas #+# #+# */
/* Updated: 2022/12/12 01:50:54 by mabbas ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/fractol.h"
#include<stdio.h>
/** Function to draw/render fractal on screen **/
void render_fractal(t_mlx *mlx)
{
int x;
int y;
t_oper scale;
scale = complex_init((mlx->max.r - mlx->min.r) \
/ (WIDTH), (mlx->max.i - mlx->min.i) / (HEIGHT));
y = 0;
while (y < HEIGHT)
{
mlx->c.i = mlx->max.i - y * scale.i;
x = 0;
while (x < WIDTH)
{
mlx->c.r = mlx->min.r + x * scale.r;
mlx->eqn(mlx);
recode_mlx_pixel_put(mlx, x, y, color_init(mlx));
x++;
}
y++;
}
mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->img->img, 0, 0);
}
/** Help me brooooo , help me **/
void help_options(void)
{
ft_putendl_fd("**************************\n", 1);
ft_putendl_fd("How to use: ./fractol <no>", 1);
ft_putendl_fd("Available Fractols:", 1);
ft_putendl_fd("mandelbrot -- 1", 1);
ft_putendl_fd("julia -- 2", 1);
ft_putendl_fd("burningship-- 3", 1);
ft_putendl_fd("\n", 1);
ft_putendl_fd("Mouse:", 1);
ft_putendl_fd("For Zoom: scrollWheel", 1);
ft_putendl_fd("Move it, move it and julia changes", 1);
ft_putendl_fd("\n", 1);
ft_putendl_fd("Keys:", 1);
ft_putendl_fd("WASD keys to move", 1);
ft_putendl_fd("Q and E to increase or decrease iterations", 1);
ft_putendl_fd("C to shift colors", 1);
ft_putendl_fd("SPACE to reset braaah", 1);
ft_putendl_fd("Don't dare to do ESC", 1);
ft_putendl_fd("**************************\n", 1);
}
/**
* @brief Setting up window
* We init everything, create new win,image
* and the hooks for mouse and keyboard
* Julia set is bit different functionality
* so used diff hooks for that. then we use
* render fractal function to draw and mlx_loop
* to keep it going until user quits
*/
void gui_init(t_mlx *mlx)
{
mlx->mlx = mlx_init();
mlx->win = mlx_new_window(mlx->mlx, WIDTH, HEIGHT, \
"FrAct-ol");
mlx->img = img_init(mlx);
default_init(mlx);
mlx_hook(mlx->win, 2, 0, keymap, mlx);
mlx_hook(mlx->win, 4, 0, ctrl_mouse, mlx);
if (mlx->eqn == &mandelbrot)
mlx_hook(mlx->win, 17, 0, close_window, mlx);
if (mlx->eqn == &julia)
{
mlx_hook(mlx->win, 5, 0, julia_key_press, mlx);
mlx_hook(mlx->win, 2, 0, keymap, mlx);
mlx_hook(mlx->win, 6, 0, change_julia, mlx);
mlx_hook(mlx->win, 17, 0, close_window, mlx);
ft_putendl_fd("Julia is overcooked", 1);
}
render_fractal(mlx);
mlx_loop(mlx->mlx);
}
/** This is for parsing with ft_strncmp and using ft_strlen to
* compare the user typed actual names. If they match, it loads
* the fractal
**/
void arg_check(char *arg)
{
t_mlx *mlx;
mlx = malloc(sizeof(t_mlx));
if (!(ft_strncmp(arg, "1", 1)))
{
mlx->eqn = &mandelbrot;
gui_init(mlx);
}
else if (!(ft_strncmp(arg, "2", 1)))
{
mlx->eqn = &julia;
gui_init(mlx);
}
else if (!(ft_strncmp(arg, "3", 1)))
{
mlx->eqn = &burning_ship;
gui_init(mlx);
}
free (mlx);
help_options();
}
int main(int argc, char **argv)
{
if (argc == 2)
arg_check(argv[1]);
else
help_options();
return (0);
}