[42-Seoul/fract-ol] 프로젝트 구현

yebeen·2022년 8월 8일
0

42-Seoul/fract-ol

목록 보기
4/4
post-thumbnail

프로젝트 구조

프로젝트 구조는 아래와 같이 구성하였며 mlx파일은 함께 제공 됩니다.

📦fractol
 ┣ 📂includes
 ┃ ┣ 📜fractol.h
 ┃ ┣ 📜keycode.h
 ┃ ┗ 📜types.h
 ┣ 📂libft
 ┃ ┣ 📜Makefile
 ┃ ┣ 📜ft_atof.c
 ┃ ┣ 📜ft_putstr_fd.c
 ┃ ┣ 📜ft_strcmp.c
 ┃ ┣ 📜ft_strlen.c
 ┃ ┗ 📜libft.h
 ┣ 📂mlx
 ┣ 📂srcs
 ┃ ┣ 📜fractol.c
 ┃ ┣ 📜fractol_draw.c
 ┃ ┣ 📜fractol_hook.c
 ┃ ┣ 📜fractol_init.c
 ┃ ┣ 📜fractol_utils.c
 ┃ ┗ 📜main.c
 ┗ 📜Makefile

프로젝트 구현

includes

fractol.h

#ifndef FRACTOL_H
# define FRACTOL_H

# include <unistd.h>
# include <stdlib.h>
# include "../libft/libft.h"
# include "../mlx/mlx.h"
# include "keycode.h"
# include "types.h"

# define TITLE(title)	title
# define WIN_WIDTH 		1080
# define WIN_HEIGHT 	1080
# define ITERATION		250

int		ft_check_type(t_fractol *f, int ac, char *av[]);
int		ft_mlx_init(t_fractol *f);
int		ft_error(char *msg, int fd);
void	ft_image_init(t_fractol *f);
void	ft_fractal_init(t_fractol *f);
void	ft_draw(t_fractol *f);
void	ft_mandelbrot(t_complex c, t_fractol *f);
void	ft_julia(t_complex c, t_fractol *f);
void	ft_color_pixel(t_fractol *f, int x, int y);
int		ft_key_hook(int keycode, t_fractol *f);
int		ft_mouse_hook(int button, int x, int y, t_fractol *f);

#endif
  • 화면 크기를 WIN_WIDTH, WIN_HEIGHT로 설정 후 쉽게 변경할 수 있도록 했습니다.
  • TITLE(title)을 설정해서 윈도우 창의 title이 프랙탈에 맞게 변경될 수 있도록 했습니다.
  • ITERATION 반복 횟수를 지정해서 쉽게 변경해서 확인할 수 있도록 했습니다.
  • types.h에는 fractol을 구현하기 위한 구조체에 대해 정의되어 있습니다.
  • keycode.h에는 hook이벤트에 사용되는 키코드에 대해 정의해뒀습니다.

srcs

main.c

int	main(int ac, char *av[])
{
	t_fractol	f;

	if (ft_check_type(&f, ac, av))
	{
		if (!ft_mlx_init(&f))
		{
			ft_error("\nERROR : SOMETHING WRONG WITH MLX, TRY AGAIN\n", 2);
			exit(1);
		}
		ft_image_init(&f);
		ft_draw(&f);
		mlx_loop(f.mlx);
	}
	return (0);
}
  • ft_check_type : fractal 종류가 맞게 잘 입력됐는지 확인합니다.
  • ft_mlx_init : mlx 필수 값을 초기화 합니다.
  • ft_image_init : mlx 이미지 사용에 필요한 필수 값을 초기화 합니다.
  • ft_draw : 조건에 맞게 화면에 이미지를 출력합니다.!
  • int mlx_loop ( void *mlx_ptr ); : 그래픽 시스템은 양방향으로 한쪽에서는 스크린에 디스플레이할 필셀과 이미지 등을 명령하고 한쪽에서는 키보드나 마우스로부터 이벤트를 받을 수 있습니다. 이 이벤트를 받기 위해서는 mlx_loop를 사용해야 하며 키보드나 마우스로부터 받은 이벤트를 기다립니다. 또한 이벤트에 연결되는 사용자 정의 함수를 호출합니다. 

fractol_init.c

- int ft_mlx_init(t_fractol *f)

int	ft_mlx_init(t_fractol *f)
{
	f->mlx = mlx_init();
	if (!f->mlx)
		return (0);
	f->win = mlx_new_window(f->mlx, WIN_WIDTH, WIN_HEIGHT, TITLE(f->title));
	if (!f->win)
		return (0);
	f->img = (t_image *)malloc(sizeof(t_image));
	if (!f->img)
		return (0);
	ft_fractal_init(f);
	mlx_key_hook(f->win, ft_key_hook, f);
	mlx_mouse_hook(f->win, ft_mouse_hook, f);
	return (1);
}
  • void * mlx_init (); : mlx 초기화로 mlx를 사용하기 위해 가장 먼저 사용하는 함수입니다.
  • void mlx_new_window ( void mlx_ptr, int size_x, int size_y, char *title ); : 새 창을 스크린에 띄웁니다. size_x, size_y는 창의 크기, title은 창 타이틀바에 표시될 문구, mlx_ptr = mlx_init이 반환한 연결 식별자입니다. 
  • int mlx_key_hook(void *win_ptr, int (*f)(), void *param); : 주요 이벤트에 연결합니다. 포커스 된 창에서 키를 누를 때마다 트리거 됩니다. 초점이 맞지 않는 창은 주요 이벤트를 등록하지 않습니다.
    • win_ptr : 윈도우 인스턴스
    • (f)() : 핸들러 함수로 (int button, int x, int y, void param)와 같이 호출됩니다.
    • param : 이벤트에 제공할 매개변수입니다.
  • int mlx_mouse_hook(void *win_ptr, int (*f)(), void *param); : 마우스 이벤트에 연결합니다. 지정된 화면의 아무 곳이나 클릭할 때마다 트리거 됩니다.현재 이러한 마우스 이벤트는 거의 작동하지 않으므로 사용하지 않는 것이 좋습니다.
    • win_ptr : 윈도우 인스턴스
    • (f)() : 핸들러 함수로 (int key_code, void param)와 같이 호출됩니다.
    • param : 이벤트에 제공할 매개변수입니다.

- void ft_image_init(t_fractol *f)

void	ft_image_init(t_fractol *f)
{
	f->img->ptr = mlx_new_image(f->mlx, WIN_WIDTH, WIN_HEIGHT);
	if (!f->img->ptr)
		exit(1);
	f->img->buff = (int *)mlx_get_data_addr(f->img->ptr, \
		&f->img->bits_per_pixel, &f->img->line_length, &f->img->endian);
	if (!f->img->buff)
		exit(1);
}
  • void mlx_new_image ( void mlx_ptr, int width, int height ); : 새 이미지를 메모리에 생성시킵니다.
  • char mlx_get_data_addr ( void img_ptr, int bits_per_pixel, int size_line, int *endian );
    • img_ptr : 사용할 이미지를 지정합니다.
    • bits_per_pixel : 픽셀 색상으로 이미지 깊이라고도 합니다.
    • size_line : 이미지의 한 줄의 메모리에 저장하는 데 사용되는 바이트 수로 다른 줄로 이동하는데 필요합니다.
    • endian : 이미지의 필셀 색상을 리틀 엔디안 또는 빅 엔디안으로 저장해야하는지 알려줍니다.


profile
🐣🐥

0개의 댓글