mirror of
https://github.com/Ladebeze66/cub3D.git
synced 2026-11-09 21:57:08 +01:00
manquecorrigetouchesetparsing
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/15 20:04:23 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/01/16 17:47:49 by fgras-ca ### ########.fr */
|
||||
/* Updated: 2024/01/21 11:54:07 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -43,7 +43,7 @@ bool handle_colors(int fd, t_texture *textures)
|
||||
{
|
||||
line[i] = '\0';
|
||||
printf("Ligne lue pour les couleurs: '%s'\n", line);
|
||||
if (process_color_line(line, textures))
|
||||
if ((line[0] == 'F' || line[0] == 'C') && process_color_line(line, textures))
|
||||
color_count++;
|
||||
i = 0;
|
||||
}
|
||||
@@ -100,9 +100,5 @@ bool load_cub_file(const char *filename,
|
||||
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,12 +6,33 @@
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/11 14:43:46 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/01/16 15:22:35 by fgras-ca ### ########.fr */
|
||||
/* Updated: 2024/01/21 13:21:28 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../cub3d.h"
|
||||
|
||||
void calculate_map_scale(t_structure_map *map_info)
|
||||
{
|
||||
int maps_x;
|
||||
int maps_y;
|
||||
|
||||
map_info->wd.width = WIDTH;
|
||||
map_info->wd.height = HEIGHT;
|
||||
if (map_info->mapX == 0 || map_info->mapY == 0)
|
||||
{
|
||||
fprintf(stderr, "Erreur: mapX ou mapY est zéro.\n");
|
||||
map_info->mapS = 0;
|
||||
return ;
|
||||
}
|
||||
maps_x = map_info->wd.width / (map_info->mapX * 4);
|
||||
maps_y = map_info->wd.height / (map_info->mapY * 2);
|
||||
if (maps_x < maps_y)
|
||||
map_info->mapS = maps_x;
|
||||
else
|
||||
map_info->mapS = maps_y;
|
||||
}
|
||||
|
||||
bool init_map_info(t_structure_map *map_info, int maxWidth, int height)
|
||||
{
|
||||
map_info->mapX = maxWidth;
|
||||
@@ -39,7 +60,52 @@ bool copy_map_data_and_check(t_map_params *params)
|
||||
return (true);
|
||||
}
|
||||
|
||||
bool process_map(const char *buffer, int length, t_structure_map *map_info)
|
||||
bool find_player_position_and_direction(const char *map_content, int length, t_structure_map *map_info)
|
||||
{
|
||||
int i;
|
||||
int line_number;
|
||||
int column_number;
|
||||
bool found_player;
|
||||
char current_char;
|
||||
|
||||
i = 0;
|
||||
line_number = 0;
|
||||
column_number = 0;
|
||||
found_player = false;
|
||||
calculate_map_scale(map_info);
|
||||
while (i < length)
|
||||
{
|
||||
current_char = map_content[i];
|
||||
if (current_char == 'N' || current_char == 'S' || current_char == 'E' || current_char == 'W')
|
||||
{
|
||||
if (found_player)
|
||||
{
|
||||
printf("Multiple player start positions found. Invalid map.\n");
|
||||
return (false);
|
||||
}
|
||||
found_player = true;
|
||||
map_info->player_x = column_number * map_info->mapS + map_info->mapS / 2;
|
||||
map_info->player_y = line_number * map_info->mapS + map_info->mapS / 2;
|
||||
map_info->player_direction = current_char;
|
||||
}
|
||||
if (current_char == '\n')
|
||||
{
|
||||
line_number++;
|
||||
column_number = 0;
|
||||
}
|
||||
else
|
||||
column_number++;
|
||||
i++;
|
||||
}
|
||||
if (!found_player)
|
||||
{
|
||||
printf("Player start position not found. Invalid map.\n");
|
||||
return (false);
|
||||
}
|
||||
return (true);
|
||||
}
|
||||
|
||||
bool parse_map(const char *buffer, int length, t_structure_map *map_info)
|
||||
{
|
||||
int maxwidth;
|
||||
int height;
|
||||
@@ -62,35 +128,8 @@ bool process_map(const char *buffer, int length, t_structure_map *map_info)
|
||||
return (false);
|
||||
}
|
||||
if (!init_map_info(map_info, maxwidth, height))
|
||||
{
|
||||
return (false);
|
||||
}
|
||||
return (copy_map_data_and_check(&map_params));
|
||||
}
|
||||
|
||||
bool parse_map(const char *map_content, int length, t_structure_map *map_info)
|
||||
{
|
||||
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);
|
||||
}
|
||||
if (!init_map_info(map_info, max_width, height))
|
||||
if (!find_player_position_and_direction(buffer, length, map_info))
|
||||
return (false);
|
||||
return (copy_map_data_and_check(&map_params));
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -6,7 +6,7 @@
|
||||
/* 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 */
|
||||
/* Updated: 2024/01/20 11:15:41 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -37,7 +37,6 @@ bool parse_texture_line(const char *line, t_texture *textures)
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -66,25 +65,41 @@ bool handle_map(int fd, char **map_buffer, int *map_length)
|
||||
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 r;
|
||||
int g;
|
||||
int b;
|
||||
int i;
|
||||
const char *ptr;
|
||||
|
||||
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');
|
||||
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);
|
||||
}
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user