# Recursive Directory Digesting Makefile V1.0
# === Project Settings ===
PROJECT := soundThing           # Change this to your project name
CFLAGS  := -Wall -Wextra -std=c11 -O2
# Preprocessor flags (deps only here)
CPPFLAGS := -MMD -MP

# === Platform Selection ===
# Build for Linux (default): make
# Cross-compile for Windows:  make PLATFORM=windows
PLATFORM ?= linux

ifeq ($(PLATFORM),windows)
    CC       := x86_64-w64-mingw32-gcc
    BIN_NAME := $(PROJECT).exe
    # Point RAYLIB_WIN at the extracted raylib-*_win64_mingw-w64 directory, e.g.:
    #   make PLATFORM=windows RAYLIB_WIN=/opt/raylib-win64
    # RAYLIB_WIN ?=
    RAYLIB_WIN = /home/qwhilla/projects/externalLibraries/win64/raylib
    ifneq ($(RAYLIB_WIN),)
        CPPFLAGS += -I$(RAYLIB_WIN)/include
        LDFLAGS  := -L$(RAYLIB_WIN)/lib -lraylib -lm -lpthread -lopengl32 -lwinmm -lgdi32 -lws2_32 -static
    else
        LDFLAGS  := -lraylib -lm -lpthread -lopengl32 -lwinmm -lgdi32 -lws2_32 -static
    endif
    MIDI_SRC := midi_windows.c
    WINDRES  := x86_64-w64-mingw32-windres
    WIN_RES  = $(OBJ_DIR)/icon_res.o
else
    CC       := gcc
    BIN_NAME := $(PROJECT)
    LDFLAGS  := -lraylib -lm -ldl -lpthread -lGL -lrt -lX11 -lasound
    MIDI_SRC := midi_linux.c
    WIN_RES  :=
endif

# === Directory Structure ===
SRC_DIR := source
OBJ_DIR := build
BIN_DIR := bin
INC_DIR := include

# === Discover headers recursively ===
INC_SUBDIRS := $(shell find $(INC_DIR) -type d)
CPPFLAGS += $(addprefix -I,$(INC_SUBDIRS))

# === Discover sources (exclude platform-split MIDI files, add the right one) ===
SRC_AUTO := $(shell find $(SRC_DIR) -type f -name '*.c' ! -name 'midi_*.c' ! -name '*.old')
SRC      := $(SRC_AUTO) $(SRC_DIR)/$(MIDI_SRC)
# Map source paths to object paths (mirror folders under build/)
OBJ := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRC))
DEP := $(OBJ:.o=.d)
BIN := $(BIN_DIR)/$(BIN_NAME)

# === Embedded assets ===
SPRITES_DATA := $(SRC_DIR)/sprites_data.c
FONT_DATA    := $(SRC_DIR)/font_data.c
ICON_DATA    := $(SRC_DIR)/icon_data.c

# === Rules ===
.PHONY: all clean run dirs

all: dirs $(SPRITES_DATA) $(FONT_DATA) $(ICON_DATA) $(BIN)

dirs:
	@mkdir -p $(BIN_DIR)
	@mkdir -p $(sort $(dir $(OBJ)))  # create object subdirs that mirror source/

# Link objects into final executable
$(BIN): $(OBJ) $(WIN_RES)
	$(CC) $(OBJ) $(WIN_RES) -o $@ $(LDFLAGS)
	@echo "Linked -> $@"

# Compile each .c into a .o, with auto header deps
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
	$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
	@echo "Compiled -> $@"

# Run the program
run: all
	@echo "Running $(BIN)..."
	@$(BIN)

# Clean build artifacts
clean:
	@rm -rf $(OBJ_DIR) $(BIN)
	@echo "Clean complete."

# Include auto-generated dependency files (if present)
-include $(DEP)

# Regenerate embedded sprite data when the PNG changes
$(SPRITES_DATA): assets/sprites.png
	xxd -i $< | sed 's/assets_sprites_png/sprites_png/g' > $@
	@echo "Generated -> $@"

# Regenerate embedded font data when the PNG changes
$(FONT_DATA): assets/myFont.png
	xxd -i $< | sed 's/assets_myFont_png/font_png/g' > $@
	@echo "Generated -> $@"

# Regenerate embedded icon data when the PNG changes
$(ICON_DATA): assets/NPNGear.png
	xxd -i $< | sed 's/assets_NPNGear_png/icon_png/g' > $@
	@echo "Generated -> $@"

# Generate .ico from PNG using pure Python3 stdlib (no Pillow required)
assets/NPNGear.ico: assets/NPNGear.png
	python3 -c "import struct; d=open('$<','rb').read(); \
	  hdr=struct.pack('<HHH',0,1,1); \
	  ent=struct.pack('<BBBBHHII',128,128,0,0,1,32,len(d),22); \
	  open('$@','wb').write(hdr+ent+d)"
	@echo "Generated -> $@"

# Compile Windows resource (icon.rc -> icon_res.o), only when cross-compiling
ifeq ($(PLATFORM),windows)
$(WIN_RES): icon.rc assets/NPNGear.ico
	$(WINDRES) $< -o $@
	@echo "Compiled resource -> $@"
endif
