From ed93072371fae6557c29e46370032c4621f3853e Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Sun, 13 Oct 2024 19:04:23 +0300 Subject: [PATCH] twn_amalgam.c: a way for single unit compilation, controlled with -DTWN_USE_AMALGAM in cmake --- CMakeLists.txt | 12 ++++++++---- src/twn_amalgam.c | 19 +++++++++++++++++++ src/twn_stb.c | 24 ++++++++++++++++++++++++ src/twn_textures.c | 2 -- src/twn_util.c | 16 ---------------- 5 files changed, 51 insertions(+), 22 deletions(-) create mode 100644 src/twn_amalgam.c create mode 100644 src/twn_stb.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 5aa1fea..7acdb6c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ set(TWN_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "") # feature configuration, set them with -DFEATURE=ON/OFF in cli option(TWN_FEATURE_DYNLIB_GAME "Enable dynamic library loading support" ON) +option(TWN_USE_AMALGAM "Enable use of twn_amalgam.c as a single compilation unit" ON) # todo: figure out how to compile for dynamic linking instead if(EMSCRIPTEN) @@ -88,7 +89,8 @@ set(TWN_THIRD_PARTY_SOURCE_FILES third-party/tomlc99/toml.c $<$>:third-party/glad/src/glad.c>) -set(TWN_SOURCE_FILES +set(TWN_NONOPT_SOURCE_FILES + src/twn_stb.c src/twn_loop.c src/twn_main.c src/twn_context.c include/twn_context.h @@ -104,7 +106,10 @@ set(TWN_SOURCE_FILES src/rendering/twn_triangles.c src/rendering/twn_circles.c src/rendering/twn_skybox.c - src/rendering/twn_fog.c + src/rendering/twn_fog.c) + +set(TWN_SOURCE_FILES + $,src/twn_amalgam.c src/twn_stb.c,${TWN_NONOPT_SOURCE_FILES}> # for dynamic load based solution main is compiled in a separate target $<$>:src/twn_main.c @@ -113,6 +118,7 @@ set(TWN_SOURCE_FILES ${SYSTEM_SOURCE_FILES}) list(TRANSFORM TWN_SOURCE_FILES PREPEND ${TWN_ROOT_DIR}/) +source_group(TREE ${TWN_ROOT_DIR} FILES ${TWN_NONOPT_SOURCE_FILES}) add_library(twn_third_parties STATIC ${TWN_THIRD_PARTY_SOURCE_FILES}) @@ -123,8 +129,6 @@ else() add_library(${TWN_TARGET} STATIC ${TWN_SOURCE_FILES} ${twn_third_parties}) endif() -source_group(TREE ${TWN_ROOT_DIR} FILES ${TWN_SOURCE_FILES}) - set_target_properties(${TWN_TARGET} PROPERTIES C_STANDARD 11 C_STANDARD_REQUIRED ON diff --git a/src/twn_amalgam.c b/src/twn_amalgam.c new file mode 100644 index 0000000..b240d93 --- /dev/null +++ b/src/twn_amalgam.c @@ -0,0 +1,19 @@ +/* a technique for faster compilation */ +/* it includes all non-optional .c files directly in a single compilation unit */ + +#include "twn_audio.c" +#include "twn_camera.c" +#include "twn_context.c" +#include "twn_input.c" +#include "twn_loop.c" +#include "twn_main.c" +#include "twn_textures.c" +#include "twn_util.c" + +#include "rendering/twn_circles.c" +#include "rendering/twn_draw.c" +#include "rendering/twn_fog.c" +#include "rendering/twn_skybox.c" +#include "rendering/twn_sprites.c" +#include "rendering/twn_text.c" +#include "rendering/twn_triangles.c" diff --git a/src/twn_stb.c b/src/twn_stb.c new file mode 100644 index 0000000..3198ee2 --- /dev/null +++ b/src/twn_stb.c @@ -0,0 +1,24 @@ +/* single compilation unit for every stb implementation */ + +#define STB_DS_IMPLEMENTATION +#define STBDS_ASSERT SDL_assert +#define STBDS_REALLOC(context,ptr,size) ((void)(context), SDL_realloc(ptr, size)) +#define STBDS_FREE(context,ptr) ((void)(context), SDL_free(ptr)) +#include + +#define STB_RECT_PACK_IMPLEMENTATION +#define STBRP_SORT SDL_qsort +#define STBRP_ASSERT SDL_assert +#include + +#define STB_TRUETYPE_IMPLEMENTATION +#define STBTT_malloc(x,u) ((void)(u), SDL_malloc(x)) +#define STBTT_free(x,u) ((void)(u), SDL_free(x)) +#define STBTT_assert(x) SDL_assert(x) +#define STBTT_strlen(x) SDL_strlen(x) +#define STBTT_memcpy SDL_memcpy +#define STBTT_memset SDL_memset +#include + +#define STB_IMAGE_IMPLEMENTATION +#include diff --git a/src/twn_textures.c b/src/twn_textures.c index 77ad865..a146f1d 100644 --- a/src/twn_textures.c +++ b/src/twn_textures.c @@ -8,8 +8,6 @@ #include #include #include - -#define STB_IMAGE_IMPLEMENTATION #include #include diff --git a/src/twn_util.c b/src/twn_util.c index 49fdef2..b11e60b 100644 --- a/src/twn_util.c +++ b/src/twn_util.c @@ -4,23 +4,7 @@ #include #include -#define STB_DS_IMPLEMENTATION -#define STBDS_ASSERT SDL_assert -#define STBDS_REALLOC(context,ptr,size) ((void)(context), SDL_realloc(ptr, size)) -#define STBDS_FREE(context,ptr) ((void)(context), SDL_free(ptr)) #include -#define STB_RECT_PACK_IMPLEMENTATION -#define STBRP_SORT SDL_qsort -#define STBRP_ASSERT SDL_assert -#include -#define STB_TRUETYPE_IMPLEMENTATION -#define STBTT_malloc(x,u) ((void)(u), SDL_malloc(x)) -#define STBTT_free(x,u) ((void)(u), SDL_free(x)) -#define STBTT_assert(x) SDL_assert(x) -#define STBTT_strlen(x) SDL_strlen(x) -#define STBTT_memcpy SDL_memcpy -#define STBTT_memset SDL_memset -#include #include