# metalslugxx - ReXGlue Recompiled Project
#
# SDK boilerplate lives in generated/rexglue.cmake.

cmake_minimum_required(VERSION 3.25)
project(metalslugxx LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(generated/rexglue.cmake)

# Sources
set(METALSLUGXX_SOURCES
    src/main.cpp
    src/metalslugxx_hooks.cpp
    src/metalslugxx_crash.cpp
    src/metalslugxx_timing.cpp
    src/metalslugxx_settings.cpp
    src/metalslugxx_dlc.cpp
    src/metalslugxx_screenshot.cpp
    # Vendored lodepng (zlib license) - PNG encoder for the F12 guest-output
    # capture (src/metalslugxx_screenshot.cpp).
    thirdparty/lodepng/lodepng.cpp
)

if(WIN32)
    enable_language(RC)

    set(METALSLUGXX_ICON
        "${CMAKE_CURRENT_SOURCE_DIR}/../IMG_0867 (0-00-00-00).ico")
    if(NOT EXISTS "${METALSLUGXX_ICON}")
        message(FATAL_ERROR "Metal Slug XX icon not found: ${METALSLUGXX_ICON}")
    endif()

    set(METALSLUGXX_RESOURCE
        "${CMAKE_CURRENT_BINARY_DIR}/metalslugxx.rc")
    configure_file(
        resources/metalslugxx.rc.in
        "${METALSLUGXX_RESOURCE}"
        @ONLY
    )
    set_property(
        SOURCE "${METALSLUGXX_RESOURCE}"
        PROPERTY OBJECT_DEPENDS "${METALSLUGXX_ICON}"
    )

    add_executable(metalslugxx WIN32 ${METALSLUGXX_SOURCES})
    target_sources(metalslugxx PRIVATE "${METALSLUGXX_RESOURCE}")
else()
    add_executable(metalslugxx ${METALSLUGXX_SOURCES})
endif()

rexglue_setup_target(metalslugxx)

# Vendored lodepng header (PNG capture in src/metalslugxx_screenshot.cpp).
target_include_directories(metalslugxx PRIVATE
    "${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/lodepng")

# dbghelp: SymFromAddr / StackWalk64 for the native-crash backtrace handler
# (src/metalslugxx_crash.cpp). winmm: timeBeginPeriod for the timer-resolution
# fix (src/metalslugxx_timing.cpp). #pragma comment(lib) covers them too, but be
# explicit.
if(WIN32)
    target_link_libraries(metalslugxx PRIVATE dbghelp winmm)
endif()

# rexglue_configure_target() injects the SDK's rex_app.cpp into THIS target.
# rex_app.cpp needs two include dirs that the SDK only exposes PRIVATE-ly (via
# rexruntime's PRIVATE links to rexcore/imgui in src/kernel/CMakeLists.txt), so
# they are not propagated through rex::runtime to consumers:
#   - <imgui.h>        (thirdparty/imgui; pulled in by console_overlay.h)
#   - <rex/version.h>  (generated by configure_file into the SDK's binary dir;
#                       rexcore exposes it BUILD_INTERFACE but that's PRIVATE here)
# Add both so the injected source compiles. Source-tree consumption only:
# REXSDK_DIR is set, and rexglue.cmake names the SDK binary subtree "rexglue-sdk".
if(REXSDK_DIR)
    target_include_directories(metalslugxx PRIVATE
        "${REXSDK_DIR}/thirdparty/imgui"
        "${CMAKE_BINARY_DIR}/rexglue-sdk/include"
    )
endif()
