default: all

#==================================================================================================#
#-------------------------------------------- Shell -----------------------------------------------#
#==================================================================================================#
SHELL		:= /bin/sh
#==================================================================================================#
#-------------------------------------------- Directories -----------------------------------------#
#==================================================================================================#
INCS_DIR	= inc/
SRCS_DIR	= src/
OBJS_DIR	= obj/
LFT_DIR		= libft/
MLX_DIR		= minilibx/
#==================================================================================================#
#---------------------------------------------- Files ---------------------------------------------#
#==================================================================================================#
INCS		= cub3d.h
INCS		+= cub3d_structs.h
#--------------------------------------------------------------------------------------------------#
SRCS_NAME	 = main.c
SRCS_NAME	+= ft_init_lists.c
SRCS_NAME	+= ft_hooks_and_loops.c
SRCS_NAME	+= ft_key_events.c
SRCS_NAME	+= ft_click_close.c
SRCS_NAME	+= ft_exit.c
SRCS_NAME	+= ft_parse_map.c
SRCS_NAME	+= ft_select_get.c
SRCS_NAME	+= ft_get_screen_size.c
SRCS_NAME	+= ft_get_res.c
SRCS_NAME	+= ft_get_tex.c
SRCS_NAME	+= ft_get_sprite.c
SRCS_NAME	+= ft_get_colors.c
SRCS_NAME	+= ft_get_map.c
SRCS_NAME	+= ft_get_player_spawn.c
SRCS_NAME	+= ft_set_minimap_scale.c
SRCS_NAME	+= ft_check_missing.c
SRCS_NAME	+= ft_check_map_line.c
SRCS_NAME	+= ft_free_words.c
SRCS_NAME	+= ft_map_error.c
SRCS_NAME	+= ft_init_winlx.c
SRCS_NAME	+= ft_draw_square.c
SRCS_NAME	+= ft_draw_map.c
SRCS_NAME	+= ft_draw_scene.c
SRCS_NAME	+= ft_basic_keys.c
SRCS_NAME	+= ft_extra_keys.c
SRCS_NAME	+= ft_draw_verline.c
SRCS_NAME	+= ft_print_list.c
SRCS_NAME	+= ft_rgb_to_hex.c
SRCS_NAME	+= ft_raycasting.c
SRCS_NAME	+= ft_init_s_ray.c
SRCS_NAME	+= ft_init_map.c
SRCS_NAME	+= ft_tex_init.c
SRCS_NAME	+= ft_detect.c
SRCS_NAME	+= ft_check_ext.c
#--------------------------------------------------------------------------------------------------#
SRCS		= $(addprefix ${SRCS_DIR},${SRCS_NAME})
#--------------------------------------------------------------------------------------------------#
OBJS		= $(patsubst ${SRCS_DIR}%.c,${OBJS_DIR}%.o,${SRCS})
#==================================================================================================#
#--------------------------------------------- UNIX -----------------------------------------------#
#==================================================================================================#
MKDIR		= mkdir -p
RM			= rm -rf
OS			= $(shell uname)
ifeq (${OS}, Darwin)
	SCR_SZE = $(shell osascript -e 'tell application "Finder" to get bounds of window of desktop' \
| tr ',' 'x' | tr -d '\n' | tr -d ' ' | cut -c 5-)
else
	SCR_SZE	= $(shell xdpyinfo | grep dimensions | awk '{print $$2}')
endif
#==================================================================================================#
#------------------------------------------ Compiler ----------------------------------------------#
#==================================================================================================#
CC			= clang
#--------------------------------------------------------------------------------------------------#
CFLAGS		 = -std=c89
CFLAGS		+= -Wall
CFLAGS		+= -Wextra
CFLAGS		+= -Werror
CFLAGS		+= -pedantic
#--------------------------------------------------------------------------------------------------#
ifdef ASAN
	CFLAGS	+= ${DEBUG}
	CFLAGS	+= ${FSANITIZE}
endif
#--------------------------------------------------------------------------------------------------#
CDEFS		 = -DFT_SCR_SIZE=\"${SCR_SZE}\"
ifneq (${OS}, Darwin)
	CDEFS	+= -DFT_W_KEY=119
	CDEFS	+= -DFT_A_KEY=97
	CDEFS	+= -DFT_S_KEY=115
	CDEFS	+= -DFT_D_KEY=100
	CDEFS	+= -DFT_F1_KEY=65470
	CDEFS	+= -DFT_TAB_KEY=65289
	CDEFS	+= -DFT_L_ARR_KEY=65361
	CDEFS	+= -DFT_R_ARR_KEY=65363
	CDEFS	+= -DFT_ESC_KEY=65307
endif
#--------------------------------------------------------------------------------------------------#
DEBUG		= -glldb
FSANITIZE	= -fsanitize=address
#--------------------------------------------------------------------------------------------------#
ifeq (${OS}, Darwin)
	NAME	= cub3d
else
	NAME	= a.out
endif
#==================================================================================================#
#------------------------------------------ Rules -------------------------------------------------#
#==================================================================================================#
${OBJS_DIR}%.o:		${SRCS_DIR}%.c ${INCS_DIR}
	@${MKDIR} ${OBJS_DIR}
ifeq (${OS}, Darwin)
	${CC} -c ${CFLAGS} ${CDEFS} -I${LFT_DIR}${INCS_DIR} -I${MLX_DIR} -I${INCS_DIR} -o $@ $<
else
	${CC} -c ${CFLAGS} ${CDEFS} -I${LFT_DIR}${INCS_DIR} -I${INCS_DIR} -o $@ $<
endif
#--------------------------------------------------------------------------------------------------#
${NAME}:	${OBJS}
ifeq (${OS}, Darwin)
	${CC} ${CFLAGS} -o $@ ${OBJS} -L${LFT_DIR} -L${MLX_DIR} -lft -lmlx -lm -framework OpenGL \
-framework AppKit
else
	${CC} ${CFLAGS} -o $@ ${OBJS} -L${LFT_DIR} -lft -lX11 -lXext -lmlx -lm -lbsd
endif
#--------------------------------------------------------------------------------------------------#
all:
ifeq (${OS}, Darwin)
	@$(MAKE) --no-print-directory -C ${MLX_DIR} all
endif
ifdef ASAN
	@$(MAKE) --no-print-directory -C ${LFT_DIR} all ASAN=1
else
	@$(MAKE) --no-print-directory -C ${LFT_DIR} all
endif
	@$(MAKE) ${NAME}
#--------------------------------------------------------------------------------------------------#
clean: 
ifeq (${OS}, Darwin)
	@$(MAKE) --no-print-directory -C ${MLX_DIR} clean
endif
	@$(MAKE) --no-print-directory -C ${LFT_DIR} clean
	${RM} ${OBJS_DIR}
#--------------------------------------------------------------------------------------------------#
fclean:		clean
	@$(MAKE) --no-print-directory -C ${LFT_DIR} fclean
	${RM} ${NAME}
	${RM} ${NAME}.dSYM
#--------------------------------------------------------------------------------------------------#
re:		fclean all
#--------------------------------------------------------------------------------------------------#
run:	all
	@./${NAME} "map/map_one.cub"
#--------------------------------------------------------------------------------------------------#
.PHONY:	all clean clean fclean re run default
#============================================== EOF ===============================================#