cmake_minimum_required(VERSION 3.20)
project(ctw_port CXX C)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(FetchContent)
set(SDL_SHARED OFF CACHE BOOL "" FORCE)
set(SDL_STATIC ON CACHE BOOL "" FORCE)
set(SDL_TEST OFF CACHE BOOL "" FORCE)
if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/../third_party/SDL2/CMakeLists.txt" AND NOT FETCHCONTENT_SOURCE_DIR_SDL2)
    # Corresponding-source release archives include the exact SDL2 source used by the build.
    set(FETCHCONTENT_SOURCE_DIR_SDL2 "${CMAKE_CURRENT_LIST_DIR}/../third_party/SDL2" CACHE PATH "Local SDL2 source")
endif()
FetchContent_Declare(SDL2 GIT_REPOSITORY https://github.com/libsdl-org/SDL.git GIT_TAG release-2.30.9 GIT_SHALLOW TRUE)
FetchContent_MakeAvailable(SDL2)

# brotli's decoder, for the interface font (WOFF2): only its C sources are built
if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/../third_party/brotli/c/include" AND NOT FETCHCONTENT_SOURCE_DIR_BROTLI)
    set(FETCHCONTENT_SOURCE_DIR_BROTLI "${CMAKE_CURRENT_LIST_DIR}/../third_party/brotli" CACHE PATH "Local brotli source")
endif()
FetchContent_Declare(brotli GIT_REPOSITORY https://github.com/google/brotli.git GIT_TAG v1.1.0 GIT_SHALLOW TRUE
    SOURCE_SUBDIR no-cmake)
FetchContent_MakeAvailable(brotli)
file(GLOB CTW_BROTLI_SOURCES ${brotli_SOURCE_DIR}/c/common/*.c ${brotli_SOURCE_DIR}/c/dec/*.c)
add_library(ctw_brotlidec STATIC ${CTW_BROTLI_SOURCES})
target_include_directories(ctw_brotlidec PUBLIC ${brotli_SOURCE_DIR}/c/include)

add_library(glad STATIC third_party/glad/src/gl.c)
target_include_directories(glad PUBLIC third_party/glad/include)

# ---- engine: everything shared by the showcase and the game (src/) ----------------------------------------
add_library(ctw_engine STATIC
    src/os/os_sdl.cpp src/os/wad.cpp src/os/gamefs.cpp src/os/dxtbin.cpp src/os/pak.cpp src/os/datafile.cpp
    src/gfx/spriteset.cpp src/gfx/font.cpp src/gfx/model.cpp src/gfx/character.cpp src/gfx/texload.cpp src/gfx/assets.cpp src/gfx/stb_impl.cpp src/gfx/uifont.cpp src/gfx/pedsprites.cpp src/gfx/rendertables.cpp src/gfx/gametext.cpp
    src/text/gxt.cpp src/audio/audio.cpp src/video/movie.cpp
    src/world/worldmap.cpp src/world/timecycle.cpp src/world/worldrenderer.cpp src/world/collision.cpp src/world/collisionsweep.cpp src/world/props.cpp)
target_include_directories(ctw_engine PUBLIC src third_party/stb third_party/minimp3)
target_link_libraries(ctw_engine PUBLIC glad SDL2::SDL2-static ctw_brotlidec)
if(WIN32)   # Media Foundation decodes the intro movie (part of Windows; no extra runtime)
    target_link_libraries(ctw_engine PUBLIC mfplat mfreadwrite mfuuid shlwapi ole32)
endif()

# ---- showcase: the asset viewer (textures, text, models, audio, fly-through city) ------------------------
add_executable(ctw_showcase showcase/main.cpp showcase/viewer.cpp showcase/modelview.cpp showcase/worldview.cpp)
target_link_libraries(ctw_showcase PRIVATE ctw_engine SDL2::SDL2main)

# ---- the game ----------------------------------------------------------------------------------------------
# GTACTW.exe. Version info (company kryo.to Inc) + the icon, which tools/make_icon.py makes from your own APK
# (data/gtactw.ico). Standalone exports (scripts/export_pc.py) put the icon into the exe themselves.
# -DCTW_RELEASE=ON: no console window (scripts/export_pc.py wants a release build).
option(CTW_RELEASE "release build: windowed, no console" OFF)
set(CTW_VERSION 0.1.0 CACHE STRING "Version written into GTACTW.exe (releases pass the Porter's version)")
set(CTW_ICON_LINE "")
if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/data/gtactw.ico)
    configure_file(${CMAKE_CURRENT_LIST_DIR}/data/gtactw.ico ${CMAKE_BINARY_DIR}/gtactw.ico COPYONLY)
    set(CTW_ICON_LINE "1 ICON \"gtactw.ico\"")
endif()
string(REPLACE "." "," CTW_VERSION_COMMAS ${CTW_VERSION})
configure_file(game/res/gtactw.rc.in ${CMAKE_BINARY_DIR}/gtactw.rc @ONLY)
set(CTW_RES "")
if(MINGW)   # windres run from the build folder with relative names (it breaks on include paths with spaces)
    find_program(CTW_WINDRES windres REQUIRED)
    add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/gtactw_res.o
        COMMAND ${CTW_WINDRES} -O coff gtactw.rc gtactw_res.o
        WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
        DEPENDS ${CMAKE_BINARY_DIR}/gtactw.rc)
    set(CTW_RES ${CMAKE_BINARY_DIR}/gtactw_res.o)
endif()
add_library(ctw_gameplay STATIC game/game.cpp game/followcam.cpp game/player.cpp game/hud.cpp game/plugins.cpp game/vehicle.cpp game/carcam.cpp game/cargens.cpp game/random.cpp game/peds.cpp game/interpolation.cpp game/debughud.cpp game/postfx.cpp game/weather.cpp game/localarea.cpp game/weapons.cpp game/flamethrower.cpp game/fire.cpp game/taser.cpp game/sniper.cpp game/projectiles.cpp game/targeting.cpp game/weaponhud.cpp game/roads.cpp game/aispline.cpp game/traffic.cpp game/particles.cpp game/sound.cpp game/skidmarks.cpp game/lookups.cpp game/watercannon.cpp game/propdynamics.cpp game/rigidbody.cpp game/radio.cpp game/stats.cpp)
target_sources(ctw_gameplay PRIVATE game/pedsocial.cpp)
target_sources(ctw_gameplay PRIVATE game/zones.cpp)
target_sources(ctw_gameplay PRIVATE game/intro.cpp)
target_sources(ctw_gameplay PRIVATE game/pickups.cpp)
target_sources(ctw_gameplay PRIVATE game/sequence.cpp game/comicscreen.cpp game/sinkingcar.cpp game/devmenu.cpp game/porterui.cpp game/stroke.cpp game/hotwiring.cpp game/minigamecam.cpp game/cutscene.cpp game/mission.cpp game/kena01.cpp game/doors.cpp game/garages.cpp game/markers.cpp game/hudtext.cpp game/contexthelp.cpp game/savegame.cpp game/safehouse.cpp)
target_sources(ctw_gameplay PRIVATE game/pedcomments.cpp)
target_sources(ctw_gameplay PRIVATE game/police.cpp game/policedriving.cpp)
target_include_directories(ctw_gameplay PUBLIC game)
target_sources(ctw_gameplay PRIVATE game/radar.cpp game/gamehud.cpp)
target_sources(ctw_gameplay PRIVATE game/gameplayapi.cpp game/cameraobstruction.cpp)
target_sources(ctw_gameplay PRIVATE game/multiplayerworld.cpp)
# The interface typeface the Porter is set in (Kryoto's Teletext, porter/public/fonts: see its README), built into
# the game as the Porter builds it into its own exe. Without the files the interface uses the game's font.
set(CTW_UI_FONTS ${CMAKE_CURRENT_LIST_DIR}/../porter/public/fonts)
set(CTW_UI_FONT_SOURCE "// Generated from porter/public/fonts by port/CMakeLists.txt.\n#include <cstddef>\n#include <cstdint>\n")
foreach(CTW_FONT regular bold)
    set(CTW_FONT_FILE ${CTW_UI_FONTS}/teletext-${CTW_FONT}.woff2)
    set(CTW_FONT_BYTES "0")
    set(CTW_FONT_SIZE "0")
    if(EXISTS ${CTW_FONT_FILE})
        set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${CTW_FONT_FILE})
        file(READ ${CTW_FONT_FILE} CTW_FONT_HEX HEX)
        string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," CTW_FONT_BYTES "${CTW_FONT_HEX}")
        set(CTW_FONT_SIZE "sizeof kUiFont_${CTW_FONT}")
    endif()
    string(APPEND CTW_UI_FONT_SOURCE "extern const uint8_t kUiFont_${CTW_FONT}[] = {${CTW_FONT_BYTES}};\n"
                                     "extern const size_t kUiFont_${CTW_FONT}_size = ${CTW_FONT_SIZE};\n")
endforeach()
file(CONFIGURE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/generated/uifonts.cpp CONTENT "${CTW_UI_FONT_SOURCE}" @ONLY)
target_sources(ctw_gameplay PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/generated/uifonts.cpp)
target_sources(ctw_gameplay PRIVATE game/vehicledebris.cpp game/vehicleskids.cpp game/trails.cpp game/vehiclereactions.cpp
    game/wanted.cpp game/crimes.cpp game/deathscreen.cpp)
target_sources(ctw_gameplay PRIVATE game/explosions.cpp)
target_sources(ctw_gameplay PRIVATE game/pausescreen.cpp game/driving.cpp game/trafficavoidance.cpp)
target_sources(ctw_gameplay PRIVATE game/loadingscreen.cpp)
target_link_libraries(ctw_gameplay PUBLIC ctw_engine)
add_executable(ctw_game game/main.cpp ${CTW_RES})
set_target_properties(ctw_game PROPERTIES OUTPUT_NAME GTACTW)
target_include_directories(ctw_game PRIVATE game)
target_link_libraries(ctw_game PRIVATE ctw_gameplay SDL2::SDL2main)
if(MINGW)
    target_link_options(ctw_game PRIVATE -static -static-libgcc -static-libstdc++)
    if(CTW_RELEASE)
        target_link_options(ctw_game PRIVATE -mwindows)
    endif()
endif()

# ---- tools --------------------------------------------------------------------------------------------------
add_executable(wadtest tools/wadtest.cpp src/os/wad.cpp src/os/datafile.cpp)
target_include_directories(wadtest PRIVATE src)
add_executable(audiotest tools/audiotest.cpp)
target_include_directories(audiotest PRIVATE third_party/minimp3)

include(CTest)
if(BUILD_TESTING)
    add_executable(multiplayerworldtest ../tests/multiplayer_world_test.cpp)
    target_link_libraries(multiplayerworldtest PRIVATE ctw_gameplay)
    add_test(NAME multiplayer_world COMMAND multiplayerworldtest)
    add_executable(npcweaponstest ../tests/npc_weapons_test.cpp)
    target_link_libraries(npcweaponstest PRIVATE ctw_gameplay)
    add_test(NAME npc_weapons COMMAND npcweaponstest)
    add_executable(zonetest ../tests/zones_test.cpp)
    target_link_libraries(zonetest PRIVATE ctw_gameplay)
    add_test(NAME zones COMMAND zonetest)
    add_executable(loadingrendertest ../tests/loading_render_test.cpp)
    target_link_libraries(loadingrendertest PRIVATE ctw_gameplay)
    add_test(NAME loading_rendering COMMAND loadingrendertest)
    set_tests_properties(loading_rendering PROPERTIES SKIP_RETURN_CODE 77)
    add_executable(traffictest ../tests/traffic_test.cpp)
    target_link_libraries(traffictest PRIVATE ctw_gameplay)
    add_test(NAME traffic_driving COMMAND traffictest)
    add_executable(wantedtest ../tests/wanted_test.cpp)
    target_link_libraries(wantedtest PRIVATE ctw_gameplay)
    add_test(NAME wanted_level COMMAND wantedtest)
    add_executable(pickupstest ../tests/pickups_test.cpp)
    target_link_libraries(pickupstest PRIVATE ctw_gameplay)
    add_test(NAME pickups_money COMMAND pickupstest)
    add_executable(sequencetest ../tests/sequence_test.cpp)
    target_link_libraries(sequencetest PRIVATE ctw_gameplay)
    add_test(NAME cutscene_sequences COMMAND sequencetest)
    add_executable(policetest ../tests/police_test.cpp)
    target_link_libraries(policetest PRIVATE ctw_gameplay)
    add_test(NAME police_arrests COMMAND policetest)
    add_executable(trafficavoidancetest ../tests/traffic_avoidance_test.cpp)
    target_link_libraries(trafficavoidancetest PRIVATE ctw_gameplay)
    add_test(NAME traffic_avoidance COMMAND trafficavoidancetest)
    add_executable(pauserendertest ../tests/pause_render_test.cpp)
    target_link_libraries(pauserendertest PRIVATE ctw_gameplay)
    add_test(NAME pause_rendering COMMAND pauserendertest)
    set_tests_properties(pause_rendering PROPERTIES SKIP_RETURN_CODE 77)
    add_executable(radartest ../tests/radar_test.cpp game/radar.cpp)
    target_include_directories(radartest PRIVATE game)
    add_test(NAME radar_map COMMAND radartest)
    add_executable(introtest ../tests/intro_test.cpp)
    target_link_libraries(introtest PRIVATE ctw_gameplay)
    add_test(NAME intro_movie COMMAND introtest)
    add_executable(hudrendertest ../tests/hud_render_test.cpp)
    target_link_libraries(hudrendertest PRIVATE ctw_gameplay)
    add_test(NAME gameplay_hud_rendering COMMAND hudrendertest)
    set_tests_properties(gameplay_hud_rendering PROPERTIES SKIP_RETURN_CODE 77)
    add_executable(fonttest ../tests/font_render_test.cpp)
    target_link_libraries(fonttest PRIVATE ctw_engine)
    add_test(NAME font_rendering COMMAND fonttest)
    set_tests_properties(font_rendering PROPERTIES SKIP_RETURN_CODE 77)
    add_executable(weaponhudrendertest ../tests/weaponhud_render_test.cpp)
    target_link_libraries(weaponhudrendertest PRIVATE ctw_gameplay)
    add_test(NAME weapon_hud_rendering COMMAND weaponhudrendertest)
    set_tests_properties(weapon_hud_rendering PROPERTIES SKIP_RETURN_CODE 77)
    add_executable(charactertest ../tests/character_test.cpp)
    target_link_libraries(charactertest PRIVATE ctw_engine)
    add_test(NAME character_models COMMAND charactertest)
    add_executable(gameplaytest ../tests/gameplay_test.cpp)
    target_include_directories(gameplaytest PRIVATE game)
    target_link_libraries(gameplaytest PRIVATE ctw_gameplay)
    add_test(NAME gameplay COMMAND gameplaytest)
    add_executable(lookuptest ../tests/lookup_test.cpp game/lookups.cpp src/gfx/rendertables.cpp src/os/datafile.cpp)
    target_include_directories(lookuptest PRIVATE game src)
    add_test(NAME lookups COMMAND lookuptest)
    add_executable(portregressiontest ../tests/port_regression_test.cpp)
    target_link_libraries(portregressiontest PRIVATE ctw_gameplay)
    add_test(NAME port_regressions COMMAND portregressiontest)
    add_executable(radiorendertest ../tests/radio_render_test.cpp)
    target_link_libraries(radiorendertest PRIVATE ctw_gameplay)
    add_test(NAME radio_rendering COMMAND radiorendertest)
    set_tests_properties(radio_rendering PROPERTIES SKIP_RETURN_CODE 77)
    add_executable(particlerendertest ../tests/particle_render_test.cpp)
    target_link_libraries(particlerendertest PRIVATE ctw_gameplay)
    add_test(NAME particle_rendering COMMAND particlerendertest)
    set_tests_properties(particle_rendering PROPERTIES SKIP_RETURN_CODE 77)
    add_executable(gameplayapitest ../tests/gameplay_api_test.cpp)
    target_link_libraries(gameplayapitest PRIVATE ctw_gameplay)
    add_test(NAME gameplay_api COMMAND gameplayapitest)
    add_executable(pedstest ../tests/peds_test.cpp)
    target_link_libraries(pedstest PRIVATE ctw_gameplay)
    add_test(NAME pedestrians COMMAND pedstest)
    add_executable(pedsocialtest ../tests/ped_social_test.cpp)
    target_link_libraries(pedsocialtest PRIVATE ctw_gameplay)
    add_test(NAME pedestrian_social COMMAND pedsocialtest)
    add_executable(weaponstest ../tests/weapons_test.cpp)
    target_link_libraries(weaponstest PRIVATE ctw_gameplay)
    add_test(NAME weapons COMMAND weaponstest)
    add_executable(armourtest ../tests/armour_test.cpp)
    target_link_libraries(armourtest PRIVATE ctw_gameplay)
    add_test(NAME armour COMMAND armourtest)
    add_executable(statstest ../tests/stats_test.cpp)
    target_link_libraries(statstest PRIVATE ctw_gameplay)
    add_test(NAME stats COMMAND statstest)
    add_executable(weaponparitytest ../tests/weapon_parity_test.cpp)
    target_link_libraries(weaponparitytest PRIVATE ctw_gameplay)
    add_test(NAME weapon_parity COMMAND weaponparitytest)
    add_executable(explosiontest ../tests/explosion_test.cpp)
    target_link_libraries(explosiontest PRIVATE ctw_gameplay)
    add_test(NAME explosions COMMAND explosiontest)
    add_executable(firewatertest ../tests/fire_water_test.cpp)
    target_link_libraries(firewatertest PRIVATE ctw_gameplay)
    add_test(NAME fire_water COMMAND firewatertest)
endif()
