cmake_minimum_required(VERSION 4.2)
project(ColorRace)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# --- CPM.cmake Bootstrap ---
set(CPM_DOWNLOAD_VERSION 0.40.2)
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
if(NOT EXISTS ${CPM_DOWNLOAD_LOCATION})
    file(DOWNLOAD
            https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
            ${CPM_DOWNLOAD_LOCATION}
    )
endif()
include(${CPM_DOWNLOAD_LOCATION})

# --- OpenGL (systemseitig, kein CPM nötig) ---
find_package(OpenGL REQUIRED)

# --- GLFW ---
CPMAddPackage(
        NAME glfw
        GITHUB_REPOSITORY glfw/glfw
        GIT_TAG 3.4
        OPTIONS
        "GLFW_BUILD_EXAMPLES OFF"
        "GLFW_BUILD_TESTS OFF"
        "GLFW_BUILD_DOCS OFF"
        "GLFW_INSTALL OFF"
)

# --- GLM (header-only) ---
CPMAddPackage(
        NAME glm
        GITHUB_REPOSITORY g-truc/glm
        GIT_TAG 1.0.1
)

# --- tinyobjloader ---
CPMAddPackage(
        NAME tinyobjloader
        GITHUB_REPOSITORY tinyobjloader/tinyobjloader
        GIT_TAG v2.0.0rc13
)

# --- stb_image (kein CMake-Projekt, nur Header) ---
CPMAddPackage(
        NAME stb
        GITHUB_REPOSITORY nothings/stb
        GIT_TAG master
        DOWNLOAD_ONLY YES
)
add_library(stb_image STATIC third_party/stb_image_impl.cpp)
target_include_directories(stb_image PUBLIC ${stb_SOURCE_DIR})

# --- Dear ImGui (Core + GLFW/OpenGL3 Backend) ---
CPMAddPackage(
        NAME imgui
        GITHUB_REPOSITORY ocornut/imgui
        GIT_TAG v1.91.5
        DOWNLOAD_ONLY YES
)
add_library(imgui STATIC
        ${imgui_SOURCE_DIR}/imgui.cpp
        ${imgui_SOURCE_DIR}/imgui_draw.cpp
        ${imgui_SOURCE_DIR}/imgui_tables.cpp
        ${imgui_SOURCE_DIR}/imgui_widgets.cpp
        ${imgui_SOURCE_DIR}/imgui_demo.cpp
        ${imgui_SOURCE_DIR}/backends/imgui_impl_glfw.cpp
        ${imgui_SOURCE_DIR}/backends/imgui_impl_opengl3.cpp
)
target_include_directories(imgui PUBLIC ${imgui_SOURCE_DIR} ${imgui_SOURCE_DIR}/backends)
target_link_libraries(imgui PUBLIC glfw glad)

# --- GLAD (manuell generiert, siehe README/Hinweis unten) ---
add_library(glad STATIC third_party/glad/src/glad.c)
target_include_directories(glad PUBLIC third_party/glad/include)

# Engine als statische Lib
add_library(Engine STATIC
        engine/src/core/Window.h
        engine/src/core/Window.cpp
        engine/src/core/Application.cpp
        engine/src/core/Application.h
        engine/src/core/EngineConfig.h
        engine/src/utils/openglWrapper/GLStateCache.cpp
        engine/src/utils/openglWrapper/GLStateCache.h
        engine/src/utils/openglWrapper/GLRenderState.cpp
        engine/src/utils/openglWrapper/GLRenderState.h
)
target_include_directories(Engine PUBLIC engine/src)
target_link_libraries(Engine PUBLIC OpenGL::GL glfw glad glm::glm imgui)

# --- Executable ---
add_executable(ColorRace
        game/src/main.cpp
        game/src/ColorRaceApp.cpp
        game/src/ColorRaceApp.h
)

target_link_libraries(ColorRace PRIVATE
        Engine
        tinyobjloader
        stb_image
)