#
#  GameEngine and Editor
#  by John Ryland
#  Copyright (c) 2023
#

include 3rdparty/glfw.pro
include 3rdparty/freetype.pro

LD = g++

TARGET         = EditorUI
VERSION        = 0.9.8
BRIEF          = "Game Engine and Editor"

ROOT_DIR       = ../..
MODULES_DIR    = .modules
#CXXFLAGS       = -std=c++14 -fno-exceptions -fno-rtti -Os -s -static
CXXFLAGS       = -std=c++17 -fno-rtti -Os -fPIC -D__DARWIN__ # -static

# for nativefiledialog support:
#CFLAGS         = $(shell pkg-config --cflags gtk+-2.0)

FA_SRC_DIR     = $(MODULES_DIR)/Font-Awesome
FW_SRC_DIR     = $(MODULES_DIR)/filewatch
IFCH_SRC_DIR   = $(MODULES_DIR)/IconFontCppHeaders
DJV_SRC_DIR    = $(MODULES_DIR)/dejavu-fonts
IMGUI_SRC_DIR  = $(MODULES_DIR)/imgui
GM_SRC_DIR     = $(MODULES_DIR)/GenericMake
NFD_SRC_DIR    = $(MODULES_DIR)/nativefiledialog
IFD_SRC_DIR    = $(MODULES_DIR)/ImFileDialog
NODES_SRC_DIR  = $(MODULES_DIR)/imgui-node-editor
EDITOR_SRC_DIR = $(MODULES_DIR)/ImGuiColorTextEdit
TOL_SRC_DIR    = $(MODULES_DIR)/tinyobjloader
GEN_SRC_DIR    = $(OUTPUT_DIR)/generated/include

# All the 3rdparty module directories
# Perhaps a better pattern is each 3rdparty module has a .pro file which is included
# here, and those .pro files define the variables that are here and the steps for
# generating the license file as a header file etc. Ultimately they could be plugins
# and the plugin interface has all the generic interfaces of adding a 3rdparty module.
FA_DIR         = $(ROOT_DIR)/$(FA_SRC_DIR)
FT2_DIR        = $(ROOT_DIR)/$(FT2_INC_DIR)
FW_DIR         = $(ROOT_DIR)/$(FW_SRC_DIR)
GLFW_DIR       = $(ROOT_DIR)/$(GLFW_INC_DIR)
IFCH_DIR       = $(ROOT_DIR)/$(IFCH_SRC_DIR)
IFD_DIR        = $(ROOT_DIR)/$(IFD_SRC_DIR)
IMGUI_DIR      = $(ROOT_DIR)/$(IMGUI_SRC_DIR)
NFD_DIR        = $(ROOT_DIR)/$(NFD_SRC_DIR)
NODES_DIR      = $(ROOT_DIR)/$(NODES_SRC_DIR)
EDITOR_DIR     = $(ROOT_DIR)/$(EDITOR_SRC_DIR)
NFD_DIR        = $(ROOT_DIR)/$(NFD_SRC_DIR)
TOL_DIR        = $(ROOT_DIR)/$(TOL_SRC_DIR)
GEN_DIR        = $(ROOT_DIR)/$(GEN_SRC_DIR)
MODS_DIR       = $(ROOT_DIR)/$(MODULES_DIR)

INCLUDES       = ./ \
                 ../Runtime \
                 ../Editor \
                 ../Tools \
                 ../Framework \
                 ../PluginFramework \
                 ../Render \
                 ../Terminal \
                 ../Views \
                 ../Vulkan \
                 ../Vulkan/sdk \
                 $(FA_DIR) \
                 $(FT2_DIR) \
                 $(FW_DIR) \
                 $(GLFW_DIR) \
                 $(IFCH_DIR) \
                 $(IFD_DIR) \
                 $(IMGUI_DIR) \
                 $(NODES_DIR) \
                 $(NODES_DIR)/external \
                 $(NODES_DIR)/external/stb_image \
                 $(NODES_DIR)/examples/blueprints-example \
                 $(EDITOR_DIR) \
                 $(NFD_DIR)/src/include \
                 $(TOL_DIR) \
                 $(GEN_DIR) \
                 $(MODS_DIR)
    
EDITOR_SOURCES = ../Runtime/Runtime.pro \
                 ../Editor/Editor.pro \
                 ../Framework/Acknowledgements.cpp \
                 ../Framework/Application.cpp \
                 ../Framework/GlfwWindowSystem.cpp \
                 ../Framework/GlfwVulkanSurface.cpp \
                 ../Framework/GlfwPlatform.cpp \
                 ../Framework/Image.cpp \
                 ../Framework/DearImGuiUiSystem.cpp \
                 ../Framework/MainWindow.cpp \
                 ../Framework/VulkanRenderer.cpp \
                 ../Framework/Window.cpp \
                 ../PluginFramework/Library.cpp \
                 ../PluginFramework/Plugin.cpp \
                 ../PluginFramework/PluginManager.cpp \
                 ../PluginFramework/SamplePlugin/SamplePlugin.pro \
                 ../Render/Camera.cpp \
                 ../Render/Model.cpp \
                 ../Render/Transformation.cpp \
                 ../Vulkan/VulkanBuffer.cpp \
                 ../Vulkan/VulkanCommandPool.cpp \
                 ../Vulkan/VulkanDepthBuffer.cpp \
                 ../Vulkan/VulkanDevice.cpp \
                 ../Vulkan/VulkanFramebuffer.cpp \
                 ../Vulkan/VulkanImageBuffer.cpp \
                 ../Vulkan/VulkanPipeline.cpp \
                 ../Vulkan/VulkanRenderPass.cpp \
                 ../Vulkan/VulkanShader.cpp \
                 ../Vulkan/VulkanSurface.cpp \
                 ../Vulkan/VulkanSwapChain.cpp \
                 ../Vulkan/VulkanTexture.cpp \
                 ../Views/CanvasEditorView.cpp \
                 ../Views/ComponentTypeEditorView.cpp \
                 ../Views/EntityTreePropertyEditorView.cpp \
                 ../Views/FileBrowser.cpp \
                 ../Views/FlatEntityListView.cpp \
                 ../Views/ImageView.cpp \
                 ../Views/RenderView.cpp \
                 ../Views/TextEditorView.cpp \
                 Credits.cpp \
                 EcsSystem.cpp \
                 EditorUI.cpp \
                 GameEditorWindow.cpp \
                 GameState.cpp \
                 $(IMGUI_DIR)/imgui.cpp \
                 $(IMGUI_DIR)/imgui_demo.cpp \
                 $(IMGUI_DIR)/imgui_draw.cpp \
                 $(IMGUI_DIR)/imgui_tables.cpp \
                 $(IMGUI_DIR)/imgui_widgets.cpp \
                 $(IMGUI_DIR)/misc/freetype/imgui_freetype.cpp \
                 $(EDITOR_DIR)/TextEditor.cpp

FD_SOURCES     = $(IFD_DIR)/ImFileDialog.cpp \
                 $(NFD_DIR)/src/nfd_common.c \
                 $(NFD_DIR)/src/nfd_zenity.c

TERMINAL_SOURCES = ../Views/TerminalView.cpp \
                 ../Terminal/TerminalEmulator.cpp \
                 ../Terminal/TerminalProcess.cpp

NODES_SOURCES =  ../Views/BlueprintsNodeEditorView.cpp \
                 ../Views/NodeEditorView.cpp \
                 $(NODES_DIR)/examples/blueprints-example/utilities/builders.cpp \
                 $(NODES_DIR)/examples/blueprints-example/utilities/drawing.cpp \
                 $(NODES_DIR)/examples/blueprints-example/utilities/widgets.cpp \
                 $(NODES_DIR)/examples/blueprints-example/blueprints-example.cpp \
                 $(NODES_DIR)/crude_json.cpp \
                 $(NODES_DIR)/imgui_canvas.cpp \
                 $(NODES_DIR)/imgui_node_editor_api.cpp \
                 $(NODES_DIR)/imgui_node_editor.cpp

SOURCES        = $(EDITOR_SOURCES) \
                 $(TERMINAL_SOURCES)

# Generated files using the generator tools
GEN_H          = $(GEN_SRC_DIR)/app_license.bin_inc \
                 $(GEN_SRC_DIR)/fa_license.bin_inc \
                 $(GEN_SRC_DIR)/ft2_license.bin_inc \
                 $(GEN_SRC_DIR)/fw_license.bin_inc \
                 $(GEN_SRC_DIR)/glfw_license.bin_inc \
                 $(GEN_SRC_DIR)/gm_license.bin_inc \
                 $(GEN_SRC_DIR)/ifch_license.bin_inc \
                 $(GEN_SRC_DIR)/imgui_license.bin_inc \
                 $(GEN_SRC_DIR)/nodes_license.bin_inc \
                 $(GEN_SRC_DIR)/editor_license.bin_inc \
                 $(GEN_SRC_DIR)/nfd_license.bin_inc \
                 $(GEN_SRC_DIR)/tol_license.bin_inc

GL_LIBS        = -lGL

# VK_LIBS        = -lvulkan
VK_LIBS        = -L./bin -lvulkan.1 -framework CoreFoundation -framework IOKit -framework Cocoa -framework QuartzCore -Wl,-rpath,@executable_path/../Frameworks

#GTK_LIBS       = $(shell pkg-config --libs gtk+-2.0)
EDITOR_LIB     = $(OUTPUT_DIR)/install/usr/local/lib/libEditor.a
RUNTIME_LIB    = $(OUTPUT_DIR)/install/usr/local/lib/libRuntime.a
ENGINE_LIBS    = $(EDITOR_LIB) $(RUNTIME_LIB)
LIBS           = $(ENGINE_LIBS) $(FT2_LIB) $(GLFW_LIB) $(GL_LIBS) $(VK_LIBS) $(GTK_LIBS) $(FFMPEG_LIBS)

ifeq ($(PLATFORM),Windows)
  DEFINES   += _WIN32
  INCLUDES  += c:/VulkanSDK/1.3.239.0/Include
  # LIBS       = $(ENGINE_LIBS) $(FT2_LIB) $(GLFW_LIB) -lopengl32 -lglu32 -lkernel32 -lgdi32 -luser32 -limm32 -L/mnt/c/VulkanSDK/1.3.239.0/Lib -lvulkan-1 -lstdc++
  LIBS       = $(ENGINE_LIBS) $(FT2_LIB) $(GLFW_LIB) -L/mnt/c/VulkanSDK/1.3.239.0/Lib -lgdi32 -lvulkan-1 -std=c++17
  # -lstdc++ -std=c++11 -static-libgcc -static-libstdc++ -lwinpthread 
endif

ifeq ($(PLATFORM),Darwin)
  # CXXFLAGS       = -mmacos-version-min=15.0 $(CXXFLAGS)
  # LIBS           = $(ENGINE_LIBS) $(FT2_LIB) $(GLFW_LIB) $(VK_LIBS) $(GTK_LIBS) $(FFMPEG_LIBS) -mmacos-version-min=14.0 -lm -Wl,-framework,CoreAudio -Wl,-framework,AudioToolbox -Wl,-weak_framework,CoreHaptics -Wl,-weak_framework,GameController -Wl,-framework,ForceFeedback -lobjc -Wl,-framework,CoreVideo -Wl,-framework,Cocoa -Wl,-framework,Carbon -Wl,-framework,IOKit -Wl,-weak_framework,QuartzCore -Wl,-weak_framework,Metal
  # LIBS           = $(ENGINE_LIBS) $(FT2_LIB) $(GLFW_LIB) $(VK_LIBS) $(GTK_LIBS) $(FFMPEG_LIBS) -lm -Wl,-framework,CoreAudio -Wl,-framework,AudioToolbox -Wl,-weak_framework,CoreHaptics -Wl,-weak_framework,GameController -Wl,-framework,ForceFeedback -lobjc -Wl,-framework,CoreVideo -Wl,-framework,Cocoa -Wl,-framework,Carbon -Wl,-framework,IOKit -Wl,-weak_framework,QuartzCore -Wl,-weak_framework,Metal -Wl,-weak_framework,MetalKit -lMoltenVK
  # LIBS           = $(ENGINE_LIBS) $(FT2_LIB) -lglfw3 $(VK_LIBS) $(GTK_LIBS) $(FFMPEG_LIBS) -lm -framework Cocoa -framework IOKit -Wl,-rpath,@executable_path/../Frameworks -Wl,-weak_framework,QuartzCore -lGL -lvulkan
#  LIBS           = $(ENGINE_LIBS) $(FT2_LIB) $(GLFW_LIB) $(VK_LIBS) $(GTK_LIBS) $(FFMPEG_LIBS) -lm -framework Cocoa -framework IOKit -Wl,-rpath,@executable_path/../Frameworks -Wl,-weak_framework,QuartzCore -lGL -lvulkan
  #  -Wl,-framework,CoreAudio -Wl,-framework,AudioToolbox -Wl,-weak_framework,CoreHaptics
  #  -Wl,-weak_framework,GameController -Wl,-framework,ForceFeedback -lobjc -Wl,-framework,CoreVideo
  #   -Wl,-framework,Cocoa -Wl,-framework,Carbon -Wl,-framework,IOKit
  #   -Wl,-weak_framework,Metal -Wl,-weak_framework,MetalKit -lMoltenVK
endif

# Generator tool
MAKEHEADER     = $(OUTPUT_DIR)/tools/MakeHeader

$(MAKEHEADER): src/Tools/MakeHeader.cpp
	@$(call MKDIR,$(dir $@))
	g++ $< -o $@

%.bin_inc:
	@$(call MKDIR,$(dir $@))
	$(MAKEHEADER) $< -o $@

$(GEN_SRC_DIR)/app_license.bin_inc:    src/EditorUI/LICENSE.txt
$(GEN_SRC_DIR)/fa_license.bin_inc:     $(FA_SRC_DIR)/LICENSE.txt
$(GEN_SRC_DIR)/ft2_license.bin_inc:    $(FT2_SRC_DIR)/LICENSE.TXT
$(GEN_SRC_DIR)/fw_license.bin_inc:     $(FW_SRC_DIR)/LICENSE
$(GEN_SRC_DIR)/djv_license.bin_inc:    $(DJV_SRC_DIR)/LICENSE
$(GEN_SRC_DIR)/glfw_license.bin_inc:   $(GLFW_SRC_DIR)/LICENSE.md
$(GEN_SRC_DIR)/gm_license.bin_inc:     $(GM_SRC_DIR)/LICENSE
$(GEN_SRC_DIR)/ifch_license.bin_inc:   $(IFCH_SRC_DIR)/licence.txt
$(GEN_SRC_DIR)/imgui_license.bin_inc:  $(IMGUI_SRC_DIR)/LICENSE.txt
$(GEN_SRC_DIR)/nodes_license.bin_inc:  $(NODES_SRC_DIR)/LICENSE
$(GEN_SRC_DIR)/editor_license.bin_inc: $(EDITOR_SRC_DIR)/LICENSE
$(GEN_SRC_DIR)/nfd_license.bin_inc:    $(NFD_SRC_DIR)/LICENSE
$(GEN_SRC_DIR)/tol_license.bin_inc:    $(TOL_SRC_DIR)/LICENSE

$(GEN_H): $(MAKEHEADER)

$(TARGET_BIN): $(FT2_LIB) $(GLFW_LIB) src/Runtime/Runtime.subproject_target src/Editor/Editor.subproject_target $(GEN_H)
