Browse Source

add: mouse handle

master
narnaud 3 years ago
parent
commit
68f45eb058
  1. 6
      includes/cub3d.h
  2. 42
      sources/hooks.c
  3. 5
      sources/main.c
  4. 3
      sources/parsing.c

6
includes/cub3d.h

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/21 19:24:12 by narnaud #+# #+# */
/* Updated: 2022/06/02 14:22:08 by narnaud ### ########.fr */
/* Updated: 2022/06/02 17:24:53 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -100,6 +100,7 @@ typedef struct s_env
t_img wall[4];
int minimap;
int controls[KEYS_LIMIT];
int mouseX;
int floorColor;
int ceilColor;
char **map;
@ -116,6 +117,9 @@ void render(t_env *env);
int key_press_hook(int keycode, t_env *env);
int key_release_hook(int keycode, t_env *env);
int mouse_down_hook(int buton, int x, int y, t_env *env);
int mouse_move_hook(int x, int y, t_env *env);
int mouse_up_hook(int buton, int x, int y, t_env *env);
int update_hook(t_env *env);
int rgb_to_int(char **rgb);

42
sources/hooks.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/01 13:47:30 by narnaud #+# #+# */
/* Updated: 2022/06/02 08:19:26 by narnaud ### ########.fr */
/* Updated: 2022/06/02 17:50:36 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -31,22 +31,56 @@ int key_release_hook(int keycode, t_env *env)
return (1);
}
int mouse_move_hook(int x, int y, t_env *env)
{
(void)y;
if (env->mouseX)
{
if (env->mouseX - x > 0)
env->playerDir = rot_vec(env->playerDir, (double)( -M_PI / 90), 1);
else if (env->mouseX - x < 0)
env->playerDir = rot_vec(env->playerDir, (double)( M_PI / 90), 1);
env->mouseX = x;
}
return (1);
}
int mouse_down_hook(int buton, int x, int y, t_env *env)
{
(void)buton;
(void)y;
if (!env->mouseX)
env->mouseX = x;
return (1);
}
int mouse_up_hook(int buton, int x, int y, t_env *env)
{
(void)buton;
(void)x;
(void)y;
env->mouseX = 0;
return (1);
}
int update_hook(t_env *env)
{
double dX;
double dY;
env->tick++;
dX = (env->controls[KEY_W] - env->controls[KEY_S]) * env->playerDir.x
dX = ((env->controls[KEY_W] - env->controls[KEY_S]) * env->playerDir.x
+ (env->controls[KEY_D] - env->controls[KEY_A]) * env->camPlan.x )
/ (20 - env->controls[KEY_SHFT] * 10);
dY = (env->controls[KEY_W] - env->controls[KEY_S]) * env->playerDir.y
dY = ((env->controls[KEY_W] - env->controls[KEY_S]) * env->playerDir.y
+ (env->controls[KEY_D] - env->controls[KEY_A]) * env->camPlan.y)
/ (20 - env->controls[KEY_SHFT] * 10);
if (env->map[(int)env->playerPos.y][(int)(env->playerPos.x + dX)] == '0')
set_vec(&env->playerPos, env->playerPos.x + dX, env->playerPos.y);
if (env->map[(int)(env->playerPos.y + dY)][(int)env->playerPos.x] == '0')
set_vec(&env->playerPos, env->playerPos.x, env->playerPos.y + dY);
env->playerDir = rot_vec(env->playerDir,
(double)(env->controls[KEY_D] - env->controls[KEY_A]) * M_PI / 72, 1);
(double)(env->controls[KEY_E] - env->controls[KEY_Q]) * M_PI / 72, 1);
env->camPlan = rot_vec(env->playerDir, M_PI / 2, 0.66);
if (env->debug && !(env->tick % 50))
{

5
sources/main.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/21 19:22:50 by narnaud #+# #+# */
/* Updated: 2022/06/02 15:31:02 by narnaud ### ########.fr */
/* Updated: 2022/06/02 17:35:19 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -67,6 +67,9 @@ int main(int argc, char **argv)
render(env);
mlx_hook(env->win, 2, 1L<<0, key_press_hook, env);
mlx_hook(env->win, 3, 1L<<1, key_release_hook, env);
mlx_hook(env->win, 4, 0L, mouse_down_hook, env);
mlx_hook(env->win, 5, 0L, mouse_up_hook, env);
mlx_hook(env->win, 6, 0L, mouse_move_hook, env);
mlx_loop_hook(env->mlx, update_hook, env);
mlx_loop(env->mlx);
mlx_destroy_image(env->mlx, env->img);

3
sources/parsing.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/23 17:20:29 by narnaud #+# #+# */
/* Updated: 2022/06/02 12:34:56 by narnaud ### ########.fr */
/* Updated: 2022/06/02 17:30:35 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -188,6 +188,7 @@ t_env *parse_envFile(char *filename)
ret = ft_calloc(1, sizeof(t_env));
ret->deep = 0;
ret->wide = 0;
// ret->mouseX = 0;
fd = open(filename, O_RDONLY);
line = get_next_line(fd);
while (line)

Loading…
Cancel
Save