modules/emscripten/src/library_browser.js in webruby-0.1.2 vs modules/emscripten/src/library_browser.js in webruby-0.2.1

- old
+ new

@@ -4,10 +4,11 @@ mergeInto(LibraryManager.library, { $Browser__deps: ['$PATH'], $Browser__postset: 'Module["requestFullScreen"] = function(lockPointer, resizeCanvas) { Browser.requestFullScreen(lockPointer, resizeCanvas) };\n' + // exports 'Module["requestAnimationFrame"] = function(func) { Browser.requestAnimationFrame(func) };\n' + + 'Module["setCanvasSize"] = function(width, height, noUpdates) { Browser.setCanvasSize(width, height, noUpdates) };\n' + 'Module["pauseMainLoop"] = function() { Browser.mainLoop.pause() };\n' + 'Module["resumeMainLoop"] = function() { Browser.mainLoop.resume() };\n' + 'Module["getUserMedia"] = function() { Browser.getUserMedia() }', $Browser: { mainLoop: { @@ -222,26 +223,36 @@ } }, false); } }, - createContext: function(canvas, useWebGL, setInModule) { + createContext: function(canvas, useWebGL, setInModule, webGLContextAttributes) { #if !USE_TYPED_ARRAYS if (useWebGL) { Module.print('(USE_TYPED_ARRAYS needs to be enabled for WebGL)'); return null; } #endif var ctx; try { if (useWebGL) { - ctx = canvas.getContext('experimental-webgl', { + var contextAttributes = { + antialias: false, + alpha: false + }; + + if (webGLContextAttributes) { + for (var attribute in webGLContextAttributes) { + contextAttributes[attribute] = webGLContextAttributes[attribute]; + } + } + #if GL_TESTING - preserveDrawingBuffer: true, + contextAttributes.preserveDrawingBuffer = true; #endif - alpha: false - }); + + ctx = canvas.getContext('experimental-webgl', contextAttributes); } else { ctx = canvas.getContext('2d'); } if (!ctx) throw ':('; } catch (e) { @@ -449,12 +460,25 @@ } } else { // Otherwise, calculate the movement based on the changes // in the coordinates. var rect = Module["canvas"].getBoundingClientRect(); - var x = event.pageX - (window.scrollX + rect.left); - var y = event.pageY - (window.scrollY + rect.top); + var x, y; + if (event.type == 'touchstart' || + event.type == 'touchend' || + event.type == 'touchmove') { + var t = event.touches.item(0); + if (t) { + x = t.pageX - (window.scrollX + rect.left); + y = t.pageY - (window.scrollY + rect.top); + } else { + return; + } + } else { + x = event.pageX - (window.scrollX + rect.left); + y = event.pageY - (window.scrollY + rect.top); + } // the canvas might be CSS-scaled compared to its backbuffer; // SDL-using content will want mouse coordinates in terms // of backbuffer units. var cw = Module["canvas"].width; @@ -672,10 +696,29 @@ Browser.safeSetTimeout(function() { _emscripten_run_script(script); }, millis); }, + emscripten_async_load_script: function(url, onload, onerror) { + Module['noExitRuntime'] = true; + + onload = Runtime.getFuncWrapper(onload, 'v'); + + assert(runDependencies === 0, 'async_load_script must be run when no other dependencies are active'); + var script = document.createElement('script'); + script.onload = function() { + if (runDependencies > 0) { + dependenciesFulfilled = onload; + } else { + onload(); + } + }; + script.onerror = onerror; + script.src = Pointer_stringify(url); + document.body.appendChild(script); + }, + emscripten_set_main_loop: function(func, fps, simulateInfiniteLoop) { Module['noExitRuntime'] = true; Browser.mainLoop.runner = function() { if (ABORT) return; @@ -708,11 +751,20 @@ if (Module['preMainLoop']) { Module['preMainLoop'](); } - Runtime.dynCall('v', func); + try { + Runtime.dynCall('v', func); + } catch (e) { + if (e instanceof ExitStatus) { + return; + } else { + if (e && typeof e === 'object' && e.stack) Module.printErr('exception thrown: ' + [e, e.stack]); + throw e; + } + } if (Module['postMainLoop']) { Module['postMainLoop'](); } @@ -805,20 +857,33 @@ }, emscripten_set_canvas_size: function(width, height) { Browser.setCanvasSize(width, height); }, + + emscripten_get_canvas_size: function(width, height, isFullscreen) { + var canvas = Module['canvas']; + {{{ makeSetValue('width', '0', 'canvas.width', 'i32') }}}; + {{{ makeSetValue('height', '0', 'canvas.height', 'i32') }}}; + {{{ makeSetValue('isFullscreen', '0', 'Browser.isFullScreen ? 1 : 0', 'i32') }}}; + }, emscripten_get_now: function() { - if (ENVIRONMENT_IS_NODE) { - var t = process['hrtime'](); - return t[0] * 1e3 + t[1] / 1e6; + if (!_emscripten_get_now.actual) { + if (ENVIRONMENT_IS_NODE) { + _emscripten_get_now.actual = function() { + var t = process['hrtime'](); + return t[0] * 1e3 + t[1] / 1e6; + } + } else if (typeof dateNow !== 'undefined') { + _emscripten_get_now.actual = dateNow; + } else if (ENVIRONMENT_IS_WEB && window['performance'] && window['performance']['now']) { + _emscripten_get_now.actual = function() { return window['performance']['now'](); }; + } else { + _emscripten_get_now.actual = Date.now; + } } - else if (ENVIRONMENT_IS_WEB && window['performance'] && window['performance']['now']) { - return window['performance']['now'](); - } else { - return Date.now(); - } + return _emscripten_get_now.actual(); }, emscripten_create_worker: function(url) { url = Pointer_stringify(url); var id = Browser.workers.length;