From 26b4e9d03d855b885c87c6dd539d3f2f4b9d7ed7 Mon Sep 17 00:00:00 2001 From: djonker Date: Sun, 29 Oct 2023 14:27:34 +0100 Subject: [PATCH] map is being parsed correctly :) --- config.cub | 29 +++++++++++++--- cub3d.h | 9 ++--- src/draw.c | 4 +-- src/init.c | 14 ++++---- src/keys.c | 2 +- src/main.c | 2 +- src/map.c | 96 +++++++++++++++++++++++++++++++-------------------- src/parse.c | 86 +++++++++++++++++++++++++++++++++++---------- src/raycast.c | 4 +-- 9 files changed, 170 insertions(+), 76 deletions(-) diff --git a/config.cub b/config.cub index bd5ec99..4f36cb2 100644 --- a/config.cub +++ b/config.cub @@ -6,8 +6,27 @@ EA ./path_to_the_east_texture SO ./path_to_the_south_texture WE ./path_to_the_west_texture -111111 -100101 -101001 -1100N1 -111111 +111111111111111111111111 +100000000000000000000001 +100000000000000000000001 +100000000000000000000001 +100000111110000101010001 +100000100010000000000001 +100000100010000100010001 +100000100010000000000001 +100000110110000101010001 +100000000000000000000001 +100000000000000000000001 +100000000000100000000001 +100000000000000000000001 +100000000000000000000001 +100000000000000000000001 +100000000000000000000001 +111111111000000000000001 +110100001000000000000001 +110000101000000000000001 +110100001000000000000001 +110111111000000000000001 +1100000000000000N0000001 +111111111000000000000001 +111111111111111111111111 diff --git a/cub3d.h b/cub3d.h index 3f27c58..cf2872f 100644 --- a/cub3d.h +++ b/cub3d.h @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 10:46:35 by houtworm #+# #+# */ -/* Updated: 2023/10/29 11:50:54 by houtworm ######## odam.nl */ +/* Updated: 2023/10/29 13:46:54 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -52,22 +52,23 @@ typedef struct s_varlist int hit; int side; double oldmouseposx; - double oldmouseposy; + double oldmouseposy; // only needed if we do vertical aiming int32_t fcolor; int32_t ccolor; char *northwt; char *eastwt; char *southwt; char *westwt; + int resize; } t_varlist; +void ft_frametime(t_varlist *vl); // init.c t_varlist initvarlist(void); // parse.c t_varlist ft_parseconfigfile(t_varlist vl, char *filename); -char **ft_getmap(void); // map.c -char **ft_getmap(void); +char **ft_getmap(t_varlist *vl, int fd); // keys.c void ft_movementkeys(t_varlist *vl); void keyhook(mlx_key_data_t kd, void *param); diff --git a/src/draw.c b/src/draw.c index 5de6af3..a1a5d70 100644 --- a/src/draw.c +++ b/src/draw.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:54:20 by houtworm #+# #+# */ -/* Updated: 2023/10/27 17:04:16 by djonker ######## odam.nl */ +/* Updated: 2023/10/29 12:28:28 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ void ft_drawline(int x, t_varlist *vl, int drawstart, int drawend) int y; y = 1; - while (vl->h >= y) + while (vl->h > y) { if (y < drawstart) mlx_put_pixel(vl->img, x, y, vl->ccolor); diff --git a/src/init.c b/src/init.c index 9d979d3..4197ada 100644 --- a/src/init.c +++ b/src/init.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:49:12 by houtworm #+# #+# */ -/* Updated: 2023/10/26 22:03:37 by houtworm ######## odam.nl */ +/* Updated: 2023/10/29 13:52:03 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -16,12 +16,14 @@ t_varlist initvarlist(void) { t_varlist vl; - vl.w = 600; + vl.w = 800; vl.h = 600; - vl.posx = 22; - vl.posy = 12; - vl.dirx = -1; - vl.diry = 0; + vl.ccolor = 0; + vl.fcolor = 0; + vl.northwt = NULL; + vl.eastwt = NULL; + vl.southwt = NULL; + vl.westwt = NULL; vl.planex = 0; vl.planey = 0.66; vl.mlx = mlx_init(vl.w, vl.h, "Cub3D", true); diff --git a/src/keys.c b/src/keys.c index 9509148..b974732 100644 --- a/src/keys.c +++ b/src/keys.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:50:23 by houtworm #+# #+# */ -/* Updated: 2023/10/29 12:10:26 by houtworm ######## odam.nl */ +/* Updated: 2023/10/29 12:26:33 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/main.c b/src/main.c index 7beeb27..289bd46 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */ -/* Updated: 2023/10/29 12:08:59 by houtworm ######## odam.nl */ +/* Updated: 2023/10/29 12:26:20 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/map.c b/src/map.c index a136c0c..91e9f86 100644 --- a/src/map.c +++ b/src/map.c @@ -6,55 +6,77 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 17:33:50 by houtworm #+# #+# */ -/* Updated: 2023/10/27 16:25:33 by djonker ######## odam.nl */ +/* Updated: 2023/10/29 14:25:40 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ #include "../cub3d.h" +#include -char **ft_getmap(void) +char ft_setplayerpos(t_varlist *vl, char dir, int y, int x) { - int y; - int x; - char **map; - char localmap[24][24] = { - {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, - {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, - {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, - {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, - {1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,1,0,1,0,1,0,0,0,1}, - {1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1}, - {1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0,0,1}, - {1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1}, - {1,0,0,0,0,0,1,1,0,1,1,0,0,0,0,1,0,1,0,1,0,0,0,1}, - {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, - {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, - {1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1}, - {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, - {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, - {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, - {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, - {1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, - {1,1,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, - {1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, - {1,1,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, - {1,1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, - {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, - {1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, - {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} - }; - map = ft_calloc(24, 8); + vl->posx = x; + vl->posy = y; + if (dir == 'N' || dir == 'E') + vl->dirx = -1; + else + vl->dirx = 0; + if (dir == 'S' || dir == 'W') + vl->diry = -1; + else + vl->diry = 0; + return ('0'); +} + +char **ft_getmap(t_varlist *vl, int fd) +{ + int y; + int x; + int ret; + char **map; + char *line; + + map = ft_calloc(512, 8); y = 0; - while (y < 24) + ret = 1; + while (y <= 512 && ret > 0) { - map[y] = ft_calloc(24, 8); - x = 0; - while (x < 24) + map[y] = ft_calloc(512, 8); + while (ret > 0) { - map[y][x] = localmap[y][x]; + ret = get_next_line(fd, &line); + if (line[0]) + break ; + } + if (ret == 0) + { + map[y] = NULL; + int i; + i = 0; + while (i < y) + { + printf("%s\n", map[i]); + i++; + } + free(line); + printf("xpos: %lf ypos %lf xdir %lf ydir %lf\n\n", vl->posy, vl->posx, vl->dirx, vl->diry); + return (map); + } + printf("line: %s\n", line); + x = 0; + while (x <= 512 && line[x]) + { + if (ft_strchr(" 0", line[x])) + map[y][x] = '0'; + else if (ft_strchr("1", line[x])) + map[y][x] = '1'; + else if (ft_strchr("NESW", line[x])) + map[y][x] = ft_setplayerpos(vl, line[x], y, x); x++; } + free(line); y++; } + free(line); return (map); } diff --git a/src/parse.c b/src/parse.c index 4259268..53bfa5f 100644 --- a/src/parse.c +++ b/src/parse.c @@ -6,14 +6,14 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:48:55 by houtworm #+# #+# */ -/* Updated: 2023/10/27 19:56:28 by djonker ######## odam.nl */ +/* Updated: 2023/10/29 13:54:50 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ #include "../cub3d.h" #include -int ft_settexture(t_varlist *vl, char *line) +int ft_settexture(t_varlist *vl, char *line, int direction) { int i; @@ -21,18 +21,18 @@ int ft_settexture(t_varlist *vl, char *line) i = 0; while (line[i] != '.') i++; - if (ft_strncmp(line, "NO ", 3)) + if (direction == 1) vl->northwt = ft_strdup(&line[i]); - if (ft_strncmp(line, "EA ", 3)) + if (direction == 2) vl->eastwt = ft_strdup(&line[i]); - if (ft_strncmp(line, "SO ", 3)) + if (direction == 3) vl->southwt = ft_strdup(&line[i]); - if (ft_strncmp(line, "WE ", 3)) + if (direction == 4) vl->westwt = ft_strdup(&line[i]); return (0); } -int ft_setcolor(t_varlist *vl, char *line) +int ft_setcolor(t_varlist *vl, char *line, int dest) { int i; int r; @@ -53,36 +53,86 @@ int ft_setcolor(t_varlist *vl, char *line) while (line[i] < '0' || line[i] > '9') i++; b = ft_atoi(&line[i]); - if (line[0] == 'F') - vl->fcolor = (r << 24 | g << 16 | b << 8 | 255); - if (line[0] == 'C') + if (dest == 1) vl->ccolor = (r << 24 | g << 16 | b << 8 | 255); + if (dest == 2) + vl->fcolor = (r << 24 | g << 16 | b << 8 | 255); return (0); } -int ft_checkline(t_varlist *vl, char *line) +char *ft_checkline(t_varlist *vl, char *line) { - if ((line[0] == 'F' || line[0] == 'C') && line[1] == ' ') - ft_setcolor(vl, line); - else if ((!ft_strncmp(line, "NO ", 3) || !ft_strncmp(line, "EA ", 3) || !ft_strncmp(line, "SO ", 3) || !ft_strncmp(line, "WE ", 3))) - ft_settexture(vl, line); - return (0); + if (line[0] == 'C' && line[1] == ' ') + { + if (!vl->ccolor) + ft_setcolor(vl, line, 1); + else + return (" ceiling color"); + } + else if (line[0] == 'F' && line[1] == ' ') + { + if (!vl->fcolor) + ft_setcolor(vl, line, 2); + else + return (" floor color"); + } + else if (!ft_strncmp(line, "NO ", 3)) + { + if (!vl->northwt) + ft_settexture(vl, line, 1); + else + return (" north texture"); + } + return (NULL); +} + +char *ft_checkline2(t_varlist *vl, char *line) +{ + if (!ft_strncmp(line, "EA ", 3)) + { + if (!vl->eastwt) + ft_settexture(vl, line, 2); + else + return (" east texture"); + } + else if (!ft_strncmp(line, "SO ", 3)) + { + if (!vl->southwt) + ft_settexture(vl, line, 3); + else + return (" south texture"); + } + else if (!ft_strncmp(line, "WE ", 3)) + { + if (!vl->westwt) + ft_settexture(vl, line, 4); + else + return (" west texture"); + } + return (NULL); } t_varlist ft_parseconfigfile(t_varlist vl, char *filename) { int fd; char *line; + char *error; fd = open(filename, O_RDONLY); if (fd == -1) ft_errorexit("file does not exist", "parseconfigfile", 1); while (get_next_line(fd, &line)) { - ft_checkline(&vl, line); + error = ft_checkline(&vl, line); + if (!error) + error = ft_checkline2(&vl, line); + if (error) + ft_errorexit("Double config declaration in .cub file", error, 1); + if (vl.ccolor && vl.fcolor && vl.northwt && vl.eastwt && vl.southwt && vl.westwt) + vl.map = ft_getmap(&vl, fd); free(line); } + close(fd); free(line); - vl.map = ft_getmap(); return (vl); } diff --git a/src/raycast.c b/src/raycast.c index 2a37665..c54949e 100644 --- a/src/raycast.c +++ b/src/raycast.c @@ -6,7 +6,7 @@ /* By: djonker +#+ */ /* +#+ */ /* Created: 2023/10/27 14:36:42 by djonker #+# #+# */ -/* Updated: 2023/10/29 05:53:32 by houtworm ######## odam.nl */ +/* Updated: 2023/10/29 14:23:45 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -64,7 +64,7 @@ void ft_raycast(t_varlist *vl) vl->mapy += vl->stepy; vl->side = 1; } - if (vl->map[vl->mapx][vl->mapy] > 0) + if (vl->map[vl->mapx][vl->mapy] == '1') vl->hit = 1; } if (vl->side == 0)