diff options
author | Alon Zakai <alonzakai@gmail.com> | 2012-05-06 18:04:12 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2012-05-06 18:04:12 -0700 |
commit | ccf2e419785aa2fdc25396c707987ba9d7fc80a2 (patch) | |
tree | 72cafef4ba0301a57762b9a675d3b8b915c0a675 | |
parent | 1d32c296019da719f323d340882c12900e85a3c2 (diff) |
emscripten_pause_main_loop and resume
-rw-r--r-- | src/library_browser.js | 38 | ||||
-rw-r--r-- | system/include/emscripten.h | 10 | ||||
-rw-r--r-- | tests/emscripten_api_browser.cpp | 13 |
3 files changed, 46 insertions, 15 deletions
diff --git a/src/library_browser.js b/src/library_browser.js index 1102acbe..07641aed 100644 --- a/src/library_browser.js +++ b/src/library_browser.js @@ -5,6 +5,10 @@ mergeInto(LibraryManager.library, { $Browser__postset: 'Module["requestFullScreen"] = function() { Browser.requestFullScreen() };\n', // export requestFullScreen $Browser: { + mainLoop: { + scheduler: null, + paused: false + }, pointerLock: false, moduleContextCreatedCallbacks: [], @@ -148,28 +152,36 @@ mergeInto(LibraryManager.library, { emscripten_set_main_loop: function(func, fps) { Module['noExitRuntime'] = true; - _emscripten_set_main_loop.cancel = false; var jsFunc = FUNCTION_TABLE[func]; - + var wrapper = function() { + if (Browser.mainLoop.paused) return; + jsFunc(); + Browser.mainLoop.scheduler(); + } if (fps && fps > 0) { - function doOne() { - if (_emscripten_set_main_loop.cancel) return; - jsFunc(); - setTimeout(doOne, 1000/fps); // doing this each time means that on exception, we stop + Browser.mainLoop.scheduler = function() { + setTimeout(wrapper, 1000/fps); // doing this each time means that on exception, we stop } - setTimeout(doOne, 1000/fps); } else { - function doOneRAF() { - if (_emscripten_set_main_loop.cancel) return; - jsFunc(); - Browser.requestAnimationFrame(doOneRAF); + Browser.mainLoop.scheduler = function() { + Browser.requestAnimationFrame(wrapper); } - Browser.requestAnimationFrame(doOneRAF); } + Browser.mainLoop.scheduler(); }, emscripten_cancel_main_loop: function(func) { - _emscripten_set_main_loop.cancel = true; + Browser.mainLoop.scheduler = null; + Browser.mainLoop.paused = true; + }, + + emscripten_pause_main_loop: function(func) { + Browser.mainLoop.paused = true; + }, + + emscripten_resume_main_loop: function(func) { + Browser.mainLoop.paused = false; + Browser.mainLoop.scheduler(); }, emscripten_async_call: function(func, millis) { diff --git a/system/include/emscripten.h b/system/include/emscripten.h index 4725e68a..fdccfe82 100644 --- a/system/include/emscripten.h +++ b/system/include/emscripten.h @@ -24,8 +24,18 @@ extern void emscripten_async_run_script(const char *script, int millis); * will call that function at a specified number of frames per * second. Setting 0 or a negative value as the fps will use * the browser's requestAnimationFrame mechanism. + * + * Pausing and resuming the main loop is useful if your app + * needs to perform some synchronous operation, for example + * to load a file from the network. It might be wrong to + * run the main loop before that finishes (the original + * code assumes that), so you can break the code up into + * asynchronous callbacks, but you must pause the main + * loop until they complete. */ extern void emscripten_set_main_loop(void (*func)(), int fps); +extern void emscripten_pause_main_loop(); +extern void emscripten_resume_main_loop(); extern void emscripten_cancel_main_loop(); /* diff --git a/tests/emscripten_api_browser.cpp b/tests/emscripten_api_browser.cpp index a2a19359..20e2dbb5 100644 --- a/tests/emscripten_api_browser.cpp +++ b/tests/emscripten_api_browser.cpp @@ -9,11 +9,20 @@ int last = 0; extern "C" { +bool fived = false; +void five() { + fived = true; + emscripten_resume_main_loop(); +} + void mainey() { static int counter = 0; printf("mainey: %d\n", counter++); - if (counter == 10) { - int result = 1; + if (counter == 20) { + emscripten_pause_main_loop(); + emscripten_async_call(five, 1000); + } else if (counter == 22) { // very soon after 20, so without pausing we fail + int result = fived; REPORT_RESULT(); } } |