find_package(glfw3 CONFIG REQUIRED)
find_package(SDL3 CONFIG REQUIRED)
find_package(glm CONFIG REQUIRED)
find_package(VulkanHeaders CONFIG REQUIRED)
find_package(volk CONFIG REQUIRED)
find_package(VulkanMemoryAllocator CONFIG REQUIRED)
find_package(Threads REQUIRED)
find_path(MINIAUDIO_INCLUDE_DIR miniaudio.h REQUIRED)
find_package(Stb REQUIRED)
find_package(nlohmann_json CONFIG REQUIRED)

# Shared value types, byte I/O and codecs sit below both the native readers and runtime.
# Paths sits here with File: every reader names its files in UTF-8 through it.
add_library(engine_base STATIC
    core/Log.cpp core/Assert.cpp core/Clock.cpp core/Strings.cpp platform/Paths.cpp
    io/File.cpp io/AssetLocator.cpp io/Sha256.cpp render/Image.cpp codec/DspAdpcm.cpp)
add_library(gdl::engine_base ALIAS engine_base)
target_include_directories(engine_base PUBLIC ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(engine_base PUBLIC glm::glm)
target_compile_definitions(engine_base PUBLIC
    GLM_FORCE_RADIANS GLM_FORCE_DEPTH_ZERO_TO_ONE GLM_ENABLE_EXPERIMENTAL)
gdl_apply_compiler_options(engine_base)
set_target_properties(engine_base PROPERTIES FOLDER "engine")

add_library(engine STATIC
    world/SceneGeometry.cpp
    assets/ShopCatalog.h
    assets/ShopCatalog.cpp
    core/Types.h
    core/SpecialMembers.h
    core/Error.h
    core/Log.h
    core/Assert.h
    core/Clock.h
    core/Strings.h
    math/Math.h
    math/PortableMath.h
    math/PortableMath.cpp
    io/ByteReader.h
    io/File.h
    io/AssetLocator.h
    platform/Window.h
    platform/DisplayTiming.h
    platform/DisplayTiming.cpp
    platform/Input.h
    platform/Input.cpp
    platform/Gamepads.h
    platform/Gamepads.cpp
    platform/Paths.h
    platform/GlfwWindow.h
    platform/GlfwWindow.cpp
    render/RenderTypes.h
    render/VulkanHandles.h
    render/Mesh.h
    render/Image.h
    render/ImmediateBatch.h
    render/ImmediateBatch.cpp
    render/RenderDevice.h
    render/ThreadedRenderDevice.h
    render/ThreadedRenderDevice.cpp
    render/DepthOfField.h
    render/DepthOfField.cpp
    render/AmbientOcclusion.h
    render/HeatDistortion.h
    render/HeatDistortion.cpp
    render/DebugTextures.h
    render/DebugTextures.cpp
    render/vulkan/VulkanCommon.h
    render/vulkan/VulkanCommon.cpp
    render/vulkan/VmaImplementation.cpp
    render/vulkan/VulkanContext.h
    render/vulkan/VulkanContext.cpp
    render/vulkan/VulkanSwapchain.h
    render/vulkan/VulkanSwapchain.cpp
    render/vulkan/VulkanPipeline.h
    render/vulkan/VulkanPipeline.cpp
    render/vulkan/VulkanPostProcess.h
    render/vulkan/VulkanPostProcess.cpp
    render/vulkan/VulkanTexture.h
    render/vulkan/VulkanTexture.cpp
    render/vulkan/VulkanRenderDevice.h
    render/vulkan/VulkanRenderDevice.cpp
    render/opengl/OpenGlFunctions.h
    render/opengl/OpenGlFunctions.cpp
    render/opengl/OpenGlShaderSource.h
    render/opengl/OpenGlShaderSource.cpp
    render/opengl/OpenGlRenderDevice.h
    render/opengl/OpenGlRenderDevice.cpp
    codec/Lzss.h
    codec/Lzss.cpp
    codec/AviReader.h
    codec/AviReader.cpp
    codec/VqVideoDecoder.h
    codec/VqVideoDecoder.cpp
    codec/DspAdpcm.h
    codec/AdsAudio.h
    codec/AdsAudio.cpp
    codec/MoviePlayback.h
    codec/MoviePlayback.cpp
    audio/AudioStream.h
    audio/AudioStream.cpp
    audio/AudioMixer.h
    audio/AudioMixer.cpp
    audio/AudioDevice.h
    audio/AudioDevice.cpp
    audio/MiniaudioImplementation.cpp
    audio/SoundClip.h
    audio/SoundPlayer.h
    audio/SoundPlayer.cpp
    audio/StreamSource.h
    audio/AdsStream.h
    audio/AdsStream.cpp
    audio/DreamcastStream.h
    audio/DreamcastStream.cpp
    audio/N64LegendsStream.h
    audio/MusicDeclicker.cpp
    audio/MusicDeclicker.h
    audio/StreamPlaylist.cpp
    audio/StreamPlaylist.h
    assets/PngImage.h
    assets/PngImage.cpp
    assets/StbImageImplementation.cpp
    assets/TextureSet.h
    assets/TextureSet.cpp
    assets/TextureBindings.cpp
    assets/TextureBindings.h
    assets/ObjectFlags.h
    assets/ObjectMaterial.h
    assets/BitmapFont.h
    assets/BitmapFont.cpp
    assets/SoundSet.h
    assets/SoundSet.cpp
    assets/NativeSoundSet.cpp
    assets/ModelSet.h
    assets/ModelSet.cpp
    assets/AssetScope.h
    assets/AssetScope.cpp
    assets/ItemArchive.h
    assets/ItemArchive.cpp
    assets/ParticleTemplate.h
    assets/ParticleTemplateJson.h
    assets/ParticleTemplate.cpp
    assets/AnimationSet.h
    assets/AnimationSet.cpp
    assets/NativeAnimationSet.cpp
    assets/NativeParticleTemplate.cpp
    assets/NativeWorldLayout.cpp
    assets/NativeWorldData.cpp
    assets/WorldData.h
    assets/WorldData.cpp
    assets/MessageTable.h
    assets/MessageTable.cpp
    assets/StringTable.h
    assets/StringTable.cpp
    assets/WorldLayout.h
    assets/WorldLayout.cpp
    ui/Canvas.h
    ui/Canvas.cpp
    ui/TextPainter.h
    ui/TextPainter.cpp
    ui/SystemFont.h
    ui/SystemFont.cpp
    ui/StbTrueTypeImplementation.cpp
    ui/ModelSprite.h
    ui/ModelSprite.cpp
    world/AmbientDimmer.h
    world/AmbientDimmer.cpp
    world/WorldLighting.h
    world/WorldLighting.cpp
    world/WorldCamera.h
    world/WorldCamera.cpp
    world/WorldScene.h
    world/SelectionHighlight.cpp
    world/WorldScene.cpp
    world/TextureAnimator.h
    world/TextureAnimator.cpp
    world/WorldAnimator.h
    world/WorldAnimator.cpp
    world/ParticleSystem.h
    world/ParticleSystem.cpp
    world/ParticleField.h
    world/ParticleField.cpp
    world/TreeParticles.h
    world/TreeParticles.cpp
    world/AssetAudit.h
    world/AssetAudit.cpp
    assets/NativeDataAudit.h
    assets/NativeDataAudit.cpp
    world/WorldCollision.h
    world/WorldCollision.cpp
    world/TreeModel.h
    world/TreeModel.cpp
    world/AnimationPlayer.h
    world/AnimationPlayer.cpp
    world/TreePose.h
    world/TreePose.cpp
    app/Application.h
    app/Application.cpp)

add_library(gdl::engine ALIAS engine)

target_include_directories(engine PUBLIC ${CMAKE_SOURCE_DIR}/src)
target_include_directories(engine SYSTEM PRIVATE ${MINIAUDIO_INCLUDE_DIR} ${Stb_INCLUDE_DIR})

target_link_libraries(engine
    PUBLIC
        gdl::engine_base
        glm::glm
    PRIVATE
        gdl::formats
        glfw
        SDL3::SDL3
        nlohmann_json::nlohmann_json
        Vulkan::Headers
        volk::volk
        GPUOpen::VulkanMemoryAllocator
        Threads::Threads
        ${CMAKE_DL_LIBS})

if(UNIX AND NOT APPLE)
    target_link_libraries(engine PRIVATE m)
endif()

target_compile_definitions(engine
    PUBLIC
        GLM_FORCE_RADIANS
        GLM_FORCE_DEPTH_ZERO_TO_ONE
        GLM_ENABLE_EXPERIMENTAL
    PRIVATE
        VK_NO_PROTOTYPES
        VMA_STATIC_VULKAN_FUNCTIONS=0
        VMA_DYNAMIC_VULKAN_FUNCTIONS=1
        GDL_ENABLE_VULKAN_VALIDATION=$<BOOL:${GDL_ENABLE_VULKAN_VALIDATION}>)

gdl_apply_compiler_options(engine)
# The N64 music sequencer needs the fetched amuse sources; without them the stream is a
# stub that never opens, and the game plays the Legends maps silently.
if(GDL_LEGENDS_AUDIO)
    target_sources(engine PRIVATE audio/N64LegendsStream.cpp)
    target_link_libraries(engine PRIVATE gdl::legends_audio_native)
    gdl_configure_legends_audio_source(audio/N64LegendsStream.cpp)
else()
    target_sources(engine PRIVATE audio/N64LegendsStreamStub.cpp)
endif()
set_target_properties(engine PROPERTIES FOLDER "engine")

get_target_property(_engine_sources engine SOURCES)
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${_engine_sources})

if(GDL_ENABLE_NETPLAY)
    add_subdirectory(net)
endif()
