mirror of
https://github.com/Ladebeze66/cub3D.git
synced 2026-11-09 13:47:10 +01:00
sprite
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/20 11:14:36 by fgras-ca ### ########.fr */
|
||||
/* Updated: 2024/01/28 22:48:56 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -94,7 +94,10 @@ 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");
|
||||
|
||||
@@ -6,13 +6,117 @@
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/11 14:49:45 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/01/11 20:02:42 by fgras-ca ### ########.fr */
|
||||
/* Updated: 2024/01/28 22:20:51 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../cub3d.h"
|
||||
|
||||
int is_map_closed(char *map, int maxWidth, int height)
|
||||
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)
|
||||
{
|
||||
if (!check_borders(map, maxWidth, height))
|
||||
return (0);
|
||||
@@ -32,52 +136,94 @@ int check_line_border(char *line, int lineLength, int y)
|
||||
return (1);
|
||||
}
|
||||
|
||||
int check_horizontal_borders(char *map, int maxWidth, int height)
|
||||
int check_vertical_borders(char *map, int maxWidth, int height)
|
||||
{
|
||||
int x;
|
||||
int x, y, first_wall_found, last_wall_found;
|
||||
|
||||
x = 0;
|
||||
while (x < maxWidth)
|
||||
{
|
||||
if (map[x] != '1' && map[x] != ' ')
|
||||
{
|
||||
printf("Border issue detected at top edge at (%d,0)\n", x);
|
||||
return (0);
|
||||
}
|
||||
if (map[(height - 1) * maxWidth + x] != '1'
|
||||
&& map[(height - 1) * maxWidth + x] != ' ')
|
||||
return (0);
|
||||
x++;
|
||||
}
|
||||
return (1);
|
||||
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_vertical_borders(char *map, int maxWidth, int height)
|
||||
int check_horizontal_borders(char *map, int maxWidth, int height)
|
||||
{
|
||||
int x;
|
||||
int columnheight;
|
||||
int x, y;
|
||||
|
||||
x = 0;
|
||||
while (x < maxWidth)
|
||||
{
|
||||
columnheight = 0;
|
||||
while (columnheight < height && map[columnheight * maxWidth + x] != ' ')
|
||||
{
|
||||
columnheight++;
|
||||
}
|
||||
if (columnheight > 0)
|
||||
{
|
||||
if (map[x] != '1')
|
||||
{
|
||||
printf("Border issue detected at top of column at (%d,0)\n", x);
|
||||
return (0);
|
||||
}
|
||||
if (map[(columnheight - 1) * maxWidth + x] != '1')
|
||||
return (0);
|
||||
}
|
||||
x++;
|
||||
}
|
||||
return (1);
|
||||
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)
|
||||
@@ -107,4 +253,4 @@ int check_borders(char *map, int maxWidth, int height)
|
||||
return (0);
|
||||
}
|
||||
return (check_vertical_borders(map, maxWidth, height));
|
||||
}
|
||||
}*/
|
||||
|
||||
@@ -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/23 20:06:58 by fgras-ca ### ########.fr */
|
||||
/* Updated: 2024/01/28 17:31:00 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
||||
@@ -6,56 +6,92 @@
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/11 19:28:43 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/01/16 15:22:26 by fgras-ca ### ########.fr */
|
||||
/* Updated: 2024/01/28 22:36:40 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../cub3d.h"
|
||||
|
||||
static int check_line(char *map, int maxWidth, int y, int linelength)
|
||||
/*static int check_line(char *map, int maxWidth, int height, int y)
|
||||
{
|
||||
int x;
|
||||
char c;
|
||||
int x;
|
||||
char c;
|
||||
|
||||
x = 1;
|
||||
while (x < linelength - 1)
|
||||
{
|
||||
c = map[y * maxWidth + x];
|
||||
if (c == ' ' && (map[y * maxWidth + (x - 1)] != '1'
|
||||
|| map[y * maxWidth + (x + 1)] != '1'
|
||||
|| (y > 0 && map[(y - 1) * maxWidth + x] != '1')
|
||||
|| (y < maxWidth - 1 && map[(y + 1) * maxWidth + x] != '1')))
|
||||
return (0);
|
||||
x++;
|
||||
}
|
||||
return (1);
|
||||
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 y)
|
||||
{
|
||||
int x;
|
||||
int linelength;
|
||||
|
||||
x = 0;
|
||||
linelength = 0;
|
||||
while (x < maxWidth && map[y * maxWidth + x] != ' ')
|
||||
{
|
||||
linelength++;
|
||||
x++;
|
||||
}
|
||||
return (check_line(map, maxWidth, y, linelength));
|
||||
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 check_interior(char *map, int maxWidth, int height)
|
||||
{
|
||||
int y;
|
||||
int y;
|
||||
|
||||
y = 0;
|
||||
while (y < height)
|
||||
{
|
||||
if (!check_row(map, maxWidth, y))
|
||||
return (0);
|
||||
y++;
|
||||
}
|
||||
return (1);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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/20 11:15:41 by fgras-ca ### ########.fr */
|
||||
/* Updated: 2024/01/28 18:06:00 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -40,31 +40,49 @@ bool parse_texture_line(const char *line, t_texture *textures)
|
||||
return (true);
|
||||
}
|
||||
|
||||
bool handle_map(int fd, char **map_buffer, int *map_length)
|
||||
{
|
||||
char line[MAX_LINE_LENGTH];
|
||||
int bytes_read;
|
||||
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;
|
||||
}
|
||||
|
||||
*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);
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user