# The source directory.
SRCDIR=.
# The extension you are using for C++ source files. Options include: cpp, cc, cxx.
CPP_EXT=cpp
# What you run to play the game!
EXECUTABLE=game.out
# The directory to put the executable.
BINDIR=.
# The C++ compiler you are using.
CC=g++
# The packages loaded by pkg-config.
PKGS=glew glfw3 freetype2 assimp irrklang
# The directory to store the compiled .o files.
OBJDIR=obj

# The flags you want to use when compiling individual objects (.o files)
# -g=generate information for debugging (used with debugging tools such as gdb
#  or valgrind)
# -MMD=Create a .d file to store the rule for the header dependencies of each object.
CFLAGS=-g -O3 -MMD -I$(SRCDIR) `pkg-config --cflags $(PKGS)`

# LDLIBS (Load Libraries)
# External libraries you are using that need to be linked.
# ``=run a shell command (command substitution)
# pkg-config=a command that generates the load libs/cflags necessary depending
# on the platform (OS/Linux/Win)
LDLIBS=`pkg-config --libs --static $(PKGS)`

# LDFLAGS (Load/linker flags)
LDFLAGS=

# MISCFLAGS (Miscellaneous flags for miscellaneous reasons)
# MAC: -Wno-shift-op-parentheses stops annoying warnings about '<<' precedence
# LINUX: -DGL_GLEXT_PROTOTYPES ?
MISCFLAGS = -Wno-shift-op-parentheses

# Directories we should avoid looking for files in
EXCLUDE=$(SRCDIR)/glm
EXCLUDE= -path "./glm" -prune

# Pretty much everything past this point is not really meant to be modified.
# But have at it if you want.

# The C++ source files (but not the headers)
SRCS=$(shell find $(SRCDIR) -type f -name "*.$(CPP_EXT)" -print \
       -o $(EXCLUDE) )

# The .o files. One per cpp file. Put them in the obj directory.
OBJECTS=$(patsubst %.$(CPP_EXT),$(OBJDIR)/%.o,$(SRCS))

# If files named all/clean/run exist in the directory, then
# Make will not run these commands (because they will be "Up-to-Date")
# .PHONY is a special way of telling Make to run these no matter what.
.PHONY: all clean run

all: $(EXECUTABLE)

# Pastes in all of the Make rules inside of the generated .d files.
# Rules are of the format (file.o: file.cpp header1.h header2.h)
# Each .d file gets generated whenever its .o file is compiled.
# Special thanks to /u/yurik for pointing this out to me!
-include $(OBJECTS:.o=.d)

# Links the objects into an executable.
# Also links the libs in $(LDLIBS)
# Puts the Executable in the bindir.
# re-link when an object or dependency has changed.
$(EXECUTABLE): $(OBJECTS)
	@mkdir -p $(BINDIR)
	$(CC) $(LDFLAGS) -o $(BINDIR)/$(EXECUTABLE) $(OBJECTS) $(LDLIBS)

# The rule for how to compile an .o (object) file from a .cpp (C++ source) file
# "-c" creates a .o file
# "-o" says to create an object file (.o)
# "$@" in this case is the name of the object file (.o). MUST FOLLOW -o
# "$<" in this case is the .cpp file
# Make the directory if it doesn't exist
$(OBJDIR)/%.o: %.$(CPP_EXT)
	@mkdir -p $(@D)
	$(CC) -c $(MISCFLAGS) $(CFLAGS) -o $@ $<

# Deletes all .o/.d files and the executable. This helps when you want to
# force recompilation.
clean:
	rm -rf $(OBJDIR)

# Just a nice way of running the game. (Since you have to be in the bindir for
# the content to load properly)
run:
	cd $(BINDIR) && ./$(EXECUTABLE)
