cmake_minimum_required(VERSION 3.10)
project(sdl3game LANGUAGES C)

include(CTest)

find_package(PkgConfig REQUIRED)
find_package(SDL3 REQUIRED)
find_package(SDL3_image REQUIRED)
find_package(SDL3_mixer REQUIRED)
find_package(SDL3_ttf REQUIRED)
find_package(akerror REQUIRED)
find_package(jansson REQUIRED)
find_package(box2d REQUIRED)

# Check for SDL3 using pkg-config
#pkg_check_modules(SDL3 REQUIRED sdl3)
#pkg_check_modules(SDL3_image REQUIRED sdl3-image)
#pkg_check_modules(SDL3_mixer REQUIRED sdl3-mixer)
#pkg_check_modules(jansson REQUIRED jansson)
#pkg_check_modules(akerror REQUIRED akerror)

set(GAMECONTROLLERDB_H "include/sdl3game/SDL_GameControllerDB.h")
set(prefix      ${CMAKE_INSTALL_PREFIX})
set(exec_prefix "\${prefix}")
set(libdir      "\${exec_prefix}/lib")
set(includedir  "\${prefix}/include")
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/sdl3game.pc.in ${CMAKE_CURRENT_BINARY_DIR}/sdl3game.pc @ONLY)

add_custom_command(
  OUTPUT ${GAMECONTROLLERDB_H}
  COMMAND ../mkcontrollermappings.sh
  COMMENT "Generating controller mappings ..."
)

# Add include directories
include_directories(${SDL3_INCLUDE_DIRS})
add_library(sdl3game SHARED
  src/actor.c
  src/actor_state_string_names.c
  src/text.c
  src/assets.c
  src/character.c
  src/draw.c
  src/game.c
  src/controller.c
  src/heap.c
  src/json_helpers.c
  src/registry.c
  src/sprite.c
  src/staticstring.c
  src/tilemap.c
  src/util.c
  ${GAMECONTROLLERDB_H}
)


add_executable(charviewer util/charviewer.c)

add_executable(test_actor tests/actor.c)
add_executable(test_bitmasks tests/bitmasks.c)
add_executable(test_character tests/character.c)
add_executable(test_registry tests/registry.c)
add_executable(test_sprite tests/sprite.c)
add_executable(test_staticstring tests/staticstring.c)
add_executable(test_tilemap tests/tilemap.c)
add_executable(test_util tests/util.c)
add_test(NAME actor COMMAND test_actor)
add_test(NAME bitmasks COMMAND test_bitmasks)
add_test(NAME character COMMAND test_character)
add_test(NAME registry COMMAND test_registry)
add_test(NAME sprite COMMAND test_sprite)
add_test(NAME staticstring COMMAND test_staticstring)
add_test(NAME tilemap COMMAND test_tilemap)
add_test(NAME util COMMAND test_util)

# Specify include directories for the library's headers (if applicable)
target_include_directories(sdl3game PUBLIC
    include/
  )

target_link_libraries(sdl3game
  PUBLIC
    SDL3::SDL3
    SDL3_image::SDL3_image
    SDL3_mixer::SDL3_mixer
    SDL3_ttf::SDL3_ttf
)
  
target_link_libraries(test_actor PRIVATE akerror::akerror sdl3game SDL3::SDL3 SDL3_ttf::SDL3_ttf SDL3_image::SDL3_image SDL3_mixer::SDL3_mixer box2d::box2d jansson::jansson -lm)
target_link_libraries(test_bitmasks PRIVATE akerror::akerror sdl3game SDL3::SDL3 SDL3_ttf::SDL3_ttf SDL3_image::SDL3_image SDL3_mixer::SDL3_mixer box2d::box2d jansson::jansson -lm)
target_link_libraries(test_character PRIVATE akerror::akerror sdl3game SDL3::SDL3 SDL3_ttf::SDL3_ttf SDL3_image::SDL3_image SDL3_mixer::SDL3_mixer box2d::box2d jansson::jansson -lm)
target_link_libraries(test_registry PRIVATE akerror::akerror sdl3game SDL3::SDL3 SDL3_ttf::SDL3_ttf SDL3_image::SDL3_image SDL3_mixer::SDL3_mixer box2d::box2d jansson::jansson -lm)
target_link_libraries(test_sprite PRIVATE akerror::akerror sdl3game SDL3::SDL3 SDL3_ttf::SDL3_ttf SDL3_image::SDL3_image SDL3_mixer::SDL3_mixer box2d::box2d jansson::jansson -lm)
target_link_libraries(test_staticstring PRIVATE akerror::akerror sdl3game SDL3::SDL3 SDL3_ttf::SDL3_ttf SDL3_image::SDL3_image SDL3_mixer::SDL3_mixer box2d::box2d jansson::jansson -lm)
target_link_libraries(test_tilemap PRIVATE akerror::akerror sdl3game SDL3::SDL3 SDL3_ttf::SDL3_ttf SDL3_image::SDL3_image SDL3_mixer::SDL3_mixer box2d::box2d jansson::jansson -lm)
target_link_libraries(test_util PRIVATE akerror::akerror sdl3game SDL3::SDL3 SDL3_ttf::SDL3_ttf SDL3_image::SDL3_image SDL3_mixer::SDL3_mixer box2d::box2d jansson::jansson -lm)

target_link_libraries(charviewer PRIVATE akerror::akerror sdl3game SDL3::SDL3 SDL3_ttf::SDL3_ttf SDL3_image::SDL3_image SDL3_mixer::SDL3_mixer box2d::box2d jansson::jansson -lm)

set(main_lib_dest "lib/sdl3game-${MY_LIBRARY_VERSION}")
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/sdl3game.pc DESTINATION "lib/pkgconfig/")
install(TARGETS sdl3game DESTINATION "lib/")
install(FILES "include/sdl3game/actor.h" DESTINATION "include/sdl3game/")
install(FILES "include/sdl3game/text.h" DESTINATION "include/sdl3game/")
install(FILES "include/sdl3game/assets.h" DESTINATION "include/sdl3game/")
install(FILES "include/sdl3game/character.h" DESTINATION "include/sdl3game/")
install(FILES "include/sdl3game/error.h" DESTINATION "include/sdl3game/")
install(FILES "include/sdl3game/draw.h" DESTINATION "include/sdl3game/")
install(FILES "include/sdl3game/game.h" DESTINATION "include/sdl3game/")
install(FILES "include/sdl3game/controller.h" DESTINATION "include/sdl3game/")
install(FILES "include/sdl3game/heap.h" DESTINATION "include/sdl3game/")
install(FILES "include/sdl3game/iterator.h" DESTINATION "include/sdl3game/")
install(FILES "include/sdl3game/json_helpers.h" DESTINATION "include/sdl3game/")
install(FILES "include/sdl3game/registry.h" DESTINATION "include/sdl3game/")
install(FILES "include/sdl3game/sprite.h" DESTINATION "include/sdl3game/")
install(FILES "include/sdl3game/staticstring.h" DESTINATION "include/sdl3game/")
install(FILES "include/sdl3game/tilemap.h" DESTINATION "include/sdl3game/")
install(FILES "include/sdl3game/util.h" DESTINATION "include/sdl3game/")
install(FILES ${GAMECONTROLLERDB_H} DESTINATION "include/sdl3game/")
