/ miniRT / practice / miniRT_practice / key_hook.c
key_hook.c
 1  /* ************************************************************************** */
 2  /*                                                                            */
 3  /*                                                        :::      ::::::::   */
 4  /*   key_hook.c                                         :+:      :+:    :+:   */
 5  /*                                                    +:+ +:+         +:+     */
 6  /*   By: salee2 <salee2@student.42seoul.kr>         +#+  +:+       +#+        */
 7  /*                                                +#+#+#+#+#+   +#+           */
 8  /*   Created: 2023/06/02 17:45:02 by salee2            #+#    #+#             */
 9  /*   Updated: 2023/06/02 17:45:04 by salee2           ###   ########.fr       */
10  /*                                                                            */
11  /* ************************************************************************** */
12  
13  #include "include/minirt.h"
14  
15  void	exit_minirt(t_vars *vars)
16  {
17  	mlx_destroy_window(vars->mlx, vars->win);
18  	free_scene(vars->scene);
19  	exit(0);
20  }
21  
22  int	key_hook(int keycode, t_vars *vars)
23  {
24  	int			i;
25  	const int	dir_to_move_key[DIR_SIZE] = {LEFT_ARROW, RIGHT_ARROW, \
26  	DOWN_ARROW, UP_ARROW, DOWN_NUM, UP_NUM};
27  	const int	dir_to_rotate_key[DIR_SIZE] = {A, D, S, W, V, T};
28  
29  	if (keycode == ESC)
30  		exit_minirt(vars);
31  	else
32  	{
33  		i = 0;
34  		while (i < DIR_SIZE)
35  		{
36  			if (keycode == dir_to_move_key[i])
37  				move_camera(keycode, vars, dir_to_move_key);
38  			if (keycode == dir_to_rotate_key[i])
39  				rotate_camera(keycode, vars, dir_to_rotate_key);
40  			++i;
41  		}
42  	}
43  	return (0);
44  }