cmake_minimum_required(VERSION 3.16)

project(witcher001 LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Build option (Windows-only): when ON, the game launches in fullscreen.
# When OFF (default), it launches in a 1280x720 window like before.
option(FULLSCREEN "Launch the game in fullscreen mode" OFF)

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

# ===========================================
# Основной проект witcher001
# ===========================================
add_executable(witcher001
    ../src/main.cpp
    ../src/Game.cpp
    ../src/Game.h
    ../src/Character.cpp
    ../src/Character.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/BoneAnimatedModelNew.cpp
    ../src/BoneAnimatedModelNew.h
    ../src/render/OpenGlExtensions.cpp
    ../src/render/OpenGlExtensions.h
    ../src/utils/Utils.cpp
    ../src/utils/Utils.h
    ../src/SparkEmitter.cpp
    ../src/SparkEmitter.h
    ../src/TeleportZone.h
    ../src/TeleportZone.cpp
	../src/utils/TaskManager.cpp
	../src/utils/TaskManager.h
	../src/render/FrameBuffer.cpp
	../src/render/FrameBuffer.h
	../src/render/ShadowMap.cpp
	../src/render/ShadowMap.h
    ../src/UiManager.cpp
    ../src/UiManager.h
	../src/render/TextRenderer.h
	../src/render/TextRenderer.cpp
	../src/MenuManager.h
	../src/MenuManager.cpp
	../src/Location.h
	../src/Location.cpp
    ../src/GameConstants.h
    ../src/GameConstants.cpp
    ../src/ScriptEngine.h
    ../src/ScriptEngine.cpp
	../src/navigation/PathFinder.h
	../src/navigation/PathFinder.cpp
    ../src/items/GameObjectLoader.h
    ../src/items/GameObjectLoader.cpp
    ../src/items/Item.h
    ../src/items/Item.cpp
    ../src/items/InteractiveObject.h
    ../src/items/InteractiveObject.cpp
	../src/dialogue/DialogueTypes.h
	../src/dialogue/DialogueDatabase.h
	../src/dialogue/DialogueDatabase.cpp
	../src/dialogue/DialogueRuntime.h
	../src/dialogue/DialogueRuntime.cpp
	../src/dialogue/DialogueOverlay.h
	../src/dialogue/DialogueOverlay.cpp
	../src/dialogue/DialogueSystem.h
	../src/dialogue/DialogueSystem.cpp
	../src/quest/QuestTypes.h
	../src/quest/QuestJournal.h
	../src/quest/QuestJournal.cpp
)

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

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

set_target_properties(witcher001 PROPERTIES
    OUTPUT_NAME "witcher001"
)

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

if(FULLSCREEN)
    target_compile_definitions(witcher001 PRIVATE FULLSCREEN)
endif()

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

# Линкуем сторонние библиотеки
target_link_libraries(witcher001 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
    sol2_external_lib
    SDL2_mixer_external_lib 
)

# Линкуем OpenGL (Windows)
if(WIN32)
    target_link_libraries(witcher001 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:witcher001>/SDL2d.dll,$<TARGET_FILE_DIR:witcher001>/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/zd.dll,${ZLIB_INSTALL_DIR}/bin/z.dll>")
    set(ZLIB_DLL_DST "$<IF:$<CONFIG:Debug>,$<TARGET_FILE_DIR:witcher001>/zd.dll,$<TARGET_FILE_DIR:witcher001>/z.dll>")
	
	set(SDL2TTF_DLL_SRC "$<IF:$<CONFIG:Debug>,${SDL2TTF_BASE_DIR}-Debug/bin/SDL2_ttfd.dll,${SDL2TTF_BASE_DIR}-Release/bin/SDL2_ttf.dll>")

    set(SDL2MIXER_DLL_SRC "$<IF:$<CONFIG:Debug>,${SDL2MIXER_BASE_DIR}-Debug/bin/SDL2_mixerd.dll,${SDL2MIXER_BASE_DIR}-Release/bin/SDL2_mixer.dll>")

    add_custom_command(TARGET witcher001 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:witcher001>/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:witcher001>"

        COMMAND ${CMAKE_COMMAND} -E copy_if_different
            "${CMAKE_CURRENT_SOURCE_DIR}/../thirdparty/SDL_mixer-release-2.8.0/install-$<CONFIG>/bin/SDL2_mixer$<$<CONFIG:Debug>:d>.dll"
            "$<TARGET_FILE_DIR:witcher001>/SDL2_mixer$<$<CONFIG:Debug>:d>.dll"
    )
endif()

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

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

# Копируем ресурсы и шейдеры в папку exe и в корень build/
foreach(resdir IN LISTS RUNTIME_RESOURCE_DIRS)
    add_custom_command(TARGET witcher001 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:witcher001>/${resdir}"
        # 2) в корень build, если захочешь запускать из этой папки
        COMMAND ${CMAKE_COMMAND} -E copy_directory
            "${CMAKE_SOURCE_DIR}/../${resdir}"
            "${CMAKE_BINARY_DIR}/${resdir}"
    )
endforeach()
