cmake_minimum_required(VERSION 4.1)
project(v)

set(CMAKE_CXX_STANDARD 23)

include(FetchContent)

find_package(Vulkan REQUIRED COMPONENTS volk)

find_library(SLANG_LIB NAMES slang HINTS "$ENV{VULKAN_SDK}/lib")
find_path(SLANG_INCLUDE_DIR NAMES slang/slang.h HINTS "$ENV{VULKAN_SDK}/include")
find_file(SLANG_DLL NAMES slang.dll HINTS "$ENV{VULKAN_SDK}/bin")

if(SLANG_LIB AND SLANG_INCLUDE_DIR)
    add_library(slang_sdk SHARED IMPORTED)
    set_target_properties(slang_sdk PROPERTIES
            IMPORTED_IMPLIB "${SLANG_LIB}"
            INTERFACE_INCLUDE_DIRECTORIES "${SLANG_INCLUDE_DIR}"
    )

    # If we found the DLL, set it as the location;
    # otherwise, on non-Windows systems, SLANG_LIB is the location.
    if(WIN32 AND SLANG_DLL)
        set_target_properties(slang_sdk PROPERTIES IMPORTED_LOCATION "${SLANG_DLL}")
    else()
        set_target_properties(slang_sdk PROPERTIES IMPORTED_LOCATION "${SLANG_LIB}")
    endif()

    message(STATUS "Slang found via VULKAN_SDK: ${SLANG_LIB}")
else()
    message(FATAL_ERROR "VULKAN_SDK env var is set, but Slang wasn't found inside it!")
endif()

FetchContent_Declare(
        glfw
        GIT_REPOSITORY https://github.com/glfw/glfw.git
        GIT_TAG 232164f62b0edbf667cba37c91bab92ffbb020d0
)
FetchContent_MakeAvailable(glfw)

FetchContent_Declare(
        glm
        GIT_REPOSITORY https://github.com/g-truc/glm.git
        GIT_TAG 1.0.3
)
FetchContent_MakeAvailable(glm)

FetchContent_Declare(
        stb
        GIT_REPOSITORY https://github.com/nothings/stb.git
        GIT_TAG        master
)

FetchContent_MakeAvailable(stb)

add_library(stb INTERFACE)
target_include_directories(stb INTERFACE ${stb_SOURCE_DIR})

add_executable(v main.cpp
        renderer/init.cpp
        renderer/init.h
        renderer/renderer.cpp
        renderer/renderer.h
        misc.cpp
        misc.h
        renderer/sprite.cpp
        renderer/sprite.h
        renderer/texture.cpp
        renderer/texture.h
        renderer/texture_sheet.cpp
        renderer/texture_sheet.h)

target_include_directories(v
        PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}
)

target_link_libraries(v PRIVATE glfw Vulkan::Vulkan glm::glm stb slang_sdk)

set(SHADER_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/shaders")
set(SHADER_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/shaders")
file(GLOB_RECURSE SHADER_SOURCES
        "${SHADER_SOURCE_DIR}/*.vert"
        "${SHADER_SOURCE_DIR}/*.frag"
        "${SHADER_SOURCE_DIR}/*.comp"
)

set(SPIRV_BINARY_FILES "")

foreach(SHADER ${SHADER_SOURCES})

    get_filename_component(FILENAME ${SHADER} NAME)
    set(OUTPUT_SPV "${SHADER_SOURCE_DIR}/${FILENAME}.spv")

    add_custom_command(
            OUTPUT ${OUTPUT_SPV}
            COMMAND Vulkan::glslc ${SHADER} -o ${OUTPUT_SPV}
            DEPENDS ${SHADER}
            COMMENT "Compiling shader: ${FILENAME}"
    )

    list(APPEND SPIRV_BINARY_FILES ${OUTPUT_SPV})
endforeach()

add_custom_target(compile_shaders ALL DEPENDS ${SPIRV_BINARY_FILES})

add_dependencies(v compile_shaders)
