# FS2 modernized C port - Makefile
#
# Layout:
#   src/         .c sources for the fs2port binary
#   include/     .h headers shared by sources
#   tools/       .c sources for the offline analysis tools
#   screenshots/ saved .png / .ppm output
#   obj/         .o object files (build output)
#   bin/         compiled fs2port binary + tool binaries (build output)

CC          = gcc
CFLAGS      = -std=c11 -O2 -Wall -Wextra -Wshadow $(shell sdl2-config --cflags) -Iinclude
LDFLAGS     = $(shell sdl2-config --libs) -lm

# Tools are plain C with no SDL dependency. dumpStations is the
# exception -- it pulls in the port's actual scenery interpreter so
# the offline scan stays bit-identical with the live renderer.
TOOL_CFLAGS = -std=c11 -O2 -Wall -Iinclude

SRC_DIR     = src
TOOL_DIR    = tools
OBJ_DIR     = obj
BIN_DIR     = bin
INC_DIR     = include

SOURCES     = $(notdir $(wildcard $(SRC_DIR)/*.c))
OBJECTS     = $(SOURCES:%.c=$(OBJ_DIR)/%.o)
HEADERS     = $(wildcard $(INC_DIR)/*.h)
TARGET      = $(BIN_DIR)/fs2port

TOOL_SRCS   = $(notdir $(wildcard $(TOOL_DIR)/*.c))
TOOLS       = $(TOOL_SRCS:%.c=$(BIN_DIR)/%)

.PHONY: all clean run tools

all: $(TARGET) tools

tools: $(TOOLS)

$(TARGET): $(OBJECTS) | $(BIN_DIR)
	$(CC) -o $@ $^ $(LDFLAGS)

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(HEADERS) | $(OBJ_DIR)
	$(CC) $(CFLAGS) -c $< -o $@

# dumpStations links with the port's scenery interpreter so the
# offline station scan uses the same dispatcher / advance counts.
$(BIN_DIR)/dumpStations: $(TOOL_DIR)/dumpStations.c $(SRC_DIR)/sceneryVm.c $(SRC_DIR)/sceneryProjection.c $(SRC_DIR)/chunk5Transform.c $(SRC_DIR)/chunk5Setup.c $(SRC_DIR)/cpu6502.c $(SRC_DIR)/hires.c $(HEADERS) | $(BIN_DIR)
	$(CC) $(TOOL_CFLAGS) -o $@ $(TOOL_DIR)/dumpStations.c $(SRC_DIR)/sceneryVm.c $(SRC_DIR)/sceneryProjection.c $(SRC_DIR)/chunk5Transform.c $(SRC_DIR)/chunk5Setup.c $(SRC_DIR)/cpu6502.c $(SRC_DIR)/hires.c -lm

# matrixProbe drives sceneryAttachCamera with controlled inputs so the
# port's $78..$89 matrix can be diffed against MAME's capture.
$(BIN_DIR)/matrixProbe: $(TOOL_DIR)/matrixProbe.c $(SRC_DIR)/sceneryVm.c $(SRC_DIR)/sceneryProjection.c $(SRC_DIR)/chunk5Transform.c $(SRC_DIR)/camera.c $(SRC_DIR)/math6502.c $(SRC_DIR)/chunk5Setup.c $(SRC_DIR)/cpu6502.c $(SRC_DIR)/hires.c $(HEADERS) | $(BIN_DIR)
	$(CC) $(TOOL_CFLAGS) -o $@ $(TOOL_DIR)/matrixProbe.c $(SRC_DIR)/sceneryVm.c $(SRC_DIR)/sceneryProjection.c $(SRC_DIR)/chunk5Transform.c $(SRC_DIR)/camera.c $(SRC_DIR)/math6502.c $(SRC_DIR)/chunk5Setup.c $(SRC_DIR)/cpu6502.c $(SRC_DIR)/hires.c -lm

# chunk5SetupTest validates the C transliteration of chunk5
# SetupViewProjection / L177B / ScaleC2ByC4 against the fs2trace
# oracle that runs the actual chunk5 binary.
$(BIN_DIR)/chunk5SetupTest: $(TOOL_DIR)/chunk5SetupTest.c $(SRC_DIR)/chunk5Setup.c $(HEADERS) | $(BIN_DIR)
	$(CC) $(TOOL_CFLAGS) -o $@ $(TOOL_DIR)/chunk5SetupTest.c $(SRC_DIR)/chunk5Setup.c -lm

# Default rule for the standalone tools (no port dependencies).
$(BIN_DIR)/%: $(TOOL_DIR)/%.c | $(BIN_DIR)
	$(CC) $(TOOL_CFLAGS) -o $@ $<

$(OBJ_DIR) $(BIN_DIR):
	mkdir -p $@

run: $(TARGET)
	./$(TARGET)

clean:
	rm -f $(OBJ_DIR)/*.o $(TARGET) $(TOOLS)
