# FS2 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
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)
PORT_OBJS   = $(filter-out $(OBJ_DIR)/main.o, $(OBJECTS))
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 $@

# chunk5SetupTest checks the C SetupViewProjection against the fs2trace
# oracle; it links the port objects (no SDL needed beyond the libs).
$(BIN_DIR)/chunk5SetupTest: $(TOOL_DIR)/chunk5SetupTest.c $(PORT_OBJS) | $(BIN_DIR)
	$(CC) $(TOOL_CFLAGS) -o $@ $(TOOL_DIR)/chunk5SetupTest.c $(PORT_OBJS) -lm

# ramToPpm decodes a RAM dump's hires page with the port's decoder.
$(BIN_DIR)/ramToPpm: $(TOOL_DIR)/ramToPpm.c $(PORT_OBJS) | $(BIN_DIR)
	$(CC) $(TOOL_CFLAGS) -o $@ $(TOOL_DIR)/ramToPpm.c $(PORT_OBJS) -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)
