# runtime_test — full runtime exerciser (issue 0072b).
# Standalone, dual-mode CMakeLists (top-level + subdir).

cmake_minimum_required(VERSION 3.16)

if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
    project(runtime_test CXX)
    set(_TOP_LEVEL TRUE)
else()
    set(_TOP_LEVEL FALSE)
endif()

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# App lives at fn_registry/apps/<X>/ (issue 0096); cpp/ is sibling.
get_filename_component(_REPO_CPP_ROOT ${CMAKE_CURRENT_LIST_DIR}/../../cpp ABSOLUTE)
set(_VENDOR ${_REPO_CPP_ROOT}/vendor)
set(_FUNCS  ${_REPO_CPP_ROOT}/functions)

if(_TOP_LEVEL)
    if(NOT EXISTS ${_VENDOR}/sdl3/CMakeLists.txt)
        message(FATAL_ERROR "SDL3 vendoring missing")
    endif()
    set(SDL_SHARED OFF CACHE BOOL "" FORCE)
    set(SDL_STATIC ON CACHE BOOL "" FORCE)
    set(SDL_TEST_LIBRARY OFF CACHE BOOL "" FORCE)
    set(SDL_TESTS OFF CACHE BOOL "" FORCE)
    set(SDL_EXAMPLES OFF CACHE BOOL "" FORCE)
    set(SDL_INSTALL OFF CACHE BOOL "" FORCE)
    set(SDL_X11_XSCRNSAVER OFF CACHE BOOL "" FORCE)
    add_subdirectory(${_VENDOR}/sdl3 ${CMAKE_BINARY_DIR}/_sdl3 EXCLUDE_FROM_ALL)
endif()

add_executable(runtime_test
    main.cpp
    sokol_impl.cpp
    ${_FUNCS}/gfx/sokol_setup.cpp
    ${_FUNCS}/gfx/sprite_batch.cpp
    ${_FUNCS}/gamedev/audio_engine.cpp
    ${_FUNCS}/gamedev/audio_play.cpp
    ${_FUNCS}/gamedev/input_unified.cpp
    ${_FUNCS}/gamedev/game_loop.cpp
    ${_FUNCS}/gamedev/camera_2d.cpp
)

target_include_directories(runtime_test PRIVATE
    ${_VENDOR}/sokol
    ${_FUNCS}/core      # math2d.h
    ${_FUNCS}/gfx       # sokol_setup.h, sprite_batch.h
    ${_FUNCS}/gamedev   # audio/input/loop/camera headers
)

target_link_libraries(runtime_test PRIVATE SDL3::SDL3-static)

if(NOT EMSCRIPTEN)
    find_package(OpenGL REQUIRED)
    target_link_libraries(runtime_test PRIVATE OpenGL::GL)
    if(UNIX AND NOT APPLE)
        target_link_libraries(runtime_test PRIVATE m pthread dl)
    endif()
endif()

if(EMSCRIPTEN)
    set_target_properties(runtime_test PROPERTIES SUFFIX ".html")
    target_compile_options(runtime_test PRIVATE
        -Os -fno-exceptions -fno-rtti -ffunction-sections -fdata-sections
    )
    target_link_options(runtime_test PRIVATE
        -Os
        -sUSE_WEBGL2=1
        -sFULL_ES3=1
        -sALLOW_MEMORY_GROWTH=1
        -sINITIAL_MEMORY=33554432
        -sASSERTIONS=0
        -sENVIRONMENT=web
        -Wl,--gc-sections
    )
endif()
