cmake_minimum_required(VERSION 3.16)

project(space-game001 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/ThirdParty.cmake)

# ===========================================
# Основной проект space-game001
# ===========================================
add_executable(space-game001
    ../src/main.cpp
    ../src/Game.cpp
    ../src/Game.h
    ../src/Environment.cpp
    ../src/Environment.h
    ../src/render/Renderer.cpp
    ../src/render/Renderer.h
    ../src/render/ShaderManager.cpp
    ../src/render/ShaderManager.h
    ../src/render/TextureManager.cpp
    ../src/render/TextureManager.h
    ../src/TextModel.cpp
    ../src/TextModel.h
    ../src/AudioPlayerAsync.cpp
    ../src/AudioPlayerAsync.h
    ../src/BoneAnimatedModel.cpp
    ../src/BoneAnimatedModel.h
    ../src/render/OpenGlExtensions.cpp
    ../src/render/OpenGlExtensions.h
    ../src/utils/Utils.cpp
    ../src/utils/Utils.h
    ../src/SparkEmitter.cpp
    ../src/SparkEmitter.h
	../src/planet/PlanetObject.cpp
	../src/planet/PlanetObject.h
	../src/planet/PlanetData.cpp
	../src/planet/PlanetData.h
	../src/utils/Perlin.cpp
	../src/utils/Perlin.h
	../src/utils/TaskManager.cpp
	../src/utils/TaskManager.h
	../src/planet/StoneObject.cpp
	../src/planet/StoneObject.h
	../src/render/FrameBuffer.cpp
	../src/render/FrameBuffer.h
    ../src/UiManager.cpp
    ../src/UiManager.h
    ../src/Projectile.h
    ../src/Projectile.cpp
	../src/network/NetworkInterface.h
	../src/network/LocalClient.h
	../src/network/LocalClient.cpp
	../src/network/ClientState.h
	../src/network/ClientState.cpp
	../src/network/WebSocketClient.h
	../src/network/WebSocketClient.cpp
	../src/network/WebSocketClientBase.h
	../src/network/WebSocketClientBase.cpp
	../src/network/WebSocketClientEmscripten.h
	../src/network/WebSocketClientEmscripten.cpp
	../src/render/TextRenderer.h
	../src/render/TextRenderer.cpp
	../src/MenuManager.h
	../src/MenuManager.cpp
	../src/Space.h
	../src/Space.cpp
	../src/GameConstants.h
	../src/GameConstants.cpp
)

# Установка проекта по умолчанию для Visual Studio
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT space-game001)

# include-пути проекта
target_include_directories(space-game001 PRIVATE
    "${CMAKE_CURRENT_SOURCE_DIR}/../src"
)

set_target_properties(space-game001 PROPERTIES
    OUTPUT_NAME "space-game001"
)

# Определения препроцессора:
# PNG_ENABLED – включает код PNG в TextureManager
# SDL_MAIN_HANDLED – отключает переопределение main -> SDL_main
target_compile_definitions(space-game001 PRIVATE
    WIN32_LEAN_AND_MEAN
    PNG_ENABLED
    SDL_MAIN_HANDLED
	NETWORK
#    SIMPLIFIED
)

# Линкуем с SDL2main, если он вообще установлен
target_link_libraries(space-game001 PRIVATE SDL2main_external_lib)

# Линкуем сторонние библиотеки
target_link_libraries(space-game001 PRIVATE
    SDL2_external_lib
    libpng_external_lib
    zlib_external_lib
    libzip_external_lib
	freetype_external_lib
	SDL2_ttf_external_lib
    eigen_external_lib
	boost_external_lib
)

# Линкуем OpenGL (Windows)
if(WIN32)
    target_link_libraries(space-game001 PRIVATE opengl32)
endif()

# ===========================================
# Копирование SDL2d.dll и zlibd.dll рядом с exe
# ===========================================
if (WIN32)

    # SDL2: в Debug - SDL2d.dll, в Release - SDL2.dll
    set(SDL2_DLL_SRC "$<IF:$<CONFIG:Debug>,${SDL2_INSTALL_DIR}/bin/SDL2d.dll,${SDL2_INSTALL_DIR}/bin/SDL2.dll>")
    set(SDL2_DLL_DST "$<IF:$<CONFIG:Debug>,$<TARGET_FILE_DIR:space-game001>/SDL2d.dll,$<TARGET_FILE_DIR:space-game001>/SDL2.dll>")
    

    set(LIBZIP_DLL_SRC "$<IF:$<CONFIG:Debug>,${LIBZIP_BASE_DIR}-Debug/bin/zip.dll,${LIBZIP_BASE_DIR}-Release/bin/zip.dll>")
	
	set(ZLIB_DLL_SRC "$<IF:$<CONFIG:Debug>,${ZLIB_INSTALL_DIR}/bin/zlibd.dll,${ZLIB_INSTALL_DIR}/bin/zlib.dll>")
    set(ZLIB_DLL_DST "$<IF:$<CONFIG:Debug>,$<TARGET_FILE_DIR:space-game001>/zlibd.dll,$<TARGET_FILE_DIR:space-game001>/zlib.dll>")
	
	set(SDL2TTF_DLL_SRC "$<IF:$<CONFIG:Debug>,${SDL2TTF_BASE_DIR}-Debug/bin/SDL2_ttfd.dll,${SDL2TTF_BASE_DIR}-Release/bin/SDL2_ttf.dll>")
    

    add_custom_command(TARGET space-game001 POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E echo "Copying DLLs to output folder..."
        
        # Копируем SDL2 (целевое имя всегда SDL2.dll)
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
            "${SDL2_DLL_SRC}"
            "${SDL2_DLL_DST}"
            
        # Копируем LIBZIP (целевое имя всегда zip.dll)
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
            "${LIBZIP_DLL_SRC}"
            "$<TARGET_FILE_DIR:space-game001>/zip.dll"
			
		COMMAND ${CMAKE_COMMAND} -E copy_if_different
            "${ZLIB_DLL_SRC}"
            "${ZLIB_DLL_DST}"
			
		COMMAND ${CMAKE_COMMAND} -E copy_if_different
			"${SDL2TTF_DLL_SRC}"
			"$<TARGET_FILE_DIR:space-game001>"
    )
endif()

# ===========================================
# Копирование ресурсов после сборки
# ===========================================

# Какие папки с ресурсами нужно копировать
set(RUNTIME_RESOURCE_DIRS
    "resources"
)

# Копируем ресурсы и шейдеры в папку exe и в корень build/
foreach(resdir IN LISTS RUNTIME_RESOURCE_DIRS)
    add_custom_command(TARGET space-game001 POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E echo "Copying ${resdir} to runtime folders..."
        # 1) туда, где лежит exe (build/Debug, build/Release и т.п.)
        COMMAND ${CMAKE_COMMAND} -E copy_directory
            "${CMAKE_SOURCE_DIR}/../${resdir}"
            "$<TARGET_FILE_DIR:space-game001>/${resdir}"
        # 2) в корень build, если захочешь запускать из этой папки
        COMMAND ${CMAKE_COMMAND} -E copy_directory
            "${CMAKE_SOURCE_DIR}/../${resdir}"
            "${CMAKE_BINARY_DIR}/${resdir}"
    )
endforeach()