partially done work on total source tree rework, separation of engine context and game context, generalization of renderer for different backends as well as web platform target

This commit is contained in:
2024-09-16 09:07:01 +03:00
parent ca0305feab
commit 551d60ef85
59 changed files with 2892 additions and 890 deletions

View File

@ -3,7 +3,10 @@ cmake_minimum_required(VERSION 3.21)
project(townengine LANGUAGES C)
# SDL dependencies
find_package(SDL2 REQUIRED GLOBAL)
# for whatever reason Emscripten has SDL2 config, but not actual SDL2 port by default
if(NOT EMSCRIPTEN)
find_package(SDL2 REQUIRED GLOBAL)
endif()
# CMake actually has no default configuration and it's toolchain file dependent, set debug if not specified
if(NOT CMAKE_BUILD_TYPE)
@ -19,6 +22,14 @@ set(TWN_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "")
option(TWN_FEATURE_DYNLIB_GAME "Enable dynamic library loading support" ON)
option(TWN_ARCHIVE_DATA "Enable archival of assets" OFF)
# todo: figure out how to compile for dynamic linking instead
if(EMSCRIPTEN)
if(TWN_FEATURE_DYNLIB_GAME)
message(WARNING "TWN_FEATURE_DYNLIB_GAME is set, but not supported - it is turned off")
set(TWN_FEATURE_DYNLIB_GAME OFF CACHE INTERNAL "")
endif()
endif()
# add -fPIC globally so that it's linked well
add_compile_options($<$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>:-fPIC>)
@ -37,8 +48,8 @@ add_subdirectory(third-party/physfs SYSTEM)
add_subdirectory(third-party/libxm SYSTEM)
if(UNIX)
set(SYSTEM_SOURCE_FILES townengine/system/linux/elf.c)
if(LINUX)
set(SYSTEM_SOURCE_FILES src/system/linux/elf.c)
else()
set(SYSTEM_SOURCE_FILES)
endif()
@ -46,22 +57,22 @@ endif()
set(TWN_THIRD_PARTY_SOURCE_FILES
third-party/physfs/extras/physfsrwops.c
third-party/stb/stb_vorbis.c
third-party/glad/src/glad.c)
$<$<NOT:$<BOOL:${EMSCRIPTEN}>>:third-party/glad/src/glad.c>)
set(TWN_SOURCE_FILES
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
townengine/util.c townengine/util.h
townengine/rendering.c townengine/rendering.h
townengine/input/input.c townengine/input.h
townengine/camera.c townengine/camera.h
townengine/textures/textures.c
townengine/twn_game_object.c
src/twn_loop.c
src/twn_main.c
src/twn_context.c include/twn_context.h
src/twn_audio.c include/twn_audio.h
src/twn_util.c include/twn_util.h
src/twn_rendering.c include/twn_rendering.h
src/twn_input.c include/twn_input.h
src/twn_camera.c include/twn_camera.h
src/twn_textures.c include/twn_textures.c
src/twn_game_object.c
$<$<NOT:$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>>:townengine/twn_main.c>
# for dynamic load based solution main is compiled in a separate target
$<$<NOT:$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>>:src/twn_main.c>
${SYSTEM_SOURCE_FILES})
@ -79,13 +90,13 @@ endif()
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
C_STANDARD 11
C_STANDARD_REQUIRED ON
C_EXTENSIONS ON) # extensions are required by stb_ds.h
# precompile commonly used not-so-small headers
target_precompile_headers(${TWN_TARGET} PRIVATE
third-party/glad/include/glad/glad.h
$<$<NOT:$<BOOL:${EMSCRIPTEN}>>:third-party/glad/include/glad/glad.h>
third-party/stb/stb_ds.h)
# distribution definitions, set them with -DX=X in cli
@ -104,7 +115,8 @@ function(give_options_without_warnings target)
-ffp-contract=fast
-fno-signed-zeros
-fno-trapping-math
-freciprocal-math)
-freciprocal-math
$<$<BOOL:${EMSCRIPTEN}>:-sUSE_SDL=2>)
set(BUILD_FLAGS_RELEASE
-O3
@ -114,8 +126,7 @@ function(give_options_without_warnings target)
-fdata-sections
-ffunction-sections
-funroll-loops
-fomit-frame-pointer
$<$<STREQUAL:${CMAKE_C_COMPILER_ID},Gnu>:-fallow-store-data-races>)
-fomit-frame-pointer)
set(BUILD_FLAGS_DEBUG
-O0
@ -124,7 +135,8 @@ function(give_options_without_warnings target)
-fno-omit-frame-pointer
-fstack-protector-all
-fsanitize=undefined
-fsanitize=address)
-fsanitize=address
$<$<BOOL:${EMSCRIPTEN}>:-gsource-map>)
target_compile_options(${target} PRIVATE
${BUILD_FLAGS}
@ -179,9 +191,9 @@ function(include_deps target)
third-party/physfs/src
third-party/physfs/extras
third-party/libxm/include
third-party/glad/include
third-party/stb
third-party/x-watcher)
third-party/x-watcher
$<$<NOT:$<BOOL:${EMSCRIPTEN}>>:third-party/glad/include>)
list(TRANSFORM THIRD_PARTY_INCLUDES PREPEND ${TWN_ROOT_DIR}/)
target_include_directories(${target} SYSTEM PRIVATE ${THIRD_PARTY_INCLUDES})
@ -193,7 +205,7 @@ endfunction()
function(link_deps target)
target_link_libraries(${target} PUBLIC
SDL2::SDL2
$<$<NOT:$<BOOL:${EMSCRIPTEN}>>:SDL2::SDL2>
physfs-static
xms)
endfunction()
@ -205,14 +217,14 @@ function(use_townengine target sources output_directory data_dir)
add_library(${target}_game SHARED ${sources})
give_options(${target}_game)
include_deps(${target}_game)
target_link_libraries(${target}_game PUBLIC SDL2::SDL2 ${TWN_TARGET})
target_link_libraries(${target}_game PUBLIC $<$<NOT:$<BOOL:${EMSCRIPTEN}>>: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 ${TWN_ROOT_DIR}/townengine/twn_main.c)
add_executable(${target}_app ${TWN_ROOT_DIR}/src/twn_main.c)
# todo: copy instead?
# put libtownengine.so alongside the binary
@ -225,7 +237,16 @@ function(use_townengine target sources output_directory data_dir)
set_target_properties(${target}_app PROPERTIES
OUTPUT_NAME launcher
LIBRARY_OUTPUT_DIRECTORY ${output_directory})
LIBRARY_OUTPUT_DIRECTORY ${output_directory}
RUNTIME_OUTPUT_DIRECTORY ${output_directory})
if(EMSCRIPTEN)
set_target_properties(${target}_app PROPERTIES
SUFFIX .html)
endif()
target_compile_options(${target}_app PRIVATE
$<$<BOOL:${EMSCRIPTEN}>:--shell-file ${TWN_ROOT_DIR}/shell_minimal.html>)
# system libraries
find_library(MATH_LIBRARY m)
@ -236,10 +257,8 @@ 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 ${TWN_TARGET})
set_target_properties(${target}_app PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${output_directory})
target_link_libraries(${target}_app PUBLIC ${TWN_TARGET})
if(WIN32)
# copy dlls for baby windows
add_custom_command(TARGET ${target}_app POST_BUILD
@ -297,5 +316,5 @@ endif()
# move compie_commands.json into root directory so that it plays nicer with code editors without any configuration
add_custom_target(copy-compile-commands ALL
${CMAKE_COMMAND} -E copy_if_different ${CMAKE_BINARY_DIR}/compile_commands.json
${TWN_ROOT_DIR})
${CMAKE_COMMAND} -E copy_if_different ${CMAKE_BINARY_DIR}/compile_commands.json
${TWN_ROOT_DIR})