115 lines
4.8 KiB
CMake
115 lines
4.8 KiB
CMake
cmake_minimum_required(VERSION 3.21) # PROJECT_IS_TOP_LEVEL
|
|
project(hd2d LANGUAGES C CXX)
|
|
|
|
# Usable standalone or from a parent project via
|
|
# `add_subdirectory(path/to/Vibe-HD-2D hd2d)`; link `hd2d` and load engine
|
|
# shaders from `HD2D_ENGINE_ASSET_DIR` (compile definition and cache variable).
|
|
# The engine provides the `imgui` target (Dear ImGui + GLFW/OpenGL3
|
|
# backends) unless the parent already defines one.
|
|
option(HD2D_BUILD_DEMO "Build the demo" ${PROJECT_IS_TOP_LEVEL})
|
|
set(HD2D_ENGINE_ASSET_DIR "${CMAKE_CURRENT_SOURCE_DIR}/assets" CACHE INTERNAL "Engine shaders and assets")
|
|
|
|
set(CMAKE_CXX_STANDARD 20)
|
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
|
if(PROJECT_IS_TOP_LEVEL)
|
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
|
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
|
|
set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "" FORCE)
|
|
endif()
|
|
endif()
|
|
|
|
include(FetchContent)
|
|
|
|
# --- Dependencies -----------------------------------------------------------
|
|
# System packages are preferred; anything missing is fetched.
|
|
find_package(OpenGL REQUIRED)
|
|
# libepoxy: pkg-config where available (Linux, MSYS2), else a plain header/library search
|
|
# (MSVC + vcpkg ships no pkg-config by default).
|
|
if(NOT TARGET PkgConfig::EPOXY)
|
|
find_package(PkgConfig QUIET)
|
|
if(PkgConfig_FOUND)
|
|
pkg_check_modules(EPOXY QUIET IMPORTED_TARGET epoxy)
|
|
endif()
|
|
if(NOT TARGET PkgConfig::EPOXY)
|
|
find_path(EPOXY_INCLUDE_DIR epoxy/gl.h)
|
|
find_library(EPOXY_LIBRARY NAMES epoxy libepoxy epoxy-0)
|
|
if(NOT EPOXY_INCLUDE_DIR OR NOT EPOXY_LIBRARY)
|
|
message(FATAL_ERROR "libepoxy not found (install it, e.g. pacman -S libepoxy, "
|
|
"pacman -S mingw-w64-ucrt-x86_64-libepoxy or vcpkg install libepoxy)")
|
|
endif()
|
|
add_library(PkgConfig::EPOXY UNKNOWN IMPORTED)
|
|
set_target_properties(PkgConfig::EPOXY PROPERTIES IMPORTED_LOCATION "${EPOXY_LIBRARY}"
|
|
INTERFACE_INCLUDE_DIRECTORIES "${EPOXY_INCLUDE_DIR}")
|
|
endif()
|
|
endif()
|
|
|
|
if(NOT TARGET glfw)
|
|
find_package(glfw3 3.3 QUIET)
|
|
if(NOT glfw3_FOUND)
|
|
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
|
|
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
|
|
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
|
|
FetchContent_Declare(glfw GIT_REPOSITORY https://github.com/glfw/glfw.git GIT_TAG 3.4)
|
|
FetchContent_MakeAvailable(glfw)
|
|
endif()
|
|
endif()
|
|
|
|
if(NOT TARGET glm::glm)
|
|
find_package(glm QUIET)
|
|
if(NOT glm_FOUND)
|
|
FetchContent_Declare(glm GIT_REPOSITORY https://github.com/g-truc/glm.git GIT_TAG 1.0.1)
|
|
FetchContent_MakeAvailable(glm)
|
|
endif()
|
|
endif()
|
|
|
|
find_path(STB_INCLUDE_DIR stb_image.h PATH_SUFFIXES stb)
|
|
if(NOT STB_INCLUDE_DIR)
|
|
FetchContent_Declare(stb GIT_REPOSITORY https://github.com/nothings/stb.git GIT_TAG master)
|
|
FetchContent_MakeAvailable(stb)
|
|
set(STB_INCLUDE_DIR ${stb_SOURCE_DIR})
|
|
endif()
|
|
|
|
find_path(CGLTF_INCLUDE_DIR cgltf.h)
|
|
if(NOT CGLTF_INCLUDE_DIR)
|
|
FetchContent_Declare(cgltf GIT_REPOSITORY https://github.com/jkuhlmann/cgltf.git GIT_TAG v1.14)
|
|
FetchContent_MakeAvailable(cgltf)
|
|
set(CGLTF_INCLUDE_DIR ${cgltf_SOURCE_DIR})
|
|
endif()
|
|
|
|
if(NOT TARGET imgui) # a parent project may provide its own
|
|
FetchContent_Declare(imgui GIT_REPOSITORY https://github.com/ocornut/imgui.git GIT_TAG v1.91.9b)
|
|
FetchContent_MakeAvailable(imgui)
|
|
add_library(imgui STATIC
|
|
${imgui_SOURCE_DIR}/imgui.cpp
|
|
${imgui_SOURCE_DIR}/imgui_draw.cpp
|
|
${imgui_SOURCE_DIR}/imgui_tables.cpp
|
|
${imgui_SOURCE_DIR}/imgui_widgets.cpp
|
|
${imgui_SOURCE_DIR}/backends/imgui_impl_glfw.cpp
|
|
${imgui_SOURCE_DIR}/backends/imgui_impl_opengl3.cpp)
|
|
target_include_directories(imgui PUBLIC ${imgui_SOURCE_DIR} ${imgui_SOURCE_DIR}/backends)
|
|
target_link_libraries(imgui PUBLIC glfw OpenGL::GL)
|
|
endif()
|
|
|
|
# --- Engine -----------------------------------------------------------------
|
|
file(GLOB_RECURSE HD2D_ENGINE_SOURCES CONFIGURE_DEPENDS engine/src/*.cpp)
|
|
add_library(hd2d STATIC ${HD2D_ENGINE_SOURCES})
|
|
target_include_directories(hd2d PUBLIC engine/include)
|
|
target_include_directories(hd2d SYSTEM PRIVATE ${STB_INCLUDE_DIR} ${CGLTF_INCLUDE_DIR}) # silence third-party warnings
|
|
target_link_libraries(hd2d PUBLIC glfw glm::glm PkgConfig::EPOXY imgui OpenGL::GL)
|
|
target_compile_features(hd2d PUBLIC cxx_std_20)
|
|
target_compile_definitions(hd2d PUBLIC GLM_FORCE_RADIANS GLM_ENABLE_EXPERIMENTAL
|
|
HD2D_ENGINE_ASSET_DIR="${HD2D_ENGINE_ASSET_DIR}")
|
|
if(MSVC)
|
|
target_compile_options(hd2d PRIVATE /W4)
|
|
else()
|
|
target_compile_options(hd2d PRIVATE -Wall -Wextra -Wno-unused-parameter)
|
|
endif()
|
|
|
|
# --- Demo -------------------------------------------------------------------
|
|
if(HD2D_BUILD_DEMO)
|
|
add_executable(hd2d_demo demo/main.cpp demo/DemoScene.cpp)
|
|
target_link_libraries(hd2d_demo PRIVATE hd2d)
|
|
# Assets/shaders are loaded relative to this directory at runtime.
|
|
target_compile_definitions(hd2d_demo PRIVATE HD2D_ASSET_DIR="${HD2D_ENGINE_ASSET_DIR}")
|
|
endif()
|