manqueunefonctionnorme

This commit is contained in:
2024-01-29 20:26:06 +01:00
parent fcbbe51d52
commit 7d67dfd452
121 changed files with 996 additions and 2858 deletions

View File

@@ -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/28 22:48:56 by fgras-ca ### ########.fr */
/* Updated: 2024/01/29 18:02:53 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@@ -43,7 +43,8 @@ bool handle_colors(int fd, t_texture *textures)
{
line[i] = '\0';
printf("Ligne lue pour les couleurs: '%s'\n", line);
if ((line[0] == 'F' || line[0] == 'C') && process_color_line(line, textures))
if ((line[0] == 'F' || line[0] == 'C')
&& process_color_line(line, textures))
color_count++;
i = 0;
}
@@ -94,10 +95,7 @@ bool load_cub_file(const char *filename,
return (cleanup_and_close(fd, map_buffer, false));
}
if (map_buffer)
{
//printf("Map buffer loaded:\n%s\n", map_buffer);
result = parse_map(map_buffer, map_length, map_info);
}
else
{
printf("No map data found in file.\n");

View File

@@ -6,97 +6,60 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/23 19:54:51 by fgras-ca #+# #+# */
/* Updated: 2024/01/23 20:22:20 by fgras-ca ### ########.fr */
/* Updated: 2024/01/29 18:58:31 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
static void initialize_variables(int *i, int *line_number, int *column_number, bool *found_player)
bool is_player_char(char c)
{
*i = 0;
*line_number = 0;
*column_number = 0;
*found_player = false;
return (c == 'N' || c == 'S' || c == 'E' || c == 'W');
}
static bool check_for_player(char current_char, bool *found_player)
static bool process_player_char(char current_char, t_player_info *player_info)
{
if (current_char == 'N' || current_char == 'S' || current_char == 'E' || current_char == 'W')
if (is_player_char(current_char))
{
if (*found_player)
if (check_for_multiple_players(*(player_info->found_player)))
{
*(player_info->found_player) = true;
update_player_info(player_info->map, player_info->line_number,
player_info->column_number, current_char);
}
else
{
printf("Multiple player start positions found. Invalid map.\n");
return false;
return (false);
}
*found_player = true;
}
return true;
return (true);
}
static void update_player_info(t_structure_map *map_info, int line_number, int column_number, char player_direction)
bool find_player(const char *map_content, int length, t_structure_map *map)
{
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 = player_direction;
printf("Player found: x = %f, y = %f, direction = %c\n", map_info->player_x, map_info->player_y, map_info->player_direction);
}
int i;
bool found_player;
t_player_info player_info;
static void update_position(char current_char, int *line_number, int *column_number)
{
if (current_char == '\n')
i = 0;
initialize_variables(&i, &player_info.line_number,
&player_info.column_number, &found_player);
player_info.map = map;
player_info.found_player = &found_player;
while (i < length)
{
(*line_number)++;
*column_number = 0;
}
else
{
(*column_number)++;
if (!found_player)
{
if (!process_player_char(map_content[i], &player_info))
return (false);
}
calculate_map(map);
update_position(map_content[i],
&player_info.line_number, &player_info.column_number);
i++;
}
if (!found_player)
return (false);
return (true);
}
static bool check_for_multiple_players(bool found_player)
{
if (found_player)
{
printf("Multiple player start positions found. Invalid map.\n");
return false;
}
return true;
}
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;
initialize_variables(&i, &line_number, &column_number, &found_player);
while (i < length)
{
char current_char = map_content[i];
if (!found_player && (current_char == 'N' || current_char == 'S' || current_char == 'E' || current_char == 'W'))
{
if (check_for_multiple_players(found_player))
{
found_player = true;
update_player_info(map_info, line_number, column_number, current_char);
}
else
{
return false;
}
}
calculate_map(map_info);
update_position(current_char, &line_number, &column_number);
i++;
}
if (!found_player)
{
printf("Player start position not found. Invalid map.\n");
return false;
}
return true;
}

View File

@@ -0,0 +1,71 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_find_player_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/29 18:57:38 by fgras-ca #+# #+# */
/* Updated: 2024/01/29 18:58:26 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void initialize_variables(int *i, int *line_number,
int *column_number, bool *found_player)
{
*i = 0;
*line_number = 0;
*column_number = 0;
*found_player = false;
}
bool check_for_player(char current_char, bool *found_player)
{
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;
}
return (true);
}
void update_player_info(t_structure_map *map_info,
int line_number, int column_number, char player_direction)
{
map_info->player_x = column_number * map_info->map_s + map_info->map_s / 2;
map_info->player_y = line_number * map_info->map_s + map_info->map_s / 2;
map_info->player_direction = player_direction;
printf("Player found: x = %f, y = %f, direction = %c\n", map_info->player_x,
map_info->player_y, map_info->player_direction);
}
void update_position(char current_char,
int *line_number, int *column_number)
{
if (current_char == '\n')
{
(*line_number)++;
*column_number = 0;
}
else
{
(*column_number)++;
}
}
bool check_for_multiple_players(bool found_player)
{
if (found_player)
{
printf("Multiple player start positions found. Invalid map.\n");
return (false);
}
return (true);
}

View File

@@ -6,251 +6,105 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 14:49:45 by fgras-ca #+# #+# */
/* Updated: 2024/01/28 22:20:51 by fgras-ca ### ########.fr */
/* Updated: 2024/01/29 19:46:50 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
int is_space_surrounded_by_walls(char *map, int maxWidth, int height, int x, int y) {
// Vérifie si les cellules adjacentes à un espace sont toutes des murs ('1')
return (x > 0 && map[y * maxWidth + (x - 1)] == '1') &&
(x < maxWidth - 1 && map[y * maxWidth + (x + 1)] == '1') &&
(y > 0 && map[(y - 1) * maxWidth + x] == '1') &&
(y < height - 1 && map[(y + 1) * maxWidth + x] == '1');
}
int flood_fill_and_check(char *map, char *visitedMap, int maxWidth, int height, int x, int y, char replacement) {
if (x < 0 || x >= maxWidth || y < 0 || y >= height) {
return 0; // Ouverture détectée
}
if (visitedMap[y * maxWidth + x] == 1 || map[y * maxWidth + x] == '1') {
return 1; // Mur ou déjà visité
}
if (map[y * maxWidth + x] == ' ' && !is_space_surrounded_by_walls(map, maxWidth, height, x, y)) {
return 0; // Espace non entouré correctement par des murs, ouverture intérieure détectée
}
visitedMap[y * maxWidth + x] = 1; // Marquer la cellule comme visitée
map[y * maxWidth + x] = replacement; // Marquer la cellule dans la carte principale
// Appliquer récursivement à toutes les directions
return flood_fill_and_check(map, visitedMap, maxWidth, height, x + 1, y, replacement) &&
flood_fill_and_check(map, visitedMap, maxWidth, height, x - 1, y, replacement) &&
flood_fill_and_check(map, visitedMap, maxWidth, height, x, y + 1, replacement) &&
flood_fill_and_check(map, visitedMap, maxWidth, height, x, y - 1, replacement);
}
int is_surrounded_by_walls(char *map, int maxWidth, int height, int x, int y) {
// Vérifier les limites
if (x <= 0 || x >= maxWidth - 1 || y <= 0 || y >= height - 1) return 1;
// Vérifier les cellules adjacentes
if (map[(y - 1) * maxWidth + x] == '1' && map[(y + 1) * maxWidth + x] == '1' &&
map[y * maxWidth + (x - 1)] == '1' && map[y * maxWidth + (x + 1)] == '1') {
return 1;
}
return 0;
}
int is_map_closed(char *map, int maxWidth, int height) {
char *copy_map = malloc(maxWidth * height);
if (copy_map)
{
ft_memcpy(copy_map, map, maxWidth * height);
}
char *visitedMap = calloc(maxWidth * height, sizeof(char));
if (!visitedMap) {
printf("Erreur d'allocation mémoire pour visitedMap.\n");
return 0;
}
int startX = -1, startY = -1;
printf("Début de la vérification de fermeture de la carte.\n");
/* Affichage de la carte avant flood_fill
printf("Carte avant flood_fill:\n");
for (int y = 0; y < height; y++) {
for (int x = 0; x < maxWidth; x++) {
printf("%c", map[y * maxWidth + x]);
}
printf("\n");
}*/
// Trouver un point de départ valide
for (int y = 0; y < height && startX == -1; y++) {
for (int x = 0; x < maxWidth && startX == -1; x++) {
if ((map[y * maxWidth + x] == '0' || map[y * maxWidth + x] == '2') && !is_surrounded_by_walls(map, maxWidth, height, x, y)) {
startX = x;
startY = y;
printf("Point de départ trouvé pour flood_fill à (%d, %d)\n", startX, startY);
break;
}
}
}
if (startX == -1) {
printf("Aucun point de départ valide trouvé.\n");
free(visitedMap);
return 1; // Si aucun point de départ n'est trouvé, la carte est considérée comme fermée
}
int is_closed = flood_fill_and_check(copy_map, visitedMap, maxWidth, height, startX, startY, '3');
/*printf("Carte après flood_fill:\n");
for (int y = 0; y < height; y++) {
for (int x = 0; x < maxWidth; x++) {
printf("%c", visitedMap[y * maxWidth + x] ? '3' : map[y * maxWidth + x]);
}
printf("\n");
}*/
free(visitedMap);
free(copy_map);
return is_closed;
}
/*int is_map_closed(char *map, int maxWidth, int height)
static int flood_fill_and_check(t_map_check *map_check,
char *visitedMap, t_point p, char replacement)
{
if (!check_borders(map, maxWidth, height))
if (p.x < 0 || p.x >= map_check->maxWidth
|| p.y < 0 || p.y >= map_check->height)
return (0);
if (!check_interior(map, maxWidth, height))
return (0);
return (1);
}
int check_line_border(char *line, int lineLength, int y)
{
if (lineLength == 0)
if (visitedMap[p.y * map_check->maxWidth + p.x] == 1
|| map_check->map[p.y * map_check->maxWidth + p.x] == '1')
return (1);
if (line[0] != '1')
if (map_check->map[p.y * map_check->maxWidth + p.x] == ' '
&& !is_space_surrounded_by_walls(map_check, p))
return (0);
if (line[lineLength - 1] != '1')
return (0);
return (1);
visitedMap[p.y * map_check->maxWidth + p.x] = 1;
map_check->map[p.y * map_check->maxWidth + p.x] = replacement;
return (flood_fill_and_check(map_check, visitedMap,
(t_point){p.x + 1, p.y}, replacement)
&& flood_fill_and_check(map_check, visitedMap,
(t_point){p.x - 1, p.y}, replacement)
&& flood_fill_and_check(map_check, visitedMap,
(t_point){p.x, p.y + 1}, replacement)
&& flood_fill_and_check(map_check, visitedMap,
(t_point){p.x, p.y - 1}, replacement));
}
int check_vertical_borders(char *map, int maxWidth, int height)
{
int x, y, first_wall_found, last_wall_found;
for (x = 0; x < maxWidth; x++)
{
first_wall_found = 0;
last_wall_found = height - 1;
// Trouver le premier '1' dans la colonne
for (y = 0; y < height; y++)
{
if (map[y * maxWidth + x] == '1')
{
first_wall_found = 1;
break;
}
}
// Trouver le dernier '1' dans la colonne
for (y = height - 1; y >= 0; y--)
{
if (map[y * maxWidth + x] == '1')
{
last_wall_found = y;
break;
}
}
// Si aucun mur trouvé, la colonne est incorrecte
if (!first_wall_found || last_wall_found == height - 1)
{
printf("Border issue detected in column %d\\n", x);
return 0;
}
// Vérifier que la colonne est correctement fermée
for (y = first_wall_found; y <= last_wall_found; y++)
{
if (map[y * maxWidth + x] != '1' && map[y * maxWidth + x] != ' ')
{
printf("Border issue detected in column %d at position %d\\n", x, y);
return 0;
}
}
}
return 1;
}
int check_horizontal_borders(char *map, int maxWidth, int height)
{
int x, y;
for (y = 0; y < height; y += height - 1) // Vérifier seulement la première et la dernière ligne
{
int first_wall_found = 0;
int last_wall_found = 0;
// Trouver le premier '1' dans la ligne
for (x = 0; x < maxWidth; x++)
{
if (map[y * maxWidth + x] == '1')
{
first_wall_found = 1;
break;
}
}
// Trouver le dernier '1' dans la ligne
for (x = maxWidth - 1; x >= 0; x--)
{
if (map[y * maxWidth + x] == '1')
{
last_wall_found = 1;
break;
}
}
// Si aucun mur '1' n'est trouvé au début ou à la fin de la ligne
if (!first_wall_found || !last_wall_found)
{
printf("Border issue detected in line %d\\n", y);
return 0;
}
}
return 1;
}
int check_borders(char *map, int maxWidth, int height)
static int find_start_point(t_map_check *map_check, t_point *startPoint)
{
int y;
int linelength;
int x;
y = 0;
while (y < height)
while (y < map_check->height)
{
linelength = 0;
x = 0;
while (x < maxWidth && map[y * maxWidth + x] != ' ')
while (x < map_check->maxWidth)
{
linelength++;
if ((map_check->map[y * map_check->maxWidth + x] == '0'
|| map_check->map[y * map_check->maxWidth + x] == '2')
&& !is_space_surrounded_by_walls(map_check, (t_point){x, y}))
{
startPoint->x = x;
startPoint->y = y;
return (1);
}
x++;
}
if (!check_line_border(&map[y * maxWidth], linelength, y))
{
return (0);
}
y++;
}
if (!check_horizontal_borders(map, maxWidth, height))
return (0);
}
static int initialize_maps(char **copy_map, char **visited_map,
int maxWidth, int height)
{
*copy_map = malloc(maxWidth * height);
*visited_map = calloc(maxWidth * height, sizeof(char));
if (!(*copy_map) || !(*visited_map))
{
free(*copy_map);
free(*visited_map);
printf("Erreur d'allocation mémoire.\n");
return (0);
}
return (check_vertical_borders(map, maxWidth, height));
}*/
return (1);
}
static void cleanup_and_print_error(char *visited_map, char *copy_map)
{
free(visited_map);
free(copy_map);
printf("Aucun point de départ valide trouvé.\n");
}
int is_map_closed(char *map, int maxWidth, int height)
{
char *copy_map;
char *visited_map;
t_map_check map_check;
t_point start_point;
int is_closed;
map_check.maxWidth = maxWidth;
map_check.height = height;
if (!initialize_maps(&copy_map, &visited_map, maxWidth, height))
return (0);
ft_memcpy(copy_map, map, maxWidth * height);
map_check.map = copy_map;
if (!find_start_point(&map_check, &start_point))
{
cleanup_and_print_error(visited_map, copy_map);
return (1);
}
is_closed = flood_fill_and_check(&map_check, visited_map, start_point, '3');
free(visited_map);
free(copy_map);
return (is_closed);
}

View File

@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_map_check_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/29 19:44:27 by fgras-ca #+# #+# */
/* Updated: 2024/01/29 19:48:01 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
int check_boundaries(t_point p, t_map_check *map_check)
{
return (p.x > 0 && p.x < map_check->maxWidth - 1
&& p.y > 0 && p.y < map_check->height - 1);
}
int is_space_surrounded_by_walls(t_map_check *map_check, t_point p)
{
if (!check_boundaries(p, map_check))
return (0);
return (map_check->map[p.y * map_check->maxWidth + (p.x - 1)] == '1'
&& map_check->map[p.y * map_check->maxWidth + (p.x + 1)] == '1'
&& map_check->map[(p.y - 1) * map_check->maxWidth + p.x] == '1'
&& map_check->map[(p.y + 1) * map_check->maxWidth + p.x] == '1');
}

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/15 22:37:27 by fgras-ca ### ########.fr */
/* Updated: 2024/01/29 16:05:03 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,12 +15,12 @@
void update_max_dimensions(t_map_params *params)
{
(*(params->height))++;
if (*(params->currentWidth) > *(params->maxWidth))
if (*(params->current_width) > *(params->max_width))
{
*(params->maxWidth) = *(params->currentWidth);
*(params->max_width) = *(params->current_width);
}
*(params->currentWidth) = 0;
*(params->isNewLine) = 1;
*(params->current_width) = 0;
*(params->is_new_line) = 1;
}
void process_character(t_map_params *params, int *i)
@@ -31,8 +31,8 @@ void process_character(t_map_params *params, int *i)
}
else if (params->buffer[*i] != '\r')
{
(*(params->currentWidth))++;
*(params->isNewLine) = 0;
(*(params->current_width))++;
*(params->is_new_line) = 0;
}
(*i)++;
}
@@ -46,16 +46,16 @@ void get_map_dimensions(t_map_params *params)
i = 0;
currentwidth = 0;
isnewline = 1;
*(params->maxWidth) = 0;
*(params->max_width) = 0;
*(params->height) = 0;
params->currentWidth = &currentwidth;
params->isNewLine = &isnewline;
params->current_width = &currentwidth;
params->is_new_line = &isnewline;
while (i < params->length)
process_character(params, &i);
if (!*(params->isNewLine))
if (!*(params->is_new_line))
update_max_dimensions(params);
printf("Map dimensions: maxWidth=%d, height=%d\n",
*(params->maxWidth), *(params->height));
*(params->max_width), *(params->height));
}
void fill_map_space(t_structure_map *map_info, int maxWidth, int height)
@@ -88,9 +88,9 @@ void copy_map_data(t_map_params *params)
y++;
x = 0;
}
else if (params->buffer[i] != '\r' && x < *params->maxWidth)
else if (params->buffer[i] != '\r' && x < *params->max_width)
{
params->map_info->map[y * *params->maxWidth + x]
params->map_info->map[y * *params->max_width + x]
= params->buffer[i];
x++;
}

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/28 17:31:00 by fgras-ca ### ########.fr */
/* Updated: 2024/01/29 19:51:28 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,8 +14,8 @@
bool init_map_info(t_structure_map *map_info, int maxWidth, int height)
{
map_info->mapX = maxWidth;
map_info->mapY = height;
map_info->map_x = maxWidth;
map_info->map_y = height;
map_info->s_win.width = WIDTH;
map_info->s_win.height = HEIGHT;
map_info->map = malloc(maxWidth * height);
@@ -31,7 +31,8 @@ bool init_map_info(t_structure_map *map_info, int maxWidth, int height)
bool copy_map_data_and_check(t_map_params *params)
{
copy_map_data(params);
if (!is_map_closed(params->map_info->map, *params->maxWidth, *params->height))
if (!is_map_closed(params->map_info->map,
*params->max_width, *params->height))
{
printf("Map is not closed!\n");
free(params->map_info->map);
@@ -45,23 +46,24 @@ void calculate_map(t_structure_map *map_info)
float maps_x;
float maps_y;
maps_x = (float)map_info->s_win.width / (map_info->mapX * 4);
maps_y = (float)map_info->s_win.height / (map_info->mapY * 2);
maps_x = (float)map_info->s_win.width / (map_info->map_x * 4);
maps_y = (float)map_info->s_win.height / (map_info->map_y * 2);
if (maps_x < maps_y)
{
map_info->mapS = (int)maps_x;
map_info->map_s = (int)maps_x;
}
else
{
map_info->mapS = (int)maps_y;
map_info->map_s = (int)maps_y;
}
if (map_info->mapS == 0)
if (map_info->map_s == 0)
{
map_info->mapS = 1;
map_info->map_s = 1;
}
}
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;
@@ -72,7 +74,7 @@ bool parse_map(const char *map_content, int length, t_structure_map *map_info)
map_params.map_info = map_info;
map_params.buffer = map_content;
map_params.length = length;
map_params.maxWidth = &max_width;
map_params.max_width = &max_width;
map_params.height = &height;
get_map_dimensions(&map_params);
if (max_width <= 0 || height <= 0)
@@ -82,7 +84,7 @@ bool parse_map(const char *map_content, int length, t_structure_map *map_info)
}
if (!init_map_info(map_info, max_width, height))
return (false);
if (!find_player_position_and_direction(map_content, length, map_info))
if (!find_player(map_content, length, map_info))
return (false);
return (copy_map_data_and_check(&map_params));
}

View File

@@ -1,97 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_read_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 19:28:43 by fgras-ca #+# #+# */
/* Updated: 2024/01/28 22:36:40 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
/*static int check_line(char *map, int maxWidth, int height, int y)
{
int x;
char c;
for (x = 0; x < maxWidth; x++)
{
c = map[y * maxWidth + x];
if (c == ' ' && ((x > 0 && map[y * maxWidth + (x - 1)] != '1') ||
(x < maxWidth - 1 && map[y * maxWidth + (x + 1)] != '1') ||
(y > 0 && map[(y - 1) * maxWidth + x] != '1') ||
(y < height - 1 && map[(y + 1) * maxWidth + x] != '1')))
return (0);
}
return (1);
}
static int check_row(char *map, int maxWidth, int height, int y)
{
return check_line(map, maxWidth, height, y);
}
int check_interior(char *map, int maxWidth, int height)
{
int y;
for (y = 0; y < height; y++)
{
if (!check_row(map, maxWidth, height, y))
return (0);
}
return (1);
}*/
void load_sprite_frames(t_sprite *sprite, void *mlx_ptr) {
sprite->frames[0] = mlx_xpm_file_to_image(mlx_ptr, "sprite/pnj/pnj01.xpm", &sprite->width, &sprite->height);
if (sprite->frames[0] == NULL) {
printf("Erreur de chargement de sprite/pnj/pnj01.xpm\n");
} else {
printf("Sprite pnj01.xpm chargé avec succès. Dimensions: %d x %d\n", sprite->width, sprite->height);
}
sprite->frames[1] = mlx_xpm_file_to_image(mlx_ptr, "sprite/pnj/pnj02.xpm", &sprite->width, &sprite->height);
if (sprite->frames[1] == NULL) {
printf("Erreur de chargement de sprite/pnj/pnj01.xpm\n");
} else {
printf("Sprite pnj02.xpm chargé avec succès. Dimensions: %d x %d\n", sprite->width, sprite->height);
}
sprite->frames[2] = mlx_xpm_file_to_image(mlx_ptr, "sprite/pnj/pnj03.xpm", &sprite->width, &sprite->height);
if (sprite->frames[2] == NULL) {
printf("Erreur de chargement de sprite/pnj/pnj01.xpm\n");
} else {
printf("Sprite pnj03.xpm chargé avec succès. Dimensions: %d x %d\n", sprite->width, sprite->height);
}
}
void update_sprite_frame(t_sprite *sprite) {
static int counter = 0;
counter = (counter + 1) % 60;
if (counter == 0) {
sprite->current_frame = (sprite->current_frame + 1) % 3;
printf("Frame du sprite mise à jour : %d\n", sprite->current_frame);
}
}
void draw_sprite(t_sprite *sprite, void *mlx_ptr, void *win_ptr, int win_width, int win_height) {
if (sprite->frames[sprite->current_frame] != NULL) {
float posX = 170;
float posY = 30;
// Logs pour débogage
printf("Dessin du sprite (frame %d)...\n", sprite->current_frame);
printf("Dimensions du sprite : largeur = %d, hauteur = %d\n", sprite->width, sprite->height);
printf("Coordonnées de dessin du sprite : X = %f, Y = %f\n", posX, posY);
// Dessiner le sprite
mlx_put_image_to_window(mlx_ptr, win_ptr, sprite->frames[sprite->current_frame], posX, posY);
printf("Sprite dessiné aux coordonnées: %f, %f\n", posX, posY);
} else {
// Log en cas d'erreur
printf("Erreur lors du dessin du sprite (frame %d)\n", sprite->current_frame);
}
}

View File

@@ -0,0 +1,91 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_text_and_col_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/29 20:07:44 by fgras-ca #+# #+# */
/* Updated: 2024/01/29 20:17:23 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 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);
}

View File

@@ -6,128 +6,53 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/16 16:19:13 by fgras-ca #+# #+# */
/* Updated: 2024/01/28 18:06:00 by fgras-ca ### ########.fr */
/* Updated: 2024/01/29 20:17:57 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
bool parse_texture_line(const char *line, t_texture *textures)
bool handle_map(int fd, char **map_buffer, int *map_length)
{
char direction[3];
char path[MAX_LINE_LENGTH];
int i;
int j;
char *line;
size_t len;
ssize_t read;
bool start_copying;
FILE *stream;
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) {
FILE *stream = fdopen(fd, "r");
if (!stream) {
perror("Error converting file descriptor to FILE *");
return false;
}
char *line = NULL;
size_t len = 0;
ssize_t read;
bool start_copying = false;
*map_length = 0;
*map_buffer = NULL;
while ((read = getline(&line, &len, stream)) != -1) {
// Vérifier si la ligne contient '1' ou '0'
if (!start_copying && (ft_strchr(line, '1') || ft_strchr(line, '0'))) {
start_copying = true;
}
// Commencer la copie si on a trouvé une ligne contenant '1' ou '0'
if (start_copying) {
*map_buffer = realloc(*map_buffer, *map_length + read + 1);
if (!*map_buffer) {
perror("Error reallocating memory for map buffer");
free(line);
fclose(stream);
return (false);
}
ft_memcpy(*map_buffer + *map_length, line, read);
*map_length += read;
(*map_buffer)[*map_length] = '\0';
}
}
free(line);
fclose(stream);
return (*map_buffer != NULL);
}
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')
stream = fdopen(fd, "r");
line = NULL;
len = 0;
start_copying = false;
*map_length = 0;
*map_buffer = NULL;
if (!stream)
{
perror("Error converting file descriptor to FILE *");
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);
while ((read = getline(&line, &len, stream)) != -1)
{
if (!start_copying && (ft_strchr(line, '1') || ft_strchr(line, '0')))
start_copying = true;
if (start_copying)
{
*map_buffer = realloc(*map_buffer, *map_length + read + 1);
if (!*map_buffer)
{
perror("Error reallocating memory for map buffer");
free(line);
fclose(stream);
return (false);
}
ft_memcpy(*map_buffer + *map_length, line, read);
*map_length += read;
(*map_buffer)[*map_length] = '\0';
}
}
free(line);
fclose(stream);
return (*map_buffer != NULL);
}
bool handle_textures(int fd, t_texture *textures)