Skip to content

[WASM] Option to use main loop timing 'timeout' instead of 'requestAnimationFrame' (RAF) #2578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ default is: `navigator.hardwareConcurrency`
- AX_WASM_ENABLE_DEVTOOLS: whether enable web devtools aka `pause`, `resume`, `step` buttons in webpage, default: `TRUE`
- AX_WASM_INITIAL_MEMORY: set the wasm initial memory size, default `1024MB`
- AX_WASM_ISA_SIMD: specify the wasm simd intrinsics type, default `none`, supports `sse`, `neon`, note the `wasm-simd` not support by axmol yet
- AX_WASM_TIMING_USE_TIMEOUT: force the wasm emscripten loop timing to use `Timeout` with FPS value of `Application::setAnimationInterval` (app still run on focus lost), default is `FALSE` with timing `requestAnimationFrame` (no run on focus lost). See https://emscripten.org/docs/api_reference/emscripten.h.html#c.emscripten_set_main_loop_timing

## The options for axmol apps

Expand Down
7 changes: 7 additions & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ if (WIN32)
endif()
endif()

if (WASM)
set(AX_WASM_TIMING_USE_TIMEOUT OFF CACHE BOOL "")
endif()

# axmolver.h
find_program(GIT_PROG NAMES git)
if(AX_UPDATE_BUILD_VERSION AND GIT_PROG AND EXISTS "${_AX_ROOT}/.git")
Expand Down Expand Up @@ -273,6 +277,9 @@ if(WASM)
target_link_options(${_AX_CORE_LIB} PUBLIC -sGL_ENABLE_GET_PROC_ADDRESS)
endif()
target_link_options(${_AX_CORE_LIB} PUBLIC -lwebsocket.js)
if(AX_WASM_TIMING_USE_TIMEOUT)
target_compile_definitions(${_AX_CORE_LIB} PUBLIC AX_WASM_TIMING_USE_TIMEOUT=1)
endif()
elseif(AX_ENABLE_MFMEDIA AND NOT WINRT AND FULL_MSVC)
target_link_options(${_AX_CORE_LIB} PUBLIC "/DELAYLOAD:mf.dll" "/DELAYLOAD:mfplat.dll")
endif()
Expand Down
5 changes: 5 additions & 0 deletions core/platform/wasm/Application-wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,12 @@ int Application::run()
as the browser’s requestAnimationFrame will make sure you render at a proper smooth rate
that lines up properly with the browser and monitor.
*/
#if AX_WASM_TIMING_USE_TIMEOUT
double fps = ceil(1.0 / (static_cast<double>(s_animationInterval) / NANOSECONDSPERSECOND));
emscripten_set_main_loop(updateFrame, static_cast<int>(fps), false);
#else
emscripten_set_main_loop(updateFrame, -1, false);
#endif

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!,,

return 0;
}
Expand Down