# The SDL3 component of the player-facing editor (sona_editor_sdl.hpp) and its sample, sona-editor-sdl. Its own
# project on sona.h and sona_editor.h, like the Studio; from native/:
#   cmake -S engines/sdl3 -B build/sdl3 && cmake --build build/sdl3 -j4 && ctest --test-dir build/sdl3
# The sample draws with OpenGL 3.3 on Linux and Windows; the component works on any backend of the renderer. SDL3 is the installed one when found, else the pinned release (zlib).
cmake_minimum_required(VERSION 3.25)
project(sona_editor_sdl VERSION 0.1.0 LANGUAGES C CXX)

get_filename_component(SONA_NATIVE "${CMAKE_CURRENT_SOURCE_DIR}/../.." ABSOLUTE)
get_filename_component(SONA_REPO "${SONA_NATIVE}/.." ABSOLUTE)
option(SONA_EDITOR_SDL_TESTING "Build the core with the test licence key" ON)
option(SONA_EDITOR_SDL_SYSTEM_SDL3 "Use an installed SDL3 when one is found" ON)

include(FetchContent)
set(SONA_SDL3_VERSION 3.4.12)
if(SONA_EDITOR_SDL_SYSTEM_SDL3)
  find_package(SDL3 ${SONA_SDL3_VERSION} CONFIG QUIET)
endif()
if(NOT SDL3_FOUND)
  if(MSVC)
    FetchContent_Declare(sona_sdl3
      URL "https://github.com/libsdl-org/SDL/releases/download/release-${SONA_SDL3_VERSION}/SDL3-devel-${SONA_SDL3_VERSION}-VC.zip"
      URL_HASH "SHA256=8793a153c7eba93b1eb8022fd2356383ec446b2584e43724a72ef68d682813ab"
      DOWNLOAD_EXTRACT_TIMESTAMP TRUE)
    FetchContent_MakeAvailable(sona_sdl3)
    find_package(SDL3 ${SONA_SDL3_VERSION} CONFIG REQUIRED PATHS "${sona_sdl3_SOURCE_DIR}/cmake" NO_DEFAULT_PATH)
  else()
    FetchContent_Declare(sona_sdl3
      URL "https://github.com/libsdl-org/SDL/releases/download/release-${SONA_SDL3_VERSION}/SDL3-${SONA_SDL3_VERSION}.tar.gz"
      URL_HASH "SHA256=f07b958a9ac5020fb7a44cadb957f658b2149c3c8abb4f63145fac9303249db7"
      DOWNLOAD_EXTRACT_TIMESTAMP TRUE)
    set(SDL_SHARED ON CACHE BOOL "" FORCE)
    set(SDL_STATIC OFF CACHE BOOL "" FORCE)
    set(SDL_TEST_LIBRARY OFF CACHE BOOL "" FORCE)
    FetchContent_MakeAvailable(sona_sdl3)
  endif()
endif()

# The core and the editor module, GL only.
set(SONA_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(SONA_BUILD_SHARED OFF CACHE BOOL "" FORCE)
set(SONA_BUILD_CLI OFF CACHE BOOL "" FORCE)
set(SONA_BUILD_TOOLS OFF CACHE BOOL "" FORCE)
set(SONA_BUILD_TRACK OFF CACHE BOOL "" FORCE)
set(SONA_TESTING ${SONA_EDITOR_SDL_TESTING} CACHE BOOL "" FORCE)
foreach(_b VULKAN D3D11 D3D12 METAL)
  set(SONA_WITH_${_b} OFF CACHE BOOL "" FORCE)
endforeach()
set(SONA_WITH_GL ON CACHE BOOL "" FORCE)
add_subdirectory("${SONA_NATIVE}" sona EXCLUDE_FROM_ALL)
if(NOT TARGET sona_editor_static)
  message(FATAL_ERROR "sona_editor_sdl: the editor module did not configure in ${SONA_NATIVE}")
endif()

add_library(sona_editor_sdl STATIC sona_editor_sdl.cpp)
target_include_directories(sona_editor_sdl PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
target_link_libraries(sona_editor_sdl PUBLIC sona_editor_static SDL3::SDL3)
set_target_properties(sona_editor_sdl PROPERTIES CXX_STANDARD 17 CXX_EXTENSIONS OFF)
if(MSVC)
  target_compile_options(sona_editor_sdl PRIVATE /W4 /WX)
else()
  target_compile_options(sona_editor_sdl PRIVATE -Wall -Wextra -Werror)
endif()

add_executable(sona-editor-sdl sample/main.cpp)
target_link_libraries(sona-editor-sdl PRIVATE sona_editor_sdl)
set_target_properties(sona-editor-sdl PROPERTIES CXX_STANDARD 17 CXX_EXTENSIONS OFF)
if(MSVC)
  target_compile_options(sona-editor-sdl PRIVATE /W4 /WX)
  add_custom_command(TARGET sona-editor-sdl POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:SDL3::SDL3> $<TARGET_FILE_DIR:sona-editor-sdl>)
else()
  target_compile_options(sona-editor-sdl PRIVATE -Wall -Wextra -Werror)
endif()
if(SONA_EDITOR_SDL_TESTING)
  target_compile_definitions(sona-editor-sdl PRIVATE "SONA_EDITOR_SDL_TEST_LICENCE=\"${SONA_NATIVE}/tests/keys/test-licence.json\"")
endif()

# The scripted runs, under SDL's offscreen video driver (EGL), when SONA_CORPUS_FULL names a folder of avatar files
# and SONA_CATALOGUE its catalogue.
enable_testing()
set(_corpus "$ENV{SONA_CORPUS_FULL}")
set(_cat "$ENV{SONA_CATALOGUE}")
if(_corpus AND _cat AND EXISTS "${_corpus}" AND EXISTS "${_cat}" AND NOT WIN32)
  foreach(_s gamepad touch)
    add_test(NAME editor_sdl_${_s} COMMAND sona-editor-sdl --dir "${_corpus}" --catalogue "${_cat}" --script ${_s}
             --shot "${CMAKE_CURRENT_BINARY_DIR}/editor-sdl-${_s}.png" --out "${CMAKE_CURRENT_BINARY_DIR}/editor-sdl-${_s}.b64")
    set_tests_properties(editor_sdl_${_s} PROPERTIES ENVIRONMENT "SDL_VIDEO_DRIVER=offscreen" TIMEOUT 300)
  endforeach()
  # the same gamepad run with the component's own GL pass for the UI
  add_test(NAME editor_sdl_own_gl_ui COMMAND sona-editor-sdl --dir "${_corpus}" --catalogue "${_cat}" --script gamepad
           --own-gl-ui --shot "${CMAKE_CURRENT_BINARY_DIR}/editor-sdl-own-gl-ui.png")
  set_tests_properties(editor_sdl_own_gl_ui PROPERTIES ENVIRONMENT "SDL_VIDEO_DRIVER=offscreen" TIMEOUT 300)
endif()
