cmake_minimum_required(VERSION 3.19)
project(BVLEVoxels LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Wicked Engine options - disable what we don't need
set(WICKED_EDITOR OFF CACHE BOOL "" FORCE)
set(WICKED_TESTS OFF CACHE BOOL "" FORCE)
set(WICKED_IMGUI_EXAMPLE OFF CACHE BOOL "" FORCE)
set(WICKED_WINDOWS_TEMPLATE OFF CACHE BOOL "" FORCE)
set(WICKED_LINUX_TEMPLATE OFF CACHE BOOL "" FORCE)
set(WICKED_ENABLE_SYMLINKS OFF CACHE BOOL "" FORCE)

add_subdirectory(engine)

# ── Voxel Engine Library ──────────────────────────────────────────
file(GLOB_RECURSE VOXEL_SOURCES src/voxel/*.cpp src/voxel/*.h)
add_library(VoxelEngine STATIC ${VOXEL_SOURCES})
target_include_directories(VoxelEngine PUBLIC src)
target_link_libraries(VoxelEngine PUBLIC WickedEngine)

# ── Main Application ─────────────────────────────────────────────
file(GLOB APP_SOURCES src/app/*.cpp src/app/*.h)
add_executable(BVLEVoxels WIN32 ${APP_SOURCES})
target_link_libraries(BVLEVoxels PRIVATE VoxelEngine WickedEngine)

# Copy Content directory (shaders, etc.) to build output
add_custom_command(TARGET BVLEVoxels POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
        ${CMAKE_SOURCE_DIR}/engine/Content
        $<TARGET_FILE_DIR:BVLEVoxels>/Content
)

# Copy DXC shader compiler DLL next to the exe (required for runtime shader compilation)
add_custom_command(TARGET BVLEVoxels POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        ${CMAKE_SOURCE_DIR}/engine/WickedEngine/dxcompiler.dll
        $<TARGET_FILE_DIR:BVLEVoxels>/dxcompiler.dll
    COMMENT "Copying DXC shader compiler DLL"
)

# Copy voxel texture assets to Content/voxel/ next to the exe
add_custom_command(TARGET BVLEVoxels POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
        ${CMAKE_SOURCE_DIR}/assets/voxel
        $<TARGET_FILE_DIR:BVLEVoxels>/Content/voxel
    COMMENT "Copying voxel texture assets"
)

# Copy our custom shader sources into Wicked's shader source tree
# so LoadShader can find and compile them as "voxel/voxelVS.cso"
add_custom_command(TARGET BVLEVoxels POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
        ${CMAKE_SOURCE_DIR}/shaders
        ${CMAKE_SOURCE_DIR}/engine/WickedEngine/shaders/voxel
    COMMENT "Copying voxel shaders to Wicked Engine shader source directory"
)

# Delete stale compiled voxel shaders to force runtime recompilation.
# Wicked Engine only compiles .hlsl → .cso when .cso is ABSENT (no timestamp check).
# Without this step, .hlsl changes are silently ignored if old .cso files exist.
add_custom_command(TARGET BVLEVoxels POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E rm -f
        $<TARGET_FILE_DIR:BVLEVoxels>/shaders/hlsl6/voxel/voxelVS.cso
        $<TARGET_FILE_DIR:BVLEVoxels>/shaders/hlsl6/voxel/voxelPS.cso
        $<TARGET_FILE_DIR:BVLEVoxels>/shaders/hlsl6/voxel/voxelCullCS.cso
        $<TARGET_FILE_DIR:BVLEVoxels>/shaders/hlsl6/voxel/voxelMeshCS.cso
        $<TARGET_FILE_DIR:BVLEVoxels>/shaders/hlsl6/voxel/voxelTopingVS.cso
        $<TARGET_FILE_DIR:BVLEVoxels>/shaders/hlsl6/voxel/voxelTopingPS.cso
        $<TARGET_FILE_DIR:BVLEVoxels>/shaders/hlsl6/voxel/voxelSmoothVS.cso
        $<TARGET_FILE_DIR:BVLEVoxels>/shaders/hlsl6/voxel/voxelSmoothPS.cso
        $<TARGET_FILE_DIR:BVLEVoxels>/shaders/hlsl6/voxel/voxelSmoothCentroidCS.cso
        $<TARGET_FILE_DIR:BVLEVoxels>/shaders/hlsl6/voxel/voxelSmoothCS.cso
        $<TARGET_FILE_DIR:BVLEVoxels>/shaders/hlsl6/voxel/voxelBLASExtractCS.cso
        $<TARGET_FILE_DIR:BVLEVoxels>/shaders/hlsl6/voxel/voxelShadowCS.cso
        $<TARGET_FILE_DIR:BVLEVoxels>/shaders/hlsl6/voxel/voxelAOBlurCS.cso
        $<TARGET_FILE_DIR:BVLEVoxels>/shaders/hlsl6/voxel/voxelAOApplyCS.cso
        $<TARGET_FILE_DIR:BVLEVoxels>/shaders/hlsl6/voxel/voxelCommon.hlsli.cso
    COMMENT "Clearing stale voxel shader cache (forces recompilation from current .hlsl sources)"
)
