cmake_minimum_required(VERSION 3.10)
project(ExampleApplications)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED True)

if(WIN32)
  if(CMAKE_GENERATOR_PLATFORM MATCHES "ARM64")
    set(ARCH_DIR arm64)
    set(ARCH_LIB_DIR arm64)
  elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
    set(ARCH_DIR x64)
    set(ARCH_LIB_DIR x64_avx2)
  elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
    set(ARCH_DIR x32)
    set(ARCH_LIB_DIR x32_avx2)
  else()
    message(FATAL_ERROR "Unknown architecture")
  endif()

  set(WINDOWS_LIB_DIR ${CMAKE_SOURCE_DIR}/../Bin/${CMAKE_SYSTEM_NAME}/${ARCH_LIB_DIR})

  if(MSVC)
    set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT /GS-")
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
    set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT /GS-")
    set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd")

    add_compile_definitions(_WIN32_WINNT=0x0600)
    add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
    add_compile_definitions(_USE_MATH_DEFINES)
    add_compile_definitions(NOMINMAX)

    add_compile_options(/W4)
  endif()

  link_libraries("${WINDOWS_LIB_DIR}/Afterwarp.lib")

else()
  if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
    set(ARCH_DIR aarch64)
  elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
    set(ARCH_DIR x64)
  elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
    set(ARCH_DIR x32)
  endif()

  add_compile_options(-O3 -Wall -Wextra -pedantic)

  link_libraries(-no-pie m afterwarp)

endif()

function(add_my_executable target)
  if(WIN32)
    add_executable(${target} WIN32 Source/${target}.c Source/Resource.rc Manifest/${target}.exe.manifest)
  else()
    add_executable(${target} WIN32 Source/${target}.c Source/Resource.rc)
  endif()
endfunction()

include_directories(../Include)

set(ICON_RESOURCE "${CMAKE_SOURCE_DIR}/../Media/afterwarp.ico")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

add_my_executable(Architecture)
add_my_executable(Chart3D)
add_my_executable(ColorTunnel)
add_my_executable(Cube)
add_my_executable(CubeTextured)
add_my_executable(CurvedText3D)
add_my_executable(DrawableTextures)
add_my_executable(FontsDemo)
add_my_executable(ImageBasics)
add_my_executable(NightBridge)
add_my_executable(ObjectPicking)
add_my_executable(ParallaxMapping)
add_my_executable(Plasma)
add_my_executable(Triangle)
add_my_executable(WarehouseObjects)

if(FALSE)
add_executable(Architecture WIN32 Source/Architecture.c Source/Resource.rc)
add_executable(Chart3D WIN32 Source/Chart3D.c Source/Resource.rc)
add_executable(ColorTunnel WIN32 Source/ColorTunnel.c Source/Resource.rc)
add_executable(Cube WIN32 Source/Cube.c Source/Resource.rc)
add_executable(CubeTextured WIN32 Source/CubeTextured.c Source/Resource.rc)
add_executable(CurvedText3D WIN32 Source/CurvedText3D.c Source/Resource.rc)
add_executable(DrawableTextures WIN32 Source/DrawableTextures.c Source/Resource.rc)
add_executable(FontsDemo WIN32 Source/FontsDemo.c Source/Resource.rc)
add_executable(ImageBasics WIN32 Source/ImageBasics.c Source/Resource.rc)
add_executable(NightBridge WIN32 Source/NightBridge.c Source/Resource.rc)
add_executable(ObjectPicking WIN32 Source/ObjectPicking.c Source/Resource.rc)
add_executable(ParallaxMapping WIN32 Source/ParallaxMapping.c Source/Resource.rc)
add_executable(Plasma WIN32 Source/Plasma.c Source/Resource.rc)
add_executable(Triangle WIN32 Source/Triangle.c Source/Resource.rc)
add_executable(WarehouseObjects WIN32 Source/WarehouseObjects.c Source/Resource.rc)
endif()

set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/..)
set(INSTALL_DIR Bin/${CMAKE_SYSTEM_NAME}/${ARCH_DIR})

install(
  TARGETS Architecture Chart3D ColorTunnel Cube CubeTextured CurvedText3D DrawableTextures FontsDemo
    ImageBasics NightBridge ObjectPicking ParallaxMapping Plasma Triangle WarehouseObjects
  RUNTIME DESTINATION ${INSTALL_DIR})