-compileargs ::= -g -Wall -Wextra -Wpedantic
-linkargs ::=
-libraries ::= -L/usr/local/lib -lGL -lGLEW -lglut -lassimp
-# Prefix all object file names with the compilation directory
-objects ::= $(addprefix out/, \
- main.o debugutil.o glut_janitor.o render.o \
- tga.o level.o performance.o)
-
-# Set executable extension for the platform
-ifeq ($(OS),Windows_NT)
- binext ::= .exe
-else
- binext ::=
-endif
-binary ::= out/shadowclad$(binext)
-
-# Default target: build executable
-$(binary) : $(objects) | out
- @echo "###### Linking executable..."
- $(CC) $(linkargs) -o $(binary) $(objects) $(libraries)
-
-# Alias for default target
-shadowclad : $(binary)
-.PHONY : shadowclad
+#PLATFORM ?= x86_64-linux-gnu
+PLATFORM ?= x86_64-w64-mingw32
-# Build and run
-run : $(binary)
- @echo
- LD_LIBRARY_PATH=/usr/local/lib $(binary)
-.PHONY : run
+CC ::= x86_64-w64-mingw32-cc
+
+BUILDDIR ?= target/$(PLATFORM)
+SRCDIR ?= src
+
+CPPFLAGS ::= -iquotesrc/ $(CPPFLAGS)
+CFLAGS ::= -g -std=c99 -Wall -Wextra -Wpedantic -Werror \
+ -Wno-error=unused-function -Wno-error=unused-parameter $(CFLAGS)
+LDFLAGS ::= $(LDFLAGS)
+LDLIBS ::= -L/usr/x86_64-w64-mingw32/bin -lm -lopengl32 -lglew32 -lglfw3 -lassimp $(LDLIBS)
+
+# ######
+# Paths
+# ######
+
+sources ::= main.c \
+ engine/asset.c \
+ engine/engine.c \
+ engine/geometry.c \
+ engine/input.c \
+ engine/logger.c \
+ engine/performance.c \
+ engine/render.c \
+ engine/scene.c \
+ engine/string.c \
+ engine/tga.c \
+ engine/ui.c \
+ game/game.c \
+ game/input.c \
+ game/level.c \
+ game/player.c
+
+srcfiles ::= $(addprefix $(SRCDIR)/, $(sources))
+
+objects ::= $(addprefix $(BUILDDIR)/, $(addsuffix .o, $(srcfiles)))
+depfiles ::= $(addprefix $(BUILDDIR)/, $(addsuffix .mk, $(srcfiles)))
-# Create compilation directory
-out :
- mkdir out/
+# Set executable name for the platform
+# TODO Base this on target platform instead of host OS
+#ifeq ($(OS),Windows_NT)
+# binext ::= .exe
+#else
+# binext ::=
+#endif
+binary ::= $(BUILDDIR)/shadowclad.exe
+#binary ::= $(BUILDDIR)/shadowclad #$(binext)
-# Alias for 'out'
-init : out
-.PHONY : init
+# ######
+# Main build rules
+# ######
-# Build each compilation unit
+# Default rule: build executable
+$(binary): $(objects)
+ @mkdir -p $(@D)
+ @echo "Linking executable"
+ $(CC) $(LDFLAGS) -o $(binary) $^ $(LOADLIBES) $(LDLIBS)
+
+# Build C translation units
+$(objects): $(BUILDDIR)/%.c.o: %.c $(BUILDDIR)/%.c.mk
+ @mkdir -p $(@D)
+ @echo "Building $@"
+ @$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<
+
+# ######
+# Setup
+# ######
+
+# Initialise build environment
+init:
+ @echo "Creating build directory $(BUILDDIR)"
+ @mkdir -p $(BUILDDIR)
+.PHONY: init
+
+# ######
+# Aliases
+# ######
+
+# Build and run
+run: $(binary)
+ @echo
+ @$(binary)
+.PHONY: run
-out/main.o : main.c debugutil.h glut_janitor.h render.h level.h performance.h | out
- $(CC) $(compileargs) -c -o out/main.o main.c
+# Build executable
+shadowclad: $(binary)
+.PHONY: shadowclad
-out/debugutil.o : debugutil.c assimp_types.h | out
- $(CC) $(compileargs) -c -o out/debugutil.o debugutil.c
+# ######
+# Prerequisites
+# ######
-out/glut_janitor.o : glut_janitor.c | out
- $(CC) $(compileargs) -c -o out/glut_janitor.o glut_janitor.c
+# Generate C prerequisite makefiles
+$(depfiles): $(BUILDDIR)/%.c.mk: %.c Makefile
+ @mkdir -p $(@D)
+ @echo "Generating prerequisites for $<"
+ @$(CPP) -MM -MT $(BUILDDIR)/$*.c.o -MF $@ $(CPPFLAGS) $<
+# Give the same prerequisites to the prerequisite makefile,
+# so that it is regenerated whenever any of said prerequisites change
+ @sed -E -i 's|^([^\s:]+)([ :])|\1 $@\2|' $@
-out/render.o : render.c render.h level.h typedefs.h performance.h | out
- $(CC) $(compileargs) -c -o out/render.o render.c
+# Include generated C prerequisites
+include $(foreach depfile, $(depfiles), $(shell [ -r "$(depfile)" ] && echo "$(depfile)"))
-out/tga.o : tga.c tga.h | out
- $(CC) $(compileargs) -c -o out/tga.o tga.c
+# Do not automatically delete generated prerequisites
+.SECONDARY: $(depfiles)
-out/level.o : level.c level.h assimp_types.h tga.h | out
- $(CC) $(compileargs) -c -o out/level.o level.c
+# ######
+# Cleanup rules
+# ######
-out/performance.o : performance.c | out
- $(CC) $(compileargs) -c -o out/performance.o performance.c
+clean:
+ @echo "Removing $(BUILDDIR)"
+ @rm -rf $(BUILDDIR)
+.PHONY: clean