1. 클릭으로 방향 바꾸기
첫번째 방식으로 채택한 방법.
42 docs의 mlx 관련 문서를 뒤져보면서 키값을 찾는다.
c
typedef enum e_event
{
MOUSE_LEFT_BUTTON=1,
MOUSE_RIGHT_BUTTON=2,
} t_event;
main() 안에는 mlx_mouse_hook()이라는 함수를 넣어줬다.
c
int main()
{
...
mlx_mouse_hook(tools->win_ptr, &ft_set_mouse, tools);
...
}
ft_set_mouse()라는 함수로 hook이 걸릴때의 실제 동작을 수행한다.
c
int main()
{
...
mlx_mouse_hook(tools->win_ptr, &ft_set_mouse, tools);
...
}
c
int ft_set_mouse(int button, int x, int y, t_box *tools)
{
if (0 < x && x < WIDTH && 0 < y && y < HEIGHT)
{
if (button == MOUSE_LEFT_BUTTON)
tools->alpha = -(M_PI / 36);
if (button == MOUSE_RIGHT_BUTTON)
tools->alpha = (M_PI / 36);
matrix_product(&(tools->dir), tools->alpha);
matrix_product(&(tools->camera), tools->alpha);
}
return (0);
}
mlx_mouse_hook()은 마우스의 클릭과 관련이 있다.
여기서 나오는 x, y 값은 마우스를 클릭하거나 스크롤했을 때 값이 뜬다.
2. 특정 위치에 가면 방향 바꾸기
좀 더 현실 FPS에 가까운 방식이라고 할 수 있다.
앞의 회전변환 & 이동 (2번째 방식)과 연관성이 있다.
기본적으로 특정 키를 누르면 마우스의 켜짐/꺼짐을 전환한다.
c
int ft_key_press(int keycode, t_box *tools)
{
if (keycode == MOUSE)
ft_set_mouse(tools);
ft_event(tools);
return (0);
}
int ft_set_mouse(t_box *tools)
{
if (tools->mouse_on == 0)
{
mlx_mouse_get_pos(tools->win_ptr, &tools->mouse.x, &tools->mouse.y);
mlx_mouse_move(tools->win_ptr, WIDTH / 2, HEIGHT / 2);
tools->mouse_on = 1;
}
else
tools->mouse_on = 0;
return (0);
}
이후 마우스의 움직임을 mlx_loop_hook()의 함수 파라미터와 연결한다.
c
int ft_event(t_box *tools)
{
mlx_mouse_get_pos(tools->win_ptr, &tools->mouse.x, &tools->mouse.y);
ft_move_mouse(tools->mouse.x, tools->mouse.y, tools);
drawing(tools);
return (0);
}
mlx_mouse_get_pos()를 이용하면 마우스의 위치 그 자체를 얻을 수 있다.
ft_move_mouse()는 다음과 같이 구성한다.
c
int ft_move_mouse(int x, int y, t_box *tools)
{
if (tools->mouse_on == 1 && \
HEIGHT / 2 - 150 < y && y < HEIGHT / 2 + 150)
{
if (0 <= x && x <= 150)
tools->alpha = -(M_PI / 72);
else if (WIDTH - 150 <= x && x <= WIDTH)
tools->alpha = (M_PI / 72);
else
tools->alpha = 0;
matrix_product(&(tools->dir), tools->alpha);
matrix_product(&(tools->camera), tools->alpha);
}
else
return (0);
return (0);
}