
cmake_minimum_required(VERSION 4.0)
project(factorygame C)

set(CMAKE_C_STANDARD 23)

find_package(PkgConfig REQUIRED)
pkg_check_modules(SDL2 REQUIRED sdl2)

# Allow setting the target platform manually
if (NOT DEFINED TARGET_PLATFORM)
    if (WIN32)
        set(TARGET_PLATFORM windows)
    elseif (UNIX)
        set(TARGET_PLATFORM linux)
    else ()
        message(FATAL_ERROR "Unsupported platform. Please set TARGET_PLATFORM manually.")
    endif ()
endif ()


set(SOURCE_FILES
        tiles/tile.c
        tiles/tile.h
        util/font.c
        util/font.h
        util/audio.c
        util/audio.h
        util/util.c
        util/util.h
        items/item.c
        items/item.h
        tiles/belt.c
        tiles/belt.h
        tiles/furnace.c
        tiles/furnace.h
        player/player.c
        player/player.h  # Ensure the target is defined before linking
        tiles/tilecallbacks.c
        tiles/tilecallbacks.h
        main.c
        util/perlin.c
        util/perlin.h
        util/atlas.c
        util/atlas.h
        tiles/miner.c
        tiles/miner.h
        util/pathfinding.c
        util/pathfinding.h
        entity/entity.c
        entity/entity.h
        tiles/turret.c
        tiles/turret.h
        util/crafter.c
        util/crafter.h
        util/gamestate.c
        util/gamestate.h
        util/button.c
        util/button.h
        tiles/ammoCrafter.c
        tiles/ammoCrafter.h
        tiles/wiredrawer.c
        tiles/wiredrawer.h
        tiles/core.c
        tiles/core.h
)

add_executable(factorygame ${SOURCE_FILES})


message(STATUS "Target platform: ${TARGET_PLATFORM}")

# Detect build type and configure SDL2 paths accordingly
if (TARGET_PLATFORM STREQUAL "windows")
    find_package(SDL2 REQUIRED)
    find_package(SDL2_ttf REQUIRED)
    include_directories(${SDL2_INCLUDE_DIRS})
    link_directories(${SDL2_LIBRARY_DIRS})
    add_definitions(${SDL2_CFLAGS_OTHER})

elseif (TARGET_PLATFORM STREQUAL "mingw")
    # Cross-compile dynamically linked Windows build
    set(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc)
    set(CMAKE_RC_COMPILER x86_64-w64-mingw32-windres)
    find_package(SDL2 REQUIRED)
    find_package(SDL2_ttf REQUIRED)
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -mwindows")
    target_link_libraries(factorygame mingw32 SDL2main SDL2 SDL2_ttf SDL2_image m)
    include_directories(${SDL2_INCLUDE_DIRS})
    link_directories(${SDL2_LIBRARY_DIRS})
    add_definitions(${SDL2_CFLAGS_OTHER})

elseif (TARGET_PLATFORM STREQUAL "linux")
    pkg_check_modules(SDL2 REQUIRED sdl2)
    pkg_check_modules(SDL2_TTF REQUIRED SDL2_ttf)
    pkg_check_modules(SDL2_IMAGE REQUIRED SDL2_image)
    target_link_libraries(factorygame SDL2 SDL2_ttf SDL2_image m)
    include_directories(${SDL2_INCLUDE_DIRS})
    add_definitions(${SDL2_CFLAGS_OTHER})

else ()
    message(FATAL_ERROR "Unsupported TARGET_PLATFORM: ${TARGET_PLATFORM}")
endif ()

# Define the path to the assets folder
set(ASSETS_SOURCE_DIR "${CMAKE_SOURCE_DIR}/assets")
set(ASSETS_BINARY_DIR "${CMAKE_BINARY_DIR}/assets")

# Copy assets directory always
add_custom_target(copy_assets ALL
        COMMAND ${CMAKE_COMMAND} -E copy_directory
        "${ASSETS_SOURCE_DIR}" "${ASSETS_BINARY_DIR}"
        COMMENT "Copying assets directory to build output..."
)

# Make sure factorygame depends on the assets being copied
add_dependencies(factorygame copy_assets)
