mirror of
https://github.com/Ladebeze66/cub3D.git
synced 2026-11-09 13:47:10 +01:00
dudu
This commit is contained in:
@@ -6,155 +6,103 @@
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/15 20:04:23 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/01/15 22:38:37 by fgras-ca ### ########.fr */
|
||||
/* Updated: 2024/01/16 17:47:49 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../cub3d.h"
|
||||
|
||||
bool parse_texture_line(const char *line, t_texture *textures)
|
||||
bool process_color_line(const char *line, t_texture *textures)
|
||||
{
|
||||
char direction[3];
|
||||
char path[MAX_LINE_LENGTH];
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
|
||||
// Extraction de la direction (NO, SO, etc.)
|
||||
while (line[i] != ' ' && line[i] != '\0' && i < 2)
|
||||
{
|
||||
direction[i] = line[i];
|
||||
i++;
|
||||
}
|
||||
direction[i] = '\0';
|
||||
|
||||
// Saut des espaces
|
||||
while (line[i] == ' ' && line[i] != '\0')
|
||||
i++;
|
||||
|
||||
// Extraction du chemin
|
||||
while (line[i] != ' ' && line[i] != '\0' && j < MAX_LINE_LENGTH - 1)
|
||||
{
|
||||
path[j] = line[i];
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
path[j] = '\0';
|
||||
|
||||
// Assignation à la structure
|
||||
if (strcmp(direction, "NO") == 0)
|
||||
textures->north = strdup(path);
|
||||
else if (strcmp(direction, "SO") == 0)
|
||||
textures->south = strdup(path);
|
||||
else if (strcmp(direction, "WE") == 0)
|
||||
textures->west = strdup(path);
|
||||
else if (strcmp(direction, "EA") == 0)
|
||||
textures->east = strdup(path);
|
||||
|
||||
return (true);
|
||||
if (line[0] == 'F')
|
||||
parse_color_line(line + 1, &textures->floor_color);
|
||||
else if (line[0] == 'C')
|
||||
parse_color_line(line + 1, &textures->ceil_color);
|
||||
else
|
||||
return (false);
|
||||
printf("Couleur reconnue et traitée: '%s'\n", line);
|
||||
return (true);
|
||||
}
|
||||
|
||||
bool parse_color_line(const char *line, unsigned int *color)
|
||||
bool handle_colors(int fd, t_texture *textures)
|
||||
{
|
||||
int r = 0, g = 0, b = 0;
|
||||
int i = 0;
|
||||
char line[MAX_LINE_LENGTH];
|
||||
int color_count;
|
||||
char ch;
|
||||
int i;
|
||||
|
||||
// Extraction des composantes de couleur
|
||||
while (line[i] != ',' && line[i] != '\0')
|
||||
{
|
||||
r = r * 10 + (line[i] - '0');
|
||||
i++;
|
||||
}
|
||||
i++; // Passer la virgule
|
||||
|
||||
while (line[i] != ',' && line[i] != '\0')
|
||||
{
|
||||
g = g * 10 + (line[i] - '0');
|
||||
i++;
|
||||
}
|
||||
i++; // Passer la virgule
|
||||
|
||||
while (line[i] != ' ' && line[i] != '\0')
|
||||
{
|
||||
b = b * 10 + (line[i] - '0');
|
||||
i++;
|
||||
}
|
||||
|
||||
*color = (r << 16) | (g << 8) | b;
|
||||
return (true);
|
||||
color_count = 0;
|
||||
i = 0;
|
||||
while (color_count < 2 && read(fd, &ch, 1) > 0)
|
||||
{
|
||||
if (ch != '\n')
|
||||
{
|
||||
line[i++] = ch;
|
||||
}
|
||||
else
|
||||
{
|
||||
line[i] = '\0';
|
||||
printf("Ligne lue pour les couleurs: '%s'\n", line);
|
||||
if (process_color_line(line, textures))
|
||||
color_count++;
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
return (color_count == 2);
|
||||
}
|
||||
|
||||
bool load_cub_file(const char *filename, t_texture *textures, t_structure_map *map_info)
|
||||
int open_file(const char *filename)
|
||||
{
|
||||
int fd;
|
||||
char line[MAX_LINE_LENGTH];
|
||||
char *map_buffer = NULL;
|
||||
int map_length = 0;
|
||||
int bytes_read;
|
||||
bool is_map_started = false;
|
||||
int fd;
|
||||
|
||||
// Ouvrir le fichier
|
||||
fd = open(filename, O_RDONLY);
|
||||
if (fd < 0)
|
||||
{
|
||||
perror("Error opening file");
|
||||
return (false);
|
||||
}
|
||||
fd = open(filename, O_RDONLY);
|
||||
if (fd < 0)
|
||||
{
|
||||
perror("Error opening file");
|
||||
}
|
||||
return (fd);
|
||||
}
|
||||
|
||||
while ((bytes_read = read(fd, line, MAX_LINE_LENGTH - 1)) > 0)
|
||||
{
|
||||
line[bytes_read] = '\0'; // Assurer que la ligne est terminée par un caractère nul
|
||||
printf("Ligne lue : %s\n", line); // Log pour vérifier chaque ligne lue
|
||||
if (!is_map_started)
|
||||
{
|
||||
// Chercher si la ligne actuelle est le début de la carte
|
||||
if (line[0] == ' ' || line[0] == '1' || line[0] == '0')
|
||||
{
|
||||
is_map_started = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Continuer à analyser les textures et couleurs
|
||||
if (line[0] == 'F' || line[0] == 'C')
|
||||
parse_color_line(line + 1, line[0] == 'F' ? &textures->floor_color : &textures->ceil_color);
|
||||
else
|
||||
parse_texture_line(line, textures);
|
||||
bool cleanup_and_close(int fd, char *map_buffer, bool result)
|
||||
{
|
||||
if (map_buffer)
|
||||
{
|
||||
free(map_buffer);
|
||||
}
|
||||
close(fd);
|
||||
return (result);
|
||||
}
|
||||
|
||||
continue; // Passer au tour suivant de la boucle
|
||||
}
|
||||
}
|
||||
bool load_cub_file(const char *filename,
|
||||
t_texture *textures, t_structure_map *map_info)
|
||||
{
|
||||
char *map_buffer;
|
||||
int map_length;
|
||||
bool result;
|
||||
int fd;
|
||||
|
||||
// Si la carte a commencé, ajouter la ligne à map_buffer
|
||||
if (is_map_started)
|
||||
{
|
||||
map_buffer = realloc(map_buffer, map_length + bytes_read + 1);
|
||||
if (!map_buffer)
|
||||
{
|
||||
perror("Error reallocating memory for map buffer");
|
||||
close(fd);
|
||||
return (false);
|
||||
}
|
||||
memcpy(map_buffer + map_length, line, bytes_read);
|
||||
map_length += bytes_read;
|
||||
map_buffer[map_length] = '\0'; // Assurer que la chaîne est terminée
|
||||
printf("Carte stockée :\n%s\n", map_buffer); // Log pour voir le contenu final de la carte
|
||||
printf("Longueur de la carte stockée : %d\n", map_length); // Log pour voir la longueur de la carte stockée
|
||||
}
|
||||
}
|
||||
|
||||
close(fd);
|
||||
|
||||
// Traiter la carte stockée en mémoire
|
||||
if (map_buffer)
|
||||
{
|
||||
printf("Calling parse_map with map content...\n");
|
||||
bool result = parse_map(map_buffer, map_length, map_info);
|
||||
printf("Result of parse_map: %s\n", result ? "Success" : "Failure");
|
||||
free(map_buffer); // Libérer la mémoire allouée pour map_buffer
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("No map data found in file.\n");
|
||||
return (false);
|
||||
}
|
||||
map_buffer = NULL;
|
||||
map_length = 0;
|
||||
fd = open_file(filename);
|
||||
if (fd < 0)
|
||||
return (false);
|
||||
if (!handle_textures(fd, textures) || !handle_colors(fd, textures))
|
||||
return (cleanup_and_close(fd, map_buffer, false));
|
||||
if (!handle_map(fd, &map_buffer, &map_length))
|
||||
{
|
||||
perror("Error reading map");
|
||||
return (cleanup_and_close(fd, map_buffer, false));
|
||||
}
|
||||
if (map_buffer)
|
||||
result = parse_map(map_buffer, map_length, map_info);
|
||||
else
|
||||
{
|
||||
printf("No map data found in file.\n");
|
||||
result = false;
|
||||
}
|
||||
printf("loadcubfile %s\n", textures->north);
|
||||
printf("loadcubfile %s\n", textures->south);
|
||||
printf("loadcubfile %s\n", textures->east);
|
||||
printf("loadcubfile %s\n", textures->west);
|
||||
return (cleanup_and_close(fd, map_buffer, result));
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -6,7 +6,7 @@
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/11 14:43:46 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/01/15 22:22:00 by fgras-ca ### ########.fr */
|
||||
/* Updated: 2024/01/16 15:22:35 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -39,18 +39,6 @@ bool copy_map_data_and_check(t_map_params *params)
|
||||
return (true);
|
||||
}
|
||||
|
||||
bool read_and_verify_map(const char *filename, char **buffer, int *length)
|
||||
{
|
||||
*length = 0;
|
||||
*buffer = read_map(filename, length);
|
||||
if (!(*buffer))
|
||||
{
|
||||
printf("Failed to read the map file.\n");
|
||||
return (false);
|
||||
}
|
||||
return (true);
|
||||
}
|
||||
|
||||
bool process_map(const char *buffer, int length, t_structure_map *map_info)
|
||||
{
|
||||
int maxwidth;
|
||||
@@ -80,35 +68,29 @@ bool process_map(const char *buffer, int length, t_structure_map *map_info)
|
||||
return (copy_map_data_and_check(&map_params));
|
||||
}
|
||||
|
||||
bool parse_map(const char *map_content, int length, t_structure_map *map_info)
|
||||
bool parse_map(const char *map_content, int length, t_structure_map *map_info)
|
||||
{
|
||||
t_map_params map_params;
|
||||
int max_width = 0;
|
||||
int height = 0;
|
||||
t_map_params map_params;
|
||||
int max_width;
|
||||
int height;
|
||||
|
||||
max_width = 0;
|
||||
height = 0;
|
||||
printf("Received map content (length = %d): \n%s\n", length, map_content);
|
||||
// Initialisation de la structure de paramètres pour le traitement de la carte
|
||||
map_params.map_info = map_info;
|
||||
map_params.buffer = map_content;
|
||||
map_params.length = length;
|
||||
|
||||
map_params.maxWidth = &max_width;
|
||||
map_params.height = &height;
|
||||
map_params.currentWidth = NULL;
|
||||
map_params.isNewLine = NULL;
|
||||
// Obtenir les dimensions de la carte
|
||||
get_map_dimensions(&map_params);
|
||||
if (max_width <= 0 || height <= 0)
|
||||
{
|
||||
printf("Invalid map dimensions: maxWidth=%d, height=%d\n", max_width, height);
|
||||
return (false);
|
||||
}
|
||||
|
||||
// Initialiser les informations de la carte
|
||||
if (!init_map_info(map_info, max_width, height))
|
||||
{
|
||||
return (false);
|
||||
}
|
||||
|
||||
// Copier les données de la carte et vérifier si la carte est fermée
|
||||
return copy_map_data_and_check(&map_params);
|
||||
map_params.map_info = map_info;
|
||||
map_params.buffer = map_content;
|
||||
map_params.length = length;
|
||||
map_params.maxWidth = &max_width;
|
||||
map_params.height = &height;
|
||||
map_params.currentWidth = NULL;
|
||||
map_params.isNewLine = NULL;
|
||||
get_map_dimensions(&map_params);
|
||||
if (max_width <= 0 || height <= 0)
|
||||
{
|
||||
printf("Invalid map dimensions: maxWidth=%d, height=%d\n", max_width, height);
|
||||
return (false);
|
||||
}
|
||||
if (!init_map_info(map_info, max_width, height))
|
||||
return (false);
|
||||
return (copy_map_data_and_check(&map_params));
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -6,66 +6,12 @@
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/11 19:28:43 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/01/15 21:48:46 by fgras-ca ### ########.fr */
|
||||
/* Updated: 2024/01/16 15:22:26 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../cub3d.h"
|
||||
|
||||
char *read_map(const char *filename, int *length)
|
||||
{
|
||||
int file;
|
||||
char buffer[1024]; // Taille de tampon temporaire pour la lecture
|
||||
int total_length = 0;
|
||||
int bytes_read;
|
||||
char *map_content;
|
||||
|
||||
// Ouvrir le fichier pour la première lecture
|
||||
file = open(filename, O_RDONLY);
|
||||
if (file < 0)
|
||||
{
|
||||
perror("Error opening file");
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
// Lire le fichier pour déterminer sa taille
|
||||
while ((bytes_read = read(file, buffer, sizeof(buffer))) > 0)
|
||||
total_length += bytes_read;
|
||||
|
||||
// Fermer le fichier après la première lecture
|
||||
close(file);
|
||||
|
||||
// Allouer de la mémoire pour le contenu de la carte
|
||||
map_content = (char *)malloc(total_length + 1);
|
||||
if (!map_content)
|
||||
{
|
||||
perror("Error allocating memory for map");
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
// Rouvrir le fichier pour la deuxième lecture
|
||||
file = open(filename, O_RDONLY);
|
||||
if (file < 0)
|
||||
{
|
||||
perror("Error opening file");
|
||||
free(map_content);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
// Lire le contenu du fichier dans le tampon
|
||||
*length = 0;
|
||||
while ((bytes_read = read(file, map_content + *length, total_length - *length)) > 0)
|
||||
*length += bytes_read;
|
||||
|
||||
// Ajouter le caractère de fin de chaîne
|
||||
map_content[*length] = '\0';
|
||||
|
||||
// Fermer le fichier
|
||||
close(file);
|
||||
|
||||
return map_content;
|
||||
}
|
||||
|
||||
static int check_line(char *map, int maxWidth, int y, int linelength)
|
||||
{
|
||||
int x;
|
||||
|
||||
Binary file not shown.
127
versionmod/parsing/ft_textures_and_colors.c
Normal file
127
versionmod/parsing/ft_textures_and_colors.c
Normal file
@@ -0,0 +1,127 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_textures_and_colors.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/16 16:19:13 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/01/16 17:31:12 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../cub3d.h"
|
||||
|
||||
bool parse_texture_line(const char *line, t_texture *textures)
|
||||
{
|
||||
char direction[3];
|
||||
char path[MAX_LINE_LENGTH];
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
while (line[i] != ' ' && line[i] != '\0' && i < 2)
|
||||
direction[i++] = line[i];
|
||||
direction[i] = '\0';
|
||||
while (line[i] == ' ' && line[i] != '\0')
|
||||
i++;
|
||||
while (line[i] != ' ' && line[i] != '\0' && j < MAX_LINE_LENGTH - 1)
|
||||
path[j++] = line[i++];
|
||||
path[j] = '\0';
|
||||
if (ft_strcmp(direction, "NO") == 0)
|
||||
textures->north = ft_strdup(path);
|
||||
else if (ft_strcmp(direction, "SO") == 0)
|
||||
textures->south = ft_strdup(path);
|
||||
else if (ft_strcmp(direction, "WE") == 0)
|
||||
textures->west = ft_strdup(path);
|
||||
else if (ft_strcmp(direction, "EA") == 0)
|
||||
textures->east = ft_strdup(path);
|
||||
printf("parse texture %s\n", textures->north);
|
||||
return (true);
|
||||
}
|
||||
|
||||
bool handle_map(int fd, char **map_buffer, int *map_length)
|
||||
{
|
||||
char line[MAX_LINE_LENGTH];
|
||||
int bytes_read;
|
||||
|
||||
*map_length = 0;
|
||||
bytes_read = read(fd, line, MAX_LINE_LENGTH - 1);
|
||||
while (bytes_read > 0)
|
||||
{
|
||||
printf("bytes read %d\n", bytes_read);
|
||||
line[bytes_read] = '\0';
|
||||
*map_buffer = realloc(*map_buffer, *map_length + bytes_read + 1);
|
||||
if (!*map_buffer)
|
||||
{
|
||||
perror("Error reallocating memory for map buffer");
|
||||
return (false);
|
||||
}
|
||||
ft_memcpy(*map_buffer + *map_length, line, bytes_read);
|
||||
*map_length += bytes_read;
|
||||
(*map_buffer)[*map_length] = '\0';
|
||||
bytes_read = read(fd, line, MAX_LINE_LENGTH - 1);
|
||||
}
|
||||
return (true);
|
||||
}
|
||||
|
||||
bool parse_color_line(const char *line, unsigned int *color)
|
||||
{
|
||||
int r;
|
||||
int g;
|
||||
int b;
|
||||
int i;
|
||||
|
||||
r = 0;
|
||||
g = 0;
|
||||
b = 0;
|
||||
i = 0;
|
||||
while (line[i] != ',' && line[i] != '\0')
|
||||
r = r * 10 + (line[i++] - '0');
|
||||
i++;
|
||||
while (line[i] != ',' && line[i] != '\0')
|
||||
g = g * 10 + (line[i++] - '0');
|
||||
i++;
|
||||
while (line[i] != ' ' && line[i] != '\0')
|
||||
b = b * 10 + (line[i++] - '0');
|
||||
*color = (r << 16) | (g << 8) | b;
|
||||
return (true);
|
||||
}
|
||||
|
||||
bool is_valid_texture(const char *line)
|
||||
{
|
||||
return (ft_strncmp(line, "NO ", 3) == 0
|
||||
|| ft_strncmp(line, "SO ", 3) == 0
|
||||
|| ft_strncmp(line, "WE ", 3) == 0
|
||||
|| ft_strncmp(line, "EA ", 3) == 0);
|
||||
}
|
||||
|
||||
bool handle_textures(int fd, t_texture *textures)
|
||||
{
|
||||
char line[MAX_LINE_LENGTH];
|
||||
int bytes_read;
|
||||
int texture_count;
|
||||
char ch;
|
||||
int i;
|
||||
|
||||
texture_count = 0;
|
||||
i = 0;
|
||||
while (texture_count < 4 && read(fd, &ch, 1) > 0)
|
||||
{
|
||||
if (ch != '\n')
|
||||
line[i++] = ch;
|
||||
else
|
||||
{
|
||||
line[i] = '\0';
|
||||
if (is_valid_texture(line))
|
||||
{
|
||||
parse_texture_line(line, textures);
|
||||
texture_count++;
|
||||
printf("Texture reconnue et traitée: '%s'\n", line);
|
||||
}
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
return (texture_count == 4);
|
||||
}
|
||||
BIN
versionmod/parsing/ft_textures_and_colors.o
Normal file
BIN
versionmod/parsing/ft_textures_and_colors.o
Normal file
Binary file not shown.
Reference in New Issue
Block a user