pthread
This commit is contained in:
		
							parent
							
								
									50b9a36fae
								
							
						
					
					
						commit
						8e9693a2be
					
				
					 6 changed files with 119 additions and 67 deletions
				
			
		|  | @ -21,4 +21,5 @@ target_link_libraries(fractol | ||||||
| 		-lft | 		-lft | ||||||
| 		-lmlx | 		-lmlx | ||||||
| 		"-framework OpenGL" | 		"-framework OpenGL" | ||||||
| 		"-framework AppKit") # linkage | 		"-framework AppKit" | ||||||
|  | 		-lpthread) # linkage | ||||||
|  |  | ||||||
|  | @ -17,10 +17,11 @@ | ||||||
| # include "mlx.h" | # include "mlx.h" | ||||||
| # include <math.h> | # include <math.h> | ||||||
| # include <fcntl.h> | # include <fcntl.h> | ||||||
|  | #include <pthread.h> | ||||||
| #include <stdio.h> | #include <stdio.h> | ||||||
| 
 | 
 | ||||||
| # define HEIGHT			1200 | # define HEIGHT			1200 | ||||||
| # define WIDTH			2500 | # define WIDTH			1200 | ||||||
| # define COLOR1			0 | # define COLOR1			0 | ||||||
| # define COLOR2			16777215 | # define COLOR2			16777215 | ||||||
| # define INIT_X			30 | # define INIT_X			30 | ||||||
|  | @ -39,7 +40,9 @@ | ||||||
| # define VIOLET			8323327 | # define VIOLET			8323327 | ||||||
| # define BLACK			0 | # define BLACK			0 | ||||||
| 
 | 
 | ||||||
| # define JUL_MAX_ITER	100 | # define JUL_MAX_ITER	300 | ||||||
|  | 
 | ||||||
|  | # define NUM_THREADS     4 | ||||||
| 
 | 
 | ||||||
| typedef struct 	s_complex | typedef struct 	s_complex | ||||||
| { | { | ||||||
|  | @ -67,12 +70,20 @@ typedef struct 	s_move | ||||||
| 	double 		z; | 	double 		z; | ||||||
| }				t_move; | }				t_move; | ||||||
| 
 | 
 | ||||||
|  | typedef struct 	s_pixel | ||||||
|  | { | ||||||
|  | 	int 		x; | ||||||
|  | 	int 		y; | ||||||
|  | }				t_pixel; | ||||||
|  | 
 | ||||||
| typedef struct 	s_julia | typedef struct 	s_julia | ||||||
| { | { | ||||||
| 	t_complex	jul_const; | 	t_complex	jul_const; | ||||||
| 	t_color 	color; | 	t_color 	color; | ||||||
| 	int 		max_iterations; |  | ||||||
| 	t_move		mov; | 	t_move		mov; | ||||||
|  | 	int 		x; | ||||||
|  | 	int 		y; | ||||||
|  | 	int 		max_itr; | ||||||
| }				t_julia; | }				t_julia; | ||||||
| 
 | 
 | ||||||
| typedef struct	s_fractals | typedef struct	s_fractals | ||||||
|  | @ -93,7 +104,6 @@ typedef struct	s_fractol | ||||||
| 	int			endian; | 	int			endian; | ||||||
| }				t_fractol; | }				t_fractol; | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| int				tab_length(char **tab); | int				tab_length(char **tab); | ||||||
| void			free_tab(char **tab); | void			free_tab(char **tab); | ||||||
| void			quit(t_fractol *fractol); | void			quit(t_fractol *fractol); | ||||||
|  |  | ||||||
							
								
								
									
										78
									
								
								src/color.c
									
										
									
									
									
								
							
							
						
						
									
										78
									
								
								src/color.c
									
										
									
									
									
								
							|  | @ -52,49 +52,49 @@ t_color	int_to_color(int c) | ||||||
| 	new.red = (c >> 16) & 0x0000FF; | 	new.red = (c >> 16) & 0x0000FF; | ||||||
| 	return (new); | 	return (new); | ||||||
| } | } | ||||||
|  | //
 | ||||||
|  | //int		rainbow(int step, int max_step)
 | ||||||
|  | //{
 | ||||||
|  | //	if (step >= 0 && step < max_step / 8)
 | ||||||
|  | //		return (RED);
 | ||||||
|  | //	if (step >= max_step / 8 && step <= max_step / 4)
 | ||||||
|  | //		return (ORANGE);
 | ||||||
|  | //	if (step >= max_step / 4 && step <= max_step / 8 * 3)
 | ||||||
|  | //		return (YELLOW);
 | ||||||
|  | //	if (step >= max_step / 8 * 3 && step <= max_step / 2)
 | ||||||
|  | //		return (BLUE);
 | ||||||
|  | //	if (step >= max_step / 2 && step <= max_step / 8 * 5)
 | ||||||
|  | //		return (VIOLET);
 | ||||||
|  | //	if (step >= max_step / 8 * 5 && step <= max_step / 8 * 6)
 | ||||||
|  | //		return (BLACK);
 | ||||||
|  | //	return (0);
 | ||||||
|  | //}
 | ||||||
| 
 | 
 | ||||||
| int		rainbow(int step, int max_step) | int		rainbow(int step, int max_step) | ||||||
| { | { | ||||||
| 	if (step >= 0 && step < max_step / 8) | 	if (step >= 0 && step < max_step / 8) | ||||||
| 		return (RED); | 		return (color_to_int( | ||||||
| 	if (step >= max_step / 8 && step <= max_step / 4) | 				color_lerp(int_to_color(RED), int_to_color(ORANGE), | ||||||
| 		return (ORANGE); | 						   (float)step / (float)max_step))); | ||||||
| 	if (step >= max_step / 4 && step <= max_step / 8 * 3) | 	if (step >= max_step / 8 && step <= max_step / 8 * 2) | ||||||
| 		return (YELLOW); | 		return (color_to_int( | ||||||
| 	if (step >= max_step / 8 * 3 && step <= max_step / 2) | 				color_lerp(int_to_color(ORANGE), int_to_color(YELLOW), | ||||||
| 		return (BLUE); | 						   step / max_step / 8 * 2))); | ||||||
| 	if (step >= max_step / 2 && step <= max_step / 8 * 5) | 	if (step >= max_step / 8 * 2 && step <= max_step / 8 * 3) | ||||||
| 		return (VIOLET); | 		return (color_to_int( | ||||||
|  | 				color_lerp(int_to_color(YELLOW), int_to_color(GREEN), | ||||||
|  | 						   step / max_step / 8 * 3))); | ||||||
|  | 	if (step >= max_step / 8 * 3 && step <= max_step / 8 * 4) | ||||||
|  | 		return (color_to_int( | ||||||
|  | 				color_lerp(int_to_color(GREEN), int_to_color(BLUE), | ||||||
|  | 						   step / max_step / 8 * 4))); | ||||||
|  | 	if (step >= max_step / 8 * 4 && step <= max_step / 8 * 5) | ||||||
|  | 		return (color_to_int( | ||||||
|  | 				color_lerp(int_to_color(BLUE), int_to_color(VIOLET), | ||||||
|  | 						   step / max_step / 8 * 5))); | ||||||
| 	if (step >= max_step / 8 * 5 && step <= max_step / 8 * 6) | 	if (step >= max_step / 8 * 5 && step <= max_step / 8 * 6) | ||||||
| 		return (BLACK); | 		return (color_to_int( | ||||||
|  | 				color_lerp(int_to_color(VIOLET), int_to_color(BLACK), | ||||||
|  | 						   step / max_step / 8 * 6))); | ||||||
| 	return (0); | 	return (0); | ||||||
| } | } | ||||||
| 
 |  | ||||||
| //int		rainbow(int step, int max_step)
 |  | ||||||
| //{
 |  | ||||||
| //	if (step >= 0 && step < max_step / 8)
 |  | ||||||
| //		return (color_to_int(
 |  | ||||||
| //				color_lerp(int_to_color(RED), int_to_color(ORANGE),
 |  | ||||||
| //						   step / max_step / 8)));
 |  | ||||||
| //	if (step >= max_step / 8 && step <= max_step / 8 * 2)
 |  | ||||||
| //		return (color_to_int(
 |  | ||||||
| //				color_lerp(int_to_color(ORANGE), int_to_color(YELLOW),
 |  | ||||||
| //						   step / max_step / 8 * 2)));
 |  | ||||||
| //	if (step >= max_step / 8 * 2 && step <= max_step / 8 * 3)
 |  | ||||||
| //		return (color_to_int(
 |  | ||||||
| //				color_lerp(int_to_color(YELLOW), int_to_color(GREEN),
 |  | ||||||
| //						   step / max_step / 8 * 3)));
 |  | ||||||
| //	if (step >= max_step / 8 * 3 && step <= max_step / 8 * 4)
 |  | ||||||
| //		return (color_to_int(
 |  | ||||||
| //				color_lerp(int_to_color(GREEN), int_to_color(BLUE),
 |  | ||||||
| //						   step / max_step / 8 * 4)));
 |  | ||||||
| //	if (step >= max_step / 8 * 4 && step <= max_step / 8 * 5)
 |  | ||||||
| //		return (color_to_int(
 |  | ||||||
| //				color_lerp(int_to_color(BLUE), int_to_color(VIOLET),
 |  | ||||||
| //						   step / max_step / 8 * 5)));
 |  | ||||||
| //	if (step >= max_step / 8 * 5 && step <= max_step / 8 * 6)
 |  | ||||||
| //		return (color_to_int(
 |  | ||||||
| //				color_lerp(int_to_color(VIOLET), int_to_color(BLACK),
 |  | ||||||
| //						   step / max_step / 8 * 6)));
 |  | ||||||
| //	return (0);
 |  | ||||||
| //}
 |  | ||||||
|  |  | ||||||
|  | @ -30,7 +30,7 @@ t_julia			*jul_init(void) | ||||||
| 
 | 
 | ||||||
| 	j = malloc(sizeof(t_julia)); | 	j = malloc(sizeof(t_julia)); | ||||||
| 	complex_equal(-0.7, 0.27015, &j->jul_const); | 	complex_equal(-0.7, 0.27015, &j->jul_const); | ||||||
| 	j->max_iterations = JUL_MAX_ITER; | 	j->max_itr = JUL_MAX_ITER; | ||||||
| 	j->mov.x = 0; | 	j->mov.x = 0; | ||||||
| 	j->mov.y = 0; | 	j->mov.y = 0; | ||||||
| 	j->mov.z = 1; | 	j->mov.z = 1; | ||||||
|  |  | ||||||
							
								
								
									
										80
									
								
								src/julia.c
									
										
									
									
									
								
							
							
						
						
									
										80
									
								
								src/julia.c
									
										
									
									
									
								
							|  | @ -1,44 +1,84 @@ | ||||||
| #include "fractol.h" | #include "fractol.h" | ||||||
| 
 | 
 | ||||||
| static void	julia_computation(int x, int y, t_julia *ju) | int		complex_parallel(t_complex new, t_complex com_const) | ||||||
| { | { | ||||||
| 	t_complex	new; | 	int			i; | ||||||
| 	t_complex	old; | 	t_complex	old; | ||||||
| 	int 		i; |  | ||||||
| 
 | 
 | ||||||
| 	new.rl = 1.5 * (x - WIDTH / 2) / (0.5 * ju->mov.z * WIDTH) + ju->mov.x; |  | ||||||
| 	new.im = (y - HEIGHT / 2) / (0.5 * ju->mov.z * HEIGHT) + ju->mov.y; |  | ||||||
| 	i = 0; | 	i = 0; | ||||||
| 	while (i < ju->max_iterations) | 	while (i < JUL_MAX_ITER) | ||||||
| 	{ | 	{ | ||||||
| 		old = new; | 		old = new; | ||||||
| 		new.rl = old.rl * old.rl - old.im * old.im + ju->jul_const.rl; | 		new.rl = old.rl * old.rl - old.im * old.im + com_const.rl; | ||||||
| 		new.im = 2 * old.rl * old.im + ju->jul_const.im; | 		new.im = 2 * old.rl * old.im + com_const.im; | ||||||
| 		if((new.rl * new.rl + new.im * new.im) > 4) | 		if((new.rl * new.rl + new.im * new.im) > 4) | ||||||
| 			break ; | 			break ; | ||||||
| 		i++; | 		i++; | ||||||
| 	} | 	} | ||||||
| 	ju->color = int_to_color(rainbow(i, ju->max_iterations)); | 	return (i); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | void	*julia_computation(void *fr) | ||||||
|  | { | ||||||
|  | 	t_complex	new; | ||||||
|  | 	t_julia		*ju; | ||||||
|  | 	int 		i; | ||||||
|  | 	int 		x; | ||||||
|  | 	int 		y; | ||||||
|  | 
 | ||||||
|  | 	ju = ((t_fractol *)fr)->fractals->jul; | ||||||
|  | 	x = ju->x; | ||||||
|  | 	y = ju->y; | ||||||
|  | 	new.rl = 1.5 * (x - WIDTH / 2) / (0.5 * ju->mov.z * WIDTH) + ju->mov.x; | ||||||
|  | 	new.im = (y - HEIGHT / 2) / (0.5 * ju->mov.z * HEIGHT) + ju->mov.y; | ||||||
|  | 	i = complex_parallel(new, ju->jul_const); | ||||||
|  | 	ju->color = int_to_color(rainbow(i, ju->max_itr)); | ||||||
|  | 	put_pixel_to_image(x, y, &((t_fractol *)fr)->fractals->jul->color, fr); | ||||||
|  | 	return (0); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | //void	julia_parralel(t_fractol *fr)
 | ||||||
|  | //{
 | ||||||
|  | //	pthread_t	threads[NUM_THREADS];
 | ||||||
|  | //	t_fractol	*fr_tmp;
 | ||||||
|  | //	int			rc;
 | ||||||
|  | //	int			i;
 | ||||||
|  | //
 | ||||||
|  | //	i = 0;
 | ||||||
|  | //	while (i < NUM_THREADS && fr->fractals->jul->y < fr->w_width)
 | ||||||
|  | //	{
 | ||||||
|  | //		fr_tmp = malloc(sizeof(t_fractol));
 | ||||||
|  | //		*fr_tmp = *fr;
 | ||||||
|  | ////		printf("In main: creating thread %d\n", i);
 | ||||||
|  | //		rc = pthread_create(&threads[i], NULL, julia_computation, (void *)fr_tmp);
 | ||||||
|  | //		if (rc){
 | ||||||
|  | //			printf("ERROR; return code from pthread_create() is %d\n", rc);
 | ||||||
|  | //			exit(-1);
 | ||||||
|  | //		}
 | ||||||
|  | //		free(fr_tmp);
 | ||||||
|  | //		fr->fractals->jul->y++;
 | ||||||
|  | //		i++;
 | ||||||
|  | //	}
 | ||||||
|  | //}
 | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| void	draw_julia(t_fractol *fr) | void	draw_julia(t_fractol *fr) | ||||||
| { | { | ||||||
| 	int 	x; | 	int 		x; | ||||||
| 	int 	y; | 	int 		y; | ||||||
| 
 |  | ||||||
| 	new_and_clear_image(fr); | 	new_and_clear_image(fr); | ||||||
| 	x = 0; | 	y = 0; | ||||||
| 	while (x < fr->w_width) | 	while (y < fr->w_height) | ||||||
| 	{ | 	{ | ||||||
| 		y = 0; | 		x = 0; | ||||||
| 		while(y < fr->w_height) | 		while(x < fr->w_width) | ||||||
| 		{ | 		{ | ||||||
| 			julia_computation(x, y, fr->fractals->jul); | 			fr->fractals->jul->x = x; | ||||||
| 			put_pixel_to_image(x, y, &fr->fractals->jul->color, fr); | 			fr->fractals->jul->y = y; | ||||||
| 			y++; | 			julia_computation(fr); | ||||||
|  | 			x++; | ||||||
| 		} | 		} | ||||||
| 		x++; | 		y++; | ||||||
| 	} | 	} | ||||||
| 	mlx_put_image_to_window(fr->mw->mlx, fr->mw->win, fr->image, 0, 0); | 	mlx_put_image_to_window(fr->mw->mlx, fr->mw->win, fr->image, 0, 0); | ||||||
| } | } | ||||||
|  | @ -11,7 +11,8 @@ | ||||||
| /* ************************************************************************** */ | /* ************************************************************************** */ | ||||||
| 
 | 
 | ||||||
| #include "fractol.h" | #include "fractol.h" | ||||||
| #include <time.h> | 
 | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| int		main(void) | int		main(void) | ||||||
| { | { | ||||||
|  | @ -25,5 +26,5 @@ int		main(void) | ||||||
| 	mlx_hook(fr->mw->win, 4, 0, mouse_button_hook, fr); | 	mlx_hook(fr->mw->win, 4, 0, mouse_button_hook, fr); | ||||||
| 	mlx_hook(fr->mw->win, 6, 0, mouse_move_hook, fr); | 	mlx_hook(fr->mw->win, 6, 0, mouse_move_hook, fr); | ||||||
| 	mlx_loop(fr->mw->mlx); | 	mlx_loop(fr->mw->mlx); | ||||||
| 	return (0); | 	pthread_exit(NULL); | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 Gregory Tertyshny
						Gregory Tertyshny