From d3412f5f2dba77c9bf50141aaa91fd32e1dfddb5 Mon Sep 17 00:00:00 2001 From: JimmySnails Date: Mon, 17 Feb 2020 18:34:30 +0100 Subject: [PATCH 01/15] emscripten cmake changes --- CMakeLists.txt | 75 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 66 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0499802443..5dfc691da5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,36 @@ option(ENABLE_AUDIO "Enable Audio" ON) option(ENABLE_MICROPROFILE "Enable microprofile" OFF) option(ENABLE_ANGELSCRIPT "Enable AngelScript" ON) option(ENABLE_MOFILEREADER "Enable MofileReader" ON) +option(CROSSBUILD_EMSCRIPTEN "Build for the EmScripten Platform" OFF) + +if (CROSSBUILD_EMSCRIPTEN) + # Turn off not features that are not compatible with emscripten + set (ENABLE_AUDIO OFF) + set (ENABLE_ANGELSCRIPT OFF) + set (ENABLE_MOFILEREADER OFF) + #include(~/.conan/data/emsdk_installer/1.39.6/bincrafters/stable/package/1492a59deb6efdf29776a5734de63fc5f5c58650/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake ) +endif() + +if("${CMAKE_SYSTEM_NAME}" STREQUAL "Emscripten") + find_package(OpenGL REQUIRED) + include_directories(${OPENGL_INCLUDE_DIR}) + + list( APPEND conan_cmake_run_params SETTINGS os.threads=true ) + + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --no-check-features") + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --no-check-features") + set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --no-check-features") + + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -s USE_PTHREADS=1 -s USE_SDL_IMAGE=2 -s USE_SDL_TTF=2 -s USE_ZLIB=1" ) + set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -pthread -s USE_PTHREADS=1 -s USE_SDL_IMAGE=2 -s USE_SDL_TTF=2 -s USE_ZLIB=1" ) + set(SDL2_LIBRARIES "-s USE_SDL=2") + set(SDL2_IMAGE_LIBRARIES "-s USE_PTHREADS=1 -s USE_SDL_IMAGE=2") + #set(SDL2_MIXER_LIBRARIES "-s USE_SDL_MIXER=2") + set(SDL2_TTF_LIBRARIES "--no-check-features -s USE_SDL_TTF=2") + set(SDL2_ZLIB_LIBRARIES "-s USE_ZLIB=1") + +endif() + # Comment-out uneeded libs if (NOT ENABLE_AUDIO) @@ -72,14 +102,27 @@ if (USE_PACKAGE_MANAGER) include(pmm) - pmm(CONAN - REMOTES - AFG https://api.bintray.com/conan/anotherfoxguy/conan-packages - catchorg https://api.bintray.com/conan/catchorg/Catch2 - ror-dependencies https://api.bintray.com/conan/anotherfoxguy/ror-dependencies - BINCRAFTERS - CMakeCM ROLLING - ) + if("${CMAKE_SYSTEM_NAME}" STREQUAL "Emscripten") + + pmm(CONAN + PROFILE "${CMAKE_CURRENT_SOURCE_DIR}/emscripten_profile" + REMOTES + AFG https://api.bintray.com/conan/anotherfoxguy/conan-packages + catchorg https://api.bintray.com/conan/catchorg/Catch2 + ror-dependencies https://api.bintray.com/conan/anotherfoxguy/ror-dependencies + BINCRAFTERS + CMakeCM ROLLING + ) + else() + pmm(CONAN + REMOTES + AFG https://api.bintray.com/conan/anotherfoxguy/conan-packages + catchorg https://api.bintray.com/conan/catchorg/Catch2 + ror-dependencies https://api.bintray.com/conan/anotherfoxguy/ror-dependencies + BINCRAFTERS + CMakeCM ROLLING + ) + endif() if (DEFINED ENV{CI}) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin) @@ -95,6 +138,19 @@ if (USE_PACKAGE_MANAGER) list(APPEND CMAKE_MODULE_PATH "${CONAN_LIB_DIRS_CATCH2}/cmake/Catch2") endif () + if("${CMAKE_SYSTEM_NAME}" STREQUAL "Emscripten") + list(APPEND _link_libraries +# "-s ERROR_ON_UNDEFINED_SYMBOLS=0" + "-s USE_PTHREADS=1" + "-s WASM=1" + "-o index.html" + CONAN_PKG::libnoise + ${SDL2_LIBRARIES} + ${SDL2_IMAGE_LIBRARIES} + ${SDL2TTF_LIBRARIES} + ${SDL2_ZLIB_LIBRARIES} + ) + else() list(APPEND _link_libraries CONAN_PKG::sdl2 CONAN_PKG::sdl2_image @@ -103,6 +159,7 @@ if (USE_PACKAGE_MANAGER) CONAN_PKG::libnoise CONAN_PKG::zlib ) + endif() if (ENABLE_AUDIO) list(APPEND _link_libraries CONAN_PKG::sdl2_mixer) @@ -197,7 +254,7 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") link_libraries(dbghelp.lib) else() # Needed for filesystem library - if(NOT APPLE) + if(NOT APPLE AND NOT EMSCRIPTEN) # TODO: Link with stdc++fs on Apple with XCode11 list(APPEND _link_libraries "stdc++fs") endif() From 1a119a5b5934972faaf11970aa513beab775db5f Mon Sep 17 00:00:00 2001 From: JimmySnails Date: Mon, 17 Feb 2020 18:45:34 +0100 Subject: [PATCH 02/15] Add conan profile for emscripten --- CMakeLists.txt | 2 +- cmake/conan_profiles/emscripten_profile | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 cmake/conan_profiles/emscripten_profile diff --git a/CMakeLists.txt b/CMakeLists.txt index 5dfc691da5..6cf022d439 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -105,7 +105,7 @@ if (USE_PACKAGE_MANAGER) if("${CMAKE_SYSTEM_NAME}" STREQUAL "Emscripten") pmm(CONAN - PROFILE "${CMAKE_CURRENT_SOURCE_DIR}/emscripten_profile" + PROFILE "${CMAKE_CURRENT_SOURCE_DIR}/cmake/conan_profiles/emscripten_profile" REMOTES AFG https://api.bintray.com/conan/anotherfoxguy/conan-packages catchorg https://api.bintray.com/conan/catchorg/Catch2 diff --git a/cmake/conan_profiles/emscripten_profile b/cmake/conan_profiles/emscripten_profile new file mode 100644 index 0000000000..0ad5440e0d --- /dev/null +++ b/cmake/conan_profiles/emscripten_profile @@ -0,0 +1,17 @@ +[settings] +os=Emscripten +arch=x86 +#arch=wasm +os_build=Linux +arch_build=x86_64 +compiler=clang +compiler.version=10 +#compiler.version=6.0 +compiler.libcxx=libc++ +[options] +threads=true +[build_requires] +emsdk_installer/1.39.6@bincrafters/stable +[options] +libjpeg-turbo:SIMD = False +sdl2_image:tif = False From 25ac49b58db0b77ff5de142af8325b262a48367e Mon Sep 17 00:00:00 2001 From: JimmySnails Date: Mon, 17 Feb 2020 21:21:16 +0100 Subject: [PATCH 03/15] more emscripten flags + moving gameloop to a seperate function outside of the class + passing arguments as void* struct --- CMakeLists.txt | 17 +++++++- src/Game.cxx | 111 ++++++++++++++++++++++++++++++++----------------- 2 files changed, 88 insertions(+), 40 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6cf022d439..4eca478aa2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -138,12 +138,25 @@ if (USE_PACKAGE_MANAGER) list(APPEND CMAKE_MODULE_PATH "${CONAN_LIB_DIRS_CATCH2}/cmake/Catch2") endif () +# these compiler options are mostly for debugging and trying to get it to work if("${CMAKE_SYSTEM_NAME}" STREQUAL "Emscripten") list(APPEND _link_libraries -# "-s ERROR_ON_UNDEFINED_SYMBOLS=0" + "-s ERROR_ON_UNDEFINED_SYMBOLS=0" + "--preload-file ${CMAKE_CURRENT_BINARY_DIR}/resources" #include resources dir in .data file "-s USE_PTHREADS=1" + # "--source-map-base" "-s WASM=1" - "-o index.html" + "-s WASM_MEM_MAX=512MB" # ALLOW_MEMORY_GROWTH demands MEM_MAX to be set + "-s ALLOW_MEMORY_GROWTH=1" #seems necessary for pthreads and dynamic memory allocation + "-o Cytopia.html" + "-s DISABLE_EXCEPTION_CATCHING=0" # 0 -> catch exceptions! 1-> is disabled + "-s TOTAL_MEMORY=33554432" + "-s SAFE_HEAP=1" + "--profiling-funcs" + "-s DEMANGLE_SUPPORT=1" #demangle C++ functions + "-s ASSERTIONS=1" # use assertions + "-Oz" # no optimization + "-g4" # most debug info CONAN_PKG::libnoise ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES} diff --git a/src/Game.cxx b/src/Game.cxx index cd870e68cc..66a0bf1e70 100644 --- a/src/Game.cxx +++ b/src/Game.cxx @@ -10,6 +10,12 @@ #include #include #include +// void gameLoop(Engine &engine, EventManager &evManager, UIManager &uiManager); + +void gameLoop(void* arg_); +#ifdef __EMSCRIPTEN__ + #include +#endif #ifdef USE_ANGELSCRIPT #include "Scripting/ScriptEngine.hxx" @@ -26,6 +32,12 @@ template void Game::LoopMain(Game::GameContext &, Game::GameVisitor); template void Game::LoopMain(Game::GameContext &, Game::UIVisitor); +typedef struct loop_arg { + Engine *engine; + EventManager *evManager; + UIManager *uiManager; +} loop_arg; + Game::Game() : m_GameContext( &m_UILoopMQ, @@ -246,6 +258,7 @@ void Game::mainMenu() void Game::run(bool SkipMenu) { + loop_arg* arg = (loop_arg*)malloc(sizeof(loop_arg)); Timer benchmarkTimer; LOG(LOG_INFO) << VERSION; @@ -255,15 +268,21 @@ void Game::run(bool SkipMenu) } benchmarkTimer.start(); + Engine &engine = Engine::instance(); + EventManager &evManager = EventManager::instance(); - LOG(LOG_DEBUG) << "Map initialized in " << benchmarkTimer.getElapsedTime() << "ms"; + UIManager &uiManager = UIManager::instance(); + // SDL_Event event; + + arg->engine = &engine; + arg->evManager = &evManager; + arg->uiManager = &uiManager; + + LOG(LOG_DEBUG) << "Map initialized in " << benchmarkTimer.getElapsedTime() << "ms"; Camera::centerScreenOnMapCenter(); - SDL_Event event; - EventManager &evManager = EventManager::instance(); - UIManager &uiManager = UIManager::instance(); uiManager.init(); #ifdef USE_ANGELSCRIPT @@ -289,44 +308,19 @@ void Game::run(bool SkipMenu) Uint32 fpsLastTime = SDL_GetTicks(); Uint32 fpsFrames = 0; + #ifdef __EMSCRIPTEN__ + // void emscripten_set_main_loop(em_callback_func func, int fps, int simulate_infinite_loop); + // emscripten_set_main_loop(&game.run(), 60, 1); + // emscripten_set_main_loop(&gameLoop, 60, 1); + emscripten_set_main_loop_arg(&gameLoop, arg, 60, 1); +#else + // GameLoop while (engine.isGameRunning()) { -#ifdef MICROPROFILE_ENABLED - MICROPROFILE_SCOPEI("Map", "Gameloop", MP_GREEN); -#endif - SDL_RenderClear(WindowManager::instance().getRenderer()); - - evManager.checkEvents(event, engine); - - // render the tileMap - if (engine.map != nullptr) - engine.map->renderMap(); - - // render the ui - uiManager.drawUI(); - - // reset renderer color back to black - SDL_SetRenderDrawColor(WindowManager::instance().getRenderer(), 0, 0, 0, SDL_ALPHA_OPAQUE); - - // Render the Frame - SDL_RenderPresent(WindowManager::instance().getRenderer()); - - fpsFrames++; - - if (fpsLastTime < SDL_GetTicks() - fpsIntervall * 1000) - { - fpsLastTime = SDL_GetTicks(); - uiManager.setFPSCounterText(std::to_string(fpsFrames) + " FPS"); - fpsFrames = 0; - } - - SDL_Delay(1); - -#ifdef MICROPROFILE_ENABLED - MicroProfileFlip(nullptr); -#endif +gameLoop( arg); } + #endif } void Game::shutdown() @@ -370,3 +364,44 @@ template void Game::LoopMain(GameContext &co // @todo: Call shutdown() here in a safe way } } + +void gameLoop(void* arg_) +// void gameLoop(Engine &engine, EventManager &evManager, UIManager &uiManager) +{ + +loop_arg* arg = (loop_arg*)arg_; + #ifdef MICROPROFILE_ENABLED + MICROPROFILE_SCOPEI("Map", "Gameloop", MP_GREEN); +#endif + SDL_RenderClear(WindowManager::instance().getRenderer()); +SDL_Event event; + arg->evManager->checkEvents(event, *arg->engine); + + // render the tileMap + if (arg->engine->map != nullptr) + arg->engine->map->renderMap(); + + // render the ui + arg->uiManager->drawUI(); + + // reset renderer color back to black + SDL_SetRenderDrawColor(WindowManager::instance().getRenderer(), 0, 0, 0, SDL_ALPHA_OPAQUE); + + // Render the Frame + SDL_RenderPresent(WindowManager::instance().getRenderer()); + + // fpsFrames++; + + // if (fpsLastTime < SDL_GetTicks() - fpsIntervall * 1000) + // { + // fpsLastTime = SDL_GetTicks(); + // uiManager.setFPSCounterText(std::to_string(fpsFrames) + " FPS"); + // fpsFrames = 0; + // } + + SDL_Delay(1); + +#ifdef MICROPROFILE_ENABLED + MicroProfileFlip(nullptr); +#endif +} \ No newline at end of file From 2262741a336ade135bcc85378c951d76782c551d Mon Sep 17 00:00:00 2001 From: JimmySnails Date: Tue, 18 Feb 2020 07:42:32 +0100 Subject: [PATCH 04/15] disable alsa on emscripten --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4eca478aa2..0ecca1b513 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,7 +65,7 @@ if (NOT ENABLE_MOFILEREADER) set(_CC_MOFILEREADER "# ") endif () -if (WIN32 OR APPLE) +if (WIN32 OR APPLE OR EMSCRIPTEN) set(_CC_WIN32 "# ") endif () From 90f9d90b901646cde75b9a72cf13d91ca0b94db0 Mon Sep 17 00:00:00 2001 From: JimmySnails Date: Wed, 19 Feb 2020 18:58:51 +0100 Subject: [PATCH 05/15] added more emscripten specific fixes, now it's building --- CMakeLists.txt | 33 ++++++++++++++++++--------------- src/Game.cxx | 5 +++++ 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0ecca1b513..2bdfca6d29 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,10 +17,8 @@ option(CROSSBUILD_EMSCRIPTEN "Build for the EmScripten Platform" OFF) if (CROSSBUILD_EMSCRIPTEN) # Turn off not features that are not compatible with emscripten - set (ENABLE_AUDIO OFF) - set (ENABLE_ANGELSCRIPT OFF) - set (ENABLE_MOFILEREADER OFF) - #include(~/.conan/data/emsdk_installer/1.39.6/bincrafters/stable/package/1492a59deb6efdf29776a5734de63fc5f5c58650/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake ) + + include(~/.conan/data/emsdk_installer/1.39.6/bincrafters/stable/package/1492a59deb6efdf29776a5734de63fc5f5c58650/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake ) endif() if("${CMAKE_SYSTEM_NAME}" STREQUAL "Emscripten") @@ -33,7 +31,12 @@ if("${CMAKE_SYSTEM_NAME}" STREQUAL "Emscripten") set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --no-check-features") set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --no-check-features") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -s USE_PTHREADS=1 -s USE_SDL_IMAGE=2 -s USE_SDL_TTF=2 -s USE_ZLIB=1" ) + #Debug information + #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --profiling-funcs -Oz -g4 -s PTHREAD_POOL_SIZE=8 -s DEMANGLE_SUPPORT=1 -pthread -s DISABLE_EXCEPTION_CATCHING=0 -s ASSERTIONS=1 -s USE_PTHREADS=1 -s USE_SDL_IMAGE=2 -s USE_SDL_TTF=2 -s USE_ZLIB=1" ) + + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --profiling-funcs -O3 -g0 --llvm-lto 1 --llvm-opts 3 -s PTHREAD_POOL_SIZE=8 -pthread -s DISABLE_EXCEPTION_CATCHING=1 -s USE_PTHREADS=1 -s USE_SDL_IMAGE=2 -s USE_SDL_TTF=2 -s USE_ZLIB=1" ) + + set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -pthread -s USE_PTHREADS=1 -s USE_SDL_IMAGE=2 -s USE_SDL_TTF=2 -s USE_ZLIB=1" ) set(SDL2_LIBRARIES "-s USE_SDL=2") set(SDL2_IMAGE_LIBRARIES "-s USE_PTHREADS=1 -s USE_SDL_IMAGE=2") @@ -141,22 +144,22 @@ if (USE_PACKAGE_MANAGER) # these compiler options are mostly for debugging and trying to get it to work if("${CMAKE_SYSTEM_NAME}" STREQUAL "Emscripten") list(APPEND _link_libraries - "-s ERROR_ON_UNDEFINED_SYMBOLS=0" - "--preload-file ${CMAKE_CURRENT_BINARY_DIR}/resources" #include resources dir in .data file + #"-s ERROR_ON_UNDEFINED_SYMBOLS=0" + "--preload-file ${CMAKE_CURRENT_BINARY_DIR}/resources@/resources" #include resources dir in .data file "-s USE_PTHREADS=1" + "--use-preload-plugins" # "--source-map-base" "-s WASM=1" "-s WASM_MEM_MAX=512MB" # ALLOW_MEMORY_GROWTH demands MEM_MAX to be set "-s ALLOW_MEMORY_GROWTH=1" #seems necessary for pthreads and dynamic memory allocation - "-o Cytopia.html" - "-s DISABLE_EXCEPTION_CATCHING=0" # 0 -> catch exceptions! 1-> is disabled + "-o index.html" + # "-s DISABLE_EXCEPTION_CATCHING=0" # 0 -> catch exceptions! 1-> is disabled "-s TOTAL_MEMORY=33554432" - "-s SAFE_HEAP=1" - "--profiling-funcs" - "-s DEMANGLE_SUPPORT=1" #demangle C++ functions - "-s ASSERTIONS=1" # use assertions - "-Oz" # no optimization - "-g4" # most debug info + # "-s SAFE_HEAP=1" + #demangle C++ functions + # "-s ASSERTIONS=0" # use assertions + "-O3" # no optimization + "-g0" # most debug info CONAN_PKG::libnoise ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES} diff --git a/src/Game.cxx b/src/Game.cxx index 66a0bf1e70..eb62b99f86 100644 --- a/src/Game.cxx +++ b/src/Game.cxx @@ -262,6 +262,11 @@ void Game::run(bool SkipMenu) Timer benchmarkTimer; LOG(LOG_INFO) << VERSION; + Engine &engine = Engine::instance(); + EventManager &evManager = EventManager::instance(); + UIManager &uiManager = UIManager::instance(); + + if (SkipMenu) { Engine::instance().newGame(); From 2cd7697037d5ccf0ce328b1b1eab45400f16332d Mon Sep 17 00:00:00 2001 From: JimmySnails Date: Wed, 19 Feb 2020 20:05:28 +0100 Subject: [PATCH 06/15] disable libmad together with sdl2_mixer --- conanfile.txt.in | 1 + 1 file changed, 1 insertion(+) diff --git a/conanfile.txt.in b/conanfile.txt.in index cf79cf02d1..490dc83be5 100644 --- a/conanfile.txt.in +++ b/conanfile.txt.in @@ -8,6 +8,7 @@ sdl2_ttf/2.0.15@bincrafters/stable sdl2_image/2.0.5@bincrafters/stable bzip2/1.0.8@conan/stable libmad/0.15.1b +@_CC_SDL2_MIXER@libmad/0.15.1b @_CC_SDL2_MIXER@sdl2_mixer/2.0.4@bincrafters/stable @_CC_MOFILEREADER@MofileReader/1.0.0@anotherfoxguy/stable libpng/1.6.37 From f406747f55c41ecec1b285cce037aa3260bb41d8 Mon Sep 17 00:00:00 2001 From: JimmySnails Date: Wed, 19 Feb 2020 20:10:22 +0100 Subject: [PATCH 07/15] Fix previous commit --- conanfile.txt.in | 1 - 1 file changed, 1 deletion(-) diff --git a/conanfile.txt.in b/conanfile.txt.in index 490dc83be5..5b43582ab3 100644 --- a/conanfile.txt.in +++ b/conanfile.txt.in @@ -7,7 +7,6 @@ sdl2/2.0.10@bincrafters/stable sdl2_ttf/2.0.15@bincrafters/stable sdl2_image/2.0.5@bincrafters/stable bzip2/1.0.8@conan/stable -libmad/0.15.1b @_CC_SDL2_MIXER@libmad/0.15.1b @_CC_SDL2_MIXER@sdl2_mixer/2.0.4@bincrafters/stable @_CC_MOFILEREADER@MofileReader/1.0.0@anotherfoxguy/stable From 7c6b4e2084fce2cf43d2c7e2dceae774d2c769f8 Mon Sep 17 00:00:00 2001 From: JimmySnails Date: Fri, 21 Feb 2020 12:05:13 +0100 Subject: [PATCH 08/15] disable unavailable signal and execinfo.h for emscripten --- src/Game.cxx | 8 ++------ src/main.cxx | 5 ++++- src/util/Exception.cxx | 8 +++++--- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Game.cxx b/src/Game.cxx index eb62b99f86..e8c89a28b1 100644 --- a/src/Game.cxx +++ b/src/Game.cxx @@ -10,13 +10,13 @@ #include #include #include -// void gameLoop(Engine &engine, EventManager &evManager, UIManager &uiManager); -void gameLoop(void* arg_); #ifdef __EMSCRIPTEN__ #include #endif +void gameLoop(void* arg_); + #ifdef USE_ANGELSCRIPT #include "Scripting/ScriptEngine.hxx" #endif @@ -274,10 +274,6 @@ void Game::run(bool SkipMenu) benchmarkTimer.start(); - Engine &engine = Engine::instance(); - EventManager &evManager = EventManager::instance(); - - UIManager &uiManager = UIManager::instance(); // SDL_Event event; arg->engine = &engine; diff --git a/src/main.cxx b/src/main.cxx index 26a8a69c3b..8fc68cf7f7 100644 --- a/src/main.cxx +++ b/src/main.cxx @@ -4,8 +4,10 @@ #include "Exception.hxx" #include "LOG.hxx" +#ifndef __EMSCRIPTEN__ #include void SIG_handler(int signal); +#endif SDL_AssertState AssertionHandler(const SDL_AssertData *, void *); @@ -52,11 +54,12 @@ int protected_main(int argc, char **argv) int main(int argc, char **argv) { +#ifndef __EMSCRIPTEN__ /* Register handler for Segmentation Fault, Interrupt, Terminate */ signal(SIGSEGV, SIG_handler); signal(SIGINT, SIG_handler); signal(SIGTERM, SIG_handler); - +#endif /* All SDL2 Assertion failures must be handled * by our handler */ SDL_SetAssertionHandler(AssertionHandler, 0); diff --git a/src/util/Exception.cxx b/src/util/Exception.cxx index 36e3684961..7c266033b1 100644 --- a/src/util/Exception.cxx +++ b/src/util/Exception.cxx @@ -42,12 +42,14 @@ void SIG_handler(int signal) exit(1); } -#else +// #else +#elif __unix__ && !__EMSCRIPTEN__ #include -#include #include +#include + void SIG_handler(int signal) { switch (signal) @@ -93,7 +95,7 @@ SDL_AssertState AssertionHandler(const SDL_AssertData *data, void *) SYMBOL_INFO symbol; for (int i = 0; i < size; ++i) std::cout << "\tat " << symbol.Name << "\n"; -#else +#elif __unix__ && !__EMSCRIPTEN__ /* We print the last 10 calls */ void *buffer[10]; size_t size; From 350e96e218084ace1bbc4f847918fbbeb22bbf4d Mon Sep 17 00:00:00 2001 From: JimmySnails Date: Mon, 24 Feb 2020 12:46:13 +0100 Subject: [PATCH 09/15] Update main.cxx --- src/main.cxx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main.cxx b/src/main.cxx index 8fc68cf7f7..8b1872e031 100644 --- a/src/main.cxx +++ b/src/main.cxx @@ -26,6 +26,10 @@ int protected_main(int argc, char **argv) skipMenu = true; } } + + #ifdef __EMSCRIPTEN + skipMenu = true; + #endif LOG(LOG_DEBUG) << "Launching Cytopia"; From 1cc74313cc6ca89a70ef40829a807b2e5189cb32 Mon Sep 17 00:00:00 2001 From: JimmySnails Date: Mon, 24 Feb 2020 12:55:18 +0100 Subject: [PATCH 10/15] fix last commit --- src/main.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cxx b/src/main.cxx index 8b1872e031..d455e4b084 100644 --- a/src/main.cxx +++ b/src/main.cxx @@ -27,7 +27,7 @@ int protected_main(int argc, char **argv) } } - #ifdef __EMSCRIPTEN + #ifdef __EMSCRIPTEN__ skipMenu = true; #endif From 94a8574b90bcc826feef04f7d721a04a4b99fd2e Mon Sep 17 00:00:00 2001 From: JimmySnails Date: Mon, 24 Feb 2020 13:03:25 +0100 Subject: [PATCH 11/15] Improve emscripten performance --- src/Game.cxx | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Game.cxx b/src/Game.cxx index e8c89a28b1..4d43d3eaf1 100644 --- a/src/Game.cxx +++ b/src/Game.cxx @@ -309,17 +309,14 @@ void Game::run(bool SkipMenu) Uint32 fpsLastTime = SDL_GetTicks(); Uint32 fpsFrames = 0; - #ifdef __EMSCRIPTEN__ - // void emscripten_set_main_loop(em_callback_func func, int fps, int simulate_infinite_loop); - // emscripten_set_main_loop(&game.run(), 60, 1); - // emscripten_set_main_loop(&gameLoop, 60, 1); - emscripten_set_main_loop_arg(&gameLoop, arg, 60, 1); +#ifdef __EMSCRIPTEN__ + emscripten_set_main_loop_arg(&gameLoop, arg, 0, 1); #else // GameLoop while (engine.isGameRunning()) { -gameLoop( arg); + gameLoop( arg); } #endif } @@ -400,9 +397,11 @@ SDL_Event event; // fpsFrames = 0; // } +#ifndef __EMSCRIPTEN__ SDL_Delay(1); +#endif #ifdef MICROPROFILE_ENABLED MicroProfileFlip(nullptr); #endif -} \ No newline at end of file +} From 89f73fe2bcddadbf0f1e2dbb95de901457b0ccf6 Mon Sep 17 00:00:00 2001 From: JimmySnails Date: Mon, 24 Feb 2020 14:00:00 +0100 Subject: [PATCH 12/15] use new instead of malloc for arg pointer and delete the pointer on exit --- src/Game.cxx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Game.cxx b/src/Game.cxx index a920ca8070..d7f7f2921d 100644 --- a/src/Game.cxx +++ b/src/Game.cxx @@ -252,7 +252,7 @@ void Game::mainMenu() void Game::run(bool SkipMenu) { - loop_arg* arg = (loop_arg*)malloc(sizeof(loop_arg)); + loop_arg* arg = new loop_arg; Timer benchmarkTimer; LOG(LOG_INFO) << VERSION; @@ -313,6 +313,8 @@ void Game::run(bool SkipMenu) gameLoop( arg); } #endif + + delete arg; } void Game::shutdown() From 02b391e9960867d616cb985e4a1e8c72c5eb577d Mon Sep 17 00:00:00 2001 From: JimmySnails Date: Mon, 24 Feb 2020 15:13:04 +0100 Subject: [PATCH 13/15] reenable fps counter --- src/Game.cxx | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Game.cxx b/src/Game.cxx index d7f7f2921d..819cd01629 100644 --- a/src/Game.cxx +++ b/src/Game.cxx @@ -38,6 +38,11 @@ typedef struct loop_arg { UIManager *uiManager; } loop_arg; + // FPS Counter variables + const float fpsIntervall = 1.0; // interval the fps counter is refreshed in seconds. + Uint32 fpsLastTime = SDL_GetTicks(); + Uint32 fpsFrames = 0; + Game::Game() : m_GameContext(&m_UILoopMQ, &m_GameLoopMQ, #ifdef USE_AUDIO @@ -298,11 +303,6 @@ void Game::run(bool SkipMenu) } #endif // USE_AUDIO - // FPS Counter variables - const float fpsIntervall = 1.0; // interval the fps counter is refreshed in seconds. - Uint32 fpsLastTime = SDL_GetTicks(); - Uint32 fpsFrames = 0; - #ifdef __EMSCRIPTEN__ emscripten_set_main_loop_arg(&gameLoop, arg, 0, 1); #else @@ -384,14 +384,14 @@ SDL_Event event; // Render the Frame SDL_RenderPresent(WindowManager::instance().getRenderer()); - // fpsFrames++; + fpsFrames++; - // if (fpsLastTime < SDL_GetTicks() - fpsIntervall * 1000) - // { - // fpsLastTime = SDL_GetTicks(); - // uiManager.setFPSCounterText(std::to_string(fpsFrames) + " FPS"); - // fpsFrames = 0; - // } + if (fpsLastTime < SDL_GetTicks() - fpsIntervall * 1000) + { + fpsLastTime = SDL_GetTicks(); + uiManager.setFPSCounterText(std::to_string(fpsFrames) + " FPS"); + fpsFrames = 0; + } #ifndef __EMSCRIPTEN__ SDL_Delay(1); From a66b2761dc884a3825b104b5b4370728800d88aa Mon Sep 17 00:00:00 2001 From: JimmySnails Date: Mon, 24 Feb 2020 15:15:07 +0100 Subject: [PATCH 14/15] use button "m" instead of F11 for displaying FPS --- src/engine/EventManager.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/engine/EventManager.cxx b/src/engine/EventManager.cxx index 98f0c5b958..6845f55886 100644 --- a/src/engine/EventManager.cxx +++ b/src/engine/EventManager.cxx @@ -42,7 +42,7 @@ void EventManager::checkEvents(SDL_Event &event, Engine &engine) case SDLK_0: break; - case SDLK_F11: + case SDLK_m: m_uiManager.toggleDebugMenu(); break; case SDLK_1: From 44e937297e22a3670941c9b867e815bbadd63832 Mon Sep 17 00:00:00 2001 From: JimmySnails Date: Mon, 24 Feb 2020 15:19:46 +0100 Subject: [PATCH 15/15] use uiManager from arg --- src/Game.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Game.cxx b/src/Game.cxx index 819cd01629..3135119d55 100644 --- a/src/Game.cxx +++ b/src/Game.cxx @@ -389,7 +389,7 @@ SDL_Event event; if (fpsLastTime < SDL_GetTicks() - fpsIntervall * 1000) { fpsLastTime = SDL_GetTicks(); - uiManager.setFPSCounterText(std::to_string(fpsFrames) + " FPS"); + arg->uiManager->setFPSCounterText(std::to_string(fpsFrames) + " FPS"); fpsFrames = 0; }