add_library(ib_native_files STATIC local_file.cpp save_file.cpp save_store.cpp game_settings.cpp)
if(WIN32)
    target_link_libraries(ib_native_files PRIVATE shell32 ole32)
endif()
add_library(ib_native_audio STATIC audio_mix.cpp)
target_include_directories(ib_native_audio PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_compile_features(ib_native_audio PUBLIC cxx_std_20)
target_include_directories(ib_native_files PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_compile_features(ib_native_files PUBLIC cxx_std_20)

add_library(ib_native_events INTERFACE)
target_include_directories(ib_native_events INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_compile_features(ib_native_events INTERFACE cxx_std_20)

add_library(ib_native_input INTERFACE)
target_include_directories(ib_native_input INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_compile_features(ib_native_input INTERFACE cxx_std_20)

add_library(ib_native_video INTERFACE)
target_include_directories(ib_native_video INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_compile_features(ib_native_video INTERFACE cxx_std_20)

# Movies (IB12): Sofdec SFD with pl_mpeg (MIT, third_party/pl_mpeg) and an ADX decoder.
add_library(ib_native_movie STATIC movie.cpp)
target_include_directories(ib_native_movie PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include"
                           PRIVATE "${PROJECT_SOURCE_DIR}/third_party/pl_mpeg")
target_compile_features(ib_native_movie PUBLIC cxx_std_20)

add_library(ib_native_render STATIC software_render.cpp)
target_include_directories(ib_native_render PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_compile_features(ib_native_render PUBLIC cxx_std_20)
# Vulkan (IB14): headers in third_party/vulkan and vulkan-1.dll loaded at run time,
# so no Vulkan SDK is needed; on by default on Windows.
if(WIN32)
    option(IB_ENABLE_VULKAN "Compilar o renderizador Vulkan" ON)
else()
    option(IB_ENABLE_VULKAN "Compilar o renderizador Vulkan" OFF)
endif()
if(WIN32)
    target_sources(ib_native_render PRIVATE windows_present.cpp)
    target_link_libraries(ib_native_render PRIVATE gdi32)
    add_executable(ib-render-preview WIN32 "${PROJECT_SOURCE_DIR}/app/render_preview.cpp")
    target_link_libraries(ib-render-preview PRIVATE ib_native_render user32 gdi32)
endif()
# The IB04 offscreen prototype (render preview) still links the SDK when it is present.
if(IB_ENABLE_VULKAN)
    find_package(Vulkan QUIET)
    if(Vulkan_FOUND)
        target_sources(ib_native_render PRIVATE vulkan_render.cpp)
        target_compile_definitions(ib_native_render PRIVATE IB_ENABLE_VULKAN)
        target_link_libraries(ib_native_render PRIVATE Vulkan::Vulkan)
        set_property(GLOBAL PROPERTY IB_PROTOTYPE_VULKAN ON)
    endif()
endif()

# Controls (IB10): bindings, dead zones and the SDL3 gamepad layer.
add_library(ib_native_controls STATIC input_map.cpp)
target_include_directories(ib_native_controls PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_compile_features(ib_native_controls PUBLIC cxx_std_20)
target_link_libraries(ib_native_controls PUBLIC ib_port)
if(TARGET SDL3::SDL3-static)
    add_library(ib_native_sdl STATIC sdl_gamepads.cpp sdl_audio.cpp)
    target_link_libraries(ib_native_sdl PUBLIC ib_native_controls ib_port PRIVATE SDL3::SDL3-static)
endif()

# Vulkan renderer of the command list (IB13); the software model is the fallback.
# The SPIR-V of the shaders is kept in native/shaders/spirv; with glslc (Vulkan
# SDK) and IB_REGENERATE_SPIRV it is rebuilt from the GLSL sources.
if(IB_ENABLE_VULKAN)
    option(IB_REGENERATE_SPIRV "Recompilar os shaders com o glslc do Vulkan SDK" OFF)
    set(_spv_dir "${CMAKE_CURRENT_SOURCE_DIR}/shaders/spirv")
    set(_spv_headers "${_spv_dir}/pvr_vert_spv.h" "${_spv_dir}/pvr_frag_spv.h")
    if(IB_REGENERATE_SPIRV)
        find_package(Vulkan REQUIRED COMPONENTS glslc)
        set(_spv_dir "${CMAKE_CURRENT_BINARY_DIR}/spirv")
        set(_spv_headers)
        foreach(_stage vert frag)
            set(_spv "${_spv_dir}/pvr_${_stage}.spv")
            set(_hdr "${_spv_dir}/pvr_${_stage}_spv.h")
            string(TOUPPER "${_stage}" _up)
            string(SUBSTRING "${_up}" 0 1 _first)
            string(SUBSTRING "${_stage}" 1 -1 _rest)
            add_custom_command(OUTPUT "${_hdr}"
                COMMAND "${CMAKE_COMMAND}" -E make_directory "${_spv_dir}"
                COMMAND Vulkan::glslc -O "${CMAKE_CURRENT_SOURCE_DIR}/shaders/pvr.${_stage}" -o "${_spv}"
                COMMAND "${CMAKE_COMMAND}" -DINPUT=${_spv} -DOUTPUT=${_hdr} -DNAME=kPvr${_first}${_rest}
                        -P "${PROJECT_SOURCE_DIR}/cmake/embed_spirv.cmake"
                DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/shaders/pvr.${_stage}" "${PROJECT_SOURCE_DIR}/cmake/embed_spirv.cmake" VERBATIM)
            list(APPEND _spv_headers "${_hdr}")
        endforeach()
    endif()
    add_library(ib_native_vulkan STATIC vulkan_backend.cpp vk_loader.cpp ${_spv_headers})
    if(WIN32)
        target_sources(ib_native_vulkan PRIVATE vulkan_present.cpp) # window presentation (IB14 performance)
    endif()
    target_include_directories(ib_native_vulkan PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include"
                               "${PROJECT_SOURCE_DIR}/third_party/vulkan/include" PRIVATE "${_spv_dir}")
    target_compile_features(ib_native_vulkan PUBLIC cxx_std_20)
endif()
