# https://www.libsdl.org/
#
# https://stackoverflow.com/a/57734435/688352
#
# https://www.libsdl.org/release/SDL2-2.0.14.zip
# https://www.libsdl.org/release/SDL2-2.0.16.zip

### DEPENDS ###
DKDEPEND(opengl)
if(MAC)
	DKDEPEND(libiconv)
endif()

### VERSION ###
#if(ANDROID)
#	DKSET(SDL2_VERSION 2.0.14)
#else()
	DKSET(SDL2_VERSION 2.0.16)
#endif()
DKSET(SDL2_NAME SDL2-${SDL2_VERSION})
DKSET(SDL2_DL https://www.libsdl.org/release/${SDL2_NAME}.zip)
DKSET(SDL2 ${3RDPARTY}/${SDL2_NAME})


### INSTALL ###
DKINSTALL(${SDL2_DL} sdl2 ${SDL2})


### PATCH ###
# https://discourse.libsdl.org/t/android-error-libsdl2-so-failed-to-load/25680/5
dkFileReplace(${SDL2}/CMakeLists.txt "add_library(hidapi SHARED" "add_library(hidapi STATIC")
dkFileReplace(${SDL2}/include/SDL_config_android.h "#define SDL_JOYSTICK_HIDAPI     1" "#undef SDL_JOYSTICK_HIDAPI")


### DKPLUGINS LINK ###
DKINCLUDE(${SDL2}/include)
ANDROID_INCLUDE(${ANDROIDNDK}/sources/android/cpufeatures)
ANDROID_INCLUDE(${SDL2}/src)
#if(ANDROID)
#	DKDEFINE(GL_GLEXT_PROTOTYPES)
#endif()
RASPBERRY_INCLUDE(/opt/vc/lib)
#LINUX_INCLUDE(/opt/local/include)
#LINUX_INCLUDE(/opt/local/lib)
#if(LINUX)
LINUX_INCLUDE(${SDL2}/${OS}/${RELEASE_DIR}/include)
#	find_package(OpenGL REQUIRED)
#	include_directories(${OpenGL_INCLUDE_DIRS})
#	link_directories(${OpenGL_LIBRARY_DIRS})
#	add_definitions(${OpenGL_DEFINITIONS})
#	if(NOT OPENGL_FOUND)
#   	message(FATAL_ERROR "OPENGL not found!")
#	endif()
#	LINUX_LIB(${OPENGL_LIBRARIES})
#endif()

WIN_DEBUG_LIB(${SDL2}/${OS}/${DEBUG_DIR}/SDL2d.lib)
WIN_RELEASE_LIB(${SDL2}/${OS}/${RELEASE_DIR}/SDL2.lib)
WIN_DEBUG_LIB(${SDL2}/${OS}/${DEBUG_DIR}/SDL2maind.lib)
WIN_RELEASE_LIB(${SDL2}/${OS}/${RELEASE_DIR}/SDL2main.lib)
MAC_DEBUG_LIB(${SDL2}/${OS}/${DEBUG_DIR}/libSDL2d.a)
MAC_RELEASE_LIB(${SDL2}/${OS}/${RELEASE_DIR}/libSDL2.a)
IOSSIM_DEBUG_LIB(${SDL2}/Xcode-iOS/SDL/build/Debug-iphonesimulator/libSDL2.a)
IOSSIM_RELEASE_LIB(${SDL2}/Xcode-iOS/SDL/build/Release-iphonesimulator/libSDL2.a) 
LINUX_DEBUG_LIB(${SDL2}/${OS}/${DEBUG_DIR}/libSDL2d.a)
LINUX_RELEASE_LIB(${SDL2}/${OS}/${RELEASE_DIR}/libSDL2.a)
#LINUX_DEBUG_LIB(${SDL2}/${OS}/${DEBUG_DIR}/libSDL2maind.a)
#LINUX_RELEASE_LIB(${SDL2}/${OS}/${RELEASE_DIR}/libSDL2main.a)
RASPBERRY_DEBUG_LIB(${SDL2}/${OS}/${DEBUG_DIR}/libSDL2d.a)
RASPBERRY_RELEASE_LIB(${SDL2}/${OS}/${RELEASE_DIR}/libSDL2.a)
##ANDROID_DEBUG_LIB(${SDL2}/${OS}/${DEBUG_DIR}/obj/local/armeabi-v7a/libSDL2.so)
##ANDROID_RELEASE_LIB(${SDL2}/${OS}/${RELEASE_DIR}/obj/local/armeabi-v7a/libSDL2.so)
#ANDROID_DEBUG_LIB(${SDL2}/${OS}/${DEBUG_DIR}/libhidapi.a)
#ANDROID_RELEASE_LIB(${SDL2}/${OS}/${RELEASE_DIR}/libhidapi.a)
ANDROID_DEBUG_LIB(${SDL2}/${OS}/${DEBUG_DIR}/libSDL2.a)
ANDROID_RELEASE_LIB(${SDL2}/${OS}/${RELEASE_DIR}/libSDL2.a)
ANDROID_DEBUG_LIB(${SDL2}/${OS}/${DEBUG_DIR}/libSDL2main.a)
ANDROID_RELEASE_LIB(${SDL2}/${OS}/${RELEASE_DIR}/libSDL2main.a)
#WIN_LIB(opengl32.lib)
WIN_LIB(winmm.lib)
WIN_LIB(imm32.lib)
WIN_LIB(version.lib)
WIN_LIB(Setupapi.lib)


### 3RDPARTY LINK ###
DKSET(SDL2_WIN
	-DSDL2_DIR=${SDL2}
	-DSDL2_INCLUDE_DIR=${SDL2}/include
	-DSDL_INCLUDE_DIR=${SDL2}/include
	-DSDL2_LIBRARY_TEMP=${SDL2}/${OS}/${RELEASE_DIR}/SDL2.lib
	-DSDL_LIBRARY=${SDL2}/${OS}/${RELEASE_DIR}/SDL2.lib
	-DSDL2_LIBRARY=${SDL2}/${OS}/${RELEASE_DIR}/SDL2.lib
	-DSDL2_LIBRARY_DEBUG=${SDL2}/${OS}/${DEBUG_DIR}/SDL2d.lib
	-DSDL2_LIBRARY_RELEASE=${SDL2}/${OS}/${RELEASE_DIR}/SDL2.lib
	-DSDL2MAIN_LIBRARY_TEMP=${SDL2}/${OS}/${RELEASE_DIR}/SDL2main.lib
	-DSDL2MAIN_LIBRARY=${SDL2}/${OS}/${RELEASE_DIR}/SDL2main.lib
	-DSDL2MAIN_LIBRARY_DEBUG=${SDL2}/${OS}/${DEBUG_DIR}/SDL2maind.lib
	-DSDL2MAIN_LIBRARY_RELEASE=${SDL2}/${OS}/${RELEASE_DIR}/SDL2main.lib)
	
DKSET(SDL2_APPLE
	-DSDL2_DIR=${SDL2}
	-DSDL2_INCLUDE_DIR=${SDL2}/include
	-DSDL2_LIBRARY_TEMP=${SDL2}/${OS}/${RELEASE_DIR}/libSDL2.a
	-DSDL2_LIBRARY=${SDL2}/${OS}/${RELEASE_DIR}/libSDL2.a
	-DSDL2_LIBRARY_DEBUG=${SDL2}/${OS}/${DEBUG_DIR}/libSDL2d.a
	-DSDL2_LIBRARY_RELEASE=${SDL2}/${OS}/${RELEASE_DIR}/libSDL2.a)
	
DKSET(SDL2_LINUX
	-DSDL2_DIR=${SDL2}
	-DSDL2_INCLUDE_DIR=${SDL2}/include
	-DSDL2_LIBRARY_TEMP=${SDL2}/${OS}/${RELEASE_DIR}/libSDL2.a
	-DSDL2_LIBRARY=${SDL2}/${OS}/${RELEASE_DIR}/libSDL2.a
	-DSDL2_LIBRARY_DEBUG=${SDL2}/${OS}/${DEBUG_DIR}/libSDL2.a
	-DSDL2_LIBRARY_RELEASE=${SDL2}/${OS}/${RELEASE_DIR}/libSDL2.a)
	
DKSET(SDL2_RASPBERRY
	-DSDL2_DIR=${SDL2}
	-DSDL2_INCLUDE_DIR=${SDL2}/include
	-DSDL2_LIBRARY_TEMP=${SDL2}/${OS}/${RELEASE_DIR}/libSDL2.a
	-DSDL2_LIBRARY=${SDL2}/${OS}/${RELEASE_DIR}/libSDL2.a
	-DSDL2_LIBRARY_DEBUG=${SDL2}/${OS}/${DEBUG_DIR}/libSDL2.a
	-DSDL2_LIBRARY_RELEASE=${SDL2}/${OS}/${RELEASE_DIR}/libSDL2.a)
	
DKSET(SDL2_ANDROID
	-DSDL2_DIR=${SDL2}/cmake
	-DSDL2_INCLUDE_DIR=${SDL2}/include
	#-DSDL2_LIBRARY_TEMP=${SDL2}/${OS}/${RELEASE_DIR}/libSDL2.a
	-DSDL2_LIBRARY=${SDL2}/${OS}/${RELEASE_DIR}/libSDL2.a
	-DSDL2_LIBRARY_DEBUG=${SDL2}/${OS}/${DEBUG_DIR}libSDL2.a
	-DSDL2_LIBRARY_RELEASE=${SDL2}/${OS}/${RELEASE_DIR}/libSDL2.a
	-DSDL2MAIN_LIBRARY_TEMP=${SDL2}/${OS}/${RELEASE_DIR}/libSDL2main.a
	-DSDL2MAIN_LIBRARY=${SDL2}/${OS}/${RELEASE_DIR}/libSDL2main.a
	-DSDL2MAIN_LIBRARY_DEBUG=${SDL2}/${OS}/${DEBUG_DIR}/libSDL2maind.a
	-DSDL2MAIN_LIBRARY_RELEASE=${SDL2}/${OS}/${RELEASE_DIR}/libSDL2main.a)
	

### COMPILE ###
WIN_PATH(${SDL2}/${OS})
WIN32_COMMAND(${DKCMAKE_WIN32} -DSDL_SHARED=OFF -DVIDEO_OPENGLES=OFF -DDIRECTX=OFF -DLIBC=ON ${SDL2})
WIN64_COMMAND(${DKCMAKE_WIN64} -DSDL_SHARED=OFF -DVIDEO_OPENGLES=OFF -DDIRECTX=OFF -DLIBC=ON ${SDL2})
WIN_VS(${SDL2_NAME} SDL2.sln SDL2-static)
WIN_VS(${SDL2_NAME} SDL2.sln SDL2main)


MAC_PATH(${SDL2}/${OS})
MAC32_COMMAND(${DKCMAKE_MAC32} -DSDL_SHARED=OFF -DVIDEO_OPENGLES=OFF -DDIRECTX=OFF -DHAVE_BUILTIN_ICONV=0 -DHAVE_LIBICONV=0 ${ICONV_APPLE} ${SDL2})
MAC64_COMMAND(${DKCMAKE_MAC64} -DSDL_SHARED=OFF -DVIDEO_OPENGLES=OFF -DDIRECTX=OFF -DHAVE_BUILTIN_ICONV=0 -DHAVE_LIBICONV=0 ${ICONV_APPLE} ${SDL2})
MAC_XCODE(${SDL2_NAME} SDL2-static)


IOS_PATH(${SDL2}/${OS})
IOS64_COMMAND(${DKCMAKE_IOS64} -DDIRECTX=OFF -DSDL_JOYSTICK=OFF -DSDL_HAPTIC=OFF ${SDL2})
IOS_XCODE(${SDL2_NAME} SDL2-static)


IOSSIM_PATH(${SDL2}/${OS})
IOSSIM64_COMMAND(${DKCMAKE_IOSSIM64} -DDIRECTX=OFF -DSDL_JOYSTICK=OFF -DSDL_HAPTIC=OFF ${SDL2})
IOSSIM_XCODE(${SDL2_NAME} SDL2-static)
IOSSIM_COMMAND(xcodebuild -configuration Debug build -arch i386 -sdk iphonesimulator6.1 WORKING_DIRECTORY ${SDL2}/Xcode-iOS/SDL)
IOSSIM_COMMAND(xcodebuild -configuration Release build -arch i386 -sdk iphonesimulator6.1 WORKING_DIRECTORY ${SDL2}/Xcode-iOS/SDL)


LINUX_DEBUG_PATH(${SDL2}/${OS}/${DEBUG_DIR})
LINUX_DEBUG_COMMAND(${DKCMAKE_LINUX_DEBUG} -DSDL_SHARED=OFF -DVIDEO_OPENGLES=OFF -DDIRECTX=OFF -DVIDEO_WAYLAND=OFF ${SDL2})
LINUX_DEBUG_COMMAND(make SDL2-static)

LINUX_RELEASE_PATH(${SDL2}/${OS}/${RELEASE_DIR})
LINUX_RELEASE_COMMAND(${DKCMAKE_LINUX_RELEASE} -DSDL_SHARED=OFF -DVIDEO_OPENGLES=OFF -DDIRECTX=OFF -DVIDEO_WAYLAND=OFF ${SDL2})
LINUX_RELEASE_COMMAND(make SDL2-static)


RASPBERRY_DEBUG_PATH(${SDL2}/${OS}/${DEBUG_DIR})
RASPBERRY_DEBUG_COMMAND(${DKCMAKE_RASPBERRY_DEBUG} -DSDL_SHARED=OFF -DVIDEO_OPENGLES=ON ${SDL2})
RASPBERRY_DEBUG_COMMAND(make SDL2-static)

RASPBERRY_RELEASE_PATH(${SDL2}/${OS}/${RELEASE_DIR})
RASPBERRY_RELEASE_COMMAND(${DKCMAKE_RASPBERRY_RELEASE} -DSDL_SHARED=OFF -DVIDEO_OPENGLES=ON ${SDL2})
RASPBERRY_RELEASE_COMMAND(make SDL2-static)


##ANDROID_NDK(${SDL2_NAME})
ANDROID_PATH(${SDL2}/${OS})
ANDROID32_COMMAND(${DKCMAKE_ANDROID32} -DCMAKE_TOOLCHAIN_FILE=${ANDROIDNDK}/build/cmake/android.toolchain.cmake -DANDROID_NDK=${ANDROIDNDK} -DANDROID_PLATFORM=26 -DLIBTYPE=STATIC -DSDL_SHARED=OFF -DVIDEO_OPENGLES=ON -DHIDAPI=OFF ${SDL2})
ANDROID64_COMMAND(${DKCMAKE_ANDROID64} -DCMAKE_TOOLCHAIN_FILE=${ANDROIDNDK}/build/cmake/android.toolchain.cmake -DANDROID_NDK=${ANDROIDNDK} -DANDROID_PLATFORM=26 -DLIBTYPE=STATIC -DSDL_SHARED=OFF -DVIDEO_OPENGLES=ON -DHIDAPI=OFF ${SDL2})
ANDROID_VS(${SDL2_NAME} SDL2.sln SDL2-static)
ANDROID_VS(${SDL2_NAME} SDL2.sln SDL2main)


