cub3d/src/map.c

83 lines
2.0 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 14:27:34 +01:00
/* Updated: 2023/10/29 14:25:40 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' || 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);
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 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)
{
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);
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 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 14:27:34 +01:00
free(line);
2023-10-26 17:40:01 +02:00
return (map);
}