cmake_minimum_required(VERSION 3.16)
project(IMX6U-Game)
include(CTest)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif()

option(TARGET_IMX "Build for IMX6U target board (framebuffer + Alsa + Evdev). OFF builds for PC (SDL2)." OFF)
if(TARGET_IMX)
    set(TARGET_PC OFF)
else()
    set(TARGET_PC ON)
endif()

set(CORE_SOURCES
    src/Core/Asset/ObjLoader.cpp
    src/Core/Asset/SpriteAssetLoader.cpp
    src/Core/Core/DepthBuffer.cpp
    src/Core/Core/FrameBuffer.cpp
    src/Core/Core/Renderer.cpp
    src/Core/Draw2D/DrawContext.cpp
    src/Core/Platform/AlsaAudioInput.cpp
    src/Core/Platform/AlsaAudioOutput.cpp
    src/Core/Platform/EvdevButtonInput.cpp
    src/Core/Platform/EvdevKeyboardState.cpp
    src/Core/Platform/EvdevTouchInput.cpp
    src/Core/Rasterizer/Rasterizer.cpp
    src/Core/Rasterizer/TriangleRasterizer.cpp
    src/Core/Scene/Camera.cpp
    src/Core/Shading/BlinnPhongShader.cpp
)

if(TARGET_IMX)
    list(APPEND CORE_SOURCES
        src/Core/Platform/FBDisplay.cpp
        src/Core/Platform/Ap3216cPhotoSensor.cpp
    )
else()
    list(APPEND CORE_SOURCES
        src/Core/Platform/SDLDisplay.cpp
        src/Core/Platform/SdlAudioInput.cpp
        src/Core/Platform/SdlAudioOutput.cpp
        src/Core/Platform/SdlKeyboardButtonInput.cpp
        src/Core/Platform/SdlKeyboardState.cpp
        src/Core/Platform/SdlPointerInput.cpp
        src/Core/Platform/SdlPhotoSensor.cpp
    )
endif()

set(CORE_INCLUDE_DIRS
    src/Core/Platform
    src/Core/Asset
    src/Core/Core
    src/Core/Draw2D
    src/Core/Math
    src/Core/Rasterizer
    src/Core/RenderData
    src/Core/Scene
    src/Core/Shading
    assets/font
    assets/sprite
)

if(NOT TARGET_IMX)
    list(APPEND CORE_SOURCES
        third_party/imgui/imgui.cpp
        third_party/imgui/imgui_demo.cpp
        third_party/imgui/imgui_draw.cpp
        third_party/imgui/imgui_tables.cpp
        third_party/imgui/imgui_widgets.cpp
        third_party/imgui/backends/imgui_impl_sdl2.cpp
        third_party/imgui/backends/imgui_impl_sdlrenderer2.cpp
    )
    list(APPEND CORE_INCLUDE_DIRS third_party/imgui)
endif()

add_library(imx6u_core STATIC ${CORE_SOURCES})
target_include_directories(imx6u_core PUBLIC ${CORE_INCLUDE_DIRS})

if(TARGET_IMX)
    target_compile_definitions(imx6u_core PUBLIC TARGET_IMX)
else()
    target_compile_definitions(imx6u_core PUBLIC TARGET_PC)
endif()

target_compile_definitions(imx6u_core PUBLIC $<$<CONFIG:Debug>:IMX6U_DEBUG>)

if(TARGET_IMX)
else()
    if(WIN32)
        if(CMAKE_SIZEOF_VOID_P EQUAL 8)
            set(SDL2_LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/libs/Win/SDL2/lib/x64")
            set(SDL2_DLL "${CMAKE_CURRENT_SOURCE_DIR}/libs/Win/SDL2/lib/x64/SDL2.dll")
            set(SDL2_IMAGE_LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/libs/Win/SDL_image/lib/x64")
            set(SDL2_IMAGE_DLL "${CMAKE_CURRENT_SOURCE_DIR}/libs/Win/SDL_image/lib/x64/SDL2_image.dll")
        else()
            set(SDL2_LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/libs/Win/SDL2/lib/x86")
            set(SDL2_DLL "${CMAKE_CURRENT_SOURCE_DIR}/libs/Win/SDL2/lib/x86/SDL2.dll")
            set(SDL2_IMAGE_LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/libs/Win/SDL_image/lib/x86")
            set(SDL2_IMAGE_DLL "${CMAKE_CURRENT_SOURCE_DIR}/libs/Win/SDL_image/lib/x86/SDL2_image.dll")
        endif()

        target_include_directories(imx6u_core PUBLIC libs/Win/SDL2/include)
        target_link_directories(imx6u_core PUBLIC ${SDL2_LIB_DIR})
        target_link_libraries(imx6u_core PUBLIC SDL2main SDL2)
    else()
        find_package(SDL2 REQUIRED)
        find_package(SDL2_image QUIET)
        target_link_libraries(imx6u_core PUBLIC SDL2::SDL2)
    endif()
endif()

if(UNIX AND NOT APPLE)
    find_package(ALSA QUIET)
    if(ALSA_FOUND)
        target_compile_definitions(imx6u_core PUBLIC PLATFORM_HAS_ALSA)
        target_include_directories(imx6u_core PUBLIC ${ALSA_INCLUDE_DIRS})
        target_link_libraries(imx6u_core PUBLIC ${ALSA_LIBRARIES})
    else()
        message(STATUS "ALSA was not found; AlsaAudioInput and AlsaAudioOutput will be unavailable backends")
    endif()
endif()

if(MSVC)
    target_compile_options(imx6u_core PRIVATE /utf-8 /W3)
endif()

function(imx6u_configure_app_target target_name)
    target_link_libraries(${target_name} PRIVATE imx6u_core)

    if(CMAKE_CONFIGURATION_TYPES)
        foreach(config ${CMAKE_CONFIGURATION_TYPES})
            string(TOUPPER "${config}" config_upper)
            set_target_properties(${target_name} PROPERTIES
                RUNTIME_OUTPUT_DIRECTORY_${config_upper} "${CMAKE_BINARY_DIR}/${config}"
            )
        endforeach()
    else()
        set_target_properties(${target_name} PROPERTIES
            RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}"
        )
    endif()

    if(WIN32 AND NOT TARGET_IMX)
        add_custom_command(TARGET ${target_name} POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_if_different
            "${SDL2_DLL}"
            "$<TARGET_FILE_DIR:${target_name}>"
        )
    endif()

    if(MSVC)
        target_compile_options(${target_name} PRIVATE /utf-8 /W3)
        set_property(TARGET ${target_name} PROPERTY INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE)
    endif()
endfunction()

add_subdirectory(src/Apps/Game)
add_subdirectory(src/Apps/Demo)
add_subdirectory(src/Apps/LightGame)

if(BUILD_TESTING AND NOT TARGET_IMX)
    add_executable(render_pipeline_tests
        tests/render_pipeline_tests.cpp
    )
    target_link_libraries(render_pipeline_tests PRIVATE imx6u_core)
    target_include_directories(render_pipeline_tests PRIVATE ${CORE_INCLUDE_DIRS})

    add_executable(game_engine_tests
        tests/game_engine_tests.cpp
        src/Apps/LightGame/src/engine/Level.cpp
        src/Apps/LightGame/src/engine/Camera2D.cpp
        src/Apps/LightGame/src/engine/Physics2D.cpp
        src/Apps/LightGame/src/engine/LevelLoader.cpp
        src/Apps/LightGame/src/systems/LightEffectSystem.cpp
    )
    target_include_directories(game_engine_tests PRIVATE
        src/Apps/LightGame/src/engine
        src/Apps/LightGame/src/systems
        src/Apps/LightGame/src/levels
        src/Apps/LightGame/generated
        ${CORE_INCLUDE_DIRS}
    )

    if(CMAKE_CONFIGURATION_TYPES)
        foreach(config ${CMAKE_CONFIGURATION_TYPES})
            string(TOUPPER "${config}" config_upper)
            set_target_properties(render_pipeline_tests PROPERTIES
                RUNTIME_OUTPUT_DIRECTORY_${config_upper} "${CMAKE_BINARY_DIR}/${config}"
            )
            set_target_properties(game_engine_tests PROPERTIES
                RUNTIME_OUTPUT_DIRECTORY_${config_upper} "${CMAKE_BINARY_DIR}/${config}"
            )
        endforeach()
    else()
        set_target_properties(render_pipeline_tests PROPERTIES
            RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}"
        )
        set_target_properties(game_engine_tests PROPERTIES
            RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}"
        )
    endif()

    if(WIN32)
        add_custom_command(TARGET render_pipeline_tests POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_if_different
            "${SDL2_DLL}"
            "$<TARGET_FILE_DIR:render_pipeline_tests>"
        )
    endif()

    if(MSVC)
        target_compile_options(render_pipeline_tests PRIVATE /utf-8 /W3)
        target_compile_options(game_engine_tests PRIVATE /utf-8 /W3)
    endif()

    add_test(NAME render_pipeline_tests COMMAND render_pipeline_tests)
    add_test(NAME game_engine_tests COMMAND game_engine_tests)
endif()
