cmake_minimum_required(VERSION 3.20)
project(DBFinalBoutRecomp LANGUAGES C CXX)

set(PSXRECOMP_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/psxrecomp" CACHE PATH
    "Path to psxrecomp")

# --- Opt-in product surfaces (OFF by default in the framework) -------------
# Scaffold prompts / flags fill these. Without recomp-ui, PSX_RECOMP_UI is OFF.
# Setup-host CI requires WIZARD_CMAKE_BLOCK / ENABLE_SETUP_WIZARD (FORCE_SETUP_HOST
# alone does not open first-run / Generate & rebuild).
# recomp-ui submodule present (PSX_RECOMP_UI defaults ON).
# Build the real netplay/lobby path for this two-player title. Runtime netplay
# remains opt-in at launch; keeping the feature compiled makes it available to
# the launcher without changing normal offline sessions.
if(EXISTS "${PSXRECOMP_ROOT}/lib/recomp-net/CMakeLists.txt")
    set(PSX_NETPLAY ON CACHE BOOL "Link recomp-net and lobby support" FORCE)
endif()
# Vulkan is an optional runtime choice. Software remains the game default and
# is still the reference renderer for Final Bout.
set(PSX_ENABLE_VULKAN ON CACHE BOOL
    "Build the Vulkan renderer when the SDK is available" FORCE)
set(PSX_SETUP_WIZARD ON CACHE BOOL
    "Advertise first-run setup wizard + Generate & rebuild in recomp-ui" FORCE)
# libchdr from a project-local copy when the network fetch is unavailable
# (GitHub rate-limits; keeps Generate & rebuild deterministic offline).
if(NOT FETCHCONTENT_SOURCE_DIR_PSX_LIBCHDR AND
   EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/deps/libchdr/CMakeLists.txt")
    set(FETCHCONTENT_SOURCE_DIR_PSX_LIBCHDR
        "${CMAKE_CURRENT_SOURCE_DIR}/deps/libchdr" CACHE PATH
        "Use project-local libchdr (offline)" FORCE)
endif()
include("${PSXRECOMP_ROOT}/runtime/runtime.cmake")

# PGXP precision-shadowing variant (full-coverage <exe>_pgxp build; the base
# exe's PGXP hook macros preprocess away, so only this variant feeds the whole
# recompiled game into the value-propagation engine — enabling the
# psx.enhancement.pgxp mod on the base exe only corrects a fraction of
# vertices and makes polygons flicker).
set(PSX_PGXP_VARIANT ON CACHE BOOL
    "Also build the <exe>_pgxp PGXP precision-shadowing variant" FORCE)

psxrecomp_add_game_runtime(psx-runtime
    ENABLE_NETPLAY_IF_PRESENT
    NETPLAY_LOBBY_URL "ws://netplay.retcomm.net:8765"
    ENABLE_SETUP_WIZARD
    WINDOW_TITLE "DBFinalBout Recompiled"
    DEFAULT_GAME_CONFIG_PATH "game.toml"
    MAX_PLAYERS 2
    APP_ICON "${CMAKE_CURRENT_SOURCE_DIR}/assets/psxrecomp.ico"
    GEN_MARKER "generated/SLUS_004.93_dispatch.c"
    GEN_FULL_GLOB "generated/SLUS_004.93_full_*.c"
    CODEGEN_SETUP_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/codegen_setup.c"
)

# Stage runtime configs next to the exe for direct build/ launches. Only copy
# files that exist — never fail POST_BUILD when game_options.toml is absent.
foreach(_PSX_RUNTIME_CONFIG game.toml game_options.toml)
    if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${_PSX_RUNTIME_CONFIG}")
        add_custom_command(TARGET psx-runtime POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_if_different
                "${CMAKE_CURRENT_SOURCE_DIR}/${_PSX_RUNTIME_CONFIG}"
                "$<TARGET_FILE_DIR:psx-runtime>/${_PSX_RUNTIME_CONFIG}"
            COMMENT "Staging ${_PSX_RUNTIME_CONFIG}"
            VERBATIM)
    endif()
endforeach()

