This commit is contained in:
whaffman 2025-04-18 10:53:35 +02:00
parent cbeb143cfa
commit 17e3d56ddb
7 changed files with 236 additions and 110 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
*.o
*.d
*.a
cub3d
cub3D

View File

@ -1,12 +1,12 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/15 11:48:46 by whaffman #+# #+# #
# Updated: 2025/04/15 12:25:12 by qmennen ### ########.fr #
# :::::::: #
# Makefile :+: :+: #
# +:+ #
# By: qmennen <qmennen@student.codam.nl> +#+ #
# +#+ #
# Created: 2024/10/15 11:48:46 by whaffman #+# #+# #
# Updated: 2025/04/18 10:53:00 by whaffman ######## odam.nl #
# #
# **************************************************************************** #
@ -119,7 +119,7 @@ clean:
$(info $(bold)$(red)Cleaning$(reset))
find $(BUILD_PATH) -type d -name 'obj' -exec $(RM) {} +
$(MAKE) -C $(LIBFT_PATH) fclean
$(MAKE) -C $(MLX42_PATH)/build fclean
$(MAKE) -C $(MLX42_PATH)/build clean
# Remove build artifacts and the executable
fclean: clean

Binary file not shown.

BIN
cub3D

Binary file not shown.

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cub3d.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 12:22:29 by qmennen #+# #+# */
/* Updated: 2025/04/15 12:51:19 by qmennen ### ########.fr */
/* :::::::: */
/* cub3d.h :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/15 12:22:29 by qmennen #+# #+# */
/* Updated: 2025/04/15 18:07:02 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -18,6 +18,7 @@
typedef enum TILE
{
TILE_VISITED = -2,
TILE_VOID = -1,
TILE_EMPTY = 0,
TILE_WALL = 1,

View File

@ -15,94 +15,94 @@ typedef struct s_things
mlx_image_t* img;
} t_things;
// Exit the program as failure.
static void ft_error(void)
{
fprintf(stderr, "%s", mlx_strerror(mlx_errno));
exit(EXIT_FAILURE);
}
// // Exit the program as failure.
// static void ft_error(void)
// {
// fprintf(stderr, "%s", mlx_strerror(mlx_errno));
// exit(EXIT_FAILURE);
// }
static int check_bounds(int x, int y, int width, int height)
{
if (x < 0 || x >= width || y < 0 || y >= height)
return (1);
return (0);
}
// static int check_bounds(int x, int y, int width, int height)
// {
// if (x < 0 || x >= width || y < 0 || y >= height)
// return (1);
// return (0);
// }
static void draw_circle(mlx_image_t* img, int x, int y, int radius, uint32_t color)
{
for (int i = -radius; i <= radius; i++)
{
for (int j = -radius; j <= radius; j++)
{
if (i * i + j * j <= radius * radius)
{
if (check_bounds(x + i, y + j, img->width, img->height))
continue;
mlx_put_pixel(img, x + i, y + j, color << 8 |(int)(((float)radius * radius - i * i - j*j ) / (float)(radius * radius) * 255));
}
}
}
}
// static void draw_circle(mlx_image_t* img, int x, int y, int radius, uint32_t color)
// {
// for (int i = -radius; i <= radius; i++)
// {
// for (int j = -radius; j <= radius; j++)
// {
// if (i * i + j * j <= radius * radius)
// {
// if (check_bounds(x + i, y + j, img->width, img->height))
// continue;
// mlx_put_pixel(img, x + i, y + j, color << 8 |(int)(((float)radius * radius - i * i - j*j ) / (float)(radius * radius) * 255));
// }
// }
// }
// }
static void draw_random_circle(mlx_image_t* img)
{
int x = rand() % img->width;
int y = rand() % img->height;
int radius = rand() % 50 + 10; // Random radius between 10 and 60
uint32_t color = (rand() % 256 << 16) | (rand() % 256 << 8) | (rand() % 256);
draw_circle(img, x, y, radius, color);
}
// static void draw_random_circle(mlx_image_t* img)
// {
// int x = rand() % img->width;
// int y = rand() % img->height;
// int radius = rand() % 50 + 10; // Random radius between 10 and 60
// uint32_t color = (rand() % 256 << 16) | (rand() % 256 << 8) | (rand() % 256);
// draw_circle(img, x, y, radius, color);
// }
// Print the window width and height.
static void ft_hook(void* param)
{
t_things *arg = param;
const mlx_t* mlx = arg->mlx;
mlx_image_t* img = arg->img;
static float last_time;
// // Print the window width and height.
// static void ft_hook(void* param)
// {
// t_things *arg = param;
// const mlx_t* mlx = arg->mlx;
// mlx_image_t* img = arg->img;
// static float last_time;
last_time += mlx->delta_time;
// printf("delta_time: %f last_time: %f\n", mlx->delta_time, last_time);
if (last_time > 0.05)
{
// last_time += mlx->delta_time;
// // printf("delta_time: %f last_time: %f\n", mlx->delta_time, last_time);
// if (last_time > 0.05)
// {
draw_random_circle(img);
printf("FPS: %d\n", (int)(1.0 / last_time));
last_time = 0;
}
}
// draw_random_circle(img);
// printf("FPS: %d\n", (int)(1.0 / last_time));
// last_time = 0;
// }
// }
int32_t main(void)
{
t_things *things;
// MLX allows you to define its core behaviour before startup.
// int32_t main(void)
// {
// t_things *things;
// // MLX allows you to define its core behaviour before startup.
things = malloc(sizeof(t_things));
if (!things)
ft_error();
// things = malloc(sizeof(t_things));
// if (!things)
// ft_error();
mlx_t* mlx = mlx_init(WIDTH, HEIGHT, "42Balls", true);
if (!mlx)
ft_error();
// mlx_t* mlx = mlx_init(WIDTH, HEIGHT, "42Balls", true);
// if (!mlx)
// ft_error();
things->mlx = mlx;
/* Do stuff */
// things->mlx = mlx;
// /* Do stuff */
// Create and display the image.
mlx_image_t* img = mlx_new_image(mlx, WIDTH, HEIGHT);
if (!img || (mlx_image_to_window(mlx, img, 0, 0) < 0))
ft_error();
things->img = img;
// // Create and display the image.
// mlx_image_t* img = mlx_new_image(mlx, WIDTH, HEIGHT);
// if (!img || (mlx_image_to_window(mlx, img, 0, 0) < 0))
// ft_error();
// things->img = img;
// Register a hook and pass mlx as an optional param.
// NOTE: Do this before calling mlx_loop!
mlx_loop_hook(mlx, ft_hook, things);
mlx_loop(mlx);
mlx_delete_image(mlx, img);
mlx_terminate(mlx);
free(things);
// // Register a hook and pass mlx as an optional param.
// // NOTE: Do this before calling mlx_loop!
// mlx_loop_hook(mlx, ft_hook, things);
// mlx_loop(mlx);
// mlx_delete_image(mlx, img);
// mlx_terminate(mlx);
// free(things);
return (EXIT_SUCCESS);
}
// return (EXIT_SUCCESS);
// }

161
src/map.c
View File

@ -1,42 +1,167 @@
#include "stdlib.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "cub3d.h"
#include "libft.h"
#define FAILURE 0
#define SUCCESS 1
t_tile **get_temp_map()
t_map *get_temp_map()
{
const t_tile map[10][10] =
const t_tile const_map[10][10] =
{
{-1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1, 1, 1},
{1, 0, 0, 2, 0, 0, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 1, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1, 1, 1},
{1, 0, 0, 0, 1, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 1, 0, 0, 0, 0, 1},
{1, 0, 0, 2, 0, 0, 0, 1, -1, 1},
{1, 1, 1, 1, 0, 1, 0, 1, -1, -1 },
{1, 1, -1, 1, 0, 1, 1, 1, -1, 1},
{1, 1, 1, 1, 0, 0, 0, 1, 1, 1},
{1, 0, 0, 0, 1, 0, 0, 1, 0, 1},
{1, 0, 0, 0, 1, 0, 0, 1, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
t_tile **temp_map = malloc(10 * sizeof(int *));
if (!temp_map)
t_tile **grid = malloc(10 * sizeof(int *));
if (!grid)
return NULL;
for (int i = 0; i < 10; i++)
{
temp_map[i] = malloc(10 * sizeof(int));
if (!temp_map[i])
grid[i] = malloc(10 * sizeof(int));
if (!grid[i])
{
for (int j = 0; j < i; j++)
free(temp_map[j]);
free(temp_map);
free(grid[j]);
free(grid);
return NULL;
}
for (int j = 0; j < 10; j++)
{
temp_map[i][j] = map[i][j];
grid[i][j] = const_map[i][j];
}
}
return temp_map;
t_map *map = malloc(sizeof(t_map));
if (!map)
{
for (int i = 0; i < 10; i++)
free(grid[i]);
free(grid);
return NULL;
}
map->width = 10;
map->height = 10;
map->grid = grid;
return map;
}
void print_map(t_map *map)
{
const int chars[] = {'X', ' ', '.', '#', 'P'};
int i = 0;
while (i < map->height)
{
int j = 0;
while (j < map->width)
{
printf("%c ", chars[map->grid[i][j] + 2]);
j++;
}
printf("\n");
i++;
}
}
void free_map(t_map **map)
{
if (!map || !*map)
return ;
int i = 0;
while (i < (*map)->height)
{
free((*map)->grid[i]);
i++;
}
free((*map)->grid);
free(*map);
*map = NULL;
}
int find_player_or_empty(t_map *map, int *x, int *y)
{
int i;
int j;
if (!map || !x || !y)
return 0;
i = 0;
while (i < map->height)
{
j = 0;
while(j < map->width)
{
if (map->grid[i][j] == TILE_PLAYER || map->grid[i][j] == TILE_EMPTY)
{
*x = j;
*y = i;
return 1;
}
j++;
}
i++;
}
return 0;
}
int floodfill(t_map *map, int x, int y)
{
if (!map || x < 0 || y < 0 || x >= map->width || y >= map->height)
return (FAILURE);
if (map->grid[y][x] == TILE_VOID)
return (FAILURE);
if (map->grid[y][x] == TILE_WALL || map->grid[y][x] == TILE_VISITED)
return (SUCCESS);
map->grid[y][x] = TILE_VISITED;
if (!floodfill(map, x + 1, y) ||
!floodfill(map, x, y - 1) ||
!floodfill(map, x, y + 1) ||
!floodfill(map, x - 1, y))
return (FAILURE);
return (SUCCESS);
}
int enclosed_map(t_map *map)
{
int x;
int y;
if (!map)
return FAILURE;
if (!find_player_or_empty(map, &x, &y))
return SUCCESS;
if (!floodfill(map, x, y))
return FAILURE;
if (!enclosed_map(map))
return (FAILURE);
return SUCCESS;
}
int main(void)
{
t_map *map;
map = get_temp_map();
if (!map)
{
fprintf(stderr, "Failed to allocate memory for map\n");
return 1;
}
print_map(map);
if(!enclosed_map(map))
fprintf(stderr, "NOT GOOD MAP FRIEND\n");
else
fprintf(stderr, "YES, GOOD MAP FRIEND\n");
free_map(&map);
return 0;
}