168 lines
4.7 KiB
C
168 lines
4.7 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* parse_config_line.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2025/04/22 13:10:06 by whaffman #+# #+# */
|
|
/* Updated: 2025/05/28 14:32:43 by qmennen ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "cub3d.h"
|
|
|
|
mlx_texture_t *load_texture(const char *path)
|
|
{
|
|
mlx_texture_t *texture;
|
|
|
|
printf("Loading texture: |%s|\n", path);
|
|
texture = mlx_load_png(path);
|
|
if (texture == NULL)
|
|
{
|
|
ft_putstr_fd("Error: Failed to load texture\n", 2);
|
|
return (NULL);
|
|
}
|
|
return (texture);
|
|
}
|
|
|
|
int handle_wall(char *token, t_map *map)
|
|
{
|
|
const char *wall_tokens[] = {
|
|
"NO", "SO", "WE", "EA", NULL
|
|
};
|
|
char *texture_path;
|
|
int i;
|
|
|
|
i = 0;
|
|
texture_path = ft_strtok(NULL, " ");
|
|
if (texture_path == NULL)
|
|
return (ft_putstr_fd("Error: Missing texture path\n", 2), FAILURE);
|
|
if (ft_strtok(NULL, " ") != NULL)
|
|
return (ft_putstr_fd("Error: Extra tokens after path\n", 2), FAILURE);
|
|
while (wall_tokens[i])
|
|
{
|
|
if (ft_strcmp(token, wall_tokens[i]) == 0)
|
|
{
|
|
map->textures[i] = load_texture(texture_path);
|
|
if (map->textures[i] == NULL)
|
|
return (FAILURE);
|
|
}
|
|
i++;
|
|
}
|
|
return (SUCCESS);
|
|
}
|
|
|
|
int handle_fc_color(char *token, t_map *map)
|
|
{
|
|
char *color_str;
|
|
int color;
|
|
|
|
color_str = ft_strtok(NULL, " ");
|
|
if (!color_str)
|
|
return (ft_putstr_fd("Error: Missing color value\n", 2), FAILURE);
|
|
if (ft_strtok(NULL, " "))
|
|
return (ft_putstr_fd("Error: Extra tokens after color value\n", 2),
|
|
FAILURE);
|
|
color = parse_color(color_str);
|
|
if (!color)
|
|
return (ft_putstr_fd("Error: Invalid color value\n", 2), FAILURE);
|
|
if (ft_strcmp(token, "F") == 0)
|
|
map->floor_color = color;
|
|
else if (ft_strcmp(token, "C") == 0)
|
|
map->ceiling_color = color;
|
|
return (SUCCESS);
|
|
}
|
|
|
|
int handle_fc_texture(char *token, t_map *map)
|
|
{
|
|
char *texture_path;
|
|
|
|
texture_path = ft_strtok(NULL, " ");
|
|
if (!texture_path)
|
|
return (ft_putstr_fd("Error: Missing texture path\n", 2), FAILURE);
|
|
if (ft_strtok(NULL, " "))
|
|
return (ft_putstr_fd("Error: Extra tokens after path\n", 2), FAILURE);
|
|
if (ft_strcmp(token, "FT") == 0)
|
|
{
|
|
map->texture_floor = load_texture(texture_path);
|
|
if (map->texture_floor == NULL)
|
|
return (FAILURE);
|
|
}
|
|
else if (ft_strcmp(token, "CT") == 0)
|
|
{
|
|
map->texture_ceiling = load_texture(texture_path);
|
|
if (map->texture_ceiling == NULL)
|
|
return (FAILURE);
|
|
}
|
|
return (SUCCESS);
|
|
}
|
|
|
|
int handle_sprite(char *token, t_map *map)
|
|
{
|
|
t_sprite_lib sprite;
|
|
char symbol;
|
|
char *texture_path;
|
|
|
|
if (token[1] == 'c')
|
|
sprite.collectible = 1;
|
|
else
|
|
sprite.collectible = 0;
|
|
symbol = *ft_strtok(NULL, " ");
|
|
if (symbol < 'a' || symbol > 'z')
|
|
return (ft_putstr_fd("Error: Invalid sprite symbol\n", 2), FAILURE);
|
|
texture_path = ft_strtok(NULL, " ");
|
|
if (texture_path == NULL)
|
|
return (ft_putstr_fd("Error: Missing texture path\n", 2), FAILURE);
|
|
if (ft_strtok(NULL, " ") != NULL)
|
|
return (ft_putstr_fd("Error: Extra tokens after path\n", 2), FAILURE);
|
|
sprite.texture = load_texture(texture_path);
|
|
if (sprite.texture == NULL)
|
|
return (FAILURE);
|
|
if (map->sprite_lib[symbol - 'a'].texture != NULL)
|
|
return (ft_putstr_fd(
|
|
"Error: Sprite already defined for this symbol\n", 2), FAILURE);
|
|
map->sprite_lib[symbol - 'a'] = sprite;
|
|
return (SUCCESS);
|
|
}
|
|
|
|
t_token_handler handle_config_token(const char *token, t_map *map)
|
|
{
|
|
const char *config_tokens[] = {
|
|
"NO", "SO", "WE", "EA", "F", "C", "FT", "CT", "-c", "-s", NULL
|
|
};
|
|
const t_token_handler handlers[] = {
|
|
handle_wall, handle_wall, handle_wall, handle_wall,
|
|
handle_fc_color, handle_fc_color, handle_fc_texture, handle_fc_texture,
|
|
handle_sprite, handle_sprite, NULL
|
|
};
|
|
int i;
|
|
|
|
i = 0;
|
|
printf("Token: %s\n", token);
|
|
while (config_tokens[i] != NULL)
|
|
{
|
|
if (ft_strcmp(token, config_tokens[i]) == 0)
|
|
return (handlers[i]);
|
|
i++;
|
|
}
|
|
return (NULL);
|
|
}
|
|
|
|
int parse_config_line(char *line, t_map *map)
|
|
{
|
|
char *token;
|
|
t_token_handler handler;
|
|
|
|
token = ft_strtok(line, " ");
|
|
if (token == NULL)
|
|
return (0);
|
|
handler = handle_config_token(token, map);
|
|
if (handler == NULL)
|
|
return (ft_putstr_fd("Error: Invalid config token\n", 2), FAILURE);
|
|
if (handler(token, map) == 0)
|
|
return (ft_putstr_fd("Error: Failed to handle config token\n", 2),
|
|
FAILURE);
|
|
return (SUCCESS);
|
|
}
|