From f25e27b10262a78132820454ff6dcd576eec84e1 Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Fri, 21 Feb 2025 19:06:19 +0300 Subject: [PATCH] changes to build system, emscipten progress (can render solid color, yippie!) --- CMakeLists.txt | 5 +++-- apps/demos/bunnymark/CMakeLists.txt | 2 +- apps/demos/scenery/CMakeLists.txt | 2 +- apps/examples/circle-raster/CMakeLists.txt | 2 +- apps/templates/c/CMakeLists.txt | 4 ++-- apps/templates/c/data/twn.toml | 1 + apps/twnlua/CMakeLists.txt | 4 ++-- apps/twnlua/game.c | 1 - apps/twnlua/minilua.c | 2 ++ src/twn_loop.c | 14 ++++++++++++-- src/twn_workers.c | 4 ++++ 11 files changed, 29 insertions(+), 12 deletions(-) create mode 100644 apps/twnlua/minilua.c diff --git a/CMakeLists.txt b/CMakeLists.txt index fb83db8..2cc8149 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -148,6 +148,7 @@ function(give_options_without_warnings target) -fno-signed-zeros -fno-trapping-math -freciprocal-math + # TODO: require packaging for web case $<$:-sUSE_SDL=2>) set(BUILD_FLAGS_RELEASE @@ -169,7 +170,8 @@ function(give_options_without_warnings target) set(LINK_FLAGS -Bsymbolic-functions - $<$:-sLEGACY_GL_EMULATION -sGL_FFP_ONLY -sGL_UNSAFE_OPTS> + $<$:-sLEGACY_GL_EMULATION -sGL_FFP_ONLY -sGL_UNSAFE_OPTS=1> + $<$:--preload-file ${TWN_OUT_DIR}/data@data -sALLOW_MEMORY_GROWTH> $<$>:-Wl,--as-needed> $<$:-Wl,--hash-style=gnu>) @@ -198,7 +200,6 @@ function(give_options_without_warnings target) target_link_options(${target} PUBLIC ${BUILD_FLAGS} ${LINK_FLAGS} - # -Wl,--no-undefined # TODO: use later for implementing no-libc $<$:${LINK_FLAGS_RELEASE}> $<$:${BUILD_FLAGS_DEBUG}>) diff --git a/apps/demos/bunnymark/CMakeLists.txt b/apps/demos/bunnymark/CMakeLists.txt index ab6057a..f60c798 100644 --- a/apps/demos/bunnymark/CMakeLists.txt +++ b/apps/demos/bunnymark/CMakeLists.txt @@ -6,7 +6,7 @@ if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Debug) endif() -add_subdirectory($ENV{TWNROOT} $ENV{TWNROOT}/build) +add_subdirectory($ENV{TWNROOT} ${CMAKE_BINARY_DIR}/twn) set(SOURCE_FILES game.c diff --git a/apps/demos/scenery/CMakeLists.txt b/apps/demos/scenery/CMakeLists.txt index 1c85cfe..c2079fa 100644 --- a/apps/demos/scenery/CMakeLists.txt +++ b/apps/demos/scenery/CMakeLists.txt @@ -6,7 +6,7 @@ if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Debug) endif() -add_subdirectory($ENV{TWNROOT} $ENV{TWNROOT}/build) +add_subdirectory($ENV{TWNROOT} ${CMAKE_BINARY_DIR}/twn) set(SOURCE_FILES game.c diff --git a/apps/examples/circle-raster/CMakeLists.txt b/apps/examples/circle-raster/CMakeLists.txt index b92f11b..2c0b53c 100644 --- a/apps/examples/circle-raster/CMakeLists.txt +++ b/apps/examples/circle-raster/CMakeLists.txt @@ -6,7 +6,7 @@ if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Debug) endif() -add_subdirectory($ENV{TWNROOT} $ENV{TWNROOT}/build) +add_subdirectory($ENV{TWNROOT} ${CMAKE_BINARY_DIR}/twn) set(SOURCE_FILES game.c diff --git a/apps/templates/c/CMakeLists.txt b/apps/templates/c/CMakeLists.txt index 9c38499..70e21c3 100644 --- a/apps/templates/c/CMakeLists.txt +++ b/apps/templates/c/CMakeLists.txt @@ -5,7 +5,7 @@ if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Debug) endif() -add_subdirectory($ENV{TWNROOT} $ENV{TWNROOT}/build) +add_subdirectory($ENV{TWNROOT} ${CMAKE_BINARY_DIR}/twn) set(SOURCE_FILES game.c @@ -13,4 +13,4 @@ set(SOURCE_FILES ) cmake_path(GET CMAKE_SOURCE_DIR STEM LAST_ONLY GAME_PROJECT_NAME) -use_townengine(${GAME_PROJECT_NAME} "${SOURCE_FILES}" ${CMAKE_CURRENT_SOURCE_DIR}) +use_townengine("${SOURCE_FILES}" ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/apps/templates/c/data/twn.toml b/apps/templates/c/data/twn.toml index 2752b58..5cb55b5 100644 --- a/apps/templates/c/data/twn.toml +++ b/apps/templates/c/data/twn.toml @@ -14,6 +14,7 @@ dev_id = "you" # Game runtime details [game] resolution = [ 640, 480 ] +background_color = [ 255, 125, 0, 255 ] #debug = true # Engine tweaks. You probably don't need to change these diff --git a/apps/twnlua/CMakeLists.txt b/apps/twnlua/CMakeLists.txt index 558f940..d3fb9c8 100644 --- a/apps/twnlua/CMakeLists.txt +++ b/apps/twnlua/CMakeLists.txt @@ -7,7 +7,7 @@ if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Debug) endif() -add_subdirectory($ENV{TWNROOT} $ENV{TWNROOT}/build) +add_subdirectory($ENV{TWNROOT} ${CMAKE_BINARY_DIR}/twn) set(FLAGS $<$>:--no-dynlib-game> @@ -34,7 +34,7 @@ add_compile_definitions(LUA_32BITS=1) set(SOURCE_FILES game.c - state.h + minilua.c ) use_townengine("${SOURCE_FILES}" ${TWN_OUT_DIR}) diff --git a/apps/twnlua/game.c b/apps/twnlua/game.c index f84ceaa..340983a 100644 --- a/apps/twnlua/game.c +++ b/apps/twnlua/game.c @@ -1,7 +1,6 @@ #include "twn_game_api.h" /* TODO: actually move it back it its own file, it doesn't give any compilation benefits */ -#define LUA_IMPL #include "minilua.h" #include "state.h" diff --git a/apps/twnlua/minilua.c b/apps/twnlua/minilua.c new file mode 100644 index 0000000..9d27522 --- /dev/null +++ b/apps/twnlua/minilua.c @@ -0,0 +1,2 @@ +#define LUA_IMPL +#include "minilua.h" diff --git a/src/twn_loop.c b/src/twn_loop.c index 5cb9706..5e77d59 100644 --- a/src/twn_loop.c +++ b/src/twn_loop.c @@ -24,9 +24,9 @@ static void pack_contents_modified(char const *path, enum FilewatchAction action); - +#ifndef EMSCRIPTEN static SDL_sem *opengl_load_semaphore; - +#endif /* note: it drives most of IO implicitly, such as audio callbacks */ static void poll_events(void) { @@ -425,6 +425,7 @@ static bool initialize(void) { toml_datum_t datum_debug = toml_bool_in(game, "debug"); ctx.game.debug = datum_debug.ok ? datum_debug.u.b : true; +#ifndef EMSCRIPTEN SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5); @@ -441,6 +442,11 @@ static bool initialize(void) { SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); +#else + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); +#endif + toml_datum_t datum_title = toml_string_in(about, "title"); if (!datum_title.ok) datum_title.u.s = SDL_strdup("townengine project"); @@ -624,9 +630,11 @@ static bool initialize(void) { profile_end("game object load"); /* delayed as further as possible so that more work is done before we have to wait */ +#ifndef EMSCRIPTEN SDL_SemWait(opengl_load_semaphore); SDL_DestroySemaphore(opengl_load_semaphore); profile_end("opengl loading"); +#endif SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0"); SDL_SetHint(SDL_HINT_WINDOWS_NO_CLOSE_ON_ALT_F4, "1"); @@ -763,6 +771,7 @@ int enter_loop(int argc, char **argv) { } profile_end("SDL initialization"); +#ifndef EMSCRIPTEN profile_start("opengl loading"); opengl_load_semaphore = SDL_CreateSemaphore(0); SDL_Thread *opengl_load_thread = SDL_CreateThread(opengl_load_thread_fn, "opengl loader", opengl_load_semaphore); @@ -773,6 +782,7 @@ int enter_loop(int argc, char **argv) { SDL_DetachThread(opengl_load_thread); opengl_load_thread = NULL; +#endif ctx.argc = argc; ctx.argv = argv; diff --git a/src/twn_workers.c b/src/twn_workers.c index e2c82fe..6c4afef 100644 --- a/src/twn_workers.c +++ b/src/twn_workers.c @@ -50,6 +50,7 @@ bool workers_init(size_t worker_count) { if (worker_count > MAX_WORKERS) worker_count = MAX_WORKERS; +#ifndef EMSCRIPTEN /* spawn a bunch of detached threads without references to them */ for (size_t i = 0; i < worker_count; ++i) { SDL_Thread *thread = SDL_CreateThread(worker_thread, "worker", NULL); @@ -61,11 +62,13 @@ bool workers_init(size_t worker_count) { workers_job_semaphore = SDL_CreateSemaphore(0); workers_exit_semaphore = SDL_CreateSemaphore(0); workers_mutex = SDL_CreateMutex(); +#endif return true; } void workers_deinit(void) { +#ifndef EMSCRIPTEN SDL_LockMutex(workers_mutex); workers_should_exit = true; SDL_UnlockMutex(workers_mutex); @@ -77,5 +80,6 @@ void workers_deinit(void) { SDL_DestroyMutex(workers_mutex); SDL_DestroySemaphore(workers_job_semaphore); SDL_DestroySemaphore(workers_exit_semaphore); +#endif workers_pool_size = 0; }