add rotate with mouse

This commit is contained in:
mcolonna 2024-11-07 18:03:38 +01:00
parent a2a0a91495
commit f77a4446b6
5 changed files with 32 additions and 8 deletions

View file

@ -6,7 +6,7 @@
/* By: mc <mc@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/31 17:00:59 by mc #+# #+# */
/* Updated: 2024/10/31 18:15:13 by mc ### ########.fr */
/* Updated: 2024/11/07 17:10:50 by mc ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,6 +19,7 @@
# define MOVE_SPEED 0.05 // Player oves by N cases by tick
# define ROT_SPEED_DIVIDE_PI 64 // Player turns by pi/N rad by tick
# define MOUSE_ROTATION_SPEED 0.001 // rad/px
# define HITBOX 0.25 // Hitbox of N cases around player.
# define COLOR_CEILING 0x29f8ff // TODO get from cub file

View file

@ -6,7 +6,7 @@
/* By: mc <mc@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 14:43:09 by mcolonna #+# #+# */
/* Updated: 2024/11/07 14:26:28 by mc ### ########.fr */
/* Updated: 2024/11/07 17:13:28 by mc ### ########.fr */
/* */
/* ************************************************************************** */
@ -23,6 +23,7 @@ typedef struct s_input_actions
bool up;
bool down;
bool quit;
int rotate;
} t_input_actions;
/// @brief Modified by the input of the user. A value becomes true
@ -43,7 +44,8 @@ int hook_keyrelease(int keycode);
/// @brief To handle the input of a window.
///
/// @param mlx_ptr mlx connection identifier
/// @param win_ptr Reference to the window to use.
void input_init(void *win_ptr);
void input_init(void *mlx_ptr, void *win_ptr);
#endif

View file

@ -6,11 +6,13 @@
/* By: mc <mc@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 14:43:36 by mcolonna #+# #+# */
/* Updated: 2024/11/07 14:25:46 by mc ### ########.fr */
/* Updated: 2024/11/07 18:03:00 by mc ### ########.fr */
/* */
/* ************************************************************************** */
#include "input.h"
#include "global.h"
#include "const.h"
t_input_actions g_input_actions;
@ -47,10 +49,27 @@ int hook_keyrelease(int keycode)
return (0);
}
void input_init(void *win_ptr)
static int hook_mousemove(int x, int y)
{
static const t_point_int window_middle = {
SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2};
static bool first_call = true;
const int dx = x - window_middle.x;
if (!first_call && dx)
g_input_actions.rotate += dx;
first_call = false;
if (x != window_middle.x || y != window_middle.y)
mlx_mouse_move(g_mlx, g_win, window_middle.x, window_middle.y);
return (0);
}
void input_init(void *mlx_ptr, void *win_ptr)
{
ft_memset(&g_input_actions, 0, sizeof(g_input_actions));
mlx_hook(win_ptr, DestroyNotify, StructureNotifyMask, set_quit, NULL);
mlx_hook(win_ptr, KeyPress, KeyPressMask, hook_keypress, NULL);
mlx_hook(win_ptr, KeyRelease, KeyReleaseMask, hook_keyrelease, NULL);
mlx_hook(win_ptr, MotionNotify, PointerMotionMask, hook_mousemove, NULL);
mlx_mouse_hide(mlx_ptr, win_ptr);
}

View file

@ -6,7 +6,7 @@
/* By: mc <mc@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/31 16:57:40 by mc #+# #+# */
/* Updated: 2024/11/07 14:52:00 by mc ### ########.fr */
/* Updated: 2024/11/07 17:12:44 by mc ### ########.fr */
/* */
/* ************************************************************************** */
@ -67,7 +67,7 @@ int main(int argc, char *argv[])
else
{
g_win = mlx_new_window(g_mlx, SCREEN_WIDTH, SCREEN_HEIGHT, "cub3d");
input_init(g_win);
input_init(g_mlx, g_win);
mlx_loop_hook(g_mlx, loop_hook, NULL);
draw_screen();
mlx_loop(g_mlx);

View file

@ -6,7 +6,7 @@
/* By: mc <mc@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 12:47:34 by mcolonna #+# #+# */
/* Updated: 2024/10/31 17:58:27 by mc ### ########.fr */
/* Updated: 2024/11/07 17:09:31 by mc ### ########.fr */
/* */
/* ************************************************************************** */
@ -82,4 +82,6 @@ void move(void)
rotate(+1);
if (g_input_actions.left && !g_input_actions.right)
rotate(-1);
g_map.player.rot += (double)g_input_actions.rotate * MOUSE_ROTATION_SPEED;
g_input_actions.rotate = 0;
}