From 56635e703ab27ae840d279be81ca6cb0886217d8 Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Tue, 22 Nov 2022 07:14:15 +0100 Subject: [PATCH] comments --- src/main.c | 11 +++++++++-- src/mlx/mlx_1.c | 11 ++++++++++- src/mlx/mlx_2.c | 7 ++++++- src/parsing/map.c | 12 +++++++++++- src/parsing/parsing.c | 17 ++++++++++++++++- src/render/raycast.c | 13 ++++++++++++- src/render/render.c | 36 +++++++++++++++++++++++++++++++++++- 7 files changed, 99 insertions(+), 8 deletions(-) diff --git a/src/main.c b/src/main.c index fd93c49..7162cf5 100644 --- a/src/main.c +++ b/src/main.c @@ -6,12 +6,20 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/21 19:22:50 by narnaud #+# #+# */ -/* Updated: 2022/11/14 16:35:36 by narnaud ### ########.fr */ +/* Updated: 2022/11/22 06:01:26 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #include "../includes/cub3d.h" +/* Cub3d main: + * + * - Verify that map is given and have the right extension and load it. + * - If map loading failed, exit. Cleanup is handle by loader. + * - Initialize mlx window and game. + * - Setup mlx loop hook and start loop. + */ + int main(int argc, char **argv) { t_env *env; @@ -26,7 +34,6 @@ int main(int argc, char **argv) init_window(env); if (init_game(env) == EXIT_FAILURE) exit(EXIT_FAILURE); - render(env); mlx_loop_hook(env->mlx, update_hook, env); mlx_loop(env->mlx); return (EXIT_SUCCESS); diff --git a/src/mlx/mlx_1.c b/src/mlx/mlx_1.c index 353a1b4..71ffb45 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/21 18:53:39 by narnaud ### ########.fr */ +/* Updated: 2022/11/22 06:27:15 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ #ifdef __linux__ +/* mlx initialization. */ void init_window(t_env *env) { void *mlx; @@ -26,6 +27,7 @@ void init_window(t_env *env) mlx_mouse_hide(mlx, window); } +/* Change the player orientation thanks to the mouse deltas. */ int mouse_move_hook(int x, int y, t_env *env) { int dx; @@ -67,6 +69,13 @@ int mouse_move_hook(int x, int y, t_env *env) } #endif +/* The game initialization: + * + * - Open the textures for walls and doors and create the structures + * which will handle them. + * - Create the render image structure. + * - Enable mlx hooks which we will care about. + */ int init_game(t_env *env) { int y; diff --git a/src/mlx/mlx_2.c b/src/mlx/mlx_2.c index dc0e787..2e3bd18 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/11/21 18:57:55 by narnaud ### ########.fr */ +/* Updated: 2022/11/22 06:23:25 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,6 +40,11 @@ int key_release_hook(int keycode, t_env *env) return (1); } +/* The loop hook of the game: + * + * Each tick, calculate new pos and orientation of the player thanks to the + * keys pressed and recalculate the render. + */ int update_hook(t_env *env) { t_dvec d; diff --git a/src/parsing/map.c b/src/parsing/map.c index 7f0e25c..bfcbead 100644 --- a/src/parsing/map.c +++ b/src/parsing/map.c @@ -6,12 +6,15 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/08/23 09:30:50 by narnaud #+# #+# */ -/* Updated: 2022/11/22 05:33:45 by narnaud ### ########.fr */ +/* Updated: 2022/11/22 06:21:24 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #include "../../includes/cub3d.h" +/* Get the initial position and orientation of the player and + * save it into env. + */ int init_player(t_env *env, int x, int y) { char orientation; @@ -31,6 +34,7 @@ int init_player(t_env *env, int x, int y) return (1); } +/* Find the player into the map */ int find_player(t_env *env) { char **map; @@ -55,6 +59,9 @@ int find_player(t_env *env) return (0); } +/* Check all around the player if he can acces the void around the map. + * Also check if the map is not to big to avoid so later stack overflow. + */ int is_in_open_room(t_env *env, int x, int y) { static char *checked; @@ -81,6 +88,9 @@ int is_in_open_room(t_env *env, int x, int y) return (0); } +/* Convert map chained list into a string array for + * render faster. + */ char **create_map_array(t_slist *e_lst, int wide, int deep) { char **ret; diff --git a/src/parsing/parsing.c b/src/parsing/parsing.c index 7346b56..72f8e31 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/22 05:34:10 by narnaud ### ########.fr */ +/* Updated: 2022/11/22 06:54:43 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,6 +37,7 @@ int cleanup_datas(t_env *env) return (1); } +/* Read a setting line of the map file and add setting to the env.*/ void register_settings(int *progress, t_env *env, char *line) { char **elem; @@ -66,6 +67,7 @@ void register_settings(int *progress, t_env *env, char *line) (*progress)--; } +/* Read a map line of the map file and a return chained list element. */ t_slist *read_map_line(t_env *env, char *line) { t_slist *ret; @@ -104,6 +106,19 @@ int is_valid_map(t_env *env) return (0); } +/* Map loader: + * + * - open map file and read it line by line. + * - For each line: + * - If line is recognize as a map line and all others settings are known, + * add line to e_map for later proccess, + * - else if line isn't commented, parse the line for the setting it contain. + * - Then if a setting or the map is missing, cleanup and say it, + * - else, save the map into env->map as a string array. + * - Then, check if the map is valid (not open, not too big, and with spawn) + * and cleanup + */ + t_env *load_map(char *filename) { int fd; diff --git a/src/render/raycast.c b/src/render/raycast.c index 7d282ba..3552ded 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/11/21 19:00:59 by narnaud ### ########.fr */ +/* Updated: 2022/11/22 07:06:38 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,6 +40,17 @@ void init_ray(t_env *env, t_rc *rc) rc->s_dist[1] = (rc->cell[1] + 1.0 - env->player_pos.y) * rc->d_dist[1]; } +/* Let the ray go to the next cell he will encounter and check if it is a wall. + * + * - First check the closer side to look for (x direction or y). That's is + * given by the lower side distance, + * - Progress to next closer cell so, + * - If the cell is not a floor: + * - if it's a wall, set the texture corresponding and return true, + * - if it's a door, verify that the wall must be draw and set + * texture and return accordingly. + * - else continue. + */ int ray_hit(t_env *env, t_rc *rc) { check_side(rc); diff --git a/src/render/render.c b/src/render/render.c index 6c8c19f..aeb642a 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/22 00:06:15 by narnaud ### ########.fr */ +/* Updated: 2022/11/22 07:13:31 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,15 @@ void get_wall(int *startY, int *endY, int height) *endY = WIN_Y_SZ - 1; } +/* Draw the wall/door using texture for it. + * + * First calculate the horizontal coordinate of the wall texture which + * was hit and his height. + * Reverse coordinate if the raycas was moving with negative direciton. + * Init step and screen Y initial coordinate. + * For each vertical pixel which must be drawn on screen, + * draw the corresponding texture pixel. + */ void draw_wall(t_env *env, t_rc *rc, int wallHeight, int *wall_lim) { int tex_height; @@ -45,6 +54,12 @@ void draw_wall(t_env *env, t_rc *rc, int wallHeight, int *wall_lim) } } +/* Draw the vertical thanks to raycast datas. + * The wall/door size is the portionaly inverse to the distance to + * the player he is. + * Cap wall drawing to the borders of the window. + * Draw the ceil part, the wall and the floor. + */ void draw_vert(t_env *env, t_rc *rc, int side) { int wall_height; @@ -61,6 +76,18 @@ void draw_vert(t_env *env, t_rc *rc, int side) (t_vec){1, WIN_Y_SZ - wall_lim[1]}, env->floor_color}, 0); } +/* Calculate player point of view: + * + * - For each vertical along the window: + * - Calculate X coordinate with center of screen as origin. + * - Initialize raycasting datas: + * - calculate unit vector for the ray corresponding to this vertical, + * - set the step vector to 0 an save the x to not reverse calculate it, + * - finish initialization with initials deltas and sides distances. + * - Shot the ray until he hit a wall or a door. + * - Check which orentation the hit point have. + * - Draw the vertical + */ void render_view(t_env *env) { t_rc rc; @@ -86,6 +113,13 @@ void render_view(t_env *env) } } +/* Generate the render for the actual tick. + * + * - Erase the previous render. + * - Calculate player point of view. + * - If minimap enabled, generate it. (minimap.c) + * - Put render in window. + */ void render(t_env *env) { ft_bzero(env->buffer, WIN_Y_SZ * WIN_X_SZ * sizeof(int));