/ src / mouse_bonus.c
mouse_bonus.c
 1  /* ************************************************************************** */
 2  /*                                                                            */
 3  /*                                                        :::      ::::::::   */
 4  /*   mouse.c                                            :+:      :+:    :+:   */
 5  /*                                                    +:+ +:+         +:+     */
 6  /*   By: ll-hotel <ll-hotel@student.42.fr>          +#+  +:+       +#+        */
 7  /*                                                +#+#+#+#+#+   +#+           */
 8  /*   Created: 2024/10/25 14:30:39 by ll-hotel          #+#    #+#             */
 9  /*   Updated: 2024/10/25 15:08:25 by ll-hotel         ###   ########.fr       */
10  /*                                                                            */
11  /* ************************************************************************** */
12  
13  #include "cub3D.h"
14  #include "mlx.h"
15  #include <X11/keysym.h>
16  #include <X11/X.h>
17  
18  int	mouse_handler(int button, int x, int y, t_cube *cube)
19  {
20  	if (!cube->player.use_pointer && button == Button1)
21  	{
22  		mlx_mouse_hide(cube->mlx.ptr, cube->mlx.win);
23  		cube->player.use_pointer = true;
24  	}
25  	if (cube->player.use_pointer)
26  	{
27  		cube->player.turning = 1;
28  		cube->player.mouse.x = x;
29  		cube->player.mouse.y = y;
30  		if (button == Button3)
31  		{
32  			mlx_mouse_show(cube->mlx.ptr, cube->mlx.win);
33  			cube->player.use_pointer = false;
34  			cube->player.turning = 0;
35  		}
36  	}
37  	return (0);
38  }
39  
40  float	turn_with_mouse(t_cube *cube, t_player *player)
41  {
42  	int		new_mouse_x;
43  	int		new_mouse_y;
44  	float	coef;
45  
46  	mlx_mouse_get_pos(cube->mlx.ptr, cube->mlx.win, &new_mouse_x, &new_mouse_y);
47  	coef = (player->mouse.x - new_mouse_x) * 0.002;
48  	if (new_mouse_x < SCREEN_WIDTH * 0.20 || new_mouse_x > SCREEN_WIDTH * 0.80)
49  	{
50  		mlx_mouse_move(cube->mlx.ptr, cube->mlx.win, \
51  				SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
52  		new_mouse_x = SCREEN_WIDTH / 2;
53  	}
54  	if (new_mouse_y < SCREEN_HEIGHT * 0.4 || new_mouse_y > SCREEN_HEIGHT * 0.6)
55  	{
56  		mlx_mouse_move(cube->mlx.ptr, cube->mlx.win, \
57  				new_mouse_x, SCREEN_HEIGHT / 2);
58  		new_mouse_y = SCREEN_HEIGHT / 2;
59  	}
60  	player->mouse.x = new_mouse_x;
61  	player->mouse.y = new_mouse_y;
62  	return (coef);
63  }