# Aliens vs Predator - https://github.com/atsb/NakedAVP
CMAKE_MINIMUM_REQUIRED(VERSION 3.15)

# On Windows, automatically use the conventional vcpkg installation at C:\vcpkg.
# This must be set before project() so CMake initializes the vcpkg toolchain.
# An explicitly supplied toolchain always takes precedence.
IF(CMAKE_HOST_WIN32 AND NOT CMAKE_TOOLCHAIN_FILE)
    SET(_AVP_VCPKG_ROOT "C:/vcpkg")
    IF(DEFINED ENV{VCPKG_ROOT} AND EXISTS "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")
        SET(_AVP_VCPKG_ROOT "$ENV{VCPKG_ROOT}")
    ENDIF()

    SET(_AVP_VCPKG_TOOLCHAIN "${_AVP_VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")
    IF(EXISTS "${_AVP_VCPKG_TOOLCHAIN}")
        SET(CMAKE_TOOLCHAIN_FILE "${_AVP_VCPKG_TOOLCHAIN}" CACHE FILEPATH "vcpkg toolchain file" FORCE)
        IF(NOT VCPKG_TARGET_TRIPLET)
            IF(CMAKE_GENERATOR_PLATFORM MATCHES "^(Win32|x86)$")
                SET(VCPKG_TARGET_TRIPLET "x86-windows" CACHE STRING "vcpkg target triplet" FORCE)
            ELSEIF(CMAKE_GENERATOR_PLATFORM MATCHES "^(ARM64|arm64)$")
                SET(VCPKG_TARGET_TRIPLET "arm64-windows" CACHE STRING "vcpkg target triplet" FORCE)
            ELSE()
                SET(VCPKG_TARGET_TRIPLET "x64-windows" CACHE STRING "vcpkg target triplet" FORCE)
            ENDIF()
        ENDIF()
        MESSAGE(STATUS "NakedAVP: using vcpkg at ${_AVP_VCPKG_ROOT} (${VCPKG_TARGET_TRIPLET})")
    ELSE()
        MESSAGE(STATUS "NakedAVP: vcpkg not found at ${_AVP_VCPKG_ROOT}; using normal CMake dependency discovery")
    ENDIF()
ENDIF()

SET(CMAKE_CXX_STANDARD 11)

# default to Release
IF(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
    MESSAGE(STATUS "No build type specified, defaulting to Release")
    SET(CMAKE_BUILD_TYPE Release CACHE STRING "Build type; one of: Debug Release RelWithDebInfo MinSizeRel" FORCE)
    SET_PROPERTY(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
ENDIF(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)

PROJECT(avp)

# Enable the Windows resource compiler for MSVC builds.
IF(WIN32 AND MSVC)
  ENABLE_LANGUAGE(RC)
ENDIF()

FIND_PACKAGE(Threads REQUIRED)

# Prefer SDL3's CMake package (provided by vcpkg). Keep the legacy FindSDL3
# module as a fallback so existing non-vcpkg builds continue to work.
IF(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/FindOpenGLES2.cmake")
    INCLUDE("${CMAKE_CURRENT_SOURCE_DIR}/FindOpenGLES2.cmake")
ELSE()
    FIND_PATH(OPENGLES_INCLUDE_DIR NAMES GLES2/gl2.h)
    FIND_PATH(EGL_INCLUDE_DIR NAMES EGL/egl.h)
    FIND_LIBRARY(OPENGLES_gl_LIBRARY NAMES GLESv2)
    FIND_LIBRARY(EGL_LIBRARIES NAMES EGL)
    IF(OPENGLES_INCLUDE_DIR AND EGL_INCLUDE_DIR AND OPENGLES_gl_LIBRARY AND EGL_LIBRARIES)
        SET(OPENGLES_FOUND TRUE)
    ENDIF()
ENDIF()
INCLUDE(FindOpenGL)
FIND_PACKAGE(SDL3 CONFIG QUIET)
IF(TARGET SDL3::SDL3)
    SET(AVP_SDL3_TARGET SDL3::SDL3)
    SET(AVP_SDL3_CONFIG_TARGET TRUE)
    SET(SDL3_FOUND TRUE)
ELSEIF(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/FindSDL3.cmake")
    INCLUDE("${CMAKE_CURRENT_SOURCE_DIR}/FindSDL3.cmake")
    IF(SDL3_FOUND)
        SET(AVP_SDL3_TARGET "${SDL3_LIBRARY}")
    ENDIF()
ELSE()
    FIND_PATH(SDL3_INCLUDE NAMES SDL3/SDL.h)
    FIND_LIBRARY(SDL3_LIBRARY NAMES SDL3)
    IF(SDL3_INCLUDE AND SDL3_LIBRARY)
        SET(SDL3_FOUND TRUE)
        SET(AVP_SDL3_TARGET "${SDL3_LIBRARY}")
    ENDIF()
ENDIF()

# FMV playback. vcpkg ships an FFmpeg CMake package with dependency-aware
# link variables; prefer it on Windows before falling back to pkg-config or
# manual library discovery.
SET(AVP_FFMPEG_TARGET "")
FIND_PACKAGE(FFMPEG QUIET)
IF(FFMPEG_FOUND AND FFMPEG_LIBRARIES)
  ADD_LIBRARY(avp_ffmpeg INTERFACE)
  IF(FFMPEG_INCLUDE_DIRS)
    TARGET_INCLUDE_DIRECTORIES(avp_ffmpeg INTERFACE ${FFMPEG_INCLUDE_DIRS})
  ELSEIF(FFMPEG_INCLUDE_DIR)
    TARGET_INCLUDE_DIRECTORIES(avp_ffmpeg INTERFACE ${FFMPEG_INCLUDE_DIR})
  ENDIF()
  IF(FFMPEG_LIBRARY_DIRS)
    TARGET_LINK_DIRECTORIES(avp_ffmpeg INTERFACE ${FFMPEG_LIBRARY_DIRS})
  ENDIF()
  TARGET_LINK_LIBRARIES(avp_ffmpeg INTERFACE ${FFMPEG_LIBRARIES})
  SET(AVP_FFMPEG_TARGET avp_ffmpeg)
ELSE()
  FIND_PACKAGE(PkgConfig QUIET)
  IF(PkgConfig_FOUND)
    PKG_CHECK_MODULES(FFMPEG QUIET IMPORTED_TARGET
        libavformat libavcodec libavutil libswscale libswresample)
  ENDIF()

  IF(TARGET PkgConfig::FFMPEG)
    SET(AVP_FFMPEG_TARGET PkgConfig::FFMPEG)
  ELSE()
    FIND_PATH(FFMPEG_INCLUDE_DIR NAMES libavformat/avformat.h)
    FIND_LIBRARY(FFMPEG_AVFORMAT NAMES avformat avformat-62 avformat-61 avformat-60 avformat-59)
    FIND_LIBRARY(FFMPEG_AVCODEC NAMES avcodec avcodec-62 avcodec-61 avcodec-60 avcodec-59)
    FIND_LIBRARY(FFMPEG_AVUTIL NAMES avutil avutil-60 avutil-59 avutil-58 avutil-57)
    FIND_LIBRARY(FFMPEG_SWSCALE NAMES swscale swscale-9 swscale-8 swscale-7 swscale-6)
    FIND_LIBRARY(FFMPEG_SWRESAMPLE NAMES swresample swresample-6 swresample-5 swresample-4)

    IF(NOT FFMPEG_INCLUDE_DIR OR NOT FFMPEG_AVFORMAT OR NOT FFMPEG_AVCODEC OR
       NOT FFMPEG_AVUTIL OR NOT FFMPEG_SWSCALE OR NOT FFMPEG_SWRESAMPLE)
      MESSAGE(FATAL_ERROR "FFmpeg development libraries not found. On Windows, run build_windows.bat to install SDL3 and FFmpeg through C:/vcpkg. Otherwise install the FFmpeg development libraries or set CMAKE_PREFIX_PATH to an FFmpeg installation.")
    ENDIF()

    ADD_LIBRARY(avp_ffmpeg_manual INTERFACE)
    TARGET_INCLUDE_DIRECTORIES(avp_ffmpeg_manual INTERFACE ${FFMPEG_INCLUDE_DIR})
    TARGET_LINK_LIBRARIES(avp_ffmpeg_manual INTERFACE
        ${FFMPEG_AVFORMAT} ${FFMPEG_AVCODEC} ${FFMPEG_AVUTIL}
        ${FFMPEG_SWSCALE} ${FFMPEG_SWRESAMPLE})
    SET(AVP_FFMPEG_TARGET avp_ffmpeg_manual)
  ENDIF()
ENDIF()

IF(NOT SDL3_FOUND)
    MESSAGE(FATAL_ERROR "SDL 3 was requested but CMake couldn't find it.")
ENDIF(NOT SDL3_FOUND)

# Use Desktop OpenGL if it is available, else try OpenGL ES.
IF(NOT OPENGL_TYPE)
    SET(OPENGL_TYPE AUTO CACHE STRING "OpenGL Version; one of: AUTO OPENGL OPENGLES2")
    SET_PROPERTY(CACHE OPENGL_TYPE PROPERTY STRINGS "AUTO" "OPENGL" "OPENGLES2")
ENDIF(NOT OPENGL_TYPE)

IF(NOT OPENGL_TYPE STREQUAL "AUTO" AND NOT OPENGL_TYPE STREQUAL "OPENGL" AND NOT OPENGL_TYPE STREQUAL "OPENGLES2")
    MESSAGE(FATAL_ERROR "Invalid OPENGL_TYPE setting ${OPENGL_TYPE}; must be one of AUTO OPENGL OPENGLES2")
ENDIF(NOT OPENGL_TYPE STREQUAL "AUTO" AND NOT OPENGL_TYPE STREQUAL "OPENGL" AND NOT OPENGL_TYPE STREQUAL "OPENGLES2")

IF(OPENGL_TYPE STREQUAL "AUTO")
  IF(OPENGL_FOUND)
    MESSAGE(STATUS "OpenGL found; using that.")
    SET(OPENGL_TYPE "OPENGL")
  ENDIF(OPENGL_FOUND)
ENDIF(OPENGL_TYPE STREQUAL "AUTO")

IF(OPENGL_TYPE STREQUAL "AUTO")
  IF(OPENGLES_FOUND)
    MESSAGE(STATUS "OpenGL ES 2 found; using that.")
    SET(OPENGL_TYPE "OPENGLES2")
  ENDIF(OPENGLES_FOUND)
ENDIF(OPENGL_TYPE STREQUAL "AUTO")

IF(OPENGL_TYPE STREQUAL "AUTO")
  MESSAGE(FATAL_ERROR "OpenGL is required but CMake couldn't find it.")
ENDIF(OPENGL_TYPE STREQUAL "AUTO")

IF(OPENGL_TYPE STREQUAL "OPENGL")
  IF(NOT OPENGL_FOUND)
    MESSAGE(FATAL_ERROR "OpenGL was requested but CMake couldn't find it.")
  ENDIF(NOT OPENGL_FOUND)
ENDIF(OPENGL_TYPE STREQUAL "OPENGL")

IF(OPENGL_TYPE STREQUAL "OPENGLES2")
  IF(NOT OPENGLES_FOUND)
    MESSAGE(FATAL_ERROR "OpenGL ES was requested but CMake couldn't find it.")
  ENDIF(NOT OPENGLES_FOUND)
ENDIF(OPENGL_TYPE STREQUAL "OPENGLES2")

# required source files
LIST(APPEND source src/engine/core/files.c)
LIST(APPEND source src/platform/winapi.c)
LIST(APPEND source src/engine/core/stubs.c)
LIST(APPEND source src/engine/render/render_workers.c)
LIST(APPEND source src/engine/core/version.c)
LIST(APPEND source src/engine/core/mathline.c)
LIST(APPEND source src/engine/network/net.c)
LIST(APPEND source src/engine/core/frustum.c)
LIST(APPEND source src/engine/core/kshape.c)
LIST(APPEND source src/engine/core/map.c)
LIST(APPEND source src/engine/core/maths.c)
LIST(APPEND source src/engine/core/md5.c)
LIST(APPEND source src/engine/core/mem3dc.c)
LIST(APPEND source src/engine/core/mem3dcpp.cpp)
LIST(APPEND source src/engine/core/module.c)
LIST(APPEND source src/engine/core/morph.c)
LIST(APPEND source src/engine/core/object.c)
LIST(APPEND source src/engine/core/shpanim.c)
LIST(APPEND source src/engine/core/sphere.c)
LIST(APPEND source src/engine/core/tables.c)
LIST(APPEND source src/engine/core/vdb.c)
LIST(APPEND source src/game/world/ai_sight.c)
LIST(APPEND source src/game/core/avpview.c)
LIST(APPEND source src/game/behaviours/bh_agun.c)
LIST(APPEND source src/game/behaviours/bh_ais.c)
LIST(APPEND source src/game/behaviours/bh_alien.c)
LIST(APPEND source src/game/behaviours/bh_binsw.c)
LIST(APPEND source src/game/behaviours/bh_cable.c)
LIST(APPEND source src/game/behaviours/bh_corpse.c)
LIST(APPEND source src/game/behaviours/bh_deathvol.c)
LIST(APPEND source src/game/behaviours/bh_debri.c)
LIST(APPEND source src/game/behaviours/bh_dummy.c)
LIST(APPEND source src/game/behaviours/bh_fan.c)
LIST(APPEND source src/game/behaviours/bh_far.c)
LIST(APPEND source src/game/behaviours/bh_fhug.c)
LIST(APPEND source src/game/behaviours/bh_gener.c)
LIST(APPEND source src/game/behaviours/bh_ldoor.c)
LIST(APPEND source src/game/behaviours/bh_lift.c)
LIST(APPEND source src/game/behaviours/bh_light.c)
LIST(APPEND source src/game/behaviours/bh_lnksw.c)
LIST(APPEND source src/game/behaviours/bh_ltfx.c)
LIST(APPEND source src/game/behaviours/bh_marin.c)
LIST(APPEND source src/game/behaviours/bh_mission.c)
LIST(APPEND source src/game/behaviours/bh_near.c)
LIST(APPEND source src/game/behaviours/bh_pargen.c)
LIST(APPEND source src/game/behaviours/bh_plachier.c)
LIST(APPEND source src/game/behaviours/bh_plift.c)
LIST(APPEND source src/game/behaviours/bh_pred.c)
LIST(APPEND source src/game/behaviours/bh_queen.c)
LIST(APPEND source src/game/behaviours/bh_rubberduck.c)
LIST(APPEND source src/game/behaviours/bh_selfdest.c)
LIST(APPEND source src/game/behaviours/bh_snds.c)
LIST(APPEND source src/game/behaviours/bh_spcl.c)
LIST(APPEND source src/game/behaviours/bh_swdor.c)
LIST(APPEND source src/game/behaviours/bh_track.c)
LIST(APPEND source src/game/behaviours/bh_types.c)
LIST(APPEND source src/game/behaviours/bh_videoscreen.c)
LIST(APPEND source src/game/behaviours/bh_waypt.c)
LIST(APPEND source src/game/behaviours/bh_weap.c)
LIST(APPEND source src/game/behaviours/bh_xeno.c)
LIST(APPEND source src/game/weapons/bonusabilities.c)
LIST(APPEND source src/game/core/cconvars.cpp)
LIST(APPEND source src/game/core/cdtrackselection.cpp)
LIST(APPEND source src/game/core/cheatmodes.c)
LIST(APPEND source src/game/world/comp_map.c)
LIST(APPEND source src/game/world/comp_shp.c)
LIST(APPEND source src/game/core/consolelog.cpp)
LIST(APPEND source src/game/core/davehook.cpp)
LIST(APPEND source src/game/core/deaths.c)
LIST(APPEND source src/game/render/decal.c)
LIST(APPEND source src/game/render/detaillevels.c)
LIST(APPEND source src/game/world/dynamics.c)
LIST(APPEND source src/game/world/dynblock.c)
LIST(APPEND source src/game/weapons/equipmnt.c)
LIST(APPEND source src/game/world/extents.c)
LIST(APPEND source src/game/core/game.c)
LIST(APPEND source src/game/core/game_statistics.c)
LIST(APPEND source src/game/core/gamecmds.cpp)
LIST(APPEND source src/game/core/gamevars.cpp)
LIST(APPEND source src/game/render/hmodel.c)
LIST(APPEND source src/game/render/hud.c)
LIST(APPEND source src/game/player/inventry.c)
LIST(APPEND source src/game/core/language.c)
LIST(APPEND source src/game/render/lighting.c)
LIST(APPEND source src/game/world/load_shp.c)
LIST(APPEND source src/game/world/los.c)
LIST(APPEND source src/game/core/mempool.c)
LIST(APPEND source src/game/core/messagehistory.c)
LIST(APPEND source src/game/core/missions.cpp)
LIST(APPEND source src/game/player/movement.c)
LIST(APPEND source src/game/render/paintball.c)
LIST(APPEND source src/game/render/particle.c)
LIST(APPEND source src/game/world/pfarlocs.c)
LIST(APPEND source src/game/world/pheromon.c)
LIST(APPEND source src/game/player/player.c)
LIST(APPEND source src/game/player/pmove.c)
LIST(APPEND source src/game/audio/psnd.c)
LIST(APPEND source src/game/audio/psndproj.c)
LIST(APPEND source src/game/world/pvisible.c)
LIST(APPEND source src/game/core/savegame.c)
LIST(APPEND source src/game/audio/scream.cpp)
LIST(APPEND source src/game/world/secstats.c)
LIST(APPEND source src/game/audio/sfx.c)
LIST(APPEND source src/game/world/stratdef.c)
LIST(APPEND source src/game/weapons/targeting.c)
LIST(APPEND source src/game/world/track.c)
LIST(APPEND source src/game/world/triggers.c)
LIST(APPEND source src/game/weapons/weapons.c)
LIST(APPEND source src/game/core/cube.c)
LIST(APPEND source src/game/support/consbind.cpp)
LIST(APPEND source src/game/support/consbtch.cpp)
LIST(APPEND source src/game/support/coordstr.cpp)
LIST(APPEND source src/game/support/daemon.cpp)
LIST(APPEND source src/game/support/indexfnt.cpp)
LIST(APPEND source src/game/support/r2base.cpp)
LIST(APPEND source src/game/support/r2pos666.cpp)
LIST(APPEND source src/game/support/reflist.cpp)
LIST(APPEND source src/game/support/refobj.cpp)
LIST(APPEND source src/game/support/rentrntq.cpp)
LIST(APPEND source src/game/support/scstring.cpp)
LIST(APPEND source src/game/support/strtab.cpp)
LIST(APPEND source src/game/support/strutil.c)
LIST(APPEND source src/game/support/trig666.cpp)
LIST(APPEND source src/game/support/wrapstr.cpp)
LIST(APPEND source src/game/platform/win32/avpchunk.cpp)
LIST(APPEND source src/game/platform/win32/cheat.c)
LIST(APPEND source src/game/platform/win32/chtcodes.cpp)
LIST(APPEND source src/game/platform/win32/d3d_hud.cpp)
LIST(APPEND source src/game/platform/win32/ddplat.cpp)
LIST(APPEND source src/game/platform/win32/endianio.c)
LIST(APPEND source src/game/platform/win32/ffread.cpp)
LIST(APPEND source src/game/platform/win32/ffstdio.cpp)
LIST(APPEND source src/game/platform/win32/gammacontrol.cpp)
LIST(APPEND source src/game/platform/win32/hierplace.cpp)
LIST(APPEND source src/game/platform/win32/iofocus.cpp)
LIST(APPEND source src/game/platform/win32/jsndsup.cpp)
LIST(APPEND source src/game/platform/win32/kzsort.c)
LIST(APPEND source src/game/platform/win32/langplat.c)
LIST(APPEND source src/game/platform/win32/modcmds.cpp)
LIST(APPEND source src/game/platform/win32/npcsetup.cpp)
LIST(APPEND source src/game/platform/win32/objsetup.cpp)
LIST(APPEND source src/game/platform/win32/pathchnk.cpp)
LIST(APPEND source src/game/platform/win32/platsup.c)
LIST(APPEND source src/game/platform/win32/pldghost.c)
LIST(APPEND source src/game/platform/win32/pldnet.c)
LIST(APPEND source src/game/platform/win32/progress_bar.cpp)
LIST(APPEND source src/game/platform/win32/projload.cpp)
LIST(APPEND source src/game/platform/win32/scrshot.cpp)
LIST(APPEND source src/game/platform/win32/strachnk.cpp)
LIST(APPEND source src/game/platform/win32/system.c)
LIST(APPEND source src/game/platform/win32/usr_io.c)
LIST(APPEND source src/game/platform/win32/vision.c)
LIST(APPEND source src/game/frontend/avp_envinfo.c)
LIST(APPEND source src/game/frontend/avp_intro.cpp)
LIST(APPEND source src/game/frontend/avp_menudata.c)
LIST(APPEND source src/game/frontend/avp_menus.c)
LIST(APPEND source src/game/frontend/avp_mp_config.cpp)
LIST(APPEND source src/game/frontend/avp_userprofile.cpp)
LIST(APPEND source src/game/ui/gadgets/ahudgadg.cpp)
LIST(APPEND source src/game/ui/gadgets/conscmnd.cpp)
LIST(APPEND source src/game/ui/gadgets/conssym.cpp)
LIST(APPEND source src/game/ui/gadgets/consvar.cpp)
LIST(APPEND source src/game/ui/gadgets/gadget.cpp)
LIST(APPEND source src/game/ui/gadgets/hudgadg.cpp)
LIST(APPEND source src/game/ui/gadgets/rootgadg.cpp)
LIST(APPEND source src/game/ui/gadgets/t_ingadg.cpp)
LIST(APPEND source src/game/ui/gadgets/teletype.cpp)
LIST(APPEND source src/game/ui/gadgets/textexp.cpp)
LIST(APPEND source src/game/ui/gadgets/textin.cpp)
LIST(APPEND source src/game/ui/gadgets/trepgadg.cpp)
LIST(APPEND source src/platform/win95/animchnk.cpp)
LIST(APPEND source src/platform/win95/animobs.cpp)
LIST(APPEND source src/platform/win95/awtexld.cpp)
LIST(APPEND source src/platform/win95/awbmpld.cpp)
LIST(APPEND source src/platform/win95/awiffld.cpp)
LIST(APPEND source src/platform/win95/awpnmld.cpp)
LIST(APPEND source src/platform/win95/bmpnames.cpp)
LIST(APPEND source src/platform/win95/chnkload.cpp)
LIST(APPEND source src/platform/win95/chnktexi.cpp)
LIST(APPEND source src/platform/win95/chnktype.cpp)
LIST(APPEND source src/platform/win95/chunk.cpp)
LIST(APPEND source src/platform/win95/chunkpal.cpp)
LIST(APPEND source src/platform/win95/db.c)
LIST(APPEND source src/platform/win95/debuglog.cpp)
LIST(APPEND source src/platform/win95/dummyobjectchunk.cpp)
LIST(APPEND source src/platform/win95/enumchnk.cpp)
LIST(APPEND source src/platform/win95/enumsch.cpp)
LIST(APPEND source src/platform/win95/envchunk.cpp)
LIST(APPEND source src/platform/win95/fail.c)
LIST(APPEND source src/platform/win95/fragchnk.cpp)
LIST(APPEND source src/platform/win95/gsprchnk.cpp)
LIST(APPEND source src/platform/win95/hierchnk.cpp)
LIST(APPEND source src/platform/win95/huffman.cpp)
LIST(APPEND source src/platform/win95/iff.cpp)
LIST(APPEND source src/platform/win95/iff_ilbm.cpp)
LIST(APPEND source src/platform/win95/ilbm_ext.cpp)
LIST(APPEND source src/platform/win95/io.c)
LIST(APPEND source src/platform/win95/list_tem.cpp)
LIST(APPEND source src/platform/win95/ltchunk.cpp)
LIST(APPEND source src/platform/win95/media.cpp)
LIST(APPEND source src/platform/win95/mishchnk.cpp)
LIST(APPEND source src/platform/win95/obchunk.cpp)
LIST(APPEND source src/platform/win95/oechunk.cpp)
LIST(APPEND source src/platform/win95/our_mem.c)
LIST(APPEND source src/platform/win95/plat_shp.c)
LIST(APPEND source src/platform/win95/plspecfn.c)
LIST(APPEND source src/platform/win95/shpchunk.cpp)
LIST(APPEND source src/platform/win95/sndchunk.cpp)
LIST(APPEND source src/platform/win95/sprchunk.cpp)
LIST(APPEND source src/platform/win95/string.cpp)
LIST(APPEND source src/platform/win95/texio.c)
LIST(APPEND source src/platform/win95/toolchnk.cpp)
LIST(APPEND source src/platform/win95/txioctrl.cpp)
LIST(APPEND source src/platform/win95/wpchunk.cpp)
LIST(APPEND source src/platform/win95/zsp.cpp)
LIST(APPEND source src/renderer/opengl.c)
LIST(APPEND source src/renderer/oglfunc.c)
LIST(APPEND source src/renderer/postprocess.c)
LIST(APPEND source src/media/video/fmv.c)
LIST(APPEND source src/media/video/movie.c)
LIST(APPEND source src/media/audio/sdl_audio.c)
LIST(APPEND source src/media/audio/cdplayer.c)
LIST(APPEND source src/frontend/menus.c)
LIST(APPEND source src/frontend/main.c)

### build all source as C++
### (not normally used)
##SET_SOURCE_FILES_PROPERTIES(${source} PROPERTIES LANGUAGE CXX)

# auto-include directories with source files
FOREACH(sourcefile IN LISTS source)
    GET_FILENAME_COMPONENT(includedir ${sourcefile} PATH) # newer CMake uses DIRECTORY
    LIST(APPEND include ${includedir})
ENDFOREACH(sourcefile)
INCLUDE_DIRECTORIES(${include})

# manually include shared game headers
INCLUDE_DIRECTORIES(src/game)
INCLUDE_DIRECTORIES(include/nakedavp)


# Embed the classic executable icon in Windows/MSVC builds.
IF(WIN32 AND MSVC)
  LIST(APPEND source "${CMAKE_CURRENT_SOURCE_DIR}/resources/avp.rc")
ENDIF()

ADD_EXECUTABLE(avp ${source})

# Ship the optional post-processing shader pack beside the executable.
ADD_CUSTOM_COMMAND(TARGET avp POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy_directory
          "${CMAKE_CURRENT_SOURCE_DIR}/shaders"
          "$<TARGET_FILE_DIR:avp>/shaders"
  COMMENT "Copying NakedAVP shaders next to the game binary")

# For dynamic vcpkg triplets, copy installed runtime DLLs beside avp.exe so
# a fresh build can run without requiring the user to edit PATH. The script
# is a no-op when vcpkg is not being used or no DLLs are present.
IF(WIN32 AND VCPKG_INSTALLED_DIR AND VCPKG_TARGET_TRIPLET)
  ADD_CUSTOM_COMMAND(TARGET avp POST_BUILD
    COMMAND ${CMAKE_COMMAND}
      "-DAVP_VCPKG_TRIPLET_DIR=${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}"
      "-DAVP_DEST_DIR=$<TARGET_FILE_DIR:avp>"
      "-DAVP_CONFIG=$<CONFIG>"
      -P "${CMAKE_CURRENT_SOURCE_DIR}/cmake/CopyVcpkgRuntime.cmake"
    COMMENT "Copying vcpkg runtime DLLs next to avp.exe")
ENDIF()

# The imported SDL3 target supplies its own include directories. The legacy
# FindSDL3 fallback exposes a path variable instead.
IF(NOT AVP_SDL3_CONFIG_TARGET AND SDL3_INCLUDE)
  INCLUDE_DIRECTORIES(${SDL3_INCLUDE})
ENDIF()

IF(OPENGL_TYPE STREQUAL "OPENGL")
  INCLUDE_DIRECTORIES(${OPENGL_INCLUDE_DIR})
ENDIF(OPENGL_TYPE STREQUAL "OPENGL")

IF(OPENGL_TYPE STREQUAL "OPENGLES2")
  ADD_DEFINITIONS(-DUSE_OPENGL_ES=2)
  INCLUDE_DIRECTORIES(${OPENGLES_INCLUDE_DIR})
  INCLUDE_DIRECTORIES(${EGL_INCLUDE_DIR})
ENDIF(OPENGL_TYPE STREQUAL "OPENGLES2")


TARGET_LINK_LIBRARIES(avp ${AVP_SDL3_TARGET})
TARGET_LINK_LIBRARIES(avp Threads::Threads)
TARGET_LINK_LIBRARIES(avp ${AVP_FFMPEG_TARGET})

IF(OPENGL_TYPE STREQUAL "OPENGL")
  TARGET_LINK_LIBRARIES(avp ${OPENGL_gl_LIBRARY})
ENDIF(OPENGL_TYPE STREQUAL "OPENGL")

IF(OPENGL_TYPE STREQUAL "OPENGLES2")
  TARGET_LINK_LIBRARIES(avp ${OPENGLES_gl_LIBRARY})
  TARGET_LINK_LIBRARIES(avp ${EGL_LIBRARIES})
ENDIF(OPENGL_TYPE STREQUAL "OPENGLES2")

# Windows system libraries used by the platform layer.
IF(WIN32)
  TARGET_LINK_LIBRARIES(avp shlwapi winmm)
ENDIF()
