diff --git a/Cub3D 2 b/Cub3D 2 deleted file mode 100755 index b4309a9..0000000 Binary files a/Cub3D 2 and /dev/null differ diff --git a/includes/cub3d.h b/includes/cub3d.h index 1c17140..0b824e5 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/30 16:30:26 by rparodi #+# #+# */ -/* Updated: 2024/11/28 15:15:31 by bgoulard ### ########.fr */ +/* Updated: 2024/11/29 16:41:34 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,7 @@ # include // utils +void dump_info(t_info *info); int render_frame(t_info *data); void my_mlx_pixel_put(t_info *data, int x, int y, int color); double deg2rad(int deg); diff --git a/includes/cub3d_struct.h b/includes/cub3d_struct.h index 5f635ff..7cc9b62 100644 --- a/includes/cub3d_struct.h +++ b/includes/cub3d_struct.h @@ -6,7 +6,7 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/28 13:53:54 by bgoulard #+# #+# */ -/* Updated: 2024/11/28 15:36:36 by bgoulard ### ########.fr */ +/* Updated: 2024/11/29 17:07:00 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,9 @@ # define FILE_EXTENSION_LEN 4 # define BG_CLG 0 # define BG_FLR 1 -#define TILE_SIZE 64 +# define TILE_SIZE 64 +# define WIN_COEF .5 +# define WIN_TITLE "Cub3D" // defines that should be mooved to a config option / file #define FOV 70 @@ -72,21 +74,16 @@ typedef enum e_tile WALL = 1, } t_tile_type; -typedef union u_tile -{ - int raw_tile; - struct { - unsigned int tile_visited: 1; // parsing - unsigned int other: 27; // disponible - unsigned int tile_type: 4; // 16 tile types possible - }; +typedef struct s_tile { + bool tile_visited; // parsing + unsigned int other; // disponible + unsigned int tile_type; // 16 tile types possible } t_tile; typedef struct s_map { int fd; char *path; - t_dpoint player_pos; t_ipoint size; t_tile *map; char **fraw; diff --git a/mlx_layer/hooks.c b/mlx_layer/hooks.c index 7dcd7d5..20992c8 100644 --- a/mlx_layer/hooks.c +++ b/mlx_layer/hooks.c @@ -6,7 +6,7 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/28 14:12:25 by bgoulard #+# #+# */ -/* Updated: 2024/11/28 14:57:01 by bgoulard ### ########.fr */ +/* Updated: 2024/11/29 17:00:33 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,9 +16,11 @@ #include "mlx_functions.h" #include +#include int key_hook(int keycode, t_info *data) { + printf("Event detected: %d\n", keycode); if (keycode == XK_Escape) mlx_loop_end(data->mlx_ptr); if (keycode == XK_w) diff --git a/mlx_layer/mlx_init.c b/mlx_layer/mlx_init.c index fb28146..ed7ed5e 100644 --- a/mlx_layer/mlx_init.c +++ b/mlx_layer/mlx_init.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/11 19:53:42 by rparodi #+# #+# */ -/* Updated: 2024/11/28 14:06:03 by bgoulard ### ########.fr */ +/* Updated: 2024/11/29 17:06:44 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,11 +15,14 @@ #include "mlx_functions.h" #include "mlx_structs.h" +#include "ft_math.h" + #include #include int c3_frame_update(void *inf_ptr); +int key_hook(int keycode, t_info *data); /* move player w keys and call to redraw screen */ int c3_keyhook(int keycode, t_info *info) { @@ -35,13 +38,14 @@ int c3_redcross(t_info *info) t_win_list *c3_init_mlx_window(t_info *info) { - int x; - int y; - - x = 0; - y = 0; - mlx_get_screen_size(info->mlx_ptr, &x, &y); - return (mlx_new_window(info->mlx_ptr, x, y, "C3D")); + mlx_get_screen_size(info->mlx_ptr, &info->screen_size.x, &info->screen_size.y); + info->screen_size.x *= WIN_COEF; + info->screen_size.y *= WIN_COEF; + ft_clamp(info->screen_size.x, 0, 1920); + ft_clamp(info->screen_size.y, 0, 1080); + return (\ + mlx_new_window(info->mlx_ptr, info->screen_size.x, info->screen_size.y, \ + WIN_TITLE)); } int init_mlx_env(t_info *info) @@ -52,8 +56,8 @@ int init_mlx_env(t_info *info) info->win_ptr = c3_init_mlx_window(info); if (!info->win_ptr) return (ERROR_MLX); - mlx_hook(info->win_ptr, KeyPress, KeyPressMask, c3_keyhook, info); + mlx_hook(info->win_ptr, KeyPress, KeyPressMask, key_hook, info); mlx_hook(info->win_ptr, DestroyNotify, StructureNotifyMask, c3_redcross, info); - mlx_loop_hook(info->mlx_ptr, (int (*)())shelves_launch, &info); + mlx_loop_hook(info->mlx_ptr, (int (*)())shelves_launch, info); return (NO_ERROR); } diff --git a/parsing/blank_for_raph.c b/parsing/blank_for_raph.c index 1ef28a3..f35e593 100644 --- a/parsing/blank_for_raph.c +++ b/parsing/blank_for_raph.c @@ -6,7 +6,7 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/13 06:44:42 by bgoulard #+# #+# */ -/* Updated: 2024/11/28 13:59:33 by bgoulard ### ########.fr */ +/* Updated: 2024/11/29 16:07:01 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,9 +17,12 @@ static t_tile char_to_tile(char c) { + t_tile tile; + + ft_bzero(&tile, sizeof(t_tile)); if (c == '1' || c == ' ') - return ((t_tile)WALL); - return ((t_tile)EMPTY); + return ((tile.tile_type = WALL, tile)); + return ((tile.tile_type = EMPTY, tile)); } /// @brief Createsa a blank map for rapahael to test the raycasting @@ -39,8 +42,8 @@ void blank(t_info *info) info->map.size.x = 5; info->map.size.y = 6; - info->map.player_pos.x = 2.5; - info->map.player_pos.y = 2.5; +// info->map.player_pos.x = 2.5; +// info->map.player_pos.y = 2.5; info->map.fraw = malloc(sizeof(char *) * 6); info->map.fraw[0] = ft_strdup("11111"); info->map.fraw[1] = ft_strdup("10001"); diff --git a/parsing/map.c b/parsing/map.c index 3857c98..2d3822f 100644 --- a/parsing/map.c +++ b/parsing/map.c @@ -6,7 +6,7 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/12 08:31:06 by bgoulard #+# #+# */ -/* Updated: 2024/11/28 15:41:02 by bgoulard ### ########.fr */ +/* Updated: 2024/11/29 16:46:52 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,7 @@ // sys std #include +#include #include #include @@ -192,14 +193,14 @@ void str_to_tile(const char *str, t_tile *tile, size_t size) i = 0; while (str[i]) { - if (str[i] == '1' || i[str] == ' ') - tile[i].raw_tile = WALL; + if (str[i] == '1' || str[i] == ' ') + tile[i].tile_type = WALL; else - tile[i].raw_tile = EMPTY; + tile[i].tile_type = EMPTY; i++; } while (i < size) - tile[i++].raw_tile = WALL; + tile[i++].tile_type = WALL; } t_vector *load_vector(t_map *map) @@ -250,9 +251,10 @@ void *load_tiles(void *data) if (pos.x != 0 || pos.y != 0 || i == 0) return (ft_vec_destroy(&str_map), \ info->last_error = ERROR_PARSE, NULL); - pos.x = i + .5; - pos.y = ft_strchrs(ft_vec_at(str_map, i), "SNWE") - ft_vec_at(str_map, i) + .5; + pos.y = i + .5; + pos.x = ft_strchrs(ft_vec_at(str_map, i), "SNWE") - ft_vec_at(str_map, i) + .5; info->player.pos = pos; + info->player.pos_i = (t_ipoint){.x = (int)pos.x, .y = (int)pos.y}; } i++; } @@ -261,12 +263,15 @@ void *load_tiles(void *data) t_tile *c3_get_cell(t_tile *map, t_ipoint dimensions, t_ipoint pos) { + if (pos.x < 0 || pos.y < 0 || pos.x >= dimensions.x || pos.y >= dimensions.y) + return (printf("runtime error: %s:%d (pos:%d,%d on dims:%d,%d)\n", + __func__, __LINE__, pos.x, pos.y, dimensions.x, dimensions.y), + NULL); return (map + (pos.y * dimensions.x + pos.x)); } bool flood_fill(t_tile *tiles, t_ipoint pos, t_ipoint maxs) { - printf("dbg marker %s\n", __func__); t_tile *current; size_t i; const t_ipoint to_check[] = { @@ -286,6 +291,8 @@ bool flood_fill(t_tile *tiles, t_ipoint pos, t_ipoint maxs) if (current->tile_visited == true || current->tile_type == WALL) return (true); current->tile_visited = true; +// printf("dbg (marker %s:%d) pos (%03d : %03d), max (%03d, %03d)\n", +// __func__, __LINE__, pos.x, pos.y, maxs.x, maxs.y); i = 0; while (i != (sizeof(to_check) / sizeof(to_check[0]))) if (flood_fill(tiles, to_check[i++], maxs) == false) @@ -295,7 +302,7 @@ bool flood_fill(t_tile *tiles, t_ipoint pos, t_ipoint maxs) void *traverse_map(void *data) { - printf("dbg marker %s\n", __func__); +// printf("dbg marker %s\n", __func__); t_info *info; t_ipoint pos_start; @@ -306,7 +313,6 @@ void *traverse_map(void *data) return (info); } -void dump_map(t_tile *map, t_ipoint size); void parse_map(t_info *info) { t_optional opt; @@ -323,5 +329,7 @@ void parse_map(t_info *info) info->map.path = info->cli_ctx.file; if (ft_optional_chain(&opt, function_list) == false) return (c3_perror(info), (void)0); - dump_map(info->map.map, info->map.size); + info->player.plane = (t_dpoint){.x = 0, .y = + 2 * atan(deg2rad(FOV / 2))}; + info->player.dir = (t_dpoint){.x = -1, .y = 0}; } diff --git a/raycast/frame_update.c b/raycast/frame_update.c index f6bf105..ff07526 100644 --- a/raycast/frame_update.c +++ b/raycast/frame_update.c @@ -6,7 +6,7 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/12 06:02:54 by bgoulard #+# #+# */ -/* Updated: 2024/11/28 15:18:47 by bgoulard ### ########.fr */ +/* Updated: 2024/11/29 17:01:14 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,6 @@ #include "ft_math.h" #include -#include #include void draw_(int side, double perpWallDist, t_ipoint step, int x, t_info *data) @@ -38,7 +37,7 @@ void draw_(int side, double perpWallDist, t_ipoint step, int x, t_info *data) my_mlx_pixel_put(data, x, drawStart++, color); } -void search_hit(t_dpoint *sideDist, t_dpoint deltaDist, t_ipoint *map, t_ipoint step, void *_data[2]) +void search_hit(t_dpoint *sideDist, t_dpoint deltaDist, t_ipoint *pos_i, t_ipoint step, void *_data[2]) { int *side = (int *)_data[1]; t_info *data = (t_info *)_data[0]; @@ -46,21 +45,25 @@ void search_hit(t_dpoint *sideDist, t_dpoint deltaDist, t_ipoint *map, t_ipoint while (true) { if (sideDist->x < sideDist->y) { sideDist->x += deltaDist.x; - map->x += step.x; + pos_i->x += step.x; *side = 0; } else { sideDist->y += deltaDist.y; - map->y += step.y; + pos_i->y += step.y; *side = 1; } - printf("map.x: %d, map.y: %d\n", map->x, map->y); - if ((*c3_get_cell(data->map.map, data->map.size, *map)) + + if (data->map.size.x < 0 || data->map.size.y < 0 || + data->map.size.x > 100 || data->map.size.y > 100) + exit (EXIT_FAILURE); + + if ((*c3_get_cell(data->map.map, data->map.size, *pos_i)) .tile_type != EMPTY) return ; } } -void static set_step(t_ipoint *step, t_dpoint raydir) +static void set_step(t_ipoint *step, t_dpoint raydir) { if (raydir.x < 0) step->x = -1; @@ -72,23 +75,23 @@ void static set_step(t_ipoint *step, t_dpoint raydir) step->y = 1; } -void static set_side_dist(t_dpoint *sideDist, t_dpoint *tb, t_ipoint map) +static void set_side_dist(t_dpoint *sideDist, t_dpoint *tb, t_ipoint pos_i) { t_dpoint rayDir = tb[0]; t_dpoint pos = tb[1]; t_dpoint deltaDist = tb[2]; if (rayDir.x < 0) - sideDist->x = (pos.x - map.x) * deltaDist.x; + sideDist->x = (pos.x - pos_i.x) * deltaDist.x; else - sideDist->x = (map.x + 1.0 - pos.x) * deltaDist.x; + sideDist->x = (pos_i.x + 1.0 - pos.x) * deltaDist.x; if (rayDir.y < 0) - sideDist->y = (pos.y - map.y) * deltaDist.y; + sideDist->y = (pos.y - pos_i.y) * deltaDist.y; else - sideDist->y = (map.y + 1.0 - pos.y) * deltaDist.y; + sideDist->y = (pos_i.y + 1.0 - pos.y) * deltaDist.y; } -void column_handler(t_ipoint map, t_dpoint rayDir, t_info *data, int x) +void column_handler(t_ipoint pos_i, t_dpoint rayDir, t_info *data, int x) { t_dpoint sideDist; t_dpoint deltaDist; @@ -98,13 +101,13 @@ void column_handler(t_ipoint map, t_dpoint rayDir, t_info *data, int x) deltaDist = (t_dpoint){fabs(1 / rayDir.x), fabs(1 / rayDir.y)}; set_step(&step, rayDir); - set_side_dist(&sideDist, (t_dpoint[]){rayDir, data->player.pos, deltaDist}, map); - search_hit(&sideDist, deltaDist, &map, step, (void *[]){data, &side}); + set_side_dist(&sideDist, (t_dpoint[]){rayDir, data->player.pos, deltaDist}, pos_i); + search_hit(&sideDist, deltaDist, &pos_i, step, (void *[]){data, &side}); if (side == 0) - perpWallDist = (map.x - data->player.pos.x + (double)(1 - step.x) / 2) + perpWallDist = (pos_i.x - data->player.pos.x + (double)(1 - step.x) / 2) / rayDir.x; else - perpWallDist = (map.y - data->player.pos.y + (double)(1 - step.y) / 2) + perpWallDist = (pos_i.y - data->player.pos.y + (double)(1 - step.y) / 2) / rayDir.y; draw_(side, perpWallDist, step, x, data); } @@ -115,12 +118,14 @@ int render_frame(t_info *data) double coef; coef = 2 * tan(deg2rad(FOV) / 2) / (double)data->screen_size.x; - ft_bzero(data->camera.img_addr, data->screen_size.x * data->screen_size.y * data->camera.bpp / 8); + ft_bzero(data->camera.img_addr, data->screen_size.x * data->screen_size.y * (data->camera.bpp / 8)); for(int x = 0; x < data->screen_size.x; x++) { camera_x = x * coef - 1; - column_handler((t_ipoint){(int)data->player.pos.x, (int)data->player.pos.y}, - (t_dpoint){data->player.dir.x + data->player.plane.x * camera_x, data->player.dir.y + data->player.plane.y * camera_x}, + column_handler(data->player.pos_i, + (t_dpoint){ + data->player.dir.x + data->player.plane.x * camera_x, + data->player.dir.y + data->player.plane.y * camera_x}, data, x); } mlx_put_image_to_window(data->mlx_ptr, data->win_ptr, data->camera.screen_buff, 0, 0); diff --git a/raycast/utils_math.c b/raycast/utils_math.c index 414b20e..808d749 100644 --- a/raycast/utils_math.c +++ b/raycast/utils_math.c @@ -6,7 +6,7 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/28 14:09:12 by bgoulard #+# #+# */ -/* Updated: 2024/11/28 14:54:32 by bgoulard ### ########.fr */ +/* Updated: 2024/11/29 15:33:19 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,10 +30,10 @@ double deg2rad(int deg) void rotate_plane(t_dpoint *plane, double angle) { double old_plane_x; - double old_plane_y; +// double old_plane_y; old_plane_x = (*plane).x; - old_plane_y = (*plane).y; +// old_plane_y = (*plane).y; plane->x = plane->x * cos(angle) - plane->y * sin(angle); plane->y = old_plane_x * sin(angle) + plane->y * cos(angle); } diff --git a/sources/cleanups.c b/sources/cleanups.c index ae030c7..cc755c6 100644 --- a/sources/cleanups.c +++ b/sources/cleanups.c @@ -6,7 +6,7 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/09 01:11:01 by bgoulard #+# #+# */ -/* Updated: 2024/11/28 13:58:48 by bgoulard ### ########.fr */ +/* Updated: 2024/11/29 17:09:01 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,7 +45,8 @@ static void cleanup_mlx(t_info *info) if (info->map.texture[i]) mlx_destroy_image(info->mlx_ptr, info->map.texture[i]); } - + if (info->camera.screen_buff) + mlx_destroy_image(info->mlx_ptr, info->camera.screen_buff); mlx_destroy_display(info->mlx_ptr); ft_free((void **)&info->mlx_ptr); } diff --git a/sources/main.c b/sources/main.c index d8a8df1..d7fd13b 100644 --- a/sources/main.c +++ b/sources/main.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/30 16:30:03 by rparodi #+# #+# */ -/* Updated: 2024/11/28 15:38:55 by bgoulard ### ########.fr */ +/* Updated: 2024/11/29 17:08:31 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,8 +20,6 @@ #include #include -void blank(t_info *info); - void dump_map(t_tile *map, t_ipoint size) { int i; @@ -31,6 +29,7 @@ void dump_map(t_tile *map, t_ipoint size) while (i < size.y) { j = 0; + printf("\t\t\t"); while (j < size.x) { printf("%d", map[i * size.x + j].tile_type); @@ -50,20 +49,24 @@ void dump_info(t_info *info) i = 0; printf("t_info:\n"); + printf("\tplayer:\n"); + printf("\t\tpos_i:\t(x: %d, y:%d)\n", info->player.pos_i.x, info->player.pos_i.y); + printf("\t\tpos:\t(x:%lf, y:%lf)\n", info->player.pos.x, info->player.pos.y); + printf("\t\tdir:\t(x:%lf, y:%lf)\n", info->player.dir.x, info->player.dir.y); + printf("\t\tplane:\t(x:%lf, y:%lf)\n", info->player.plane.x, info->player.plane.y); + printf("\tcli_ctx:\n"); - printf("\t\tfile: %s\n", info->cli_ctx.file); - printf("\t\tdebug: %s\n", bool_str[info->cli_ctx.debug]); - printf("\t\tsave: %s\n", bool_str[info->cli_ctx.save]); - printf("\t\thelp: %s\n", bool_str[info->cli_ctx.help]); + printf("\t\tfile:\t%s\n", info->cli_ctx.file); + printf("\t\tdebug:\t%s\n", bool_str[info->cli_ctx.debug]); + printf("\t\tsave:\t%s\n", bool_str[info->cli_ctx.save]); + printf("\t\thelp:\t%s\n", bool_str[info->cli_ctx.help]); printf("\tmap:\n"); printf("\t\tpath:%s\n", info->map.path); - printf("\t\tfd:%d\n", info->map.fd); + printf("\t\tfd:\t%d\n", info->map.fd); printf("\t\tsize:\t(x:%d, y:%d)\n", info->map.size.x, info->map.size.y); - printf("\t\tplayer_pos:\t(x:%lf, y:%lf)\n", info->map.player_pos.x, \ - info->map.player_pos.y); while (info->map.fraw[i]) { - printf("\t\tmap.fraw[%zu]: %s\n", i, info->map.fraw[i]); + printf("\t\tmap.fraw[%*zu]: %s\n", 3, i, info->map.fraw[i]); i++; } printf("\t\ttexture[0]: %p\n", info->map.texture[0]); @@ -72,9 +75,9 @@ void dump_info(t_info *info) printf("\t\ttexture[3]: %p\n", info->map.texture[3]); printf("\t\tmap: %p\n", info->map.map); dump_map(info->map.map, info->map.size); - printf("\tlast_error: %d\n", info->last_error); printf("\terno_state: %d\n", info->errno_state); + printf("\n"); } void check_err(t_info *info) @@ -100,14 +103,12 @@ void run_cub3d(t_info *info) dump_info(info); if (info->last_error != NO_ERROR) return ; - if (info->cli_ctx.no_graphics) - printf("no graphics mode\n"); - // todo: here - // - validity check - printf("launching mlx\n"); + info->camera.screen_buff = \ + mlx_new_image(info->mlx_ptr, info->screen_size.x, info->screen_size.y); + info->camera.img_addr = \ + mlx_get_data_addr(info->camera.screen_buff, &info->camera.bpp, \ + &info->camera.line_len, &info->camera.endian); mlx_loop(info->mlx_ptr); -// - game loop : already loops over the mlx_ptr -// -> get events if key pressed move player + run math to re-draw screen } /// @brief main function of the cub3d executable