windows build compatibility, twn_game_object as abstraction for bridging game code and the engine

This commit is contained in:
veclavtalica
2024-08-27 00:33:37 +03:00
parent 9892bf71dc
commit 2f36d9ea3c
19 changed files with 454 additions and 261 deletions

View File

@ -2,10 +2,6 @@ cmake_minimum_required(VERSION 3.21)
project(townengine LANGUAGES C)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# dependencies
find_package(SDL2 REQUIRED GLOBAL)
find_package(SDL2_image REQUIRED GLOBAL)
@ -15,14 +11,14 @@ if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif ()
set(TOWNENGINE_TARGET townengine CACHE INTERNAL "")
set(TOWNENGINE_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "")
set(TWN_TARGET townengine CACHE INTERNAL "")
set(TWN_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "")
option(TOWNENGINE_HOT_RELOAD "Enable hot reloading support" ON)
option(TOWNENGINE_ARCHIVE_DATA "Enable archival of assets" OFF)
option(TWN_FEATURE_DYNLIB_GAME "Enable dynamic library loading support" ON)
option(TWN_ARCHIVE_DATA "Enable archival of assets" OFF)
# add -fPIC globally so that it's linked well
add_compile_options($<$<BOOL:${TOWNENGINE_HOT_RELOAD}>:-fPIC>)
add_compile_options($<$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>:-fPIC>)
set(PHYSFS_BUILD_SHARED FALSE)
set(PHYSFS_DISABLE_INSTALL TRUE)
@ -45,12 +41,13 @@ else()
set(SYSTEM_SOURCE_FILES)
endif()
set(TOWNENGINE_SOURCE_FILES
set(TWN_SOURCE_FILES
third-party/physfs/extras/physfsrwops.c
third-party/stb/stb_vorbis.c
third-party/glad/src/glad.c
townengine/main.c
townengine/twn_loop.c
townengine/twn_main.c
townengine/config.h
townengine/context/context.c townengine/context.h
townengine/audio/audio.c townengine/audio.h
@ -59,25 +56,29 @@ set(TOWNENGINE_SOURCE_FILES
townengine/input/input.c townengine/input.h
townengine/camera.c townengine/camera.h
townengine/textures/textures.c
townengine/twn_game_object.c
$<$<NOT:$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>>:townengine/twn_main.c>
${SYSTEM_SOURCE_FILES})
list(TRANSFORM TOWNENGINE_SOURCE_FILES PREPEND ${TOWNENGINE_ROOT_DIR}/)
list(TRANSFORM TWN_SOURCE_FILES PREPEND ${TWN_ROOT_DIR}/)
# base engine code, reused for games and tools
if (TOWNENGINE_HOT_RELOAD)
add_library(${TOWNENGINE_TARGET} SHARED ${TOWNENGINE_SOURCE_FILES})
if (TWN_FEATURE_DYNLIB_GAME)
add_library(${TWN_TARGET} SHARED ${TWN_SOURCE_FILES})
else ()
add_library(${TOWNENGINE_TARGET} STATIC ${TOWNENGINE_SOURCE_FILES})
add_library(${TWN_TARGET} STATIC ${TWN_SOURCE_FILES})
endif ()
source_group(TREE ${TOWNENGINE_ROOT_DIR} FILES ${TOWNENGINE_SOURCE_FILES})
set_target_properties(${TOWNENGINE_TARGET} PROPERTIES
source_group(TREE ${TWN_ROOT_DIR} FILES ${TWN_SOURCE_FILES})
set_target_properties(${TWN_TARGET} PROPERTIES
C_STANDARD 11
C_STANDARD_REQUIRED ON
C_EXTENSIONS ON) # extensions are required by stb_ds.h
target_precompile_headers(${TOWNENGINE_TARGET} PRIVATE
target_precompile_headers(${TWN_TARGET} PRIVATE
third-party/glad/include/glad/glad.h
third-party/stb/stb_ds.h)
@ -163,7 +164,7 @@ function(give_options target)
ORGANIZATION_NAME="${ORGANIZATION_NAME}"
APP_NAME="${APP_NAME}"
PACKAGE_EXTENSION="${PACKAGE_EXTENSION}"
$<$<BOOL:${TOWNENGINE_HOT_RELOAD}>:HOT_RELOAD_SUPPORT>)
$<$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>:TWN_FEATURE_DYNLIB_GAME>)
endfunction()
@ -178,18 +179,17 @@ function(include_deps target)
third-party/stb
third-party/x-watcher)
list(TRANSFORM THIRD_PARTY_INCLUDES PREPEND ${TOWNENGINE_ROOT_DIR}/)
list(TRANSFORM THIRD_PARTY_INCLUDES PREPEND ${TWN_ROOT_DIR}/)
target_include_directories(${target} SYSTEM PRIVATE ${THIRD_PARTY_INCLUDES})
# allow access to headers from any point in source tree
target_include_directories(${target} PRIVATE ${TOWNENGINE_ROOT_DIR})
target_include_directories(${target} PRIVATE ${TWN_ROOT_DIR})
endfunction()
function(link_deps target)
target_link_libraries(${target} PUBLIC
SDL2::SDL2
SDL2::SDL2main
SDL2_image::SDL2_image
SDL2_ttf::SDL2_ttf
physfs-static
@ -198,22 +198,24 @@ endfunction()
function(use_townengine target sources output_directory data_dir)
if (TOWNENGINE_HOT_RELOAD)
if (TWN_FEATURE_DYNLIB_GAME)
# 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)
target_link_libraries(${target}_game PUBLIC SDL2::SDL2 ${TWN_TARGET})
set_target_properties(${target}_game PROPERTIES
OUTPUT_NAME game
LIBRARY_OUTPUT_DIRECTORY ${output_directory}
RUNTIME_OUTPUT_DIRECTORY ${output_directory})
# launcher binary, loads game and engine shared library
add_executable(${target}_app ${TOWNENGINE_ROOT_DIR}/townengine/null.c)
add_executable(${target}_app ${TWN_ROOT_DIR}/townengine/twn_main.c)
# todo: copy instead?
# put libtownengine.so alongside the binary
set_target_properties(${TOWNENGINE_TARGET} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${output_directory})
set_target_properties(${TWN_TARGET} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${output_directory})
else ()
add_executable(${target}_app ${sources})
endif ()
@ -231,42 +233,51 @@ function(use_townengine target sources output_directory data_dir)
give_options(${target}_app)
include_deps(${target}_app)
link_deps(${target}_app)
target_link_libraries(${target}_app PUBLIC ${TOWNENGINE_TARGET})
target_link_libraries(${target}_app PUBLIC ${TWN_TARGET})
set_target_properties(${target}_app PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${output_directory})
# copy dlls for baby windows
add_custom_command(TARGET ${target}_app POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy -t $<TARGET_FILE_DIR:${target}_app>
$<TARGET_RUNTIME_DLLS:${target}_app>
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_RUNTIME_DLLS:${target}_app>
$<TARGET_FILE_DIR:${target}_app>
COMMAND_EXPAND_LISTS)
if (UNIX)
# create a bootstrapping script
set(TOWNENGINE_BOOTSTRAP_EXEC_ARGS
"$<IF:$<BOOL:${TOWNENGINE_ARCHIVE_DATA}>,--data-dir ./data.${PACKAGE_EXTENSION},--data-dir ${data_dir}>")
set(TWN_BOOTSTRAP_EXEC_ARGS
"$<IF:$<BOOL:${TWN_ARCHIVE_DATA}>,--data-dir ./data.${PACKAGE_EXTENSION},--data-dir ${data_dir}>")
string(JOIN "\n" TOWNENGINE_BOOTSTRAP
"#!/bin/env sh"
"cd \"$(dirname \"$0\")\""
"LD_LIBRARY_PATH=./ ./launcher ${TOWNENGINE_BOOTSTRAP_EXEC_ARGS}"
string(JOIN "\n" TWN_BOOTSTRAP_SH
"#!/bin/env sh"
"cd \"$(dirname \"$0\")\""
"LD_LIBRARY_PATH=./ ./launcher ${TWN_BOOTSTRAP_EXEC_ARGS}"
"")
FILE(GENERATE OUTPUT ${output_directory}/${target}
CONTENT "${TWN_BOOTSTRAP_SH}"
FILE_PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ)
if (WIN32)
string(JOIN "\n" TWN_BOOTSTRAP_BAT
"pushd \"%~dp0\""
"launcher ${TWN_BOOTSTRAP_EXEC_ARGS}"
"")
FILE(GENERATE OUTPUT ${output_directory}/${target}
CONTENT "${TOWNENGINE_BOOTSTRAP}"
FILE(GENERATE OUTPUT ${output_directory}/${target}.bat
CONTENT "${TWN_BOOTSTRAP_BAT}"
FILE_PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ)
endif ()
if (TOWNENGINE_ARCHIVE_DATA)
if (TWN_ARCHIVE_DATA)
# zip up assets
add_custom_command(TARGET ${target}_app POST_BUILD
COMMAND cd ${data_dir} && zip -r ${output_directory}/data.${PACKAGE_EXTENSION} ./*
COMMAND cd ${data_dir} && ${CMAKE_COMMAND} -E tar "cf" ${output_directory}/data.${PACKAGE_EXTENSION} --format=zip ./
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
endif ()
endfunction()
give_options(${TOWNENGINE_TARGET})
include_deps(${TOWNENGINE_TARGET})
link_deps(${TOWNENGINE_TARGET})
give_options(${TWN_TARGET})
include_deps(${TWN_TARGET})
link_deps(${TWN_TARGET})
# build the testgame if this cmake list is built directly
if (${CMAKE_PROJECT_NAME} MATCHES townengine)
@ -274,6 +285,7 @@ if (${CMAKE_PROJECT_NAME} MATCHES townengine)
endif ()
# move compie_commands.json into root directory so that it plays nicer with editors
add_custom_command(TARGET ${TOWNENGINE_TARGET} POST_BUILD
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_custom_command(TARGET ${TWN_TARGET} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_BINARY_DIR}/compile_commands.json
${TOWNENGINE_ROOT_DIR})
${TWN_ROOT_DIR})