#######################################################################
#
# Instructions:
#
# make
#   Compiles all .cpp files in the src directory to .o
#   files in the obj directory, and links them into an
#   executable in the currect directory.
#
# make clean
#   Removes all .o files from the obj directory and the game executable.
#
# Optional parameters:
#
# STATICLINK=1
#   Compiles/removes a statically linked version of the game without
#   DLL dependencies. The static object files are put in obj/static
#   and the executable has '_static' appended to the name.
#
# If you use add-on libraries, add them to the lines starting with
# 'LIBS='. Make sure you enter the libraries in both lines, for the
# normal and static version!
#
#######################################################################

#note: STATICLINK=1 overrides BUILD=debug

#BUILD=DEBUG
BUILD=RELEASE

CC = gcc
CXX = g++
LD = g++
CFLAGS = -Iinclude -W -Wall

ifeq ($(BUILD),RELEASE)
	CFLAGS += -O3 -s
endif
ifeq ($(BUILD),DEBUG)
	CFLAGS += -g -DDEBUG
endif

# Add-on libraries go here
ifdef STATICLINK
	LIBS = -ltegel -laldmd -ldumbd 
else
	LIBS = -ltegel -laldmd -ldumbd 
endif


ifndef NAME
	NAME = fole1
endif

ifndef WINDOWS
ifdef MINGDIR
	WINDOWS = 1
endif
endif

ifdef WINDOWS
	RM = del /q
	CFLAGS += -D__GTHREAD_HIDE_WIN32API
	LFLAGS = -Wl,--subsystem,windows
	ifdef STATICLINK
		CFLAGS += -DSTATICLINK
		LIBS += -lalleg_s -lkernel32 -luser32 -lgdi32 -lcomdlg32 -lole32 -ldinput -lddraw -ldxguid -lwinmm -ldsound
		OBJDIR = obj/static
		BIN = $(NAME)_static.exe
	else
		ifeq ($(BUILD),RELEASE)
			LIBS += -lalleg
		endif
		ifeq ($(BUILD),DEBUG)
			LIBS += -lalld
		endif
		OBJDIR = obj
		BIN = $(NAME).exe
	endif
	ICONOBJ = $(OBJDIR)/icon.o	
else
	RM = rm -f
	ifdef STATICLINK
		LIBS += `allegro-config --libs --static` -lXrender
		OBJDIR = obj/static
		BIN = $(NAME)_static
	else
		ifeq ($(BUILD),RELEASE)
			LIBS += `allegro-config --libs`
		endif
		ifeq ($(BUILD),DEBUG)
			LIBS += `allegro-config --libs debug`
		endif
		OBJDIR = obj
		BIN = $(NAME)
	endif
endif

OBJ_CPP := $(addprefix $(OBJDIR)/, $(subst src/,,$(patsubst %.cpp,%.o,$(wildcard src/*.cpp))))
DEP = $(addprefix $(OBJDIR)/, $(subst src/,,$(patsubst %.cpp,%.d,$(wildcard src/*.cpp))))

all: game

$(OBJDIR)/%.o: src/%.cpp
	$(CXX) $(CFLAGS) -MMD -o $@ -c $<

game: $(OBJ_CPP) $(ICONOBJ)
	$(LD) -o $(BIN) $(OBJ_CPP) $(ICONOBJ) $(LIBS) $(LFLAGS)

obj/icon.o : icon.rc icon.ico
	windres -I rc -O coff -i icon.rc -o obj\icon.o

# include the .d files
# they contain extra rules about how .o files depend on headers
-include $(DEP)

clean:
	-$(RM) $(BIN)
ifdef WINDOWS
	-$(RM) $(subst /,\,$(OBJ_CPP)) $(subst /,\,$(DEP))
else
	-$(RM) $(OBJ_CPP) $(DEP)
endif
