cmake_minimum_required(VERSION 3.20)
project(doa3_game C)

# Dead or Alive 3 - Recompiled game executable (boot bring-up host)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)

# ── NV2A GPU emulation (from xemu) — FULL stack, mirrors burnout3 ──
set(NV2A_DIR "${CMAKE_SOURCE_DIR}/src/nv2a")
set(NV2A_SOURCES
    ${NV2A_DIR}/nv2a_core.c
    ${NV2A_DIR}/nv2a_mmio_hook.c
    ${NV2A_DIR}/nv2a_pb_test.c
    ${NV2A_DIR}/nv2a_pgraph_d3d11.c
    ${NV2A_DIR}/nv2a_vertex_program.c
    # nv2a_pb_replay.c -- Burnout 3 menu-replay harness, unreachable from DOA3
)

# Game host sources, shared by every release
set(GAME_SOURCES
    main.c
    doa3.rc                  # window/taskbar/exe icon
    movie_present.c
    video_settings.c
    log_settings.c           # log files on/off (Esc menu, doa3_settings.ini)
    xiso_extract.c           # first-run install of the game files from a disc image
    burnout_compat_stubs.c   # inert stubs for burnout-only symbols the nv2a srcs need
    ${NV2A_SOURCES}
)

if(MSVC)
    # Generated/recompiled code is mechanical: silence its warnings, and allow
    # the huge translation units (/bigobj).
    set_source_files_properties(recomp/recomp_manual.c recomp/recomp_manual_jpn.c
        PROPERTIES COMPILE_FLAGS "/w /bigobj")
    # NV2A emulation (from xemu) — silence its warnings too.
    set_source_files_properties(${NV2A_SOURCES} PROPERTIES COMPILE_FLAGS "/w")
endif()

# One executable per disc release. gen_dir holds that release's recompiled
# functions, generated by (DOA3_REGION=usa|jpn):
function(doa3_game_exe target output_name gen_dir manual)
    file(GLOB gen_sources "${CMAKE_CURRENT_SOURCE_DIR}/recomp/${gen_dir}/recomp_*.c")
    add_executable(${target} ${GAME_SOURCES} recomp/${manual} ${gen_sources})

    # Link compatibility layers (mirrors burnout3)
    target_link_libraries(${target} PRIVATE
        xbox_kernel
        d3d8_compat
        dsound_compat
        input_compat
        apu_emu
        doa3_ui
        doa3_online
    )

    # System libraries (mirrors burnout3)
    target_link_libraries(${target} PRIVATE
        kernel32
        user32
        gdi32
        d3d11
        dxgi
    )

    # Include paths
    target_include_directories(${target} PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}
        ${CMAKE_CURRENT_SOURCE_DIR}/recomp
        ${CMAKE_CURRENT_SOURCE_DIR}/recomp/${gen_dir}
        ${CMAKE_SOURCE_DIR}/src
        ${CMAKE_SOURCE_DIR}/third_party
        ${NV2A_DIR}
    )

    if(MSVC)
        target_compile_options(${target} PRIVATE
            /W3
            /WX-
            $<$<CONFIG:Debug>:/O2>
            $<$<CONFIG:Debug>:/Ob2>
            $<$<NOT:$<CONFIG:Debug>>:/O2>
        )
        set_property(TARGET ${target} PROPERTY MSVC_RUNTIME_CHECKS "")
        set_source_files_properties(${gen_sources} PROPERTIES COMPILE_FLAGS "/w /bigobj")
        target_compile_definitions(${target} PRIVATE _CRT_SECURE_NO_WARNINGS)
        # Disable ASLR so Xbox memory can be mapped at fixed addresses.
        # Large stack: each Xbox call/ret becomes a native C call/return.
        target_link_options(${target} PRIVATE
            /DYNAMICBASE:NO
            /FIXED:NO
            $<$<CONFIG:Debug>:/MAP>
            $<$<CONFIG:Debug>:/DEBUG>
            $<$<CONFIG:Release>:/SUBSYSTEM:WINDOWS>
            $<$<CONFIG:Release>:/ENTRY:mainCRTStartup>
            /STACK:33554432
            /LARGEADDRESSAWARE
        )
    endif()

    # Keep configuration outputs separate. Every release's exe lands in the
    # same folder, so each can hand a disc of the other release to its exe.
    set_target_properties(${target} PROPERTIES
        OUTPUT_NAME "${output_name}"
        RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
        RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}/debug"
        RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}/release"
        PDB_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}/debug"
    )
endfunction()

# One release per build folder, so the two never share an output folder: cmake
# -S . -B build -> build/release/DOA3.exe cmake -S .
set(DOA3_RELEASE "USA" CACHE STRING "Disc release this build folder builds (USA or JPN)")
set_property(CACHE DOA3_RELEASE PROPERTY STRINGS USA JPN)
if(DOA3_RELEASE STREQUAL "USA")
    doa3_game_exe(doa3 DOA3 gen recomp_manual.c)
elseif(DOA3_RELEASE STREQUAL "JPN")
    # Japan (En,Ja,Fr,De,Es,It): its own recompiled tree (XDK 4134).
    doa3_game_exe(doa3_jpn DOA3 gen_jpn recomp_manual_jpn.c)
    target_compile_definitions(doa3_jpn PRIVATE DOA3_REGION_JPN=1)
else()
    message(FATAL_ERROR "DOA3_RELEASE must be USA or JPN (got '${DOA3_RELEASE}')")
endif()
