# The UI demo, quoted by the UI chapter's `c excerpt=` blocks.
#
# A real target built by `all`, on the same terms as the tutorial games: a
# chapter whose program does not compile is the failure the documentation
# harness exists to stop.

add_executable(uidemo
  uidemo.c
)

target_link_libraries(uidemo
  PRIVATE akstdlib::akstdlib akerror::akerror akgl
          SDL3::SDL3 SDL3_ttf::SDL3_ttf SDL3_image::SDL3_image SDL3_mixer::SDL3_mixer
          jansson::jansson -lm)
target_include_directories(uidemo PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
target_compile_options(uidemo PRIVATE ${AKGL_WARNING_FLAGS})

# The demo needs no art -- its "world" is akgl_draw_background -- but it does
# need one font, and it uses the same test-asset face the JRPG does, compiled
# in as an absolute path for the same run-from-anywhere reason.
get_filename_component(UIDEMO_REPO_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../.." ABSOLUTE)
target_compile_definitions(uidemo PRIVATE
  UIDEMO_FONT_FILE="${UIDEMO_REPO_ROOT}/tests/assets/akgl_test_mono.ttf"
)

if(AKGL_VENDORED_DEPENDENCIES)
  set_target_properties(uidemo PROPERTIES BUILD_RPATH "${AKGL_VENDORED_RPATH}")
endif()

# The headless smoke run. `--demo` tours every screen through the real event
# chain -- a mouse click on the title menu (the consumed path), keyboard into
# and out of the raw-CLAY options screen, the play screen's dialog opened and
# dismissed, and Quit confirmed from the menu -- rather than just proving that
# main() returns. There is no physics here, so nothing needs a driven clock;
# `--frames` is a backstop above the script's last step, not the exit path.
add_test(NAME example_uidemo COMMAND uidemo --frames 240 --demo)
set_tests_properties(example_uidemo PROPERTIES TIMEOUT 120)
# set_property, not set_tests_properties: the latter parses its PROPERTIES
# arguments as name/value *pairs*, so a semicolon-separated value is split and
# every element after the first is consumed as a bogus property name. See the
# same note in the other examples' CMakeLists.
set_property(TEST example_uidemo PROPERTY ENVIRONMENT
  "SDL_VIDEODRIVER=dummy"
  "SDL_RENDER_DRIVER=software"
  "SDL_AUDIODRIVER=dummy"
)

# LD_LIBRARY_PATH for the vendored satellite libraries. Only needed when they
# were vendored; an installed build resolves them normally.
if(AKGL_VENDORED_DEPENDENCIES)
  if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.22")
    set(UIDEMO_TEST_ENV_MOD "")
    foreach(dir IN LISTS AKGL_TEST_LIBPATH)
      list(APPEND UIDEMO_TEST_ENV_MOD "LD_LIBRARY_PATH=path_list_prepend:${dir}")
    endforeach()
    set_property(TEST example_uidemo PROPERTY ENVIRONMENT_MODIFICATION ${UIDEMO_TEST_ENV_MOD})
  else()
    string(REPLACE ";" ":" UIDEMO_TEST_LIBPATH_JOINED "${AKGL_TEST_LIBPATH}")
    set_property(TEST example_uidemo APPEND PROPERTY ENVIRONMENT
      "LD_LIBRARY_PATH=${UIDEMO_TEST_LIBPATH_JOINED}:$ENV{LD_LIBRARY_PATH}")
  endif()
endif()
