프로젝트 구조는 아래와 같이 구성하였며 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
#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
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);
}
int mlx_loop ( void *mlx_ptr );
: 그래픽 시스템은 양방향으로 한쪽에서는 스크린에 디스플레이할 필셀과 이미지 등을 명령하고 한쪽에서는 키보드나 마우스로부터 이벤트를 받을 수 있습니다. 이 이벤트를 받기 위해서는 mlx_loop를 사용해야 하며 키보드나 마우스로부터 받은 이벤트를 기다립니다. 또한 이벤트에 연결되는 사용자 정의 함수를 호출합니다. 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);
: 주요 이벤트에 연결합니다. 포커스 된 창에서 키를 누를 때마다 트리거 됩니다. 초점이 맞지 않는 창은 주요 이벤트를 등록하지 않습니다.int mlx_mouse_hook(void *win_ptr, int (*f)(), void *param);
: 마우스 이벤트에 연결합니다. 지정된 화면의 아무 곳이나 클릭할 때마다 트리거 됩니다.현재 이러한 마우스 이벤트는 거의 작동하지 않으므로 사용하지 않는 것이 좋습니다.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 );