diff --git a/src/PowderToy.cpp b/src/PowderToy.cpp index cd506120f..a1183009e 100644 --- a/src/PowderToy.cpp +++ b/src/PowderToy.cpp @@ -510,10 +510,7 @@ int main(int argc, char * argv[]) } } - while (engine.Running()) - { - EngineProcess(); - } + MainLoop(); }; if (enableBluescreen) diff --git a/src/PowderToySDL.h b/src/PowderToySDL.h index 3c7b66508..012a5f3e0 100644 --- a/src/PowderToySDL.h +++ b/src/PowderToySDL.h @@ -26,6 +26,7 @@ extern bool mouseDown; extern bool calculatedInitialMouse; extern bool hasMouseMoved; +void MainLoop(); void EngineProcess(); void StartTextInput(); void StopTextInput(); diff --git a/src/PowderToySDLCommon.cpp b/src/PowderToySDLCommon.cpp new file mode 100644 index 000000000..80ff83fed --- /dev/null +++ b/src/PowderToySDLCommon.cpp @@ -0,0 +1,10 @@ +#include "PowderToySDL.h" +#include "gui/interface/Engine.h" + +void MainLoop() +{ + while (ui::Engine::Ref().Running()) + { + EngineProcess(); + } +} diff --git a/src/PowderToySDLEmscripten.cpp b/src/PowderToySDLEmscripten.cpp new file mode 100644 index 000000000..d0705cb4b --- /dev/null +++ b/src/PowderToySDLEmscripten.cpp @@ -0,0 +1,37 @@ +#include "PowderToySDL.h" +#include "gui/interface/Engine.h" +#include +#include + +static float lastFpsLimit; +static void updateFpsLimit() +{ + lastFpsLimit = ui::Engine::Ref().FpsLimit; + if (lastFpsLimit == 60.0f) // TODO: rework FPS cap so 60.0 is not the default + { + emscripten_set_main_loop_timing(EM_TIMING_RAF, 1); + std::cerr << "implicit fps limit via vsync" << std::endl; + } + else + { + auto delay = int(1000.f / lastFpsLimit); + emscripten_set_main_loop_timing(EM_TIMING_SETTIMEOUT, delay); + std::cerr << "explicit fps limit: " << delay << "ms delays" << std::endl; + } +} + +static void mainLoopBody() +{ + EngineProcess(); + if (lastFpsLimit != ui::Engine::Ref().FpsLimit) + { + updateFpsLimit(); + } +} + +void MainLoop() +{ + emscripten_set_main_loop(mainLoopBody, 0, 0); + updateFpsLimit(); + mainLoopBody(); +} diff --git a/src/common/platform/Common.cpp b/src/common/platform/Common.cpp index 2b15b4d47..a775ccc8f 100644 --- a/src/common/platform/Common.cpp +++ b/src/common/platform/Common.cpp @@ -3,7 +3,6 @@ #include "common/tpt-rand.h" #include "Config.h" #include -#include #include #include #include @@ -108,20 +107,4 @@ bool WriteFile(const std::vector &fileData, ByteString filename) } return true; } - -std::list exitFuncs; - -void Atexit(ExitFunc exitFunc) -{ - exitFuncs.push_front(exitFunc); -} - -void Exit(int code) -{ - for (auto exitFunc : exitFuncs) - { - exitFunc(); - } - exit(code); -} } diff --git a/src/common/platform/Emscripten.cpp b/src/common/platform/Emscripten.cpp index 14fe1e98d..8965f6082 100644 --- a/src/common/platform/Emscripten.cpp +++ b/src/common/platform/Emscripten.cpp @@ -29,4 +29,12 @@ bool Install() { return false; } + +void Atexit(ExitFunc exitFunc) +{ +} + +void Exit(int code) +{ +} } diff --git a/src/common/platform/ExitCommon.cpp b/src/common/platform/ExitCommon.cpp new file mode 100644 index 000000000..0c4383b83 --- /dev/null +++ b/src/common/platform/ExitCommon.cpp @@ -0,0 +1,22 @@ +#include "Platform.h" +#include +#include + +namespace Platform +{ +std::list exitFuncs; + +void Atexit(ExitFunc exitFunc) +{ + exitFuncs.push_front(exitFunc); +} + +void Exit(int code) +{ + for (auto exitFunc : exitFuncs) + { + exitFunc(); + } + exit(code); +} +} diff --git a/src/common/platform/meson.build b/src/common/platform/meson.build index d8d19d26d..546a403a7 100644 --- a/src/common/platform/meson.build +++ b/src/common/platform/meson.build @@ -10,18 +10,21 @@ if host_platform == 'windows' path_sep_char = '\\\\' common_files += files( 'Windows.cpp', + 'ExitCommon.cpp', ) elif host_platform == 'darwin' can_install_enforce_no = true common_files += files( 'Darwin.cpp', 'Posix.cpp', + 'ExitCommon.cpp', ) elif host_platform == 'android' can_install_enforce_no = true common_files += files( 'Android.cpp', 'Posix.cpp', + 'ExitCommon.cpp', ) elif host_platform == 'emscripten' use_bluescreen = false @@ -36,12 +39,14 @@ elif host_platform == 'linux' common_files += files( 'Linux.cpp', 'Posix.cpp', + 'ExitCommon.cpp', ) else can_install_enforce_no = true common_files += files( 'Null.cpp', 'Posix.cpp', + 'ExitCommon.cpp', ) endif conf_data.set('SET_WINDOW_ICON', set_window_icon ? 'true' : 'false') diff --git a/src/meson.build b/src/meson.build index a5607a331..96f97a708 100644 --- a/src/meson.build +++ b/src/meson.build @@ -46,6 +46,15 @@ powder_files = files( 'lua/TPTScriptInterface.cpp', 'lua/TPTSTypes.cpp', ) +if host_platform == 'emscripten' + powder_files += files( + 'PowderToySDLEmscripten.cpp', + ) +else + powder_files += files( + 'PowderToySDLCommon.cpp', + ) +endif if is_x86 powder_files += files('X86KillDenormals.cpp') endif