
# Platform library — all platform-specific code lives here.
# Only the windowed Platform library links SDL directly.
# MoX and MoM include Platform.h but do NOT link any platform library;
# the executables link the appropriate platform implementation themselves.
#
# Two distinct libraries are always produced:
#   Platform_Windowless — no display, no audio, no external deps.  DeMoM and
#                       the test executables link this.
#   Platform          — the windowed backend (SDL2 default, or USE_SDL3 /
#                       USE_WIN32).  ReMoM links this.

option(USE_SDL3 "Use SDL3 instead of SDL2 for the windowed backend (default: SDL2)" OFF)
option(USE_WIN32 "Use native Win32 backend instead of SDL for the windowed backend (no SDL dependency)" OFF)
option(MOUSE_DEBUG "Enable verbose mouse input debug logging to stderr" OFF)



# =========================================================================
#  Platform_Windowless — always built, no external dependencies.
# =========================================================================

add_library(Platform_Windowless STATIC
    windowless/windowless_PFL.c
    windowless/windowless_KD.c
    windowless/windowless_Timer.c
    windowless/windowless_EMM.c
    windowless/windowless_Audio.c
    windowless/windowless_State.c
    replay/Replay.c
    metrics/PFL_Perf.c
    viewport/PFL_Viewport.c        # Replay.c writes the scale mode into the replay header (fullscreen PLAN phase 5)
    viewport/PFL_Video_Settings.c
)

target_include_directories(Platform_Windowless PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/include
)
target_include_directories(Platform_Windowless PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/windowless
)

target_compile_definitions(Platform_Windowless PUBLIC NO_SOUND_LIBRARY)

if(MOUSE_DEBUG)
    target_compile_definitions(Platform_Windowless PUBLIC MOUSE_DEBUG)
endif()



# =========================================================================
#  Platform — preset-selected windowed backend.
# =========================================================================

if(USE_WIN32)
    set(PLATFORM_BACKEND_DIR win32)
    set(PLATFORM_SOURCES
        win32/win_PFL.cpp
        win32/win_KD.c
        win32/win_MD.c
        win32/win_CLK.c
        win32/win_EMS.c
        win32/win_Exit.c
        win32/win_Video.c
        win32/win_State.c
        replay/Replay.c
    )
    set(PLATFORM_LINK_LIBS winmm)
    set(PLATFORM_DEFINES _STU_WIN NO_SOUND_LIBRARY)
elseif(USE_SDL3)
    set(PLATFORM_BACKEND_DIR sdl3)
    set(PLATFORM_SOURCES
        sdl3/sdl3_EMM.c
        sdl3/sdl3_Init.c
        sdl3/sdl3_KD.c
        sdl3/sdl3_MD.c
        sdl3/sdl3_PFL.c
        sdl3/sdl3_State.c
        sdl3/sdl3_Timer.c
        sdl3/sdl3_Video.c
        replay/Replay.c
    )
    set(PLATFORM_LINK_LIBS SDL3::SDL3)
    set(PLATFORM_DEFINES USE_SDL3)
    if(NOT DISABLE_AUDIO)
        list(APPEND PLATFORM_SOURCES sdl3/sdl3_Audio.c)
        list(APPEND PLATFORM_LINK_LIBS SDL3_mixer::SDL3_mixer)
    endif()
else()
    set(PLATFORM_BACKEND_DIR sdl2)
    set(PLATFORM_SOURCES
        sdl2/sdl2_EMM.c
        sdl2/sdl2_Init.c
        sdl2/sdl2_KD.c
        sdl2/sdl2_MD.c
        sdl2/sdl2_PFL.c
        sdl2/sdl2_State.c
        sdl2/sdl2_Timer.c
        sdl2/sdl2_Video.c
        replay/Replay.c
    )
    set(PLATFORM_LINK_LIBS SDL2::SDL2)
    set(PLATFORM_DEFINES "")
    if(NOT DISABLE_AUDIO)
        list(APPEND PLATFORM_SOURCES sdl2/sdl2_Audio.c)
        list(APPEND PLATFORM_LINK_LIBS SDL2_mixer::SDL2_mixer)
        # sdl2_Audio.c uses dlsym() to filter transitively-loaded FluidSynth's
        # log spam; CMAKE_DL_LIBS is "dl" on Linux, empty on Windows.
        list(APPEND PLATFORM_LINK_LIBS ${CMAKE_DL_LIBS})
    endif()
endif()

# In-engine A/V capture for rendering the scripted demo video.  Backend-agnostic: each backend's
# Platform_Video_Update() feeds it frames; the SDL backends also feed it post-mix audio.
list(APPEND PLATFORM_SOURCES capture/PFL_Capture.c)

# Platform-Input Layer 1: input responsiveness/smoothness metrics (runtime-gated, off by default).
# Backend-agnostic; the windowed backend feeds it ticks, cursor polls, and present timing.
list(APPEND PLATFORM_SOURCES metrics/PFL_Input_Metrics.c)

# Performance layer: the PERF_ZONE/PERF_CALL zone recorder and percentile reporter.  Also built
# into Platform_Windowless above, because the windowless DeMoM runner is where logic timing (Mode A)
# is measured.  The zone macros are compiled out unless the PERF_INSTRUMENT build option is ON;
# this file is always compiled so the runtime gate and reporter link either way.
list(APPEND PLATFORM_SOURCES metrics/PFL_Perf.c)

# Viewport: where the game image goes in the window, and the window <-> game mouse mapping (fullscreen PLAN phase 1).
# Pure C, backend-agnostic; unit-tested in platform/tests.
list(APPEND PLATFORM_SOURCES viewport/PFL_Viewport.c)
# [Video] settings from ReMoM.ini (fullscreen PLAN phase 3), shared by every windowed backend.
list(APPEND PLATFORM_SOURCES viewport/PFL_Video_Settings.c)

add_library(Platform STATIC ${PLATFORM_SOURCES})

if(PLATFORM_DEFINES)
    target_compile_definitions(Platform PUBLIC ${PLATFORM_DEFINES})
endif()

if(MOUSE_DEBUG)
    target_compile_definitions(Platform PUBLIC MOUSE_DEBUG)
endif()

target_include_directories(Platform PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/include
)
target_include_directories(Platform PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/${PLATFORM_BACKEND_DIR}
)

target_link_libraries(Platform PRIVATE ${PLATFORM_LINK_LIBS})

add_subdirectory(tests)
