cub3d/src/map.c

98 lines
2.2 KiB
C
Raw Normal View History

2023-10-26 17:40:01 +02:00
/* ************************************************************************** */
/* */
/* :::::::: */
/* map.c :+: :+: */
/* +:+ */
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 17:33:50 by houtworm #+# #+# */
2023-10-29 15:59:24 +01:00
/* Updated: 2023/10/29 15:23:13 by houtworm ######## odam.nl */
2023-10-26 17:40:01 +02:00
/* */
/* ************************************************************************** */
#include "../cub3d.h"
2023-10-29 14:27:34 +01:00
#include <stdio.h>
2023-10-26 17:40:01 +02:00
2023-10-29 14:27:34 +01:00
char ft_setplayerpos(t_varlist *vl, char dir, int y, int x)
2023-10-26 17:40:01 +02:00
{
2023-10-29 14:27:34 +01:00
vl->posx = x;
vl->posy = y;
if (dir == 'N')
{
2023-10-29 14:27:34 +01:00
vl->dirx = -1;
vl->diry = 0;
vl->planex = 0;
vl->planey = 0.66;
}
else if (dir == 'E')
{
2023-10-29 14:27:34 +01:00
vl->dirx = 0;
vl->diry = 1;
vl->planex = 0.66;
vl->planey = 0;
}
else if (dir == 'S')
{
vl->dirx = 1;
2023-10-29 14:27:34 +01:00
vl->diry = 0;
vl->planex = 0;
vl->planey = -0.66;
}
else if (dir == 'W')
{
vl->dirx = 0;
vl->diry = -1;
vl->planex = -0.66;
vl->planey = 0;
}
2023-10-29 14:27:34 +01:00
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);
2023-10-26 17:40:01 +02:00
y = 0;
2023-10-29 14:27:34 +01:00
ret = 1;
while (y <= 512 && ret > 0)
2023-10-26 17:40:01 +02:00
{
2023-10-29 15:59:24 +01:00
if (y > 500)
return (NULL);
2023-10-29 14:27:34 +01:00
map[y] = ft_calloc(512, 8);
while (ret > 0)
{
ret = get_next_line(fd, &line);
if (line[0])
break ;
}
if (ret == 0)
{
2023-10-29 15:59:24 +01:00
free(map[y]);
2023-10-29 14:27:34 +01:00
free(line);
return (map);
}
2023-10-26 17:40:01 +02:00
x = 0;
2023-10-29 14:27:34 +01:00
while (x <= 512 && line[x])
2023-10-26 17:40:01 +02:00
{
2023-10-29 15:59:24 +01:00
if (x > 500)
return (NULL);
2023-10-29 14:27:34 +01:00
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);
2023-10-26 17:40:01 +02:00
x++;
}
2023-10-29 14:27:34 +01:00
free(line);
2023-10-26 17:40:01 +02:00
y++;
}
2023-10-29 15:59:24 +01:00
ft_errorexit("Something went wrong", "ft_getmap", 1);
return (0);
2023-10-26 17:40:01 +02:00
}