Browse Source

norm

master
narnaud 2 years ago
parent
commit
e809d71aba
  1. 4
      Makefile
  2. 6
      includes/cub3d.h
  3. 1
      maps/racetrack.cub
  4. 1
      maps/scene.cub
  5. 1
      maps/scene_big.cub
  6. 3
      maps/simple.cub
  7. 1
      maps/tester.cub
  8. 33
      src/hooks.c
  9. 22
      src/main.c
  10. 37
      src/parsing/parsing.c
  11. 6
      src/render/minimap.c
  12. 2
      src/render/render.c
  13. 0
      src/vectors.c

4
Makefile

@ -3,7 +3,7 @@ LIBFT = libft.a
MLX = includes/mlx.h
SRCS = src/main.c src/hooks.c src/utils.c
SRCS = src/main.c src/hooks.c src/vectors.c
SRCS += src/parsing/parsing.c src/parsing/getline.c src/parsing/map.c src/parsing/utils.c
SRCS += src/render/render.c src/render/minimap.c src/render/utils.c src/render/raycast.c
OBJS = ${SRCS:.c=.o}
@ -16,7 +16,7 @@ ifeq ($(UNAME_S), Linux)
LFLAGS = -L ./mlx -lmlx_Linux -lXext -lX11 -lm -lz -L. -lft
endif
ifeq ($(UNAME_S), Darwin)
LFLAGS = -framework OpenGL -framework AppKit -L. -lft -lmlxmac
LFLAGS = -L ./mlx -lmlx -framework OpenGL -framework AppKit -L. -lft
endif
CFLAGS = -Werror -Wall -Wextra -O3 -ffast-math -funsafe-math-optimizations

6
includes/cub3d.h

@ -13,10 +13,6 @@
#ifndef CUB3D_H
# define CUB3D_H
# ifndef DEBUG
# define DEBUG 0
# endif
# define WIN_X_SZ 1760
# define WIN_Y_SZ 900
# define BUFFER_SIZE 4096
@ -83,8 +79,6 @@ typedef struct s_img
typedef struct s_env
{
long tick;
int debug;
void *mlx;
void *win;
void *img;

1
maps/racetrack.cub

@ -2,6 +2,7 @@ NO ./textures/wood.xpm
EA ./textures/colorstone.xpm
SO ./textures/wood.xpm
WE ./textures/wood.xpm
DO ./textures/wood.xpm
F 18 ,176, 12
C 27, 210, 227

1
maps/scene.cub

@ -2,6 +2,7 @@ NO ./textures/wood.xpm
EA ./textures/colorstone.xpm
SO ./textures/redbrick.xpm
WE ./textures/greystone.xpm
DO ./textures/wood.xpm
F 100, 70, 20
C 27, 210, 227

1
maps/scene_big.cub

@ -23,7 +23,6 @@ C 27, 210, 227
10000000000001 101111000010010011111111111111111111111001001
10000E00000001 100000000010010000000000000000000000000001001
10000000000001 100000000010011111111111111111111111111111001
12111111111110 111111111110000000000000000000000000000000001
100000001 100000000112211111111111111111111111111111101
111111001111100000001000000002 101

3
maps/simple.cub

@ -2,6 +2,7 @@ NO ./textures/redbrick.xpm
EA ./textures/colorstone.xpm
SO ./textures/redbrick.xpm
WE ./textures/redbrick.xpm
DO ./textures/wood.xpm
F 147, 159, 153
C 219, 213, 213
@ -9,4 +10,4 @@ C 219, 213, 213
1111111
100N001
1000001
1111111
1111111

1
maps/tester.cub

@ -2,6 +2,7 @@ NO ./textures/wood.xpm
EA ./textures/colorstone.xpm
SO ./textures/wood.xpm
WE ./textures/wood.xpm
DO ./textures/wood.xpm
F 0, 156, 8
C 27, 210, 227

33
src/hooks.c

@ -16,14 +16,10 @@ int key_press_hook(int keycode, t_env *env)
{
if (keycode == KEY_M)
env->minimap = !env->minimap;
else if (keycode == KEY_F1)
env->debug = !env->debug;
else if (keycode == KEY_ECHAP)
exit(EXIT_SUCCESS);
else
env->controls[keycode] = 1;
if (env->debug)
printf("Key pressed: %d\n", keycode);
return (1);
}
@ -42,36 +38,27 @@ int mouse_move_hook(int x, int y, t_env *env)
if (dx)
env->playerDir = dvec_rot(env->playerDir,
(double)(-M_PI * dx / 1800.0), 1);
mlx_mouse_move(env->win, WIN_X_SZ / 2, WIN_Y_SZ / 2);
mlx_mouse_move(env->mlx, env->win, WIN_X_SZ / 2, WIN_Y_SZ / 2);
return (1);
}
int update_hook(t_env *env)
{
double dx;
double dy;
t_dvec d;
env->tick++;
dx = ((env->controls[KEY_W] - env->controls[KEY_S]) * env->playerDir.x \
d.x = ((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 \
d.y = ((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)] != '1')
dvec_set(&env->playerPos, env->playerPos.x + dx, env->playerPos.y);
if (env->map[(int)(env->playerPos.y + dy)][(int)env->playerPos.x] != '1')
dvec_set(&env->playerPos, env->playerPos.x, env->playerPos.y + dy);
env->playerDir = dvec_rot(env->playerDir, \
(double)(env->controls[KEY_RIGHT] - env->controls[KEY_LEFT]) * M_PI / 72, 1);
if (env->map[(int)env->playerPos.y][(int)(env->playerPos.x + d.x)] != '1')
dvec_set(&env->playerPos, env->playerPos.x + d.x, env->playerPos.y);
if (env->map[(int)(env->playerPos.y + d.y)][(int)env->playerPos.x] != '1')
dvec_set(&env->playerPos, env->playerPos.x, env->playerPos.y + d.y);
env->playerDir = dvec_rot(env->playerDir, (double)(env->controls[KEY_RIGHT]
- env->controls[KEY_LEFT]) * M_PI / 180, 1);
env->camPlan = dvec_rot(env->playerDir, M_PI / 2, 0.66);
if (env->debug && !(env->tick % 50))
{
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);
}

22
src/main.c

@ -21,9 +21,9 @@ void init_window(t_env *env)
mlx = mlx_init();
env->mlx = mlx;
window = mlx_new_window(mlx, WIN_X_SZ, WIN_Y_SZ, "FdF");
window = mlx_new_window(mlx, WIN_X_SZ, WIN_Y_SZ, "Cub3D");
env->win = window;
mlx_mouse_hide();
mlx_mouse_hide(mlx, window);
}
void init_game(t_env *env)
@ -51,23 +51,6 @@ void init_game(t_env *env)
mlx_hook(env->win, 6, 0L, mouse_move_hook, env);
}
void debug(t_env *env)
{
int y;
if (!DEBUG)
return ;
y = 0;
printf("\e[1;32m========> WALLS <========\e[0m\nNorth: %s\nSouth: %s\nWest: %s\nEast: %s\n",\
env->tex[0].file, env->tex[1].file,\
env->tex[2].file, env->tex[3].file);
printf("\e[1;32m========================\e[0m\n");
printf("=> Floor color: %d\n=> Ceil color: %d\n", env->floorColor, env->ceilColor);
printf("\e[1;32m========> MAP <========\e[0m\n");
while (env->map[y])
printf("%s\n", env->map[y++]);
}
int main(int argc, char **argv)
{
t_env *env;
@ -77,7 +60,6 @@ int main(int argc, char **argv)
env = parse_file(argv[1]);
if (!env)
return (EXIT_FAILURE);
debug(env);
init_window(env);
init_game(env);
render(env);

37
src/parsing/parsing.c

@ -83,21 +83,26 @@ t_slist *read_map_line(t_env *env, char *line)
return (ret);
}
int is_valid(t_env *env)
{
if (!find_player(env)
|| is_in_open_room(env, env->playerPos.x, env->playerPos.y))
{
printf("Error: You are using an invalid map.\n");
return (0);
}
return (1);
}
t_env *parse_file(char *filename)
{
int fd;
char *line;
int progress;
t_slist *e_map;
t_env *ret;
int fd;
char *line;
static int progress;
static t_slist *e_map;
t_env *ret;
progress = 0;
e_map = NULL;
ret = ft_calloc(1, sizeof(t_env));
ret->deep = 0;
ret->wide = 0;
ret->mouseX = 0;
ret->debug = DEBUG;
fd = open(filename, O_RDONLY);
line = get_next_line(fd);
while (line)
@ -111,14 +116,8 @@ t_env *parse_file(char *filename)
}
if (progress < 7 && cleanup_datas(ret))
return (NULL);
else
ret->map = create_map_array(e_map, ret->wide, ret->deep);
if ((!find_player(ret) \
|| is_in_open_room(ret, ret->playerPos.x, ret->playerPos.y)) \
&& cleanup_datas(ret))
{
printf("Error: You are using an open map.");
ret->map = create_map_array(e_map, ret->wide, ret->deep);
if (!is_valid(ret) && cleanup_datas(ret))
return (NULL);
}
return (ret);
}

6
src/render/minimap.c

@ -47,7 +47,7 @@ void render_minimap(t_env *env, t_vec size)
pos.y++;
}
draw_rectangle(env, (t_rect){\
(t_vec){env->playerPos.x * delt.x - delt.x / 4, \
env->playerPos.y * delt.y - delt.y / 4}, \
(t_vec){delt.x / 2, delt.y / 2}, 0x0000FF}, 0);
(t_vec){env->playerPos.x * delt.x - delt.x / 4.0, \
env->playerPos.y * delt.y - delt.y / 4.0}, \
(t_vec){delt.x / 2, delt.y / 2}, 0xFF0000}, 0);
}

2
src/render/render.c

@ -34,7 +34,7 @@ void draw_wall(t_env *env, t_rc *rc, int wallHeight, int *wall_lim)
if ((!rc->side && rc->vec.x > 0) || (rc->side && rc->vec.y < 0))
tex.x = env->tex[rc->tex].width - tex.x - 1;
step = 1.0 * tex_height / wallHeight;
y_out = (wall_lim[0] - WIN_Y_SZ / 2 + wallHeight / 2) * step;
y_out = (wall_lim[0] - WIN_Y_SZ / 2.0 + wallHeight / 2.0) * step;
while (wall_lim[0] < wall_lim[1])
{
tex.y = (int)y_out & (tex_height - 1);

0
src/utils.c → src/vectors.c

Loading…
Cancel
Save