From 0a089e04f845d271bc10cfa8f9b4c977b320aeb1 Mon Sep 17 00:00:00 2001 From: whaffman Date: Mon, 14 Apr 2025 14:27:56 +0200 Subject: [PATCH] basics --- .gitignore | 4 ++ MLX42.supp | 56 ++++++++++++++++++++++ Makefile | 52 ++++++++++---------- compile_commands.json | 7 +++ src/main.c | 108 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 201 insertions(+), 26 deletions(-) create mode 100644 .gitignore create mode 100644 MLX42.supp create mode 100644 compile_commands.json create mode 100644 src/main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1edbcb4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.o +*.d +*.a +cub3d diff --git a/MLX42.supp b/MLX42.supp new file mode 100644 index 0000000..8175bf6 --- /dev/null +++ b/MLX42.supp @@ -0,0 +1,56 @@ +{ + + Memcheck:Leak + ... + obj:/usr/lib/x86_64-linux-gnu/libnvidia* +} + +{ + + Memcheck:Leak + ... + obj:/usr/lib/x86_64-linux-gnu/libdbus* +} + +{ + + Memcheck:Leak + ... + obj:/usr/lib/x86_64-linux-gnu/libglfw* +} + +{ + + Memcheck:Leak + ... + obj:/usr/lib/x86_64-linux-gnu/libX11* +} + +{ + + Memcheck:Leak + ... + obj:/usr/lib/x86_64-linux-gnu/dri* +} + +{ + + Memcheck:Leak + ... + obj:/usr/lib/x86_64-linux-gnu/libLLVM* +} + +{ + + Memcheck:Leak + ... + fun:_dl_open +} + +{ + + Memcheck:Addr8 + ... + fun:dl_open_worker +} + diff --git a/Makefile b/Makefile index 3573d7e..16f496f 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: qmennen +#+ # # +#+ # # 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: diff --git a/compile_commands.json b/compile_commands.json new file mode 100644 index 0000000..6d1b83f --- /dev/null +++ b/compile_commands.json @@ -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" + } +] \ No newline at end of file diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..97a8a43 --- /dev/null +++ b/src/main.c @@ -0,0 +1,108 @@ +#include +#include +#include +#include + +#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); +} \ No newline at end of file