# ------------------------------------------------------------
#
#  Engine library build config
#
# ------------------------------------------------------------

#  Source
# ------------------------------------------------------------

set( ENGINE_SRC
	# Core
	source/Core/String.cpp

	# System
	source/System/ByteOrder.cpp
	source/System/Event.cpp
	source/System/EventListener.cpp
	source/System/File.cpp
	source/System/Log.cpp
	source/System/MessageHandler.cpp
	source/System/MessageQueue.cpp
	source/System/Path.cpp
	source/System/Stopwatch.cpp
	source/System/Log/FileWriter.cpp

	# Platform
	source/Platform/PlatformApplication.cpp
	source/Platform/PlatformDisplay.cpp

	# Math
	source/Math/Color.cpp
	source/Math/Logarithm.cpp
	source/Math/Time.cpp
	source/Math/Transform.cpp
	source/Math/Math.cpp

	# Input
	source/Input/InputDevice.cpp
	source/Input/InputModule.cpp
	source/Input/Keyboard.cpp
	source/Input/Mouse.cpp

	# Display
	source/Display/Display.cpp
	source/Display/DisplayDescription.cpp
	source/Display/DisplayMode.cpp
	source/Display/GLContext.cpp

	# GfxDriver
	source/GfxDriver/ShaderProgram.cpp

	# Graphics
	source/Graphics/BatchRenderer2D.cpp
	source/Graphics/DefaultRenderer2D.cpp
	source/Graphics/Renderable2D.cpp
	source/Graphics/Renderer2D.cpp
	source/Graphics/RenderState.cpp
	source/Graphics/Sprite.cpp
	source/Graphics/Text.cpp
	source/Graphics/Texture.cpp
	source/Graphics/Transformable.cpp
	source/Graphics/Vertex2D.cpp
	source/Graphics/Graphics.cpp

	# Graphics - Font
	source/Graphics/Font/Engine/FreeTypeEngine.cpp
	source/Graphics/Font/Engine/FreeTypeError.cpp
	source/Graphics/Font/Engine/FreeTypeLib.cpp
	source/Graphics/Font/FontDescription.cpp
	source/Graphics/Font.cpp

	# Graphics - Image
	source/Graphics/Image/IcoFormat.cpp
	source/Graphics/ImageLoader.cpp
	source/Graphics/PixelFormat.cpp
	source/Graphics/Image.cpp

	# Graphics - GL
	source/Graphics/GL/glad.c
	source/Graphics/GL/CheckError.cpp

	# Scene
	source/Scene/Camera2D.cpp

	# Game
	source/Game/FPSCounter.cpp
	source/Game/GameTime.cpp
	source/Game.cpp
)

set(ENGINE_GFXDRIVER_OPENGL_SRC
	source/GfxDriver/OpenGL/OpenGLDrv.cpp
	source/GfxDriver/OpenGL/OpenGLShaderProgram.cpp
)

set(ENGINE_PLATFORM_WIN32_SRC
	source/Platform/Win32/Win32Application.cpp
	source/Platform/Win32/Win32Display.cpp
	source/Platform/Win32/Win32GLContext.cpp
	source/Platform/Win32/Win32Input.cpp
	source/Platform/Win32/Win32Internal.cpp
	source/Platform/Win32/Win32Keyboard.cpp
	source/Platform/Win32/Win32Misc.cpp
	source/Platform/Win32/Win32Mouse.cpp
	source/Platform/Win32/Win32EventQueue.cpp
	source/Platform/Win32/Win32System.cpp
	source/Platform/Win32/glad_wgl.c
)

set(ENGINE_PLATFORM_UNIX_SRC
	source/Platform/Unix/Xlib.cpp
	source/Platform/Unix/UnixApplication.cpp
	source/Platform/Unix/X11Display.cpp
	source/Platform/Unix/GLXContext.cpp
	source/Platform/Unix/X11Input.cpp
	source/Platform/Unix/X11Keyboard.cpp
	source/Platform/Unix/X11Mouse.cpp
	source/Platform/Unix/X11EventQueue.cpp
	source/Platform/Unix/X11WindowEventHandler.cpp
	source/Platform/Unix/UnixMisc.cpp
	source/Platform/Unix/UnixSystem.cpp
	source/Platform/Unix/glad_glx.c
)


#  Library
# ------------------------------------------------------------

add_library( Spectre STATIC ${ENGINE_SRC} )
target_include_directories(Spectre
	PUBLIC
		$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
		$<INSTALL_INTERFACE:include>
	PRIVATE
		${CMAKE_CURRENT_LIST_DIR}/source
)

if (WIN32)
	target_compile_options(Spectre PRIVATE "-DSPECTRE_PLATFORM_WIN=1")
else()
	target_compile_options(Spectre PRIVATE "-DSPECTRE_PLATFORM_UNIX=1")
endif()


#  Dependancies
# ------------------------------------------------------------

# Platform specific
if (WIN32)
	target_sources(Spectre PRIVATE ${ENGINE_PLATFORM_WIN32_SRC})
else()
	target_sources(Spectre PRIVATE ${ENGINE_PLATFORM_UNIX_SRC})

	# Unix needs to link against X11
	find_package(X11 REQUIRED COMPONENTS Xrandr)
	target_link_libraries(Spectre PRIVATE X11::X11 X11::Xrandr)
endif (WIN32)

# Select graphics API

# Only OpenGL Driver for now
target_sources(Spectre PRIVATE ${ENGINE_GFXDRIVER_OPENGL_SRC})
if (WIN32)
	find_package(OpenGL REQUIRED COMPONENTS OpenGL)
	target_link_libraries(Spectre PRIVATE OpenGL::GL)
else()
	find_package(OpenGL REQUIRED COMPONENTS OpenGL GLX)
	target_link_libraries(Spectre PRIVATE OpenGL::OpenGL OpenGL::GLX)
endif()

# FreeType
add_subdirectory(vendor/FreeType2)
target_sources(Spectre PRIVATE $<TARGET_OBJECTS:freetype>)
target_include_directories(Spectre PRIVATE $<TARGET_PROPERTY:freetype,INTERFACE_INCLUDE_DIRECTORIES>)

# STB
target_include_directories(Spectre PRIVATE ${CMAKE_CURRENT_LIST_DIR}/vendor/stb/include)

#  Install
# ------------------------------------------------------------

# Includes
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/Spectre TYPE INCLUDE)

# Assets
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/assets DESTINATION ${CMAKE_INSTALL_DATADIR})


#  Install - CMake targets
# ------------------------------------------------------------

install(TARGETS Spectre
	EXPORT SpectreTargets
	LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
	ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
	RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
	INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

install(EXPORT SpectreTargets
        FILE SpectreTargets.cmake
        DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Spectre
)

# Build directory target
export(EXPORT SpectreTargets
       FILE "${CMAKE_CURRENT_BINARY_DIR}/SpectreTargets.cmake"
)

#  CMake config
# ------------------------------------------------------------

# Version
include(CMakePackageConfigHelpers)
write_basic_package_version_file(SpectreConfigVersion.cmake COMPATIBILITY SameMajorVersion)

set (ASSETS_DIR ${CMAKE_INSTALL_DATADIR}/assets)
configure_package_config_file(cmake/SpectreConfig.cmake.in
	${CMAKE_CURRENT_BINARY_DIR}/cmake/SpectreConfig.cmake
	INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Spectre
	PATH_VARS ASSETS_DIR)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/cmake/SpectreConfig.cmake
	${CMAKE_CURRENT_BINARY_DIR}/SpectreConfigVersion.cmake
  DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Spectre)

#  CMake config - build directory
# ------------------------------------------------------------
set (ASSETS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/assets)
configure_package_config_file(cmake/SpectreConfig.cmake.in
	${CMAKE_CURRENT_BINARY_DIR}/SpectreConfig.cmake
	INSTALL_DESTINATION ${CMAKE_CURRENT_BINARY_DIR}
	INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR}
	PATH_VARS ASSETS_DIR)
