diff --git a/Makefile b/Makefile index 0dd6801..4a24af9 100644 --- a/Makefile +++ b/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 diff --git a/includes/cub3d.h b/includes/cub3d.h index 521bff3..43685ee 100644 --- a/includes/cub3d.h +++ b/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; diff --git a/maps/racetrack.cub b/maps/racetrack.cub index 278d32f..a69095e 100755 --- a/maps/racetrack.cub +++ b/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 diff --git a/maps/scene.cub b/maps/scene.cub index f81a050..6eb01f7 100755 --- a/maps/scene.cub +++ b/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 diff --git a/maps/scene_big.cub b/maps/scene_big.cub index 0e19ea6..ec80485 100755 --- a/maps/scene_big.cub +++ b/maps/scene_big.cub @@ -23,7 +23,6 @@ C 27, 210, 227 10000000000001 101111000010010011111111111111111111111001001 10000E00000001 100000000010010000000000000000000000000001001 10000000000001 100000000010011111111111111111111111111111001 - 12111111111110 111111111110000000000000000000000000000000001 100000001 100000000112211111111111111111111111111111101 111111001111100000001000000002 101 diff --git a/maps/simple.cub b/maps/simple.cub index f2e65a2..cd72949 100755 --- a/maps/simple.cub +++ b/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 \ No newline at end of file +1111111 diff --git a/maps/tester.cub b/maps/tester.cub index fb10246..fc6eedd 100644 --- a/maps/tester.cub +++ b/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 diff --git a/src/hooks.c b/src/hooks.c index 518641e..f57ee31 100644 --- a/src/hooks.c +++ b/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); } diff --git a/src/main.c b/src/main.c index 860ef1a..7266467 100644 --- a/src/main.c +++ b/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); diff --git a/src/parsing/parsing.c b/src/parsing/parsing.c index fb1cbac..49c8822 100644 --- a/src/parsing/parsing.c +++ b/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); } diff --git a/src/render/minimap.c b/src/render/minimap.c index db95391..267111c 100644 --- a/src/render/minimap.c +++ b/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); } diff --git a/src/render/render.c b/src/render/render.c index e09ba97..bad9083 100644 --- a/src/render/render.c +++ b/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); diff --git a/src/utils.c b/src/vectors.c similarity index 100% rename from src/utils.c rename to src/vectors.c