2024-07-08 00:44:20 +00:00
|
|
|
cmake_minimum_required(VERSION 3.21)
|
|
|
|
|
2024-07-29 10:06:23 +00:00
|
|
|
project(townengine LANGUAGES C)
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
|
|
|
|
|
2024-07-08 00:56:00 +00:00
|
|
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
|
|
|
|
2024-07-08 00:44:20 +00:00
|
|
|
# dependencies
|
2024-07-29 22:20:30 +00:00
|
|
|
find_package(SDL2 REQUIRED GLOBAL)
|
|
|
|
find_package(SDL2_image REQUIRED GLOBAL)
|
|
|
|
find_package(SDL2_ttf REQUIRED GLOBAL)
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-07-08 16:13:53 +00:00
|
|
|
if (NOT CMAKE_BUILD_TYPE)
|
|
|
|
set(CMAKE_BUILD_TYPE Debug)
|
|
|
|
endif ()
|
|
|
|
|
2024-07-29 22:20:30 +00:00
|
|
|
set(TOWNENGINE_TARGET townengine CACHE INTERNAL "")
|
2024-08-22 03:38:31 +00:00
|
|
|
set(TOWNENGINE_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "")
|
2024-07-08 16:13:53 +00:00
|
|
|
|
2024-08-22 15:36:04 +00:00
|
|
|
option(TOWNENGINE_HOT_RELOAD "Enable hot reloading support" ON)
|
|
|
|
option(TOWNENGINE_ARCHIVE_DATA "Enable archival of assets" OFF)
|
|
|
|
|
|
|
|
# add -fPIC globally so that it's linked well
|
2024-08-21 13:55:34 +00:00
|
|
|
add_compile_options($<$<BOOL:${TOWNENGINE_HOT_RELOAD}>:-fPIC>)
|
|
|
|
|
2024-07-08 00:44:20 +00:00
|
|
|
set(PHYSFS_BUILD_SHARED FALSE)
|
|
|
|
set(PHYSFS_DISABLE_INSTALL TRUE)
|
|
|
|
set(PHYSFS_TARGETNAME_UNINSTALL "physfs_uninstall")
|
2024-08-20 14:15:12 +00:00
|
|
|
set(PHYSFS_ARCHIVE_GRP OFF)
|
|
|
|
set(PHYSFS_ARCHIVE_WAD OFF)
|
|
|
|
set(PHYSFS_ARCHIVE_HOG OFF)
|
|
|
|
set(PHYSFS_ARCHIVE_MVL OFF)
|
|
|
|
set(PHYSFS_ARCHIVE_QPAK OFF)
|
|
|
|
set(PHYSFS_ARCHIVE_SLB OFF)
|
|
|
|
set(PHYSFS_ARCHIVE_ISO9660 OFF)
|
|
|
|
set(PHYSFS_ARCHIVE_VDF OFF)
|
2024-07-08 00:44:20 +00:00
|
|
|
add_subdirectory(third-party/physfs)
|
2024-07-08 13:58:23 +00:00
|
|
|
add_subdirectory(third-party/libxm)
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
|
2024-07-27 22:44:39 +00:00
|
|
|
if(UNIX)
|
2024-08-21 13:55:34 +00:00
|
|
|
set(SYSTEM_SOURCE_FILES townengine/system/linux/elf.c)
|
2024-07-27 22:44:39 +00:00
|
|
|
else()
|
|
|
|
set(SYSTEM_SOURCE_FILES)
|
|
|
|
endif()
|
|
|
|
|
2024-07-29 19:53:26 +00:00
|
|
|
set(TOWNENGINE_SOURCE_FILES
|
2024-07-08 00:44:20 +00:00
|
|
|
third-party/physfs/extras/physfsrwops.c
|
2024-07-08 06:46:12 +00:00
|
|
|
third-party/stb/stb_vorbis.c
|
2024-07-09 12:35:54 +00:00
|
|
|
third-party/glad/src/glad.c
|
2024-07-08 06:46:12 +00:00
|
|
|
|
2024-07-30 19:31:18 +00:00
|
|
|
townengine/main.c
|
|
|
|
townengine/config.h
|
2024-08-21 13:55:34 +00:00
|
|
|
townengine/context/context.c townengine/context.h
|
|
|
|
townengine/audio/audio.c townengine/audio.h
|
2024-07-30 19:31:18 +00:00
|
|
|
townengine/util.c townengine/util.h
|
|
|
|
townengine/rendering.c townengine/rendering.h
|
2024-08-21 13:55:34 +00:00
|
|
|
townengine/input/input.c townengine/input.h
|
2024-07-30 19:31:18 +00:00
|
|
|
townengine/text.c townengine/text.h
|
|
|
|
townengine/camera.c townengine/camera.h
|
|
|
|
townengine/textures/textures.c
|
2024-07-29 19:53:26 +00:00
|
|
|
|
2024-08-21 13:55:34 +00:00
|
|
|
${SYSTEM_SOURCE_FILES})
|
|
|
|
|
2024-08-22 03:38:31 +00:00
|
|
|
list(TRANSFORM TOWNENGINE_SOURCE_FILES PREPEND ${TOWNENGINE_ROOT_DIR}/)
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-07-29 19:53:26 +00:00
|
|
|
# base engine code, reused for games and tools
|
2024-08-21 13:55:34 +00:00
|
|
|
if (TOWNENGINE_HOT_RELOAD)
|
|
|
|
add_library(${TOWNENGINE_TARGET} SHARED ${TOWNENGINE_SOURCE_FILES})
|
|
|
|
else ()
|
|
|
|
add_library(${TOWNENGINE_TARGET} STATIC ${TOWNENGINE_SOURCE_FILES})
|
|
|
|
endif ()
|
2024-08-22 03:38:31 +00:00
|
|
|
source_group(TREE ${TOWNENGINE_ROOT_DIR} FILES ${TOWNENGINE_SOURCE_FILES})
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-07-29 22:20:30 +00:00
|
|
|
set_target_properties(${TOWNENGINE_TARGET} PROPERTIES
|
|
|
|
C_STANDARD 11
|
|
|
|
C_STANDARD_REQUIRED ON
|
|
|
|
C_EXTENSIONS ON) # extensions are required by stb_ds.h
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-08-20 14:25:01 +00:00
|
|
|
target_precompile_headers(${TOWNENGINE_TARGET} PRIVATE
|
|
|
|
third-party/glad/include/glad/glad.h
|
2024-08-21 13:55:34 +00:00
|
|
|
third-party/stb/stb_ds.h)
|
2024-08-20 14:25:01 +00:00
|
|
|
|
2024-07-08 00:44:20 +00:00
|
|
|
# distribution definitions
|
|
|
|
set(ORGANIZATION_NAME "wanp" CACHE STRING
|
|
|
|
"App developer/publisher's identifier")
|
2024-07-29 10:06:23 +00:00
|
|
|
set(APP_NAME "townengine" CACHE STRING
|
2024-07-08 00:44:20 +00:00
|
|
|
"App identifier")
|
|
|
|
set(PACKAGE_EXTENSION "btw" CACHE STRING
|
|
|
|
"File extension used to look for data archives")
|
|
|
|
|
2024-07-29 19:53:26 +00:00
|
|
|
function(give_options target)
|
|
|
|
if(MSVC)
|
|
|
|
# close enough i say
|
|
|
|
target_compile_options(${target} PRIVATE
|
|
|
|
/W4
|
|
|
|
$<$<CONFIG:Release>:/GL>)
|
|
|
|
|
|
|
|
target_link_options(${target} PRIVATE
|
|
|
|
$<$<CONFIG:Release>:/LTCG>)
|
|
|
|
|
|
|
|
else()
|
|
|
|
set(WARNING_FLAGS_CLANG
|
|
|
|
-Weverything
|
|
|
|
-Wno-padded
|
|
|
|
-Wno-declaration-after-statement
|
|
|
|
-Wno-unsafe-buffer-usage
|
|
|
|
-Wno-unused-command-line-argument)
|
|
|
|
|
|
|
|
set(WARNING_FLAGS
|
|
|
|
-Wall
|
|
|
|
-Wextra
|
|
|
|
-Wpedantic
|
|
|
|
-Wshadow
|
|
|
|
-Wdouble-promotion
|
|
|
|
-Wconversion -Wno-sign-conversion
|
|
|
|
-Werror=vla
|
|
|
|
$<$<STREQUAL:${CMAKE_C_COMPILER_ID},Gnu>:-Wcast-align=strict>
|
|
|
|
$<$<STREQUAL:${CMAKE_C_COMPILER_ID},Clang>:${WARNING_FLAGS_CLANG}>)
|
|
|
|
|
|
|
|
set(BUILD_FLAGS
|
|
|
|
# these SHOULDN'T break anything...
|
|
|
|
-fno-math-errno
|
|
|
|
-ffp-contract=fast
|
|
|
|
-fno-signed-zeros
|
|
|
|
-fno-trapping-math
|
2024-08-21 18:45:49 +00:00
|
|
|
-freciprocal-math)
|
2024-07-29 19:53:26 +00:00
|
|
|
|
|
|
|
set(BUILD_FLAGS_RELEASE
|
|
|
|
-O3
|
|
|
|
-flto
|
|
|
|
-mavx -mavx2
|
|
|
|
-Wl,--gc-sections
|
|
|
|
-fdata-sections
|
|
|
|
-ffunction-sections
|
|
|
|
-funroll-loops
|
|
|
|
-fomit-frame-pointer
|
|
|
|
$<$<STREQUAL:${CMAKE_C_COMPILER_ID},Gnu>:-fallow-store-data-races>)
|
|
|
|
|
|
|
|
set(BUILD_FLAGS_DEBUG
|
|
|
|
-O0
|
|
|
|
-g3
|
|
|
|
-gdwarf
|
|
|
|
-fno-omit-frame-pointer
|
|
|
|
-fstack-protector-all
|
|
|
|
-fsanitize=undefined
|
|
|
|
-fsanitize=address)
|
|
|
|
|
|
|
|
target_compile_options(${target} PRIVATE
|
|
|
|
${WARNING_FLAGS}
|
|
|
|
${BUILD_FLAGS}
|
|
|
|
$<$<CONFIG:Release>:${BUILD_FLAGS_RELEASE}>
|
|
|
|
$<$<CONFIG:Debug>:${BUILD_FLAGS_DEBUG}>)
|
|
|
|
|
|
|
|
target_link_options(${target} PRIVATE
|
|
|
|
${BUILD_FLAGS}
|
|
|
|
$<$<CONFIG:Release>:${BUILD_FLAGS_RELEASE}>
|
|
|
|
$<$<CONFIG:Debug>:${BUILD_FLAGS_DEBUG}>)
|
|
|
|
|
|
|
|
endif()
|
|
|
|
|
|
|
|
target_compile_definitions(${target} PRIVATE
|
|
|
|
ORGANIZATION_NAME="${ORGANIZATION_NAME}"
|
|
|
|
APP_NAME="${APP_NAME}"
|
2024-08-21 13:55:34 +00:00
|
|
|
PACKAGE_EXTENSION="${PACKAGE_EXTENSION}"
|
|
|
|
$<$<BOOL:${TOWNENGINE_HOT_RELOAD}>:HOT_RELOAD_SUPPORT>)
|
2024-07-29 19:53:26 +00:00
|
|
|
endfunction()
|
|
|
|
|
|
|
|
|
2024-08-21 13:55:34 +00:00
|
|
|
function(include_deps target)
|
2024-07-29 19:53:26 +00:00
|
|
|
# header-only libraries should be marked as "system includes"
|
|
|
|
# to suppress compiler warnings in their code (it's not my problem after all)
|
2024-07-29 22:20:30 +00:00
|
|
|
set(THIRD_PARTY_INCLUDES
|
2024-07-29 19:53:26 +00:00
|
|
|
third-party/physfs/src
|
|
|
|
third-party/physfs/extras
|
|
|
|
third-party/libxm/include
|
|
|
|
third-party/glad/include
|
|
|
|
third-party/stb
|
2024-08-21 13:55:34 +00:00
|
|
|
third-party/x-watcher)
|
|
|
|
|
2024-08-22 03:38:31 +00:00
|
|
|
list(TRANSFORM THIRD_PARTY_INCLUDES PREPEND ${TOWNENGINE_ROOT_DIR}/)
|
2024-07-29 22:20:30 +00:00
|
|
|
target_include_directories(${target} SYSTEM PRIVATE ${THIRD_PARTY_INCLUDES})
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-07-29 20:40:15 +00:00
|
|
|
# allow access to headers from any point in source tree
|
2024-08-22 03:38:31 +00:00
|
|
|
target_include_directories(${target} PRIVATE ${TOWNENGINE_ROOT_DIR})
|
2024-08-21 13:55:34 +00:00
|
|
|
endfunction()
|
|
|
|
|
2024-07-29 20:40:15 +00:00
|
|
|
|
2024-08-21 13:55:34 +00:00
|
|
|
function(link_deps target)
|
2024-07-29 19:53:26 +00:00
|
|
|
target_link_libraries(${target} PUBLIC
|
|
|
|
SDL2::SDL2
|
|
|
|
SDL2::SDL2main
|
|
|
|
SDL2_image::SDL2_image
|
|
|
|
SDL2_ttf::SDL2_ttf
|
|
|
|
physfs-static
|
2024-08-21 13:55:34 +00:00
|
|
|
xms)
|
2024-07-29 19:53:26 +00:00
|
|
|
endfunction()
|
|
|
|
|
|
|
|
|
2024-08-21 18:38:24 +00:00
|
|
|
function(use_townengine target sources output_directory data_dir)
|
2024-08-21 13:55:34 +00:00
|
|
|
if (TOWNENGINE_HOT_RELOAD)
|
2024-08-21 18:38:24 +00:00
|
|
|
# game shared library, for reloading
|
|
|
|
add_library(${target}_game SHARED ${sources})
|
|
|
|
set_target_properties(${target}_game PROPERTIES
|
|
|
|
OUTPUT_NAME game
|
|
|
|
LIBRARY_OUTPUT_DIRECTORY ${output_directory})
|
|
|
|
give_options(${target}_game)
|
|
|
|
include_deps(${target}_game)
|
|
|
|
target_link_libraries(${target}_game PUBLIC SDL2::SDL2)
|
|
|
|
|
|
|
|
# launcher binary, loads game and engine shared library
|
2024-08-22 03:38:31 +00:00
|
|
|
add_executable(${target}_app ${TOWNENGINE_ROOT_DIR}/townengine/null.c)
|
2024-08-21 18:38:24 +00:00
|
|
|
|
|
|
|
# put libtownengine.so alongside the binary
|
2024-08-21 14:29:06 +00:00
|
|
|
set_target_properties(${TOWNENGINE_TARGET} PROPERTIES
|
2024-08-21 18:38:24 +00:00
|
|
|
LIBRARY_OUTPUT_DIRECTORY ${output_directory})
|
2024-08-21 13:55:34 +00:00
|
|
|
else ()
|
2024-08-21 18:38:24 +00:00
|
|
|
add_executable(${target}_app ${sources})
|
2024-08-21 13:55:34 +00:00
|
|
|
endif ()
|
2024-08-20 14:50:50 +00:00
|
|
|
|
2024-08-22 16:49:30 +00:00
|
|
|
set_target_properties(${target}_app PROPERTIES
|
|
|
|
OUTPUT_NAME launcher
|
|
|
|
LIBRARY_OUTPUT_DIRECTORY ${output_directory})
|
|
|
|
|
2024-07-29 19:53:26 +00:00
|
|
|
# system libraries
|
|
|
|
find_library(MATH_LIBRARY m)
|
2024-08-21 13:55:34 +00:00
|
|
|
if (MATH_LIBRARY)
|
2024-08-21 18:38:24 +00:00
|
|
|
target_link_libraries(${target}_app PUBLIC ${MATH_LIBRARY})
|
2024-08-21 13:55:34 +00:00
|
|
|
endif ()
|
2024-07-29 19:53:26 +00:00
|
|
|
|
2024-08-21 18:38:24 +00:00
|
|
|
give_options(${target}_app)
|
|
|
|
include_deps(${target}_app)
|
|
|
|
link_deps(${target}_app)
|
|
|
|
target_link_libraries(${target}_app PUBLIC ${TOWNENGINE_TARGET})
|
|
|
|
set_target_properties(${target}_app PROPERTIES
|
2024-08-22 03:38:31 +00:00
|
|
|
RUNTIME_OUTPUT_DIRECTORY ${output_directory})
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-07-29 19:53:26 +00:00
|
|
|
# copy dlls for baby windows
|
2024-08-21 18:38:24 +00:00
|
|
|
add_custom_command(TARGET ${target}_app POST_BUILD
|
2024-08-22 03:38:31 +00:00
|
|
|
COMMAND ${CMAKE_COMMAND} -E copy -t $<TARGET_FILE_DIR:${target}_app>
|
|
|
|
$<TARGET_RUNTIME_DLLS:${target}_app>
|
|
|
|
COMMAND_EXPAND_LISTS)
|
2024-08-21 18:38:24 +00:00
|
|
|
|
|
|
|
if (UNIX)
|
|
|
|
# create a bootstrapping script
|
2024-08-22 15:36:04 +00:00
|
|
|
set(TOWNENGINE_BOOTSTRAP_EXEC_ARGS
|
|
|
|
"$<IF:$<BOOL:${TOWNENGINE_ARCHIVE_DATA}>,--data-dir ./data.${PACKAGE_EXTENSION},--data-dir ${data_dir}>")
|
|
|
|
|
2024-08-21 18:38:24 +00:00
|
|
|
string(JOIN "\n" TOWNENGINE_BOOTSTRAP
|
|
|
|
"#!/bin/env sh"
|
|
|
|
"cd \"$(dirname \"$0\")\""
|
2024-08-22 15:36:04 +00:00
|
|
|
"LD_LIBRARY_PATH=./ ./launcher ${TOWNENGINE_BOOTSTRAP_EXEC_ARGS}"
|
2024-08-21 18:40:32 +00:00
|
|
|
"")
|
2024-08-21 18:38:24 +00:00
|
|
|
FILE(GENERATE OUTPUT ${output_directory}/${target}
|
|
|
|
CONTENT "${TOWNENGINE_BOOTSTRAP}"
|
|
|
|
FILE_PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ)
|
|
|
|
endif ()
|
2024-08-22 15:36:04 +00:00
|
|
|
|
|
|
|
if (TOWNENGINE_ARCHIVE_DATA)
|
|
|
|
# zip up assets
|
|
|
|
add_custom_command(TARGET ${target}_app POST_BUILD
|
|
|
|
COMMAND cd ${data_dir} && zip -r ${output_directory}/data.${PACKAGE_EXTENSION} ./*
|
|
|
|
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
|
|
|
|
endif ()
|
2024-07-29 19:53:26 +00:00
|
|
|
endfunction()
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-07-29 22:20:30 +00:00
|
|
|
give_options(${TOWNENGINE_TARGET})
|
2024-08-21 13:55:34 +00:00
|
|
|
include_deps(${TOWNENGINE_TARGET})
|
|
|
|
link_deps(${TOWNENGINE_TARGET})
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-08-21 18:38:24 +00:00
|
|
|
# build the testgame if this cmake list is built directly
|
2024-08-20 14:50:50 +00:00
|
|
|
if (${CMAKE_PROJECT_NAME} MATCHES townengine)
|
|
|
|
add_subdirectory(apps/testgame)
|
|
|
|
endif ()
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-08-22 03:38:31 +00:00
|
|
|
# move compie_commands.json into root directory so that it plays nicer with editors
|
|
|
|
add_custom_command(TARGET ${TOWNENGINE_TARGET} POST_BUILD
|
|
|
|
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_BINARY_DIR}/compile_commands.json
|
|
|
|
${TOWNENGINE_ROOT_DIR})
|