Skip to content

Commit a5567de

Browse files
author
Mickael Brement
committed
first try
1 parent 10885fc commit a5567de

33 files changed

+90
-6532
lines changed

Bonus/Error/error.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: mbrement <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/02/03 22:07:57 by mbrement #+# #+# */
9-
/* Updated: 2023/02/20 08:48:01 by mbrement ### ########lyon.fr */
9+
/* Updated: 2023/02/22 06:19:22 by mbrement ### ########lyon.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -16,6 +16,8 @@ int end_of_prog(t_fract *value)
1616
{
1717
if (value->mlx)
1818
{
19+
mlx_destroy_image(value->mlx, value->data->img);
20+
free(value->data);
1921
if (value->window)
2022
mlx_destroy_window(value->mlx, value->window);
2123
mlx_destroy_display(value->mlx);

Bonus/Fractal/julia.c

+4-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: mbrement <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/02/05 14:19:22 by mbrement #+# #+# */
9-
/* Updated: 2023/02/20 08:37:36 by mbrement ### ########lyon.fr */
9+
/* Updated: 2023/02/22 06:20:30 by mbrement ### ########lyon.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -16,21 +16,16 @@ void my_mlx_pixel_put(t_data *data, int x, int y, unsigned int color)
1616
{
1717
char *dst;
1818

19-
dst = data->addr + (y * data->line_length + x * (data->bits_per_pixel / 8));
19+
dst = data->addr + (y * data->line_length + x * 4);
2020
*(unsigned int *)dst = color;
2121
}
2222

2323
void algo_julia(struct s_fract *v)
2424
{
25-
t_data img;
2625
int x;
2726
int y;
2827
int itr;
2928

30-
img.img = mlx_new_image(v->mlx, WIN_W, WIN_H);
31-
img.addr = mlx_get_data_addr(img.img, &img.bits_per_pixel,
32-
&img.line_length, &img.endian);
33-
v->data = &img;
3429
y = -1;
3530
while (++y < WIN_H)
3631
{
@@ -39,12 +34,11 @@ void algo_julia(struct s_fract *v)
3934
{
4035
itr = -1;
4136
v->x.x = (((float)x / WIN_W) * 3.0f - 2.0f) * v->zoom + v->off_x;
42-
v->x.y = (((float)y / WIN_H) * 2.5f - 1.25f) * v->zoom;
37+
v->x.y = (((float)y / WIN_H) * 2.5f - 1.25f) * v->zoom + v->off_y;
4338
while (++itr < 128 && module_cplx_pow2(v->x) < 4.0)
44-
v->x = algo_cplx(1, mult_cplx(v->x, v->x), 1, v->y);
39+
v->x = algo_cplx(mult_cplx(v->x, v->x), v->y);
4540
my_mlx_pixel_put(v->data, x, y, ft_color(itr, v));
4641
}
4742
}
4843
mlx_put_image_to_window(v->mlx, v->window, v->data->img, 0, 0);
49-
mlx_destroy_image(v->mlx, v->data->img);
5044
}

Bonus/Fractal/mandelbrot.c

+2-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: mbrement <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/02/05 14:19:58 by mbrement #+# #+# */
9-
/* Updated: 2023/02/20 08:23:43 by mbrement ### ########lyon.fr */
9+
/* Updated: 2023/02/23 12:56:30 by mbrement ### ########lyon.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -15,12 +15,7 @@
1515
void algo_mandelbrot(struct s_fract *v)
1616
{
1717
int c[3];
18-
t_data img;
1918

20-
img.img = mlx_new_image(v->mlx, WIN_W, WIN_H);
21-
img.addr = mlx_get_data_addr(img.img, &img.bits_per_pixel,
22-
&img.line_length, &img.endian);
23-
v->data = &img;
2419
c[0] = -1;
2520
while (++c[0] < WIN_H)
2621
{
@@ -31,11 +26,10 @@ void algo_mandelbrot(struct s_fract *v)
3126
c[2] = -1;
3227
v->y.x = (((float)c[1] / WIN_W) * 3.0f - 2.0f) * v->zoom + v->off_x;
3328
while (++c[2] < 128 && module_cplx_pow2(v->x) < 4)
34-
v->x = algo_cplx(1, mult_cplx(v->x, v->x), 1, v->y);
29+
v->x = algo_cplx(mult_cplx(v->x, v->x), v->y);
3530
v->x = v->x_o;
3631
my_mlx_pixel_put(v->data, c[1], c[0], ft_color(c[2], v));
3732
}
3833
}
3934
mlx_put_image_to_window(v->mlx, v->window, v->data->img, 0, 0);
40-
mlx_destroy_image(v->mlx, v->data->img);
4135
}

Bonus/Fractal/math.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: mbrement <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/02/10 11:25:03 by mbrement #+# #+# */
9-
/* Updated: 2023/02/15 16:10:36 by mbrement ### ########lyon.fr */
9+
/* Updated: 2023/02/23 12:56:44 by mbrement ### ########lyon.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -21,12 +21,12 @@ t_complex add_complex(t_complex a, t_complex b)
2121
return (rtn);
2222
}
2323

24-
t_complex algo_cplx(float l, t_complex a, float m, t_complex b)
24+
t_complex algo_cplx(t_complex a, t_complex b)
2525
{
2626
t_complex r;
2727

28-
r.x = l * a.x + m * b.x;
29-
r.y = l * a.y + m * b.y;
28+
r.x = a.x + b.x;
29+
r.y = a.y + b.y;
3030
return (r);
3131
}
3232

Bonus/Fractal/smandelbrot.c

+3-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: mbrement <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/02/20 04:54:38 by mbrement #+# #+# */
9-
/* Updated: 2023/02/20 09:57:06 by mbrement ### ########lyon.fr */
9+
/* Updated: 2023/02/22 06:20:59 by mbrement ### ########lyon.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -27,6 +27,7 @@ struct s_fract fract_compleat_smandelbrot(struct s_fract *val)
2727
val->off_y_o = val->off_y;
2828
val->off_y_o = val->off_x;
2929
val->zoom_o = val->zoom - 0.0001;
30+
init_img(val);
3031
return (*val);
3132
}
3233

@@ -45,12 +46,7 @@ t_complex ft_smand(t_fract *fract)
4546
void algo_smandelbrot(struct s_fract *v)
4647
{
4748
int c[3];
48-
t_data img;
4949

50-
img.img = mlx_new_image(v->mlx, WIN_W, WIN_H);
51-
img.addr = mlx_get_data_addr(img.img, &img.bits_per_pixel,
52-
&img.line_length, &img.endian);
53-
v->data = &img;
5450
c[0] = -1;
5551
while (++c[0] < WIN_H)
5652
{
@@ -61,11 +57,10 @@ void algo_smandelbrot(struct s_fract *v)
6157
c[2] = -1;
6258
v->y.x = (((float)c[1] / WIN_W) * 3.0f - 2.0f) * v->zoom + v->off_x;
6359
while (++c[2] < 128 && module_cplx_pow2(v->x) < 4)
64-
v->x = algo_cplx(1, ft_smand(v), 1, v->y);
60+
v->x = algo_cplx(ft_smand(v), v->y);
6561
v->x = v->x_o;
6662
my_mlx_pixel_put(v->data, c[1], c[0], ft_color(c[2], v));
6763
}
6864
}
6965
mlx_put_image_to_window(v->mlx, v->window, v->data->img, 0, 0);
70-
mlx_destroy_image(v->mlx, v->data->img);
7166
}

Bonus/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ OBJS = ${SRCS:%.c=${DIR_OBJS}%.o}
2929

3030

3131
CC = cc
32-
CFLAGS = -Wall -Wextra -Werror -O2 -ffast-math -march=native
33-
# -fsanitize=address -g3
32+
CFLAGS = -Wall -Wextra -Werror -O2 -ffast-math -march=native
33+
#-fsanitize=address -g3
3434

3535

3636
RM = rm -rf

Bonus/Project_core/algo_core.c

+24-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: mbrement <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/02/04 13:20:29 by mbrement #+# #+# */
9-
/* Updated: 2023/02/20 09:54:17 by mbrement ### ########lyon.fr */
9+
/* Updated: 2023/02/22 06:35:51 by mbrement ### ########lyon.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -33,6 +33,20 @@ int loop(void *fractal)
3333
return (0);
3434
}
3535

36+
void init_img(t_fract *v)
37+
{
38+
t_data *i;
39+
40+
i = malloc(sizeof(t_data));
41+
i->bits_per_pixel = 0;
42+
i->line_length = 0;
43+
i->endian = 0;
44+
i->img = mlx_new_image(v->mlx, WIN_W, WIN_H);
45+
i->addr = mlx_get_data_addr(i->img, &i->bits_per_pixel,
46+
&i->line_length, &i->endian);
47+
v->data = i;
48+
}
49+
3650
static struct s_fract fract_compleat_julia(struct s_fract *val)
3751
{
3852
val->x.x = -2;
@@ -47,6 +61,7 @@ static struct s_fract fract_compleat_julia(struct s_fract *val)
4761
val->zoom_o = val->zoom - 0.0001;
4862
val->off_y_o = val->off_y;
4963
val->off_y_o = val->off_x;
64+
init_img(val);
5065
return (*val);
5166
}
5267

@@ -65,6 +80,7 @@ static struct s_fract fract_compleat_mandelbrot(struct s_fract *val)
6580
val->off_y_o = val->off_y;
6681
val->off_y_o = val->off_x;
6782
val->zoom_o = val->zoom - 0.0001;
83+
init_img(val);
6884
return (*val);
6985
}
7086

@@ -74,21 +90,21 @@ void algo_init(int value, int fractal_nb)
7490

7591
fractal.value = value;
7692
fractal.fractal_nb = fractal_nb;
77-
if (fractal_nb == 1)
78-
fractal = fract_compleat_julia(&fractal);
79-
else if (fractal_nb == 2)
80-
fractal = fract_compleat_mandelbrot(&fractal);
81-
else
82-
fractal = fract_compleat_smandelbrot(&fractal);
8393
fractal.mlx = mlx_init();
8494
if (fractal.mlx == 0)
8595
ft_error(4);
8696
fractal.window = mlx_new_window(fractal.mlx, WIN_W, WIN_H, "fract_ol");
8797
if (fractal.window == 0)
8898
ft_error(4);
99+
if (fractal_nb == 1)
100+
fractal = fract_compleat_julia(&fractal);
101+
else if (fractal_nb == 2)
102+
fractal = fract_compleat_mandelbrot(&fractal);
103+
else
104+
fractal = fract_compleat_smandelbrot(&fractal);
105+
loop(&fractal);
89106
mlx_hook(fractal.window, 17, (1L << 5), end_of_prog, &fractal);
90107
mlx_mouse_hook(fractal.window, *hook_mouse, &fractal);
91108
mlx_key_hook(fractal.window, *hook, &fractal);
92-
loop(&fractal);
93109
mlx_loop(fractal.mlx);
94110
}

Bonus/Project_core/hook.c

+1-14
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: mbrement <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/02/12 14:51:18 by mbrement #+# #+# */
9-
/* Updated: 2023/02/21 08:36:01 by mbrement ### ########lyon.fr */
9+
/* Updated: 2023/02/23 12:56:18 by mbrement ### ########lyon.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -79,25 +79,12 @@ int hook(int key, t_fract *fract)
7979
return (0);
8080
}
8181

82-
#include <stdio.h>
83-
8482
int hook_mouse(int key, int x, int y, t_fract *fract)
8583
{
86-
printf("O = %f | %f\n", fract->off_x, fract->off_y);
8784
if (key == 4)
88-
{
89-
// fract->off_x = (((float)WIN_H/2) - (((float)WIN_H-x)/2))/1000;
90-
fract->off_y = (((float)WIN_W/2) - (((float)WIN_W-y)/2))/1000;
9185
fract->zoom = fract->zoom - fract->zoom / 10;
92-
printf("- = %f | %f\n", fract->off_x, fract->off_y);
93-
}
9486
else if (key == 5)
95-
{
96-
// fract->off_x = (((float)WIN_H/2) - ((float)WIN_H/2))/1000;
97-
fract->off_y = (((float)WIN_W/2) - ((float)WIN_W/2))/1000;
9887
fract->zoom = fract->zoom + fract->zoom / 10;
99-
printf("+ = %f | %f\n", fract->off_x, fract->off_y);
100-
}
10188
if (fract->mouse)
10289
{
10390
if (fract->fractal_nb == 1)

Bonus/fract_ol.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: mbrement <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/02/03 15:34:08 by mbrement #+# #+# */
9-
/* Updated: 2023/02/20 09:59:01 by mbrement ### ########lyon.fr */
9+
/* Updated: 2023/02/22 06:15:43 by mbrement ### ########lyon.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -63,12 +63,13 @@ unsigned int ft_color(int color, struct s_fract *value);
6363
void my_mlx_pixel_put(t_data *data, int x, int y,
6464
unsigned int color);
6565
int loop(void *fractal);
66+
void init_img(t_fract *v);
6667

6768
//MATH
6869
void algo_julia(struct s_fract *value);
6970
void algo_mandelbrot(struct s_fract *value);
7071
t_complex add_complex(t_complex a, t_complex b);
71-
t_complex algo_cplx(float l, t_complex a, float m, t_complex b);
72+
t_complex algo_cplx(t_complex a, t_complex b);
7273
t_complex mult_cplx(t_complex a, t_complex b);
7374
float module_cplx_pow2(t_complex z);
7475
struct s_fract fract_compleat_smandelbrot(struct s_fract *val);

Error/error.c

+9-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: mbrement <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/02/03 22:07:57 by mbrement #+# #+# */
9-
/* Updated: 2023/02/21 19:02:00 by mbrement ### ########lyon.fr */
9+
/* Updated: 2023/02/23 12:55:56 by mbrement ### ########lyon.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -16,18 +16,22 @@ void end_of_prog(t_fract *value)
1616
{
1717
if (value->mlx)
1818
{
19+
mlx_destroy_image(value->mlx, value->data->img);
20+
free(value->data);
1921
if (value->window)
2022
mlx_destroy_window(value->mlx, value->window);
2123
mlx_destroy_display(value->mlx);
2224
free(value->mlx);
23-
mlx_destroy_image(value->mlx, value->data->img);
24-
// mlx_destroy_image(value->mlx, value->data_tmp->img);
25-
free(value->data);
26-
// free(value->data_tmp);
2725
}
2826
exit(0);
2927
}
3028

29+
int on_destroy(struct s_fract *fractal)
30+
{
31+
end_of_prog(fractal);
32+
exit(0);
33+
}
34+
3135
void ft_error(int error_code)
3236
{
3337
int fd;

0 commit comments

Comments
 (0)