cub3d/src/parse.c

144 lines
3.4 KiB
C
Raw Normal View History

/* ************************************************************************** */
/* */
/* :::::::: */
/* parse.c :+: :+: */
/* +:+ */
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 16:48:55 by houtworm #+# #+# */
2023-10-29 15:59:24 +01:00
/* Updated: 2023/10/29 15:14:40 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
2023-10-27 20:07:11 +02:00
#include <stdio.h>
2023-10-29 14:27:34 +01:00
int ft_settexture(t_varlist *vl, char *line, int direction)
2023-10-27 20:07:11 +02:00
{
int i;
printf("line: %s\n", line);
i = 0;
while (line[i] != '.')
i++;
2023-10-29 14:27:34 +01:00
if (direction == 1)
2023-10-27 20:07:11 +02:00
vl->northwt = ft_strdup(&line[i]);
2023-10-29 14:27:34 +01:00
if (direction == 2)
2023-10-27 20:07:11 +02:00
vl->eastwt = ft_strdup(&line[i]);
2023-10-29 14:27:34 +01:00
if (direction == 3)
2023-10-27 20:07:11 +02:00
vl->southwt = ft_strdup(&line[i]);
2023-10-29 14:27:34 +01:00
if (direction == 4)
2023-10-27 20:07:11 +02:00
vl->westwt = ft_strdup(&line[i]);
return (0);
}
2023-10-29 14:27:34 +01:00
int ft_setcolor(t_varlist *vl, char *line, int dest)
2023-10-27 20:07:11 +02:00
{
int i;
int r;
int g;
int b;
i = 0;
while (line[i] < '0' || line[i] > '9')
i++;
r = ft_atoi(&line[i]);
while (line[i] >= '0' && line[i] <= '9')
i++;
while (line[i] < '0' || line[i] > '9')
i++;
g = ft_atoi(&line[i]);
while (line[i] >= '0' && line[i] <= '9')
i++;
while (line[i] < '0' || line[i] > '9')
i++;
b = ft_atoi(&line[i]);
2023-10-29 14:27:34 +01:00
if (dest == 1)
2023-10-27 20:07:11 +02:00
vl->ccolor = (r << 24 | g << 16 | b << 8 | 255);
2023-10-29 14:27:34 +01:00
if (dest == 2)
vl->fcolor = (r << 24 | g << 16 | b << 8 | 255);
2023-10-27 20:07:11 +02:00
return (0);
}
2023-10-29 14:27:34 +01:00
char *ft_checkline(t_varlist *vl, char *line)
2023-10-27 20:07:11 +02:00
{
2023-10-29 14:27:34 +01:00
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);
2023-10-27 20:07:11 +02:00
}
2023-10-26 17:40:01 +02:00
t_varlist ft_parseconfigfile(t_varlist vl, char *filename)
{
2023-10-26 17:40:01 +02:00
int fd;
char *line;
2023-10-29 14:27:34 +01:00
char *error;
2023-10-26 17:40:01 +02:00
fd = open(filename, O_RDONLY);
if (fd == -1)
ft_errorexit("file does not exist", "parseconfigfile", 1);
while (get_next_line(fd, &line))
{
2023-10-29 14:27:34 +01:00
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)
2023-10-29 15:59:24 +01:00
{
2023-10-29 14:27:34 +01:00
vl.map = ft_getmap(&vl, fd);
2023-10-29 15:59:24 +01:00
break ;
}
2023-10-26 17:40:01 +02:00
free(line);
}
2023-10-29 14:27:34 +01:00
close(fd);
2023-10-27 20:07:11 +02:00
free(line);
if (!vl.map)
ft_errorexit("The map is too big, 500x500 max\n", "", 1);
2023-10-26 17:40:01 +02:00
return (vl);
}