Browse Source

rework of controls

master
narnaud 3 years ago
parent
commit
6ecded8d72
  1. 2
      Makefile
  2. 10
      includes/cub3d.h
  3. 53
      sources/hooks.c
  4. 80
      sources/main.c
  5. 5
      sources/render.c
  6. 8
      sources/utils.c

2
Makefile

@ -3,7 +3,7 @@ LIBFT = libft.a
MLX = includes/mlx.h
SRCS = sources/main.c sources/parsing.c sources/getline.c sources/utils.c
SRCS += sources/render.c
SRCS += sources/render.c sources/hooks.c
OBJS = ${SRCS:.c=.o}
CC = gcc

10
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/01 12:44:59 by narnaud ### ########.fr */
/* Updated: 2022/06/01 14:23:18 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -78,7 +78,8 @@ typedef struct s_env
int line_bytes;
int endian;
int *buffer;
t_control control;
int minimap;
int controls[256];
char *wallTexture[4];
int floorColor;
int ceilColor;
@ -94,9 +95,14 @@ void render_minimap(t_env *env);
void render_view(t_env *env);
void render(t_env *env);
int key_press_hook(int keycode, t_env *env);
int key_release_hook(int keycode, t_env *env);
int update_hook(t_env *env);
int rgb_to_int(char **rgb);
void set_vec(t_vec_d *vec, double x, double y);
double vec_len(t_vec_d vec);
void rot_vec(t_vec_d *vec, double rad);
t_env *parse_envFile(char *filename);
char *get_next_line(int fd);

53
sources/hooks.c

@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hooks.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/01 13:47:30 by narnaud #+# #+# */
/* Updated: 2022/06/01 14:26:26 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/cub3d.h"
int key_press_hook(int keycode, t_env *env)
{
if (keycode == KEY_M)
env->minimap = !env->minimap;
else
env->controls[keycode] = 1;
return (1);
}
int key_release_hook(int keycode, t_env *env)
{
env->controls[keycode] = 0;
return (1);
}
int update_hook(t_env *env)
{
double dX;
double dY;
dX = (env->controls[KEY_W] - env->controls[KEY_S]) * env->playerDir.x / 10;
dY = (env->controls[KEY_W] - env->controls[KEY_S]) * env->playerDir.y / 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);
rot_vec(&env->playerDir,
(double)(env->controls[KEY_D] - env->controls[KEY_A]) / 36);
rot_vec(&env->camPlan,
(double)(env->controls[KEY_D] - env->controls[KEY_A]) / 36);
if (DEBUG)
{
printf("playerPos: %f, %f. playerDir: %f, %f\n", env->playerPos.x, env->playerPos.y, env->playerDir.x, env->playerDir.y);
printf("camPlan: %f, %f\n", env->camPlan.x, env->camPlan.y);
}
render(env);
return (1);
}

80
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/01 13:04:31 by narnaud ### ########.fr */
/* Updated: 2022/06/01 13:49:31 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -27,84 +27,6 @@ void init_window(t_env *env)
env->win = window;
}
int key_press_hook(int keycode, void *param)
{
t_env *env;
env = (t_env *)param;
if (keycode == KEY_W)
env->control.up = 1;
if (keycode == KEY_S)
env->control.down = 1;
if (keycode == KEY_A)
env->control.left = 1;
if (keycode == KEY_D)
env->control.right = 1;
return (1);
}
int key_release_hook(int keycode, void *param)
{
t_env *env;
env = (t_env *)param;
if (keycode == KEY_W)
env->control.up = 0;
if (keycode == KEY_S)
env->control.down = 0;
if (keycode == KEY_A)
env->control.left = 0;
if (keycode == KEY_D)
env->control.right = 0;
return (1);
}
int update_hook(void *param)
{
t_env *env;
env = (t_env *)param;
if (env->control.up)
{
if (env->map[(int)env->playerPos.y][(int)(env->playerPos.x + env->playerDir.x / 10)] == '0')
set_vec(&env->playerPos, env->playerPos.x + env->playerDir.x / 10, env->playerPos.y);
if (env->map[(int)(env->playerPos.y + env->playerDir.y / 10)][(int)env->playerPos.x] == '0')
set_vec(&env->playerPos, env->playerPos.x, env->playerPos.y + env->playerDir.y / 10);
}
if (env->control.down)
{
if (env->map[(int)env->playerPos.y][(int)(env->playerPos.x - env->playerDir.x / 10)] == '0')
set_vec(&env->playerPos, env->playerPos.x - env->playerDir.x / 10, env->playerPos.y);
if (env->map[(int)(env->playerPos.y - env->playerDir.y / 10)][(int)env->playerPos.x] == '0')
set_vec(&env->playerPos, env->playerPos.x, env->playerPos.y - env->playerDir.y / 10);
}
if (env->control.left)
{
set_vec(&env->playerDir,
cos(-M_PI / 36) * env->playerDir.x - sin(-M_PI / 36) * env->playerDir.y,
sin(-M_PI / 36) * env->playerDir.x + cos(-M_PI / 36) * env->playerDir.y);
set_vec(&env->camPlan,
cos(-M_PI / 36) * env->camPlan.x - sin(-M_PI / 36) * env->camPlan.y,
sin(-M_PI / 36) * env->camPlan.x + cos(-M_PI / 36) * env->camPlan.y);
}
if (env->control.right)
{
set_vec(&env->playerDir,
cos(M_PI / 36) * env->playerDir.x - sin(M_PI / 36) * env->playerDir.y,
sin(M_PI / 36) * env->playerDir.x + cos(M_PI / 36) * env->playerDir.y);
set_vec(&env->camPlan,
cos(M_PI / 36) * env->camPlan.x - sin(M_PI / 36) * env->camPlan.y,
sin(M_PI / 36) * env->camPlan.x + cos(M_PI / 36) * env->camPlan.y);
}
if (DEBUG)
{
printf("playerPos: %f, %f. playerDir: %f, %f\n", env->playerPos.x, env->playerPos.y, env->playerDir.x, env->playerDir.y);
printf("camPlan: %f, %f\n", env->camPlan.x, env->camPlan.y);
}
render(env);
return (1);
}
int main(int argc, char **argv)
{
t_env *env;

5
sources/render.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/31 09:33:02 by narnaud #+# #+# */
/* Updated: 2022/06/01 12:22:39 by narnaud ### ########.fr */
/* Updated: 2022/06/01 14:06:23 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -186,7 +186,8 @@ void render(t_env *env)
&env->pixel_bits, &env->line_bytes, &env->endian);
env->line_bytes /= 4;
render_view(env);
render_minimap(env);
if (env->minimap)
render_minimap(env);
mlx_put_image_to_window(env->mlx, env->win, env->img, 0, 0);
mlx_destroy_image(env->mlx, env->img);
}

8
sources/utils.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/31 08:59:01 by narnaud #+# #+# */
/* Updated: 2022/05/31 12:22:41 by narnaud ### ########.fr */
/* Updated: 2022/06/01 14:21:41 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -23,6 +23,12 @@ void set_vec(t_vec_d *vec, double x, double y)
vec->y = y;
}
void rot_vec(t_vec_d *vec, double rad)
{
vec->x = cos(M_PI * rad) * vec->x - sin(M_PI * rad) * vec->y;
vec->y = sin(M_PI * rad) * vec->x + cos(M_PI * rad) * vec->y;
}
int rgb_to_int(char **rgb)
{
int ret;

Loading…
Cancel
Save