add mouse movement

This commit is contained in:
ReverseSky 2024-11-06 15:21:24 +01:00
parent d193b99c56
commit 0396180cd8
5 changed files with 57 additions and 9 deletions

22
algo.c
View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* algo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <grobledo@student.42.fr> +#+ +:+ +#+ */
/* By: greg <greg@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/01 16:24:58 by grobledo #+# #+# */
/* Updated: 2024/10/29 15:28:25 by grobledo ### ########.fr */
/* Updated: 2024/11/06 15:16:02 by greg ### ########.fr */
/* */
/* ************************************************************************** */
@ -77,6 +77,23 @@ static int loop_hook(void *param)
return (0);
}
// static int rotate( int dx)
// {
// g_map.player.rot += PI / ROT_SPEED_DIVIDE_PI * dx;
// return (0);
// }
int mouse_move(int x, int y)
{
(void)y;
int dx = x - g_player.old_mouse_x; // Calcul de la différence de mouvement
if (dx != 0) {
rotate(dx);
g_player.old_mouse_x = x; // Mise à jour de la position précédente
}
return (0);
}
int main(int argc, char *argv[])
{
g_mlx = mlx_init();
@ -92,6 +109,7 @@ int main(int argc, char *argv[])
mlx_hook(g_win, KeyPress, KeyPressMask, keypress, NULL);
mlx_hook(g_win, KeyRelease, KeyReleaseMask, keyrelease, NULL);
mlx_hook(g_win, 17, 1L<<17, ft_exit, NULL);
mlx_hook(g_win, 6, 1L<< 6, mouse_move, NULL);
mlx_loop_hook(g_mlx, loop_hook, NULL);
draw_screen();
mlx_loop(g_mlx);

8
algo.h
View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* algo.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <grobledo@student.42.fr> +#+ +:+ +#+ */
/* By: greg <greg@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/30 15:45:59 by grobledo #+# #+# */
/* Updated: 2024/10/29 15:29:30 by grobledo ### ########.fr */
/* Updated: 2024/11/06 15:18:45 by greg ### ########.fr */
/* */
/* ************************************************************************** */
@ -87,4 +87,8 @@ void draw_wall(t_tex *tex, t_ray *ray, const int line_height, int x,
int y, u_int32_t *img_data);
void clean_img(t_tex *tex);
int rotate(int factor);
int mouse_move(int x, int y);
#endif

6
map.h
View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* map.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <mcolonna@student.42perpignan.fr +#+ +:+ +#+ */
/* By: greg <greg@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/01 13:59:04 by mcolonna #+# #+# */
/* Updated: 2024/10/16 17:59:12 by mcolonna ### ########.fr */
/* Updated: 2024/11/06 14:48:12 by greg ### ########.fr */
/* */
/* ************************************************************************** */
@ -67,6 +67,8 @@ typedef struct s_player
t_point_double pos;
// player rotation (rad)
double rot;
int old_mouse_x;
} t_player;
/// @brief Represents a map.

6
move.c
View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* move.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* By: greg <greg@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 12:47:34 by mcolonna #+# #+# */
/* Updated: 2024/10/17 15:06:43 by mcolonna ### ########.fr */
/* Updated: 2024/11/06 15:17:15 by greg ### ########.fr */
/* */
/* ************************************************************************** */
@ -59,7 +59,7 @@ static int move_forward(int factor)
return (0);
}
static int rotate(int factor)
int rotate(int factor)
{
g_map.player.rot += PI / ROT_SPEED_DIVIDE_PI * factor;
return (0);

24
move2.c Normal file
View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* move2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: greg <greg@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/06 15:18:21 by greg #+# #+# */
/* Updated: 2024/11/06 15:18:34 by greg ### ########.fr */
/* */
/* ************************************************************************** */
#include "algo.h"
int mouse_move(int x, int y)
{
(void)y;
int dx = x - g_player.old_mouse_x; // Calcul de la différence de mouvement
if (dx != 0) {
rotate(dx);
g_player.old_mouse_x = x; // Mise à jour de la position précédente
}
return (0);
}