engine.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. Function('return this')()['Engine'] = (function() {
  2. var preloader = new Preloader();
  3. var wasmExt = '.wasm';
  4. var unloadAfterInit = true;
  5. var loadPath = '';
  6. var loadPromise = null;
  7. var initPromise = null;
  8. var stderr = null;
  9. var stdout = null;
  10. var progressFunc = null;
  11. function load(basePath) {
  12. if (loadPromise == null) {
  13. loadPath = basePath;
  14. loadPromise = preloader.loadPromise(basePath + wasmExt);
  15. preloader.setProgressFunc(progressFunc);
  16. requestAnimationFrame(preloader.animateProgress);
  17. }
  18. return loadPromise;
  19. };
  20. function unload() {
  21. loadPromise = null;
  22. };
  23. /** @constructor */
  24. function Engine() {
  25. this.canvas = null;
  26. this.executableName = '';
  27. this.rtenv = null;
  28. this.customLocale = null;
  29. this.resizeCanvasOnStart = false;
  30. this.onExit = null;
  31. };
  32. Engine.prototype.init = /** @param {string=} basePath */ function(basePath) {
  33. if (initPromise) {
  34. return initPromise;
  35. }
  36. if (loadPromise == null) {
  37. if (!basePath) {
  38. initPromise = Promise.reject(new Error("A base path must be provided when calling `init` and the engine is not loaded."));
  39. return initPromise;
  40. }
  41. load(basePath);
  42. }
  43. var config = {};
  44. if (typeof stdout === 'function')
  45. config.print = stdout;
  46. if (typeof stderr === 'function')
  47. config.printErr = stderr;
  48. var me = this;
  49. initPromise = new Promise(function(resolve, reject) {
  50. config['locateFile'] = Utils.createLocateRewrite(loadPath);
  51. config['instantiateWasm'] = Utils.createInstantiatePromise(loadPromise);
  52. Godot(config).then(function(module) {
  53. me.rtenv = module;
  54. if (unloadAfterInit) {
  55. unload();
  56. }
  57. resolve();
  58. config = null;
  59. });
  60. });
  61. return initPromise;
  62. };
  63. /** @type {function(string, string):Object} */
  64. Engine.prototype.preloadFile = function(file, path) {
  65. return preloader.preload(file, path);
  66. };
  67. /** @type {function(...string):Object} */
  68. Engine.prototype.start = function() {
  69. // Start from arguments.
  70. var args = [];
  71. for (var i = 0; i < arguments.length; i++) {
  72. args.push(arguments[i]);
  73. }
  74. var me = this;
  75. return me.init().then(function() {
  76. if (!me.rtenv) {
  77. reject(new Error('The engine must be initialized before it can be started'));
  78. }
  79. if (!(me.canvas instanceof HTMLCanvasElement)) {
  80. me.canvas = Utils.findCanvas();
  81. }
  82. // Canvas can grab focus on click, or key events won't work.
  83. if (me.canvas.tabIndex < 0) {
  84. me.canvas.tabIndex = 0;
  85. }
  86. // Disable right-click context menu.
  87. me.canvas.addEventListener('contextmenu', function(ev) {
  88. ev.preventDefault();
  89. }, false);
  90. // Until context restoration is implemented warn the user of context loss.
  91. me.canvas.addEventListener('webglcontextlost', function(ev) {
  92. alert("WebGL context lost, please reload the page");
  93. ev.preventDefault();
  94. }, false);
  95. // Browser locale, or custom one if defined.
  96. var locale = me.customLocale;
  97. if (!locale) {
  98. locale = navigator.languages ? navigator.languages[0] : navigator.language;
  99. locale = locale.split('.')[0];
  100. }
  101. me.rtenv['locale'] = locale;
  102. me.rtenv['canvas'] = me.canvas;
  103. me.rtenv['thisProgram'] = me.executableName;
  104. me.rtenv['resizeCanvasOnStart'] = me.resizeCanvasOnStart;
  105. me.rtenv['noExitRuntime'] = true;
  106. me.rtenv['onExit'] = function(code) {
  107. if (me.onExit)
  108. me.onExit(code);
  109. me.rtenv = null;
  110. }
  111. return new Promise(function(resolve, reject) {
  112. preloader.preloadedFiles.forEach(function(file) {
  113. Utils.copyToFS(me.rtenv['FS'], file.path, file.buffer);
  114. });
  115. preloader.preloadedFiles.length = 0; // Clear memory
  116. me.rtenv['callMain'](args);
  117. initPromise = null;
  118. resolve();
  119. });
  120. });
  121. };
  122. Engine.prototype.startGame = function(execName, mainPack, extraArgs) {
  123. // Start and init with execName as loadPath if not inited.
  124. this.executableName = execName;
  125. var me = this;
  126. return Promise.all([
  127. this.init(execName),
  128. this.preloadFile(mainPack, mainPack)
  129. ]).then(function() {
  130. var args = ['--main-pack', mainPack];
  131. if (extraArgs)
  132. args = args.concat(extraArgs);
  133. return me.start.apply(me, args);
  134. });
  135. };
  136. Engine.prototype.setWebAssemblyFilenameExtension = function(override) {
  137. if (String(override).length === 0) {
  138. throw new Error('Invalid WebAssembly filename extension override');
  139. }
  140. wasmExt = String(override);
  141. };
  142. Engine.prototype.setUnloadAfterInit = function(enabled) {
  143. unloadAfterInit = enabled;
  144. };
  145. Engine.prototype.setCanvas = function(canvasElem) {
  146. this.canvas = canvasElem;
  147. };
  148. Engine.prototype.setCanvasResizedOnStart = function(enabled) {
  149. this.resizeCanvasOnStart = enabled;
  150. };
  151. Engine.prototype.setLocale = function(locale) {
  152. this.customLocale = locale;
  153. };
  154. Engine.prototype.setExecutableName = function(newName) {
  155. this.executableName = newName;
  156. };
  157. Engine.prototype.setProgressFunc = function(func) {
  158. progressFunc = func;
  159. };
  160. Engine.prototype.setStdoutFunc = function(func) {
  161. var print = function(text) {
  162. if (arguments.length > 1) {
  163. text = Array.prototype.slice.call(arguments).join(" ");
  164. }
  165. func(text);
  166. };
  167. if (this.rtenv)
  168. this.rtenv.print = print;
  169. stdout = print;
  170. };
  171. Engine.prototype.setStderrFunc = function(func) {
  172. var printErr = function(text) {
  173. if (arguments.length > 1)
  174. text = Array.prototype.slice.call(arguments).join(" ");
  175. func(text);
  176. };
  177. if (this.rtenv)
  178. this.rtenv.printErr = printErr;
  179. stderr = printErr;
  180. };
  181. Engine.prototype.setOnExit = function(onExit) {
  182. this.onExit = onExit;
  183. }
  184. Engine.prototype.copyToFS = function(path, buffer) {
  185. if (this.rtenv == null) {
  186. throw new Error("Engine must be inited before copying files");
  187. }
  188. Utils.copyToFS(this.rtenv['FS'], path, buffer);
  189. }
  190. // Closure compiler exported engine methods.
  191. /** @export */
  192. Engine['isWebGLAvailable'] = Utils.isWebGLAvailable;
  193. Engine['load'] = load;
  194. Engine['unload'] = unload;
  195. Engine.prototype['init'] = Engine.prototype.init;
  196. Engine.prototype['preloadFile'] = Engine.prototype.preloadFile;
  197. Engine.prototype['start'] = Engine.prototype.start;
  198. Engine.prototype['startGame'] = Engine.prototype.startGame;
  199. Engine.prototype['setWebAssemblyFilenameExtension'] = Engine.prototype.setWebAssemblyFilenameExtension;
  200. Engine.prototype['setUnloadAfterInit'] = Engine.prototype.setUnloadAfterInit;
  201. Engine.prototype['setCanvas'] = Engine.prototype.setCanvas;
  202. Engine.prototype['setCanvasResizedOnStart'] = Engine.prototype.setCanvasResizedOnStart;
  203. Engine.prototype['setLocale'] = Engine.prototype.setLocale;
  204. Engine.prototype['setExecutableName'] = Engine.prototype.setExecutableName;
  205. Engine.prototype['setProgressFunc'] = Engine.prototype.setProgressFunc;
  206. Engine.prototype['setStdoutFunc'] = Engine.prototype.setStdoutFunc;
  207. Engine.prototype['setStderrFunc'] = Engine.prototype.setStderrFunc;
  208. Engine.prototype['setOnExit'] = Engine.prototype.setOnExit;
  209. Engine.prototype['copyToFS'] = Engine.prototype.copyToFS;
  210. return Engine;
  211. })();