# **************************************************************************** #
#                                                                              #
#                                                         :::      ::::::::    #
#    Makefile                                           :+:      :+:    :+:    #
#                                                     +:+ +:+         +:+      #
#    By: bszilas <bszilas@student.42vienna.com>     +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2024/09/08 16:30:33 by victor            #+#    #+#              #
#    Updated: 2025/06/02 12:04:19 by victor           ###   ########.fr        #
#                                                                              #
# **************************************************************************** #

CC := cc

CFLAGS := -Wall -Werror -Wextra

LDFLAGS := -lm

OUT_DIR := out

NAME := $(OUT_DIR)/miniRT

SRCDIR := src

SRC := minirt.c

PLATFORM ?= PLATFORM_DESKTOP

LIBRAYLIB := libraylib.a

DISPLAY_WIDTH := 800
DISPLAY_HEIGHT := 600

WEB_OUTPUT_DIR := web
WEB_OUTPUT := $(WEB_OUTPUT_DIR)/miniRT
WEB_OUTPUT_HTML := $(WEB_OUTPUT).html
WEB_OUTPUT_JS := $(WEB_OUTPUT).js
WEB_OUTPUT_WASM := $(WEB_OUTPUT).wasm
WEB_OUTPUT_DATA := $(WEB_OUTPUT).data

OBJDIR_BASE := obj

ifeq ($(PLATFORM), PLATFORM_WEB)
LIBRAYLIB := libraylib.web.a
OUT_DIR := $(WEB_OUTPUT_DIR)
NAME := WE
CC := emcc
AR := emar
CFLAGS += -v -DROOT_DIRECTORY=\"/data\"
LDFLAGS += -o $(WEB_OUTPUT_HTML) \
		   -s USE_GLFW=3 \
		   -s ASYNCIFY \
		   -s ASSERTIONS=1 \
		   -s SAFE_HEAP=1 \
		   --preload-file assets/scenes@/data/assets/scenes \
		   --preload-file assets@/data/assets \
		   --shell-file raylib/src/shell.html \
		   -DPLATFORM_WEB \
		   -s INITIAL_MEMORY=2147483648

OBJDIR := $(OBJDIR_BASE)/web

else
CFLAGS += -DROOT_DIRECTORY=\"$(PWD)\"
OBJDIR := $(OBJDIR_BASE)/native

endif

VNC_SCRIPT := ./scripts/run_vnc.sh

SCENEDIR := scene
SRCSCENE := $(addprefix $(SCENEDIR)/, $(addsuffix .c,\
            camera camera_info camera_ray_define \
            light scene scene_create sphere sphere_calc \
            cylinder cylinder_info cylinder_calc cylinder_uv \
            disk disk_calc plane plane_calc add_body \
            cone_tracing cone_utils cone_info))

RENDERDIR := rendering
SRCRENDER :=    $(addprefix $(RENDERDIR)/, $(addsuffix .c,\
                rendering image pixel ray_utils color drawing threads \
                color2 light_shadow phong))

IODIR := io
SRCIO :=    $(addprefix $(IODIR)/, $(addsuffix .c,\
            ppm id bump mouse_press mouse key_press keys parsing2 parsing glyph \
            item_value_manip items_create menu slider container \
            key_change_scene buffer menu_body_create menu_body_map \
            menu_value_add menu_value_map explorer explorer_helper \
            explorer_load_file menu_file_map))

UTILDIR := utils
SRCUTIL :=	$(addprefix $(UTILDIR)/, $(addsuffix .c,\
            minirt_utils vector_math2 vector_math vector \
            detect_overflow_utils detect_overflow))

LIBS := libft/libft.a memory/memory.a raylib/src/$(LIBRAYLIB)

MAP := mapgen
OBJ := $(SRC:%.c=$(OBJDIR)/%.o)
OBJRENDER := $(SRCRENDER:%.c=$(OBJDIR)/%.o)
OBJSCENE := $(SRCSCENE:%.c=$(OBJDIR)/%.o)
OBJIO := $(SRCIO:%.c=$(OBJDIR)/%.o)
OBJUTIL := $(SRCUTIL:%.c=$(OBJDIR)/%.o)

ifdef DEBUG
    CFLAGS += -D VALGRIND
    CFLAGS += -g3
else
    CFLAGS += -O3
endif

all: $(LIBS) $(OBJDIR) $(NAME)

$(NAME): $(OUT_DIR) $(OBJ) minirt.h $(OBJIO) $(OBJSCENE) $(OBJUTIL) $(OBJRENDER)
	$(CC) $(CFLAGS) -o $@ $(OBJ) $(OBJIO) $(OBJSCENE) $(OBJUTIL) $(OBJRENDER) $(LIBS) $(LDFLAGS)

$(OBJDIR)/%.o: $(SRCDIR)/$(SCENEDIR)/%.c minirt.h
	$(CC) $(CFLAGS) -c $< -o $@

$(OBJDIR)/%.o: $(SRCDIR)/$(RENDERDIR)/%.c minirt.h
	$(CC) $(CFLAGS) -c $< -o $@

$(OBJDIR)/%.o: $(SRCDIR)/$(UTILDIR)/%.c minirt.h
	$(CC) $(CFLAGS) -c $< -o $@

$(OBJDIR)/%.o: $(SRCDIR)/$(IODIR)/%.c minirt.h
	$(CC) $(CFLAGS) -c $< -o $@

$(OBJDIR)/%.o: $(SRCDIR)/%.c minirt.h
	$(CC) $(CFLAGS) -c $< -o $@

$(MAP): src/mapgeb/map_gen_buffer.c src/mapgeb/map.c
	make all
	$(CC) $(CFLAGS) $^ src/io/buffer.c $(LIBS) -o $@ -lm

$(LIBS):
	make AR=$(AR) CC=$(CC) -C libft
	make AR=$(AR) CC=$(CC) -C memory
	make AR=$(AR) CC=$(CC) -C libft/printf
	make -C raylib/src PLATFORM=$(PLATFORM)

$(OUT_DIR):
	mkdir -p $@

$(OBJDIR):
	mkdir -p $(OBJDIR) $(OBJDIR)/$(SRCDIR) $(OBJDIR)/$(RENDERDIR) $(OBJDIR)/$(IODIR) $(OBJDIR)/$(UTILDIR) $(OBJDIR)/$(SCENEDIR)

vnc: clean clean_vnc all
	$(VNC_SCRIPT) $(DISPLAY_WIDTH) $(DISPLAY_HEIGHT)

clean_vnc: clean
	pkill -9 websockify || true
	pkill -9 Xvnc || true

clean: clean_vnc
	rm -rf $(OBJDIR_BASE)
	make clean -C libft
	make clean -C memory
	make clean -C libft/printf
	make clean -C raylib/src

fclean: clean
	rm -rf $(NAME) $(MAP) $(WEB_OUTPUT_DIR) $(OUT_DIR) $(WEB_OUTPUT_JS) $(WEB_OUTPUT_HTML) $(WEB_OUTPUT_WASM) $(WEB_OUTPUT_DATA)
	make fclean -C libft
	make fclean -C memory
	make fclean -C libft/printf

re: fclean all

.PHONY: all clean fclean re debug
