Firstcommit

This commit is contained in:
root
2025-08-25 20:07:37 +00:00
commit 8eea3162d5
123 changed files with 2680 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_colors.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/25 18:41:26 by lfirmin #+# #+# */
/* Updated: 2025/08/25 18:41:26 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
int check_colors(t_textures *texture)
{
if (is_rgb(texture->floor))
return (ft_error(ERROR_COL), 1);
if (is_rgb(texture->ceiling))
return (ft_error(ERROR_COL), 1);
return (0);
}
int is_rgb(int color[3])
{
int i;
i = 0;
while (i < 3)
{
if (color[i] > 255 || color[i] < 0)
return (1);
i++;
}
return (0);
}
+40
View File
@@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_file.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/22 15:07:29 by lfirmin #+# #+# */
/* Updated: 2025/08/22 15:07:33 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
int check_file(char *map_path, t_data_parsing *parsing)
{
if (!map_path)
return (ft_error(ERROR_EMPT_PATH), 1);
if (check_extension(map_path))
return (1);
parsing->fd_map = open(map_path, O_RDONLY);
if (parsing->fd_map < 0)
return (perror("\033[1m[❌]\033[0m\033[1;31mError\033[0m"), 1);
parsing->fd_map_dup = open(map_path, O_RDONLY);
if (parsing->fd_map_dup < 0)
return (perror("\033[1m[❌]\033[0m\033[1;31mError\033[0m"), 1);
return (0);
}
int check_extension(char *map_path)
{
int len_map_path;
len_map_path = ft_strlen(map_path);
if (len_map_path < 4)
return (ft_error(ERROR_EXT), 1);
if (ft_strcmp(map_path + len_map_path - 4, ".cub") != 0)
return (ft_error(ERROR_EXT), 1);
return (0);
}
+85
View File
@@ -0,0 +1,85 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/23 13:29:28 by lfirmin #+# #+# */
/* Updated: 2025/08/23 13:29:28 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
int get_map(t_data *data)
{
int nb_line;
nb_line = line_counter(data->parsing.fd_map);
if (nb_line <= 0)
return (ft_error(ERROR_EMPTY), 1);
data->parsing.raw_map = ft_calloc(nb_line + 1, sizeof(char*));
put_map_on_array(data);
//print_char_array_debug(data->parsing.raw_map, "debug");
//printf("---deb_texture---\n%s\n%s\n%s\n%s\n", data->texture->north, data->texture->south, data->texture->west, data->texture->east);
// printf("---deb_colors_f---\n%d,%d,%d\n", data->texture->floor[0], data->texture->floor[1], data->texture->floor[2]);
// printf("---deb_colors_c---\n%d,%d,%d\n", data->texture->ceiling[0], data->texture->ceiling[1], data->texture->ceiling[2]);
return (0);
}
int line_counter(int fd)
{
int nb_line;
char *line;
line = NULL;
nb_line = 0;
while ((line = get_next_line(fd)) != NULL)
{
nb_line++;
free(line);
}
free(line);
line = get_next_line(-1); // force cleanup
free(line);
return (nb_line);
}
int put_map_on_array(t_data *data)
{
char *line;
char *cleaned;
int i;
i = 0;
while ((line = get_next_line(data->parsing.fd_map_dup)) != NULL)
{
if (is_config_line(line))
{
cleaned = clean_line(line);
get_texture_path(cleaned, data->texture);
free(line);
free(cleaned);
}
else if (is_empty_line(line))
free(line);
else
data->parsing.raw_map[i++] = line;
}
line = get_next_line(-1);
if (line)
free(line);
data->parsing.raw_map[i] = NULL;
return (0);
}
char *clean_line(char *raw_line)
{
char *cleaned;
if (!raw_line)
return (NULL);
cleaned = ft_strtrim(raw_line, " \t\n\r");
return (cleaned);
}
+53
View File
@@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_textures.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/24 12:37:11 by lfirmin #+# #+# */
/* Updated: 2025/08/24 12:37:11 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
int get_texture_path(char *line, t_textures *texture)
{
if (is_texture_line(line) == 1)
texture->north = ft_strtrim(line + 2, " \t\n\r");
else if (is_texture_line(line) == 2)
texture->south = ft_strtrim(line + 2, " \t\n\r");
else if (is_texture_line(line) == 3)
texture->west = ft_strtrim(line + 2, " \t\n\r");
else if (is_texture_line(line) == 4)
texture->east = ft_strtrim(line + 2, " \t\n\r");
else if (is_color_line(line) == 1) // F
get_rgb_values(line, texture->floor);
else if (is_color_line(line) == 2) // C
get_rgb_values(line, texture->ceiling);
return (0);
}
int get_rgb_values(char *line, int rgb[3])
{
char *start;
char **parts;
int i;
if (!line || !rgb)
return (1);
start = ft_strchr(line, ' ');
if (!start)
return (1);
parts = ft_split(start + 1, ',');
if (!parts)
return (1);
i = 0;
while (parts[i] && i < 3)
{
rgb[i] = ft_atoi(parts[i]);
i++;
}
free_char_array(parts);
return (i == 3 ? 0 : 1);
}
+50
View File
@@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init_parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/23 13:33:00 by lfirmin #+# #+# */
/* Updated: 2025/08/23 13:33:00 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
int init_parsing(t_data_parsing *parsing)
{
if (!parsing)
return (ft_error(ERROR_INIT_PARS), 1);
parsing->fd_map = 0;
parsing->raw_map = NULL;
return (0);
}
int init_textures(t_textures *textures)
{
int i;
if (!textures)
return (ft_error(ERROR_INIT_TEX), 1);
textures->north = NULL;
textures->south = NULL;
textures->east = NULL;
textures->west = NULL;
i = 0;
while (i < 3)
{
textures->floor[i] = -1;
textures->ceiling[i] = -1;
i++;
}
return (0);
}
int free_textures(t_textures *textures)
{
free(textures->north);
free(textures->south);
free(textures->east);
free(textures->west);
return (0);
}
+94
View File
@@ -0,0 +1,94 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* line_detect.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/24 11:58:39 by lfirmin #+# #+# */
/* Updated: 2025/08/24 11:58:39 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
int is_config_line(char *line)
{
char *trimmed;
int result;
result = 0;
if (!line)
return (0);
trimmed = ft_strtrim(line, " \t\n\r");
if (!trimmed)
return (0);
if (ft_strncmp(trimmed, "NO ", 3) == 0 ||
ft_strncmp(trimmed, "SO ", 3) == 0 ||
ft_strncmp(trimmed, "WE ", 3) == 0 ||
ft_strncmp(trimmed, "EA ", 3) == 0 ||
ft_strncmp(trimmed, "F ", 2) == 0 ||
ft_strncmp(trimmed, "C ", 2) == 0)
result = 1;
free(trimmed);
return (result);
}
int is_empty_line(char *line)
{
int i;
i = 0;
if (!line)
return (1); // NULL = vide
while (line[i])
{
if (line[i] != ' ' && line[i] != '\t' &&
line[i] != '\n' && line[i] != '\r')
return (0);
i++;
}
return (1); // Que des whitespaces
}
int is_color_line(char *line)
{
char *trimmed;
int result;
result = 0;
if (!line)
return (0);
trimmed = ft_strtrim(line, " \t\n\r");
if (!trimmed)
return (0);
if (ft_strncmp(trimmed, "F ", 2) == 0)
result = 1;
else if (ft_strncmp(trimmed, "C ", 2) == 0)
result = 2;
free(trimmed);
return (result);
}
int is_texture_line(char *line)
{
char *trimmed;
int result;
result = 0;
if (!line)
return (0);
trimmed = ft_strtrim(line, " \t\n\r");
if (!trimmed)
return (0);
if (ft_strncmp(trimmed, "NO ", 3) == 0)
result = 1;
else if (ft_strncmp(trimmed, "SO ", 3) == 0)
result = 2;
else if (ft_strncmp(trimmed, "WE ", 3) == 0)
result = 3;
else if (ft_strncmp(trimmed, "EA ", 3) == 0)
result = 4;
free(trimmed);
return (result);
}
+24
View File
@@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/22 14:17:58 by lfirmin #+# #+# */
/* Updated: 2025/08/22 14:17:58 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
int parsing(t_data *data)
{
if (check_file(data->map_path, &data->parsing) == 1)
return (1);
get_map(data);
close(data->parsing.fd_map);
close(data->parsing.fd_map_dup);
if (check_colors(data->texture) == 1)
return (1);
return (0);
}