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 *.o
*.d *.d
*.a *.a
cub3d cub3D

View File

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

Binary file not shown.

BIN
cub3D

Binary file not shown.

View File

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

View File

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