Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function _emscripten_set_main_loop_timing(mode, value) {
- console.log("_emscripten_set_main_loop_timing called with " + mode + ", " + value); // 1: _emscripten_set_main_loop_timing called with 0, 33.333333333333336
- Browser.mainLoop.timingMode = mode;
- Browser.mainLoop.timingValue = value;
- if (!Browser.mainLoop.func) {
- return 1;
- }
- if (!Browser.mainLoop.running) {
- Browser.mainLoop.running = true;
- }
- if (mode == 0) {
- console.log("setting timeout"); // 2: called next
- Browser.mainLoop.scheduler = function Browser_mainLoop_scheduler_setTimeout() {
- var timeUntilNextTick = Math.max(0, Browser.mainLoop.tickStartTime + value - _emscripten_get_now()) | 0;
- console.log("timeUntilNextTick: " + timeUntilNextTick); // 3: called regularly with "timeUntilNextTick: 32" or "...33"
- setTimeout(Browser.mainLoop.runner, timeUntilNextTick);
- };
- Browser.mainLoop.method = "timeout";
- } else if (mode == 1) {
- Browser.mainLoop.scheduler = function Browser_mainLoop_scheduler_rAF() {
- Browser.requestAnimationFrame(Browser.mainLoop.runner);
- };
- Browser.mainLoop.method = "rAF";
- } else if (mode == 2) {
- if (typeof setImmediate === "undefined") {
- var setImmediates = [];
- var emscriptenMainLoopMessageId = "setimmediate";
- var Browser_setImmediate_messageHandler = function (event) {
- if (event.data === emscriptenMainLoopMessageId || event.data.target === emscriptenMainLoopMessageId) {
- event.stopPropagation();
- setImmediates.shift()();
- }
- };
- addEventListener("message", Browser_setImmediate_messageHandler, true);
- setImmediate = function Browser_emulated_setImmediate(func) {
- setImmediates.push(func);
- if (ENVIRONMENT_IS_WORKER) {
- if (Module["setImmediates"] === undefined) Module["setImmediates"] = [];
- Module["setImmediates"].push(func);
- postMessage({ target: emscriptenMainLoopMessageId });
- } else postMessage(emscriptenMainLoopMessageId, "*");
- };
- }
- Browser.mainLoop.scheduler = function Browser_mainLoop_scheduler_setImmediate() {
- setImmediate(Browser.mainLoop.runner);
- };
- Browser.mainLoop.method = "immediate";
- }
- return 0;
- }
- function setMainLoop(browserIterationFunc, fps, simulateInfiniteLoop, arg, noSetTiming) {
- assert(!Browser.mainLoop.func, "emscripten_set_main_loop: there can only be one main loop function at once: call emscripten_cancel_main_loop to cancel the previous one before setting a new one with different parameters.");
- Browser.mainLoop.func = browserIterationFunc;
- Browser.mainLoop.arg = arg;
- var thisMainLoopId = Browser.mainLoop.currentlyRunningMainloop;
- function checkIsRunning() {
- if (thisMainLoopId < Browser.mainLoop.currentlyRunningMainloop) {
- maybeExit();
- return false;
- }
- return true;
- }
- Browser.mainLoop.running = false;
- Browser.mainLoop.runner = function Browser_mainLoop_runner() {
- if (ABORT) return;
- if (Browser.mainLoop.queue.length > 0) {
- var start = Date.now();
- var blocker = Browser.mainLoop.queue.shift();
- blocker.func(blocker.arg);
- if (Browser.mainLoop.remainingBlockers) {
- var remaining = Browser.mainLoop.remainingBlockers;
- var next = remaining % 1 == 0 ? remaining - 1 : Math.floor(remaining);
- if (blocker.counted) {
- Browser.mainLoop.remainingBlockers = next;
- } else {
- next = next + 0.5;
- Browser.mainLoop.remainingBlockers = (8 * remaining + next) / 9;
- }
- }
- console.log('main loop blocker "' + blocker.name + '" took ' + (Date.now() - start) + " ms");
- Browser.mainLoop.updateStatus();
- if (!checkIsRunning()) return;
- console.log("Setting timeout to 0");
- setTimeout(Browser.mainLoop.runner, 0);
- return;
- }
- if (!checkIsRunning()) return;
- Browser.mainLoop.currentFrameNumber = (Browser.mainLoop.currentFrameNumber + 1) | 0;
- if (Browser.mainLoop.timingMode == 1 && Browser.mainLoop.timingValue > 1 && Browser.mainLoop.currentFrameNumber % Browser.mainLoop.timingValue != 0) {
- console.log("Browser.mainLoop.timingMode == 1");
- Browser.mainLoop.scheduler();
- return;
- } else if (Browser.mainLoop.timingMode == 0) {
- console.log("Browser.mainLoop.timingMode == 0");
- Browser.mainLoop.tickStartTime = _emscripten_get_now();
- }
- GL.newRenderingFrameStarted();
- Browser.mainLoop.runIter(browserIterationFunc);
- if (!checkIsRunning()) return;
- if (typeof SDL === "object" && SDL.audio && SDL.audio.queueNewAudioData) SDL.audio.queueNewAudioData();
- Browser.mainLoop.scheduler();
- };
- if (!noSetTiming) {
- console.log("fps: " + fps);
- fps = 30;
- if (fps && fps > 0) _emscripten_set_main_loop_timing(0, 1e3 / fps);
- else _emscripten_set_main_loop_timing(1, 1);
- Browser.mainLoop.scheduler();
- }
- if (simulateInfiniteLoop) {
- throw "unwind";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment