diff --git a/includes/cub3d.h b/includes/cub3d.h index 1a816a2..686ed3b 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/21 19:24:12 by narnaud #+# #+# */ -/* Updated: 2022/11/14 16:33:15 by narnaud ### ########.fr */ +/* Updated: 2022/11/21 18:55:22 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,20 +43,20 @@ typedef struct s_rectangle { t_vec start; t_vec size; - int color; + int color; } t_rect; typedef struct s_raycast { t_dvec vec; int *cell; - double sDist[2]; - double dDist[2]; + double s_dist[2]; + double d_dist[2]; int *step; int side; int tex; double dist; - double wallX; + double wall_x; int x; } t_rc; @@ -85,15 +85,15 @@ typedef struct s_env t_img tex[5]; int minimap; int controls[KEYS_LIMIT]; - int mouseX; - int floorColor; - int ceilColor; + int mouse_x; + int floor_color; + int ceil_color; char **map; int wide; int deep; - t_dvec playerPos; - t_dvec playerDir; - t_dvec camPlan; + t_dvec player_pos; + t_dvec player_dir; + t_dvec cam_plan; } t_env; // Render @@ -115,7 +115,7 @@ int mouse_up_hook(int buton, int x, int y, t_env *env); int update_hook(t_env *env); void init_window(t_env *nv); int exit_game(t_env *env); -int init_game(t_env *env); +int init_game(t_env *env); // Utils void vec_set(t_vec *vec, int x, int y); diff --git a/src/mlx/mlx_1.c b/src/mlx/mlx_1.c index fc6987c..353a1b4 100644 --- a/src/mlx/mlx_1.c +++ b/src/mlx/mlx_1.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/08/25 14:14:46 by narnaud #+# #+# */ -/* Updated: 2022/11/14 16:28:51 by narnaud ### ########.fr */ +/* Updated: 2022/11/21 18:53:39 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -60,7 +60,7 @@ int mouse_move_hook(int x, int y, t_env *env) (void)y; dx = WIN_X_SZ / 2 - x; if (dx) - env->playerDir = dvec_rot(env->playerDir, + env->player_dir = dvec_rot(env->player_dir, (double)(-M_PI * dx / 1800.0), 1); mlx_mouse_move(env->win, WIN_X_SZ / 2, WIN_Y_SZ / 2); return (1); diff --git a/src/mlx/mlx_2.c b/src/mlx/mlx_2.c index b54c2bf..dc0e787 100644 --- a/src/mlx/mlx_2.c +++ b/src/mlx/mlx_2.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/01 13:47:30 by narnaud #+# #+# */ -/* Updated: 2022/08/25 14:18:26 by narnaud ### ########.fr */ +/* Updated: 2022/11/21 18:57:55 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,19 +44,20 @@ int update_hook(t_env *env) { t_dvec d; - d.x = ((env->controls[KEY_W] - env->controls[KEY_S]) * env->playerDir.x \ - + (env->controls[KEY_D] - env->controls[KEY_A]) * env->camPlan.x) \ + d.x = ((env->controls[KEY_W] - env->controls[KEY_S]) * env->player_dir.x \ + + (env->controls[KEY_D] - env->controls[KEY_A]) * env->cam_plan.x) \ / (20 - env->controls[KEY_SHFT] * 10); - d.y = ((env->controls[KEY_W] - env->controls[KEY_S]) * env->playerDir.y \ - + (env->controls[KEY_D] - env->controls[KEY_A]) * env->camPlan.y) \ + d.y = ((env->controls[KEY_W] - env->controls[KEY_S]) * env->player_dir.y \ + + (env->controls[KEY_D] - env->controls[KEY_A]) * env->cam_plan.y) \ / (20 - env->controls[KEY_SHFT] * 10); - 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->map[(int)env->player_pos.y][(int)(env->player_pos.x + d.x)] != '1') + dvec_set(&env->player_pos, env->player_pos.x + d.x, env->player_pos.y); + if (env->map[(int)(env->player_pos.y + d.y)][(int)env->player_pos.x] != '1') + dvec_set(&env->player_pos, env->player_pos.x, env->player_pos.y + d.y); + env->player_dir = dvec_rot(env->player_dir, + (double)(env->controls[KEY_RIGHT] - env->controls[KEY_LEFT]) + * M_PI / 180, 1); + env->cam_plan = dvec_rot(env->player_dir, M_PI / 2, 0.66); render(env); return (1); } diff --git a/src/parsing/map.c b/src/parsing/map.c index 7c250c1..270320f 100644 --- a/src/parsing/map.c +++ b/src/parsing/map.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/08/23 09:30:50 by narnaud #+# #+# */ -/* Updated: 2022/08/30 16:05:29 by narnaud ### ########.fr */ +/* Updated: 2022/11/21 19:05:12 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,16 +18,16 @@ int init_player(t_env *env, int x, int y) orientation = env->map[y][x]; env->map[y][x] = '0'; - dvec_set(&env->playerPos, x + 0.5, y + 0.5); + dvec_set(&env->player_pos, x + 0.5, y + 0.5); if (orientation == 'N') - dvec_set(&env->playerDir, 0, -1); + dvec_set(&env->player_dir, 0, -1); else if (orientation == 'S') - dvec_set(&env->playerDir, 0, 1); + dvec_set(&env->player_dir, 0, 1); else if (orientation == 'E') - dvec_set(&env->playerDir, 1, 0); + dvec_set(&env->player_dir, 1, 0); else if (orientation == 'W') - dvec_set(&env->playerDir, -1, 0); - env->camPlan = dvec_rot(env->playerDir, M_PI / 2, 0.66); + dvec_set(&env->player_dir, -1, 0); + env->cam_plan = dvec_rot(env->player_dir, M_PI / 2, 0.66); return (1); } diff --git a/src/parsing/parsing.c b/src/parsing/parsing.c index 6096c8c..b9e8751 100644 --- a/src/parsing/parsing.c +++ b/src/parsing/parsing.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/23 17:20:29 by narnaud #+# #+# */ -/* Updated: 2022/11/14 11:48:57 by narnaud ### ########.fr */ +/* Updated: 2022/11/21 19:00:21 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,12 +26,12 @@ int cleanup_datas(t_env *env) free(env->tex[3].file); if (env->tex[4].file) free(env->tex[4].file); - if (env->map) { + if (env->map) + { i = 0; while (i < env->deep) free(env->map[i++]); free(env->map); - } free(env); return (1); @@ -55,14 +55,14 @@ void register_settings(int *progress, t_env *env, char *line) else if (!env->tex[4].file && !ft_strncmp(elem[0], "DO", 3)) env->tex[4].file = ft_strtrim(elem[1], "\n"); else if (!ft_strncmp(elem[0], "F", 2)) - env->floorColor = rgb_to_int(elem + 1); + env->floor_color = rgb_to_int(elem + 1); else if (!ft_strncmp(elem[0], "C", 2)) - env->ceilColor = rgb_to_int(elem + 1); + env->ceil_color = rgb_to_int(elem + 1); else (*progress)--; (*progress)++; ft_free_split(elem); - if (env->floorColor == -1 || env->ceilColor == -1) + if (env->floor_color == -1 || env->ceil_color == -1) (*progress)--; } @@ -99,7 +99,7 @@ int is_valid_map(t_env *env) { if (!find_player(env)) printf("Error: There is no player on the map.\n"); - else if (is_in_open_room(env, env->playerPos.x, env->playerPos.y)) + else if (is_in_open_room(env, env->player_pos.x, env->player_pos.y)) printf("Error: You are using an open map.\n"); else return (1); diff --git a/src/render/minimap.c b/src/render/minimap.c index 267111c..018c6dd 100644 --- a/src/render/minimap.c +++ b/src/render/minimap.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/08/23 08:14:08 by narnaud #+# #+# */ -/* Updated: 2022/08/23 16:01:38 by narnaud ### ########.fr */ +/* Updated: 2022/11/21 19:01:36 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -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.0, \ - env->playerPos.y * delt.y - delt.y / 4.0}, \ + (t_vec){env->player_pos.x * delt.x - delt.x / 4.0, \ + env->player_pos.y * delt.y - delt.y / 4.0}, \ (t_vec){delt.x / 2, delt.y / 2}, 0xFF0000}, 0); } diff --git a/src/render/raycast.c b/src/render/raycast.c index 4c46b87..7d282ba 100644 --- a/src/render/raycast.c +++ b/src/render/raycast.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/08/23 09:27:55 by narnaud #+# #+# */ -/* Updated: 2022/08/23 16:03:32 by narnaud ### ########.fr */ +/* Updated: 2022/11/21 19:00:59 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ void check_side(t_rc *rc) { - if (rc->sDist[0] > rc->sDist[1]) + if (rc->s_dist[0] > rc->s_dist[1]) rc->side = 1; else rc->side = 0; @@ -23,21 +23,21 @@ void check_side(t_rc *rc) void init_ray(t_env *env, t_rc *rc) { if (rc->vec.x == 0) - rc->dDist[0] = 1e30; + rc->d_dist[0] = 1e30; else - rc->dDist[0] = fabs(1 / rc->vec.x); + rc->d_dist[0] = fabs(1 / rc->vec.x); if (rc->vec.y == 0) - rc->dDist[1] = 1e30; + rc->d_dist[1] = 1e30; else - rc->dDist[1] = fabs(1 / rc->vec.y); + rc->d_dist[1] = fabs(1 / rc->vec.y); if (rc->vec.x < 0 && --rc->step[0]) - rc->sDist[0] = (env->playerPos.x - rc->cell[0]) * rc->dDist[0]; + rc->s_dist[0] = (env->player_pos.x - rc->cell[0]) * rc->d_dist[0]; else if (++rc->step[0]) - rc->sDist[0] = (rc->cell[0] + 1.0 - env->playerPos.x) * rc->dDist[0]; + rc->s_dist[0] = (rc->cell[0] + 1.0 - env->player_pos.x) * rc->d_dist[0]; if (rc->vec.y < 0 && --rc->step[1]) - rc->sDist[1] = (env->playerPos.y - rc->cell[1]) * rc->dDist[1]; + rc->s_dist[1] = (env->player_pos.y - rc->cell[1]) * rc->d_dist[1]; else if (++rc->step[1]) - rc->sDist[1] = (rc->cell[1] + 1.0 - env->playerPos.y) * rc->dDist[1]; + rc->s_dist[1] = (rc->cell[1] + 1.0 - env->player_pos.y) * rc->d_dist[1]; } int ray_hit(t_env *env, t_rc *rc) @@ -53,13 +53,13 @@ int ray_hit(t_env *env, t_rc *rc) else return (0); if (!rc->side) - rc->wallX = env->playerPos.y + rc->sDist[rc->side] * rc->vec.y; + rc->wall_x = env->player_pos.y + rc->s_dist[rc->side] * rc->vec.y; else - rc->wallX = env->playerPos.x + rc->sDist[rc->side] * rc->vec.x; - rc->wallX -= floor(rc->wallX); - if (rc->tex != 4 || 2 * rc->wallX - dvec_len((t_dvec){\ - rc->cell[0] - env->playerPos.x, \ - rc->cell[1] - env->playerPos.y}) < -2) + rc->wall_x = env->player_pos.x + rc->s_dist[rc->side] * rc->vec.x; + rc->wall_x -= floor(rc->wall_x); + if (rc->tex != 4 || 2 * rc->wall_x - dvec_len((t_dvec){\ + rc->cell[0] - env->player_pos.x, \ + rc->cell[1] - env->player_pos.y}) < -2) return (1); } else diff --git a/src/render/render.c b/src/render/render.c index 2574e76..2d4ff71 100644 --- a/src/render/render.c +++ b/src/render/render.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/31 09:33:02 by narnaud #+# #+# */ -/* Updated: 2022/11/14 16:45:42 by narnaud ### ########.fr */ +/* Updated: 2022/11/21 19:04:16 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,7 +30,7 @@ void draw_wall(t_env *env, t_rc *rc, int wallHeight, int *wall_lim) double y_out; tex_height = env->tex[rc->tex].height; - tex.x = (int)(rc->wallX * (double)env->tex[rc->tex].width); + tex.x = (int)(rc->wall_x * (double)env->tex[rc->tex].width); 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; @@ -50,15 +50,15 @@ void draw_vert(t_env *env, t_rc *rc, int side) int wall_height; int wall_lim[2]; - wall_height = (int)(WIN_Y_SZ / rc->sDist[side]); + wall_height = (int)(WIN_Y_SZ / rc->s_dist[side]); if (wall_height < 0) wall_height = 0; get_wall(wall_lim, wall_lim + 1, wall_height); draw_rectangle(env, (t_rect){(t_vec){rc->x, 0}, - (t_vec){1, wall_lim[0]}, env->ceilColor}, 0); + (t_vec){1, wall_lim[0]}, env->ceil_color}, 0); draw_wall(env, rc, wall_height, wall_lim); draw_rectangle(env, (t_rect){(t_vec){rc->x, wall_lim[1]}, - (t_vec){1, WIN_Y_SZ - wall_lim[1]}, env->floorColor}, 0); + (t_vec){1, WIN_Y_SZ - wall_lim[1]}, env->floor_color}, 0); } void render_view(t_env *env) @@ -72,14 +72,14 @@ void render_view(t_env *env) { ft_bzero(&rc, sizeof(rc)); cam_x = 2 * x / (double)WIN_X_SZ - 1; - dvec_set(&rc.vec, env->playerDir.x + env->camPlan.x * cam_x, - env->playerDir.y + env->camPlan.y * cam_x); - rc.cell = (int [2]){env->playerPos.x, env->playerPos.y}; + dvec_set(&rc.vec, env->player_dir.x + env->cam_plan.x * cam_x, + env->player_dir.y + env->cam_plan.y * cam_x); + rc.cell = (int [2]){env->player_pos.x, env->player_pos.y}; rc.step = (int [2]){0, 0}; rc.x = x; init_ray(env, &rc); while (!ray_hit(env, &rc)) - rc.sDist[rc.side] += rc.dDist[rc.side]; + rc.s_dist[rc.side] += rc.d_dist[rc.side]; check_side(&rc); draw_vert(env, &rc, rc.side); x++;