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_defines.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_tex_nl.c
SRCS_NAME	+= ft_get_path_nl.c
SRCS_NAME	+= ft_get_nlvl_pos.c
SRCS_NAME	+= ft_get_player_spawn.c
SRCS_NAME	+= ft_get_music.c
SRCS_NAME	+= ft_set_minimap_scale.c
SRCS_NAME	+= ft_check_missing.c
SRCS_NAME	+= ft_check_not_found.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_circle.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_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_NAME	+= ft_draw_textures.c
SRCS_NAME	+= ft_key_release.c
SRCS_NAME	+= ft_convert_keycode.c
SRCS_NAME	+= ft_key_loop.c
SRCS_NAME	+= ft_draw_hud.c
SRCS_NAME	+= ft_music.c
SRCS_NAME	+= ft_draw_sprite.c
SRCS_NAME	+= ft_warp_level.c
SRCS_NAME	+= ft_save_to_bmp.c
SRCS_NAME	+= ft_treat_args.c
SRCS_NAME	+= ft_init_funptr.c
#--------------------------------------------------------------------------------------------------#
SRCS		= $(addprefix ${SRCS_DIR},${SRCS_NAME})
#--------------------------------------------------------------------------------------------------#
OBJS		= $(patsubst ${SRCS_DIR}%.c,${OBJS_DIR}%.o,${SRCS})
#--------------------------------------------------------------------------------------------------#
LFT_SRCS	= $(shell find libft/src -name "*.c")
#==================================================================================================#
#--------------------------------------------- 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
	USER	= $(shell w | grep tty7 | awk '{print $$1}')
	SCR_SZE	= $(shell xdpyinfo | grep dimensions | awk '{print $$2}')
endif
#==================================================================================================#
#------------------------------------------ Compiler ----------------------------------------------#
#==================================================================================================#
DEBUG		= -glldb
FSANITIZE	= -fsanitize=address
#--------------------------------------------------------------------------------------------------#
CC			= clang
#--------------------------------------------------------------------------------------------------#
CFLAGS		 = -std=c89
CFLAGS		+= -Wall
CFLAGS		+= -Wextra
CFLAGS		+= -Werror
CFLAGS		+= -pedantic
CFLAGS		+= ${DEBUG}
#--------------------------------------------------------------------------------------------------#
ifdef ASAN
	CFLAGS	+= ${FSANITIZE}
endif
#--------------------------------------------------------------------------------------------------#
CDEFS		 = -DFT_SCR_SIZE=\"${SCR_SZE}\"
ifeq (${OS}, Darwin)
	CDEFS	+= -DFT_OS=1
else
	CDEFS	+= -DFT_OS=2
endif
#--------------------------------------------------------------------------------------------------#
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 -pthread ${CFLAGS} ${CDEFS} -I${LFT_DIR}${INCS_DIR} -I${INCS_DIR} -o $@ $<
endif
#--------------------------------------------------------------------------------------------------#
${NAME}:	${OBJS} ${LFT_SRCS} ${LFT_DIR}${INCS_DIR}libft.h
ifeq (${OS}, Darwin)
	@$(MAKE) --no-print-directory -C ${MLX_DIR} all
endif
ifdef ASAN
	@$(MAKE) --no-print-directory -C ${LFT_DIR} all ASAN=1
endif
ifdef POWER
	@$(MAKE) --no-print-directory -C ${LFT_DIR} all OPTI=1
else
	@$(MAKE) --no-print-directory -C ${LFT_DIR} all
endif
ifeq (${OS}, Darwin)
	${CC} ${CFLAGS} -o $@ ${OBJS} -L${LFT_DIR} -L${MLX_DIR} -lft -lmlx -lm -lpthread \
-framework OpenGL -framework AppKit
else
	${CC} ${CFLAGS} -o $@ ${OBJS} -L${LFT_DIR} -lft -lX11 -lXext -lmlx -lm -lbsd -lpthread
endif
#--------------------------------------------------------------------------------------------------#
all: ${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/lvl_one.cub"
#--------------------------------------------------------------------------------------------------#
.PHONY:	all clean clean fclean re run default
#============================================== EOF ===============================================#