cmake_minimum_required(VERSION 3.25)

project(mikemon VERSION 0.1 LANGUAGES CXX C)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_COMPILE_WARNING_AS_ERROR ON)

option(TRACY_ENABLE "Enable Tracy profiling" OFF)

# SDL
set(SDL_SHARED OFF)
set(SDL_STATIC ON)
add_subdirectory(libs/SDL3)

# Dear ImGui
add_library(imgui STATIC
    libs/imgui/imgui.cpp
    libs/imgui/imgui_draw.cpp
    libs/imgui/imgui_demo.cpp
    libs/imgui/imgui_tables.cpp
    libs/imgui/imgui_widgets.cpp
    libs/imgui/backends/imgui_impl_sdl3.cpp
    libs/imgui/backends/imgui_impl_sdlgpu3.cpp
)
target_include_directories(imgui PUBLIC
    libs/imgui
    libs/imgui/backends
)
target_link_libraries(imgui PRIVATE SDL3::SDL3)

# Shaders
find_program(SLANGC 
    NAMES slangc
    PATHS /opt/shader-slang-bin/bin
)

# stb_image
add_library(stb_image STATIC libs/stb/stb_image.c)
target_include_directories(stb_image INTERFACE libs/stb)

# tracy
option(TRACY_ONLY_LOCALHOST "" ON)
add_subdirectory(libs/tracy)

function(add_shader name)
    set(INPUT_FILE  ${CMAKE_CURRENT_SOURCE_DIR}/assets/shader/${name}.slang)
    set(OUTPUT_FILE ${CMAKE_CURRENT_SOURCE_DIR}/assets/shader/${name}.h)
    set(DEP_FILE    ${CMAKE_CURRENT_SOURCE_DIR}/assets/shader/${name}.dep)

    if (SLANGC)
        if (CMAKE_BUILD_TYPE STREQUAL "Debug")
            set(SLANG_FLAGS -g3 -O0)
        else()
            set(SLANG_FLAGS -O3)
        endif()

        add_custom_command(
            OUTPUT ${OUTPUT_FILE}
            COMMAND ${SLANGC} -target spirv -validate-uniformity -o ${OUTPUT_FILE} -depfile ${DEP_FILE} -source-embed-style u8 -source-embed-name SPIRV_${name} ${SLANG_FLAGS} -- ${INPUT_FILE}
            DEPFILE ${DEP_FILE}
        )
    endif()

    add_custom_target(SHADER_${name}
        DEPENDS ${OUTPUT_FILE}
    )
endfunction()

add_shader(basic)
add_shader(world)

add_executable(mikemon
    src/log.cpp
    src/load_entire_file.cpp
    src/smol-atlas.cpp
    src/math_graphics.cpp
    src/main.cpp
)
target_link_libraries(mikemon
    PRIVATE
        SDL3::SDL3
        stb_image
        imgui
        TracyClient
)
add_dependencies(mikemon
    SHADER_basic
    SHADER_world
)
