# The Lord of the Rings: The Third Age (original Xbox, NTSC-U), statically recompiled

cmake_minimum_required(VERSION 3.25)
project(game LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# the SDK: the one shared sdk/ at the repository root (ports/<port>/<project>/ -> ../../../sdk) comes first, so a
# stale per-port sdk/ left over from before it was shared can never win; then sdk/ here, two folders up inside the
# SDK's templates, a checkout beside this repository, else downloaded
get_filename_component(XBE_SHARED_SDK "${CMAKE_CURRENT_LIST_DIR}/../../../sdk" ABSOLUTE)
if(EXISTS ${XBE_SHARED_SDK}/cmake/XbeRecomp.cmake)
    set(SDK ${XBE_SHARED_SDK})
elseif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/sdk/cmake/XbeRecomp.cmake)
    set(SDK ${CMAKE_CURRENT_SOURCE_DIR}/sdk)
elseif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/XbeRecomp.cmake)
    set(SDK ${CMAKE_CURRENT_SOURCE_DIR}/../..)
elseif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../XboxXBE-Recomp/cmake/XbeRecomp.cmake)
    set(SDK ${CMAKE_CURRENT_SOURCE_DIR}/../XboxXBE-Recomp)
else()
    include(FetchContent)
    FetchContent_Declare(xbe_sdk GIT_REPOSITORY https://github.com/TGP482/XboxXBE-Recomp.git GIT_TAG main)
    FetchContent_MakeAvailable(xbe_sdk)
    set(SDK ${xbe_sdk_SOURCE_DIR})
endif()
include(${SDK}/cmake/XbeRecomp.cmake)

file(GLOB XBE_HOST_SOURCES CONFIGURE_DEPENDS src/*/*.cpp)
list(FILTER XBE_HOST_SOURCES EXCLUDE REGEX "/src/game/")

# partitions as xbe_symbols wrote them into config/game.toml
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS config/game.toml)
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/config/game.toml)
    file(STRINGS config/game.toml GAME_PARTITIONS REGEX "^partitions *=")
    string(REGEX MATCH "[0-9]+" GAME_PARTITIONS "${GAME_PARTITIONS}")
endif()
# no config yet: build.bat writes it with xbe_symbols, the change reconfigures
if(NOT GAME_PARTITIONS)
    set(GAME_PARTITIONS 8)
endif()

# target name = GAME_EXE in build.bat, xbe = GAME_XBE
# Draw on the title's thread, as The Two Towers' port and the headless harness do; the threaded renderer can be
# tried once the game runs.
# the version and name Windows shows in the exe's properties (keep in step with src/common/game_info.cpp)
set(XBE_VERSION 0.1.0)
set(XBE_PRODUCT "Third Age Xbox PC port")
add_xbe_image(TAXbox default.xbe game.toml ${GAME_PARTITIONS} game)
target_compile_definitions(TAXbox PRIVATE XBE_RENDER_THREAD=0)
