cmake_minimum_required(VERSION 4.0)
project(siege_deck CXX)

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

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

if (WIN32)
    add_compile_definitions(PLATFORM_WINDOWS)
elseif (UNIX)
    add_compile_definitions(PLATFORM_LINUX)
endif ()

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include(Cpm)

# ---------------------------------------------------------------------------
# Dependencies
#
# Every entry below is a CPMAddPackage, so this tree configures unchanged on a
# machine with no nix (Windows, CI, a bare checkout) — CPM fetches and builds
# each one from the pinned tag. Inside the devShell, CPM_USE_LOCAL_PACKAGES=ON
# flips each call to try find_package first, and every one of these resolves
# against nixpkgs, so nothing is downloaded or rebuilt.
#
# That only holds because NAME is the find_package name (SDL3, not SDL) and
# VERSION matches what nixpkgs ships. Both paths are pinned to the SAME
# version deliberately: a dependency that differs between "on my machine" and
# "in the nix shell" is the exact bug this setup exists to avoid. Bumping one
# means bumping the other, or waiting for nixpkgs to catch up.
# ---------------------------------------------------------------------------

set(LIBS)

# Boost, for pretty type names via boost::typeindex (see Common.h).
#
# Boost::type_index only exists in the modular boost-cmake build CPM pulls;
# nixpkgs' b2-based install exposes only the aggregate Boost::headers. Since
# type_index is header-only, Boost::headers carries it in both worlds.
CPMAddPackage(
        NAME Boost
        VERSION 1.89.0
        URL "https://github.com/boostorg/boost/releases/download/boost-1.89.0/boost-1.89.0-cmake.tar.xz"
        FIND_PACKAGE_ARGUMENTS "CONFIG COMPONENTS headers")
list(APPEND LIBS Boost::headers)

# Logging.
CPMAddPackage(
        NAME spdlog
        VERSION 1.17.0
        GITHUB_REPOSITORY gabime/spdlog
        GIT_TAG v1.17.0
        FIND_PACKAGE_ARGUMENTS "CONFIG")
list(APPEND LIBS spdlog::spdlog)

# Windowing, input, 2D rendering.
CPMAddPackage(
        NAME SDL3
        VERSION 3.4.14
        GITHUB_REPOSITORY libsdl-org/SDL
        GIT_TAG release-3.4.14
        FIND_PACKAGE_ARGUMENTS "CONFIG")
list(APPEND LIBS SDL3::SDL3)

# Sprite loading (PNG, JPG, ...).
CPMAddPackage(
        NAME SDL3_image
        VERSION 3.4.4
        GITHUB_REPOSITORY libsdl-org/SDL_image
        GIT_TAG release-3.4.4
        FIND_PACKAGE_ARGUMENTS "CONFIG")
list(APPEND LIBS SDL3_image::SDL3_image)

# Text: gold, wave counters, tower costs, tooltips.
CPMAddPackage(
        NAME SDL3_ttf
        VERSION 3.2.2
        GITHUB_REPOSITORY libsdl-org/SDL_ttf
        GIT_TAG release-3.2.2
        FIND_PACKAGE_ARGUMENTS "CONFIG")
list(APPEND LIBS SDL3_ttf::SDL3_ttf)

# Audio.
CPMAddPackage(
        NAME SDL3_mixer
        VERSION 3.2.4
        GITHUB_REPOSITORY libsdl-org/SDL_mixer
        GIT_TAG release-3.2.4
        FIND_PACKAGE_ARGUMENTS "CONFIG")
list(APPEND LIBS SDL3_mixer::SDL3_mixer)

# Vector math.
CPMAddPackage(
        NAME glm
        VERSION 1.0.3
        GITHUB_REPOSITORY g-truc/glm
        GIT_TAG 1.0.3
        FIND_PACKAGE_ARGUMENTS "CONFIG")
list(APPEND LIBS glm::glm)

# ECS.
CPMAddPackage(
        NAME EnTT
        VERSION 3.16.0
        GITHUB_REPOSITORY skypjack/entt
        GIT_TAG v3.16.0
        FIND_PACKAGE_ARGUMENTS "CONFIG")
list(APPEND LIBS EnTT::EnTT)

# Debug/tools UI. Not a CPMAddPackage call because upstream ImGui ships no
# CMakeLists at all — see cmake/ImGui.cmake for what that costs.
include(ImGui)
list(APPEND LIBS imgui::imgui)

# ---------------------------------------------------------------------------
# Target
# ---------------------------------------------------------------------------

add_executable(${PROJECT_NAME}
        Source/Main.cpp
        Source/Common.h
        Source/Pch.h)

target_include_directories(${PROJECT_NAME} PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}/Source)

target_link_libraries(${PROJECT_NAME} PRIVATE ${LIBS})

target_precompile_headers(${PROJECT_NAME} PRIVATE Source/Pch.h)

# Assets are looked up relative to the executable, so the same path works from
# the build dir, from CLion's runner, and from an installed copy.
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_directory
        ${CMAKE_CURRENT_SOURCE_DIR}/Assets
        $<TARGET_FILE_DIR:${PROJECT_NAME}>/Assets)
