This commit is contained in:
whaffman 2025-04-14 14:27:56 +02:00
parent 18d72cf261
commit 0a089e04f8
5 changed files with 201 additions and 26 deletions

4
.gitignore vendored Normal file
View File

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

56
MLX42.supp Normal file
View File

@ -0,0 +1,56 @@
{
<MLX42>
Memcheck:Leak
...
obj:/usr/lib/x86_64-linux-gnu/libnvidia*
}
{
<MLX42>
Memcheck:Leak
...
obj:/usr/lib/x86_64-linux-gnu/libdbus*
}
{
<MLX42>
Memcheck:Leak
...
obj:/usr/lib/x86_64-linux-gnu/libglfw*
}
{
<MLX42>
Memcheck:Leak
...
obj:/usr/lib/x86_64-linux-gnu/libX11*
}
{
<MLX42>
Memcheck:Leak
...
obj:/usr/lib/x86_64-linux-gnu/dri*
}
{
<MLX42>
Memcheck:Leak
...
obj:/usr/lib/x86_64-linux-gnu/libLLVM*
}
{
<MLX42>
Memcheck:Leak
...
fun:_dl_open
}
{
<MLX42>
Memcheck:Addr8
...
fun:dl_open_worker
}

View File

@ -6,7 +6,7 @@
# By: qmennen <qmennen@student.codam.nl> +#+ #
# +#+ #
# Created: 2024/10/15 11:48:46 by whaffman #+# #+# #
# Updated: 2025/04/14 13:08:34 by whaffman ######## odam.nl #
# Updated: 2025/04/14 14:02:01 by whaffman ######## odam.nl #
# #
# **************************************************************************** #
@ -34,7 +34,7 @@ INCLUDES = -I./$(INC_PATH) -I./$(LIBFT_INC_PATH) -I./$(MLX42_INC_PATH)
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
LDLIBS := -L$(LIBFT_PATH) -lft -lreadline
LDLIBS := -L$(LIBFT_PATH) -lft -L$(MLX42_PATH)/build -lmlx42 -ldl -lglfw -pthread -lm
endif
VPATH = $(shell find $(SRC_PATH) -type d | tr '\n' ':')
@ -82,7 +82,7 @@ endef
# Copy the release build configuration to the root
$(NAME): release
$(info $(bold)$(green)Copying release build to root$(reset))
cp ./build/release/minishell $@
cp $(BUILD_PATH)/release/$(NAME) $@
$(foreach config,$(BUILD_CONFIGS),$(eval $(call BUILD_TARGETS,$(config))))
@ -135,34 +135,34 @@ re: fclean
# Run the project with the release build configuration
run: run_release
run_valgrind: release
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --suppressions=readline.supp ./$(BUILD_PATH)/release/$(NAME)
run_valgrind: debug
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --suppressions=MLX42.supp ./$(BUILD_PATH)/debug/$(NAME)
#
# Remove sources.mk to force regeneration
srcs:
$(info $(bold)$(green)Regenerating sources.mk on next make$(reset))
$(RM) sources.mk
dist: $(SRC_PATH) $(INC_PATH) $(LIB_PATH) release_makefile.mk
@if [ ! -d $(DIST_PATH) ]; then \
mkdir -p $(DIST_PATH); \
else \
rm -rf $(DIST_PATH)/*; \
fi
@rsync -av --include='*/' --include='*.c' --include='*.h' --include='Makefile' --exclude='*' src/ $(DIST_PATH)/src/
@rsync -av --include='*/' --include='*.c' --include='*.h' --include='Makefile' --exclude='*' inc/ $(DIST_PATH)/inc/
@rsync -av --include='*/' --include='*.c' --include='*.h' --include='Makefile' --exclude='*' lib/ $(DIST_PATH)/lib/
@cp release_makefile.mk $(DIST_PATH)/Makefile
@make -s sources.mk
@cp sources.mk $(DIST_PATH)/sources.mk
@norminette $(DIST_PATH) > /dev/null; \
if [ $$? -ne 0 ]; then \
echo "\e[31mNorminette failed, deleting dist folder\e[0m"; \
norminette $(DIST_PATH) | grep "Error"; \
rm -rf $(DIST_PATH); \
exit 1; \
fi
@echo -e "\e[32mDistribute successful\e[0m"
# dist: $(SRC_PATH) $(INC_PATH) $(LIB_PATH) release_makefile.mk
# @if [ ! -d $(DIST_PATH) ]; then \
# mkdir -p $(DIST_PATH); \
# else \
# rm -rf $(DIST_PATH)/*; \
# fi
# @rsync -av --include='*/' --include='*.c' --include='*.h' --include='Makefile' --exclude='*' src/ $(DIST_PATH)/src/
# @rsync -av --include='*/' --include='*.c' --include='*.h' --include='Makefile' --exclude='*' inc/ $(DIST_PATH)/inc/
# @rsync -av --include='*/' --include='*.c' --include='*.h' --include='Makefile' --exclude='*' lib/ $(DIST_PATH)/lib/
# @cp release_makefile.mk $(DIST_PATH)/Makefile
# @make -s sources.mk
# @cp sources.mk $(DIST_PATH)/sources.mk
# @norminette $(DIST_PATH) > /dev/null; \
# if [ $$? -ne 0 ]; then \
# echo "\e[31mNorminette failed, deleting dist folder\e[0m"; \
# norminette $(DIST_PATH) | grep "Error"; \
# rm -rf $(DIST_PATH); \
# exit 1; \
# fi
# @echo -e "\e[32mDistribute successful\e[0m"
# Show this help.
help:

7
compile_commands.json Normal file
View File

@ -0,0 +1,7 @@
[
{
"command": "cc -c -Wall -Werror -Werror -O2 -I./inc -I./lib/libft/inc -I./lib/MLX42/include/MLX42 -o build/release/obj/main.o src/main.c",
"directory": "/home/whaffman/Projects/cub3d",
"file": "/home/whaffman/Projects/cub3d/src/main.c"
}
]

108
src/main.c Normal file
View File

@ -0,0 +1,108 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include "MLX42.h"
#define WIDTH 1024
#define HEIGHT 1024
typedef struct s_things t_things;
typedef struct s_things
{
mlx_t* mlx;
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);
}
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_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;
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;
}
}
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();
mlx_t* mlx = mlx_init(WIDTH, HEIGHT, "42Balls", true);
if (!mlx)
ft_error();
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;
// 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);
}