diff --git a/includes/cub3d.h b/includes/cub3d.h index 1037f71..77300e7 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/05/24 16:43:16 by narnaud ### ########.fr */ +/* Updated: 2022/05/30 18:02:08 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,26 +46,26 @@ typedef struct s_vector int y; } t_vec; -typedef struct s_map -{ - size_t wide; - size_t deep; - char **map; -} t_map; - typedef struct s_env { - int step; + int wide; + int deep; char *wallTexture[4]; int floorColor; int ceilColor; - t_slist *raw_map; - int deep; - t_map *map; + char **map; void *mlx; - void *window; + void *win; + void *img; + int pixel_bits; + int line_bytes; + int endian; + int *buffer; + t_vec playerPos; + int yaw; } t_env; +int rgb_to_int(char **rgb); t_env *parse_envFile(char *filename); char *get_next_line(int fd); diff --git a/maps/scene.cub b/maps/scene.cub index 5269b0f..bcd85ad 100755 --- a/maps/scene.cub +++ b/maps/scene.cub @@ -6,25 +6,36 @@ WE ./textures/wood.xpm F 147, 159, 153 C 219, 213, 213 - 1111 - 1001 - 1001 - 1001 -11111 11111 1001 + 11110000 + 100100000 + 100111110 + 100000000 +11111 11111 100100000 10001 1000111110011111111111 10001 1000000000000000000001 10001 1000000000000000000001 -10001 1000111101111111100001 -10001 10001 101 100001 +10000 1000111101111111100000 +10001 10001 101000000100001 10001 10001 101111111100001 -10001111110001 100000000000001 +10001101110001 100000000000001 10000000000001 111110111100001 10000000000001 10111100001 -10000E00000001 10000000001 -10000000002001 10000000001 +00000E00000001 10000000001 +10000000000001 10000000001 10111111111111 11111111111 100000001 10000000001 -1111110011111111111110000000001 -1000000010001000000000000000011 -100000000000000000000000000001 -111111111111111111111111111111 \ No newline at end of file +1111110011111101111110000000001 +10000000100010000000000000000111 +10000000000000000000000000000101 +11111111110000000001111111111101 +11111111110000000001111111111101 +11111111110000000001111111111101 +11111111110000000001111111111101 +11111111110000000001111111111101 +11111111110000000001111111111101 +11000000000111100000000011111101 +11000000000111100000000011111101 +11000000000111100000000011111101 +11000000000111100000000011111101 +11111111111101111111100000000001 +11111111111111111111111111111111 diff --git a/sources/main.c b/sources/main.c index 2bd4e75..890c96a 100644 --- a/sources/main.c +++ b/sources/main.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/21 19:22:50 by narnaud #+# #+# */ -/* Updated: 2022/05/24 16:47:16 by narnaud ### ########.fr */ +/* Updated: 2022/05/30 16:50:21 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,18 +18,66 @@ void init_window(t_env *env) void *window; mlx = mlx_init(); - if (!mlx) - exit(error_msg("Mlx fail to init", 1)); + //if (!mlx) + //exit(error_msg("Mlx fail to init", 1)); env->mlx = mlx; window = mlx_new_window(mlx, WIN_X_SZ, WIN_Y_SZ, "FdF"); - if (!window) - exit(error_msg("Mlx fail to create window", 1)); + //if (!window) + //exit(error_msg("Mlx fail to create window", 1)); env->win = window; } +/* +void launch_game(){ + +} +*/ + +void draw_square(t_env *env, t_vec vec, int size, int color) +{ + int step_x; + int step_y; + + step_y = 0; + while (step_y < size) + { + step_x = 0; + while (step_x < size) + { + env->buffer[(env->line_bytes * (vec.y * size + step_y)) + (vec.x * size + step_x)] = color; + step_x++; + } + step_y++; + } +} + +void render_minimap(t_env *env) +{ + char **map; + t_vec vec; + + vec.y = 0; + map = env->map; + while (map[vec.y]) + { + vec.x = 0; + while (map[vec.y][vec.x]) + { + if (map[vec.y][vec.x] == '0') + draw_square(env, vec, 24, 39424); + else if (map[vec.y][vec.x] == '1') + draw_square(env, vec, 24, 10420483); + else + draw_square(env, vec, 24, 255); + vec.x++; + } + vec.y++; + } +} int main(int argc, char **argv) { t_env *env; + int y; if (argc != 2) return (EXIT_FAILURE); @@ -43,19 +91,18 @@ int main(int argc, char **argv) printf("=> Floor color: %d\n=> Ceil color: %d\n", env->floorColor, env->ceilColor); printf("\e[1;32m========> MAP <========\e[0m\n"); y = 0; - raw_map = env->raw_map; - while (y < env->deep) - { - printf("%s\n", (char *)raw_map->content); - y++; - raw_map = raw_map->next; - } + while (env->map[y]) + printf("%s\n", env->map[y++]); } init_window(env); - lanch_game(env); - render(env); - mlx_key_hook(env->win, key_hook_primary, datas); + env->img = mlx_new_image(env->mlx, WIN_X_SZ, WIN_Y_SZ); + env->buffer = (int *)mlx_get_data_addr(env->img, \ + &env->pixel_bits, &env->line_bytes, &env->endian); + env->line_bytes /= 4; + //launch_game(env); + render_minimap(env); + mlx_put_image_to_window(env->mlx, env->win, env->img, 0, 0); + //mlx_key_hook(env->win, key_hook_primary, env); mlx_loop(env->mlx); return (EXIT_SUCCESS); - } diff --git a/sources/parsing.c b/sources/parsing.c index 2fcb233..a033faf 100644 --- a/sources/parsing.c +++ b/sources/parsing.c @@ -6,39 +6,25 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/23 17:20:29 by narnaud #+# #+# */ -/* Updated: 2022/05/24 16:17:37 by narnaud ### ########.fr */ +/* Updated: 2022/05/30 18:07:33 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #include "../includes/cub3d.h" -void get_map_datas(t_env *env, char *line) +int cleanup_datas(t_env *env) { - t_slist *lst_e; - char *map_y; - int i; - - i = 0; - map_y = malloc((ft_strlen(line)) * sizeof(char)); - while (*line) - { - if (*line == '\n') - break ; - else if (!ft_isspace(*line)) - map_y[i] = *line; - else - map_y[i] = '0'; - line++; - i++; - } - map_y[i] = '\0'; - env->deep++; - lst_e = malloc(sizeof(*lst_e)); - lst_e->next = NULL; - lst_e->content = map_y; - ft_slst_add_back(&(env->raw_map), lst_e); -} + if (env->wallTexture[0]) + free(env->wallTexture[0]); + if (env->wallTexture[1]) + free(env->wallTexture[1]); + if (env->wallTexture[2]) + free(env->wallTexture[2]); + if (env->wallTexture[3]) + free(env->wallTexture[3]); + return (1); +} int rgb_to_int(char **rgb) { int ret; @@ -71,15 +57,10 @@ int rgb_to_int(char **rgb) return (ret); } -void register_data(t_env *env, char *line) +void register_settings(int *progress, t_env *env, char *line) { char **elem; - if (*line && *line != '\n' && env->step > 5) - { - get_map_datas(env, line); - return ; - } elem = ft_split(line, ' '); if (!elem[0] || !elem[1]) return ; @@ -96,30 +77,148 @@ void register_data(t_env *env, char *line) else if (!ft_strncmp(elem[0],"C", 2)) env->ceilColor = rgb_to_int(elem); else - env->step--; - env->step++; + (*progress)--; + (*progress)++; ft_free_split(elem); } +/* TODO : measure wide and deep progressively + */ +t_slist *read_map_line(t_env *env, char *line) +{ + t_slist *ret; + char *map_y; + int i; + + ret = malloc(sizeof(t_slist)); + i = 0; + map_y = malloc((ft_strlen(line)) * sizeof(char)); + while (*line) + { + if (*line == '\n') + break ; + else if (!ft_isspace(*line)) + map_y[i] = *line; + else + map_y[i] = '0'; + line++; + i++; + } + map_y[i] = '\0'; + if (i > env->wide) + env->wide = i; + env->deep++; + ret->next = NULL; + ret->content = map_y; + return (ret); +} + +char **create_map_array(t_slist *e_lst, int wide, int deep) +{ + char **ret; + char *endLine; + t_slist *tmp; + int i; + + if (!e_lst) + return (ft_calloc(1, sizeof(char *))); + ret = ft_calloc(deep + 1, sizeof(char *)); + i = 0; + while (i < deep) + { + endLine = ft_calloc(wide - ft_strlen(e_lst->content) + 1, sizeof(char)); + endLine = memset(endLine, '0', wide - ft_strlen(e_lst->content) * sizeof(char)); + ret[i] = ft_strjoin(e_lst->content, endLine); + free(endLine); + free(e_lst->content); + tmp = e_lst; + e_lst = e_lst->next; + free(tmp); + i++; + } + return (ret); +} + +void find_player(t_env *env) +{ + char **map; + int x; + int y; + char cell; + + y = 0; + + map = env->map; + while (map[y]) + { + x = 0; + while (map[y][x]) + { + cell = map[y][x]; + if (cell == 'N' || cell == 'S' || cell == 'E' || cell == 'W') + { + env->playerPos.x = x; + env->playerPos.y = y; + env->yaw = cell; + return ; + } + x++; + } + y++; + } +} + +int is_in_open_room(t_env *env, int x, int y) +{ + static char *checked; + + if (x < 0 || x >= env->wide || y < 0 || y >= env->deep) + return (1); + if (!checked) + checked = ft_calloc(env->deep * env->wide + 1, sizeof(char)); + if (checked[y * env->wide + x]) + return (0); + else if (env->map[y][x] == '1') + return (0); + else if (is_in_open_room(env, x + 1, y) + || is_in_open_room(env, x - 1, y) + || is_in_open_room(env, x, y + 1) + || is_in_open_room(env, x, y - 1)) + return (1); + else + return (0); +} + t_env *parse_envFile(char *filename) { int fd; char *line; + int progress; + t_slist *e_map; t_env *ret; - int y; - t_slist *raw_map; - ret = malloc(sizeof(t_env)); + progress = 0; + e_map = NULL; + ret = ft_calloc(1, sizeof(t_env)); ret->deep = 0; + ret->wide = 0; fd = open(filename, O_RDONLY); line = get_next_line(fd); while (line) { - register_data(ret, line); + if (*line && *line != '\n' && progress > 5 && progress++) + ft_slst_add_back(&e_map, read_map_line(ret, line)); + else + register_settings(&progress, ret, line); free(line); line = get_next_line(fd); } - if (ret->step < 6) + if (progress < 6 && cleanup_datas(ret)) + return (NULL); + else + ret->map = create_map_array(e_map, ret->wide, ret->deep); + find_player(ret); + if (is_in_open_room(ret, ret->playerPos.x, ret->playerPos.y) && cleanup_datas(ret)) //add map cleanup in cleanup_datas return (NULL); return (ret); }