OUTDIR = build

CXXFLAGS += $(shell sdl2-config --cflags) -std=c++14 -Wall -g
CFLAGS += $(shell sdl2-config --cflags)
CPPFLAGS += -I. -I$(OUTDIR) \
	    -I3rdparty/imgui -I3rdparty/glm \
	    -I3rdparty/icon-font-headers \
	    -I3rdparty/ebcl/include \
	    -DREAL_BUILD -DGLM_ENABLE_EXPERIMENTAL

BUILDER_LIBS = $(LIBEBCL) -latomic
TOOL_LIBS = $(shell sdl2-config --libs) -lGL -lGLEW -ldl $(BUILDER_LIBS)
	

BUILTIN_SHADERS = \
		  copy \
		  dbg-alpha \
		  dbg-depth-linear \
		  dbg-depth-reinhard \
		  dbg-reinhard \
		  fullscreen \
# END BUILTIN_SHADERS

IMGUI = imgui.cpp imgui_draw.cpp

COMMON = \
	 common.cc \
	 c-buildcfg.cc \
	 c-camera.cc \
	 c-filewatcher.cc \
	 c-project.cc \
	 c-shaders.cc \
	 c-utilities.cc \
	 c-undo.cc \
	 \
	 c-ops.cc \
	 c-opast.cc \
	 c-opopt.cc \
	 c-opparser.cc \
	 c-opcomp.cc \
	 c-opmgr.cc \
	 \
	 c-sync.cc \
	 c-syncedit.cc \
	 c-syncoverrides.cc \
# END COMMON

TOOL = \
	 m-tool.cc \
	 ui.cc \
	 ui-actions.cc \
	 ui-app.cc \
	 ui-colorgrading.cc \
	 ui-demo.cc \
	 ui-dialogs.cc \
	 ui-imgui-sdl.cc \
	 ui-odbg.cc \
	 ui-opemu.cc \
	 ui-overrides.cc \
	 ui-profiling.cc \
	 ui-rendertarget.cc \
	 ui-sequencer.cc \
	 ui-shaders.cc \
	 ui-sync.cc \
	 ui-texture.cc \
	 ui-utilities.cc \
# END TOOL

BUILDER = \
	 m-builder.cc \
# END BUILDER


BUILTIN_SHADERS_SRC = $(addprefix toolshaders/, $(addsuffix .glsl, $(BUILTIN_SHADERS)))
BUILTIN_SHADERS_H = $(addprefix $(OUTDIR)/bs-, $(addsuffix .inl, $(BUILTIN_SHADERS)))

IMGUI_OBJS = $(addprefix $(OUTDIR)/, $(addsuffix .o, $(basename $(IMGUI))))
COMMON_OBJS = $(addprefix $(OUTDIR)/, $(addsuffix .o, $(basename $(COMMON))))
TOOL_OBJS = $(addprefix $(OUTDIR)/, $(addsuffix .o, $(basename $(TOOL))))
BUILDER_OBJS = $(addprefix $(OUTDIR)/, $(addsuffix .o, $(basename $(BUILDER))))
LIBEBCL = $(OUTDIR)/ebcl/libebcorelib.a
PCH=externals.hh.gch

COMMON_DEPS = $(COMMON_OBJS:$(OUTDIR)/%.o=$(OUTDIR)/%.d)
DEMO_DEPS = $(TOOL_OBJS:$(OUTDIR)/%.o=$(OUTDIR)/%.d)
BUILDER_DEPS = $(BUILDER_OBJS:$(OUTDIR)/%.o=$(OUTDIR)/%.d)

COMMON_REQ = $(COMMON_OBJS)

DEMO_BUILD = $(TOOL_OBJS) $(COMMON_REQ) $(IMGUI_OBJS)
BUILDER_BUILD = $(BUILDER_OBJS) $(COMMON_REQ)

ALL_OBJS = $(TOOL_OBJS) $(BUILDER_OBJS) $(COMMON_REQ)
ALL_DEPS = $(COMMON_DEPS) $(DEMO_DEPS) $(BUILDER_DEPS)


all:	tourista tourista-builder

tourista:	$(LIBEBCL) $(DEMO_BUILD)
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o tourista \
		$(DEMO_BUILD) $(TOOL_LIBS)

tourista-builder: $(LIBEBCL) $(BUILDER_BUILD)
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o tourista-builder \
		$(BUILDER_BUILD) $(BUILDER_LIBS)

clean:
	rm -f $(ALL_OBJS) $(BUILTIN_SHADERS_H) tourista tourista-builder $(PCH) $(OUTDIR)/font-awesome.inl

depclean: clean
	rm -f $(ALL_DEPS)

fullclean: depclean
	@+make -C 3rdparty/ebcl fullclean CONFIG=../../ebcl-config
	rm -f $(OUTDIR)/font-to-c

.PHONY: all clean fullclean


outdir:
	@if ! [ -d "$(OUTDIR)" ]; then mkdir "$(OUTDIR)"; fi
.PHONY: outdir

#-------------------------------------------------------------------------------

$(IMGUI_OBJS): $(OUTDIR)/%.o: 3rdparty/imgui/%.cpp | outdir
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c -o $@ $<

#-------------------------------------------------------------------------------

-include $(DEMO_DEPS)
$(TOOL_OBJS): $(OUTDIR)/%.o: %.cc $(PCH) | outdir $(BUILTIN_SHADERS_H)
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c -o $@ $<
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) -M -MF $(@:%.o=%.d) -MT $@ $<

#-------------------------------------------------------------------------------

-include $(COMMON_DEPS)
$(COMMON_OBJS): $(OUTDIR)/%.o: %.cc $(PCH) | outdir
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c -o $@ $<
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) -M -MF $(@:%.o=%.d) -MT $@ $<

#-------------------------------------------------------------------------------

-include $(BUILDER_DEPS)
$(BUILDER_OBJS): $(OUTDIR)/%.o: %.cc $(PCH) | outdir
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c -o $@ $<
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) -M -MF $(@:%.o=%.d) -MT $@ $<

#-------------------------------------------------------------------------------

$(LIBEBCL):	ebcl-config
	+make -C 3rdparty/ebcl CONFIG=../../ebcl-config

$(PCH): externals.hh
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c -o $@ $<

#-------------------------------------------------------------------------------

$(BUILTIN_SHADERS_H): $(OUTDIR)/bs-%.inl: toolshaders/%.glsl | outdir
	xxd -i < $< > $@

#-------------------------------------------------------------------------------

$(OUTDIR)/ui-app.o: ui-app.cc $(OUTDIR)/font-awesome.inl

$(OUTDIR)/font-awesome.inl: 3rdparty/font-awesome/fonts/fontawesome-webfont.ttf $(OUTDIR)/font-to-c
	$(OUTDIR)/font-to-c -base85 3rdparty/font-awesome/fonts/fontawesome-webfont.ttf \
		FontAwesome_ >$@

$(OUTDIR)/font-to-c: 3rdparty/imgui/extra_fonts/binary_to_compressed_c.cpp
	$(CXX) -o $@ $<