diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-01-11 12:37:34 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-01-11 12:37:34 +0100 |
| commit | cff05e83256a67b8cb23b16b1e1e6f761ff52f4c (patch) | |
| tree | 33eb6eae520a085d6774205601f13d07dc99aaf9 | |
| parent | 0dcba6ff7e68ed13f8e6caadd80b77506b917050 (diff) | |
| download | cub3d-cff05e83256a67b8cb23b16b1e1e6f761ff52f4c.tar.gz cub3d-cff05e83256a67b8cb23b16b1e1e6f761ff52f4c.tar.bz2 cub3d-cff05e83256a67b8cb23b16b1e1e6f761ff52f4c.zip | |
fixing a few segfault, map border checking
| -rw-r--r-- | Makefile | 17 | ||||
| -rw-r--r-- | cub3d.h | 22 | ||||
| -rw-r--r-- | helper.c | 9 | ||||
| -rw-r--r-- | main.c | 63 | ||||
| -rw-r--r-- | minimalist.cub | 8 | ||||
| -rw-r--r-- | parse/parse.c | 19 | ||||
| -rw-r--r-- | parse/parse_check.c | 44 | ||||
| -rw-r--r-- | state.c | 5 |
8 files changed, 131 insertions, 56 deletions
@@ -2,15 +2,24 @@ LIBFT_PATH = ./libft MINILIBX_PATH = ./miniLibX CC = gcc -CCFLAGS = -I$(LIBFT_PATH) -I$(MINILIBX_PATH) -I. -Wall -Wextra #-Werror +CCFLAGS = -I$(LIBFT_PATH) -I$(MINILIBX_PATH) -I. -Wall -Wextra -g #-Werror LDFLAGS = -L$(LIBFT_PATH) -lft \ -L$(MINILIBX_PATH) -lmlx \ -framework OpenGL -framework AppKit -lm NAME = cub3D -SRC = main.c event.c parse/parse.c parse/parse_textures.c \ - parse/parse_color.c parse/parse_resolution.c \ - state.c vector.c render.c helper.c error.c +SRC = main.c \ + parse/parse.c \ + parse/parse_textures.c \ + parse/parse_color.c \ + parse/parse_resolution.c \ + parse/parse_check.c \ + event.c \ + state.c \ + vector.c \ + render.c \ + helper.c \ + error.c OBJ = $(SRC:.c=.o) INCLUDE = cub3d.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:40:37 by cacharle #+# #+# */ -/* Updated: 2020/01/11 10:24:44 by cacharle ### ########.fr */ +/* Updated: 2020/01/11 10:59:58 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include <stdio.h> @@ -60,13 +60,13 @@ typedef union typedef enum { - CELL_EMPTY = 1 << 1, - CELL_WALL = 1 << 2, - CELL_ITEM = 1 << 3, - CELL_LOOK_NORTH = 1 << 4, - CELL_LOOK_SOUTH = 1 << 5, - CELL_LOOK_WEST = 1 << 6, - CELL_LOOK_EAST = 1 << 7 + CELL_EMPTY = 0, + CELL_WALL = 1, + CELL_ITEM = 2, + CELL_LOOK_NORTH, + CELL_LOOK_SOUTH, + CELL_LOOK_WEST, + CELL_LOOK_EAST } t_cell; typedef t_cell** t_map; @@ -138,6 +138,12 @@ t_state *parse_map(t_state *state, char **lines); t_cell *create_map_row(char *line); /* +** parse/parse_check.c +*/ + +t_state *parse_check(t_state *state); + +/* ** parse/parse_resolution.c */ @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/11 07:32:20 by cacharle #+# #+# */ -/* Updated: 2020/01/11 10:37:54 by cacharle ### ########.fr */ +/* Updated: 2020/01/11 11:22:15 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,9 +20,12 @@ t_bool helper_is_player_cell(t_cell cell) void helper_free_splited(char **splited) { + int i; + if (splited == NULL) return ; - while (splited != NULL) - free(*splited++); + i = -1; + while (splited[++i] != NULL) + free(splited[i]); free(splited); } @@ -6,19 +6,20 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:39:39 by cacharle #+# #+# */ -/* Updated: 2020/01/11 10:09:51 by cacharle ### ########.fr */ +/* Updated: 2020/01/11 12:33:37 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d.h" +/* int main(int argc, char **argv) { t_state *state; - /* if (argc == 3 && ft_strcmp(argv[2], "--save") == 0) */ - /* return (save_image()); */ - /*else*/ + if (argc == 3 && ft_strcmp(argv[2], "--save") == 0) + return (save_image()); + else if (argc != 2) error_put_usage_exit(argv[0]); if ((state = state_new(parse(argv[1]))) == NULL) @@ -30,39 +31,51 @@ int main(int argc, char **argv) mlx_loop_hook(state->mlx_ptr, render_update, (void*)state); mlx_loop(state->mlx_ptr); return (0); -} +}*/ -/* -int main(int argc, char argv) +int main(int argc, char **argv) { - t_parsing *p = parse(argv[1]); - if (p == NULL) + (void)argc; + t_state *s = parse(argv[1]); + if (s == NULL) + return (1); + if (parse_check(s) == NULL) + { + printf("wrong .cub format"); return (1); - printf("R %d %d\n", p->resolution_width, p->resolution_height); - printf("NO %s\n", p->north_texture_path); - printf("SO %s\n", p->south_texture_path); - printf("WE %s\n", p->west_texture_path); - printf("EA %s\n\n", p->east_texture_path); - printf("S %s\n", p->sprite_texture_path); - printf("F %d,%d,%d\n", p->floor_color.r, p->floor_color.g, p->floor_color.b); - printf("C %d,%d,%d\n\n", p->ceilling_color.r, p->ceilling_color.g, p->ceilling_color.b); + } + printf("R %d %d\n", s->window.width, s->window.height); + printf("NO %s\n", s->textures_path[TEX_NORTH]); + printf("SO %s\n", s->textures_path[TEX_SOUTH]); + printf("WE %s\n", s->textures_path[TEX_WEST]); + printf("EA %s\n\n", s->textures_path[TEX_EAST]); + printf("S %s\n", s->textures_path[TEX_SPRITE]); + printf("F %d,%d,%d\n", s->floor_color.rgb.r, s->floor_color.rgb.g, s->floor_color.rgb.b); + printf("C %d,%d,%d\n\n", s->ceilling_color.rgb.r, s->ceilling_color.rgb.g, s->ceilling_color.rgb.b); - printf("%dx%d\n", p->map_height, p->map_width); - for (int i = 0; i < p->map_height; i++) + printf("%dx%d\n", s->map_height, s->map_width); + for (int i = 0; i < s->map_height; i++) { - for (int j = 0; j < p->map_width; j++) + for (int j = 0; j < s->map_width; j++) { - if (p->map[i][j] == CELL_WALL) + if (s->map[i][j] == CELL_WALL) printf("#"); - else if (p->map[i][j] == CELL_EMPTY) + else if (s->map[i][j] == CELL_EMPTY) printf(" "); else - printf("%d", p->map[i][j]); - if (j != p->map_width - 1) + printf("%d", s->map[i][j]); + if (j != s->map_width - 1) printf(" "); } printf("\n"); } + printf("post state_new\n"); + if ((s = state_new(s)) == NULL) + { + printf("Error: state new"); + return 1; + } + printf("state->pos [%f %f]\n", s->pos.x, s->pos.y); + state_destroy(s); return 0; } -*/ diff --git a/minimalist.cub b/minimalist.cub index f054994..2272c1b 100644 --- a/minimalist.cub +++ b/minimalist.cub @@ -1,10 +1,10 @@ R 640 480 NO ./textures/brick.xpm -SO ./path_to_the_south_texture -WE ./path_to_the_west_texture -EA ./path_to_the_east_texture +SO ./textures/brick.xpm +WE ./textures/brick.xpm +EA ./textures/brick.xpm -S ./path_to_the_sprite_texture +S ./textures/brick.xpm F 220,0,0 C 0,225,0 diff --git a/parse/parse.c b/parse/parse.c index 145d5ad..84a8957 100644 --- a/parse/parse.c +++ b/parse/parse.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 09:29:21 by cacharle #+# #+# */ -/* Updated: 2020/01/11 10:12:10 by cacharle ### ########.fr */ +/* Updated: 2020/01/11 11:29:45 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,8 @@ t_state *parse(char *filename) return (NULL); if ((lines = get_file_lines(filename)) == NULL) return (state_destroy(state)); + /* for (int i = 0; lines[i]; i++) */ + /* printf("%d [%s]\n", i, lines[i]); */ i = -1; while (lines[++i] != NULL) { @@ -46,16 +48,17 @@ char **get_file_lines(char *filename) { int fd; int ret; - char buf[BUFFER_SIZE]; + char buf[BUFFER_SIZE + 1]; char *file; - fd = open(filename, O_RDONLY); + if ((fd = open(filename, O_RDONLY)) < 0) + return (NULL); if ((file = ft_strdup("")) == NULL) return (NULL); while ((ret = read(fd, buf, BUFFER_SIZE)) > 0) { buf[ret] = '\0'; - if ((file = ft_strjoin_free(file, buf, 1)) == NULL) + if ((file = ft_strjoin(file, buf)) == NULL) return (NULL); } if (ret == -1) @@ -122,12 +125,8 @@ t_cell *create_map_row(char *line) i = 0; while (*line) { - if (*line == '0') - row[i++] = CELL_EMPTY; - else if (*line == '1') - row[i++] = CELL_WALL; - else if (*line == '2') - row[i++] = CELL_ITEM; + if (*line == '0' || *line == '1' || *line == '2') + row[i++] = *line - '0'; else if (*line == 'N') row[i++] = CELL_LOOK_NORTH; else if (*line == 'S') diff --git a/parse/parse_check.c b/parse/parse_check.c new file mode 100644 index 0000000..7bcfc8b --- /dev/null +++ b/parse/parse_check.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/11 10:59:15 by cacharle #+# #+# */ +/* Updated: 2020/01/11 11:15:04 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +t_state *parse_check(t_state *state) +{ + int i; + int j; + int player_count; + + i = -1; + while (++i < state->map_width) + if (state->map[0][i] != CELL_WALL + || state->map[state->map_height - 1][i] != CELL_WALL) + return (state_destroy(state)); + i = -1; + while (++i < state->map_height) + if (state->map[i][0] != CELL_WALL + || state->map[i][state->map_width - 1] != CELL_WALL) + return (state_destroy(state)); + // maybe not necessary + /* player_count = 0; */ + /* i = -1; */ + /* while (++i < state->map_height) */ + /* { */ + /* j = -1; */ + /* while (++j < state->map_width) */ + /* if (helper_is_player_cell(state->map[i][j])) */ + /* player_count++; */ + /* } */ + /* if (player_count != 1) */ + /* return (state_destroy(state)); */ + return (state); +} @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/19 16:39:57 by cacharle #+# #+# */ -/* Updated: 2020/01/11 10:24:53 by cacharle ### ########.fr */ +/* Updated: 2020/01/11 12:32:13 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -106,7 +106,8 @@ void *state_destroy(t_state *state) while (++i < TEXTURES_NUM) { free(state->textures_path[i]); - mlx_destroy_image(state->mlx_ptr, state->textures[i].id); + if (state->mlx_ptr != NULL&& state->textures[i].id != NULL) + mlx_destroy_image(state->mlx_ptr, state->textures[i].id); } if (state->mlx_ptr && state->window_ptr) mlx_destroy_window(state->mlx_ptr, state->window_ptr); |
