versiontexturecouleur

This commit is contained in:
2024-01-23 17:39:25 +01:00
parent 84c17810af
commit ffca051a01
898 changed files with 381 additions and 124265 deletions

104
parsing/ft_find_map_start.c Normal file
View File

@@ -0,0 +1,104 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_find_map_start.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/15 20:04:23 by fgras-ca #+# #+# */
/* Updated: 2024/01/20 11:14:36 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
bool process_color_line(const char *line, t_texture *textures)
{
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 handle_colors(int fd, t_texture *textures)
{
char line[MAX_LINE_LENGTH];
int color_count;
char ch;
int i;
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 ((line[0] == 'F' || line[0] == 'C') && process_color_line(line, textures))
color_count++;
i = 0;
}
}
return (color_count == 2);
}
int open_file(const char *filename)
{
int fd;
fd = open(filename, O_RDONLY);
if (fd < 0)
{
perror("Error opening file");
}
return (fd);
}
bool cleanup_and_close(int fd, char *map_buffer, bool result)
{
if (map_buffer)
{
free(map_buffer);
}
close(fd);
return (result);
}
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;
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;
}
return (cleanup_and_close(fd, map_buffer, result));
}

View File

@@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 22:17:19 by fgras-ca #+# #+# */
/* Updated: 2024/01/11 22:19:14 by fgras-ca ### ########.fr */
/* Updated: 2024/01/15 22:37:27 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */

View File

@@ -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/11 22:17:53 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,17 +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 *filename, t_structure_map *map_info)
bool parse_map(const char *map_content, int length, t_structure_map *map_info)
{
char *buffer;
int length;
bool result;
if (!read_and_verify_map(filename, &buffer, &length))
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);
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);
}
result = process_map(buffer, length, map_info);
free(buffer);
return (result);
if (!init_map_info(map_info, max_width, height))
return (false);
return (copy_map_data_and_check(&map_params));
}

View File

@@ -6,39 +6,12 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 19:28:43 by fgras-ca #+# #+# */
/* Updated: 2024/01/11 20:02:52 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)
{
char *buffer;
FILE *file;
file = fopen(filename, "r");
if (!file)
{
perror("Error opening file");
return (NULL);
}
fseek(file, 0, SEEK_END);
*length = ftell(file);
fseek(file, 0, SEEK_SET);
buffer = malloc(*length + 1);
if (!buffer)
{
perror("Error allocating memory for map");
fclose(file);
return (NULL);
}
fread(buffer, 1, *length, file);
buffer[*length] = '\0';
fclose(file);
return (buffer);
}
static int check_line(char *map, int maxWidth, int y, int linelength)
{
int x;

View File

@@ -0,0 +1,142 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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/20 11:15:41 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);
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_number_from_str(const char **str, int *number)
{
*number = 0;
while (**str >= '0' && **str <= '9')
{
*number = *number * 10 + (**str - '0');
(*str)++;
}
if (**str != ',' && **str != ' ' && **str != '\0')
{
return (false);
}
return (true);
}
bool parse_color_line(const char *line, unsigned int *color)
{
int r;
int g;
int b;
const char *ptr;
r = 0;
g = 0;
b = 0;
ptr = line;
while (*ptr == ' ') ptr++;
if (!parse_number_from_str(&ptr, &r) || *ptr++ != ',')
return (false);
while (*ptr == ' ') ptr++;
if (!parse_number_from_str(&ptr, &g) || *ptr++ != ',')
return (false);
while (*ptr == ' ') ptr++;
if (!parse_number_from_str(&ptr, &b))
return (false);
*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);
}