2
0

html5_shell_classref.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. .. _doc_html5_shell_classref:
  2. HTML5 shell class reference
  3. ===========================
  4. Projects exported for the Web expose the ``Engine`` class to the JavaScript environment, that allows
  5. fine control over the engine's start-up process.
  6. This API is built in an asynchronous manner and requires basic understanding
  7. of `Promises <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises>`__.
  8. Engine
  9. ------
  10. The ``Engine`` class provides methods for loading and starting exported projects on the Web. For default export
  11. settings, this is already part of the exported HTML page. To understand practical use of the ``Engine`` class,
  12. see :ref:`Custom HTML page for Web export <doc_customizing_html5_shell>`.
  13. Static Methods
  14. ^^^^^^^^^^^^^^
  15. +---------+-------------------------------------------------------------------------------------------------------------------+
  16. | Promise | `Engine.load <#Engine.load>`__ **(** string basePath **)** |
  17. +---------+-------------------------------------------------------------------------------------------------------------------+
  18. | void | `Engine.unload <#Engine.unload>`__ **(** **)** |
  19. +---------+-------------------------------------------------------------------------------------------------------------------+
  20. | boolean | `Engine.isWebGLAvailable <#Engine.isWebGLAvailable>`__ **(** *[ number majorVersion=1 ]* **)** |
  21. +---------+-------------------------------------------------------------------------------------------------------------------+
  22. | void | `Engine.setWebAssemblyFilenameExtension <#Engine.setWebAssemblyFilenameExtension>`__ **(** string extension **)** |
  23. +---------+-------------------------------------------------------------------------------------------------------------------+
  24. Instance Properties
  25. ^^^^^^^^^^^^^^^^^^^
  26. +-----------------------+----------------------------------+
  27. | Emscripten ``Module`` | `engine.rtenv <#engine.rtenv>`__ |
  28. +-----------------------+----------------------------------+
  29. Instance Methods
  30. ^^^^^^^^^^^^^^^^
  31. +---------+------------------------------------------------------------------------------------------------------+
  32. | Engine | `Engine <#Engine>`__ **(** **)** |
  33. +---------+------------------------------------------------------------------------------------------------------+
  34. | Promise | `engine.init <#engine.init>`__ **(** *[ string basePath ]* **)** |
  35. +---------+------------------------------------------------------------------------------------------------------+
  36. | Promise | `engine.preloadFile <#engine.preloadFile>`__ **(** string\|ArrayBuffer file *[, string path ]* **)** |
  37. +---------+------------------------------------------------------------------------------------------------------+
  38. | Promise | `engine.start <#engine.start>`__ **(** *[ string arg1, string arg2, … ]* **)** |
  39. +---------+------------------------------------------------------------------------------------------------------+
  40. | Promise | `engine.startGame <#engine.startGame>`__ **(** string execName, string mainPack **)** |
  41. +---------+------------------------------------------------------------------------------------------------------+
  42. | void | `engine.setUnloadAfterInit <#engine.setUnloadAfterInit>`__ **(** boolean enabled **)** |
  43. +---------+------------------------------------------------------------------------------------------------------+
  44. | void | `engine.setCanvas <#engine.setCanvas>`__ **(** HTMLCanvasElement canvasElem **)** |
  45. +---------+------------------------------------------------------------------------------------------------------+
  46. | void | `engine.setCanvasResizedOnStart <#engine.setCanvasResizedOnStart>`__ **(** boolean enabled **)** |
  47. +---------+------------------------------------------------------------------------------------------------------+
  48. | void | `engine.setLocale <#engine.setLocale>`__ **(** string locale **)** |
  49. +---------+------------------------------------------------------------------------------------------------------+
  50. | void | `engine.setExecutableName <#engine.setExecutableName>`__ **(** string execName **)** |
  51. +---------+------------------------------------------------------------------------------------------------------+
  52. | void | `engine.setProgressFunc <#engine.setProgressFunc>`__ **(** function callback **)** |
  53. +---------+------------------------------------------------------------------------------------------------------+
  54. | void | `engine.setStdoutFunc <#engine.setStdoutFunc>`__ **(** function callback **)** |
  55. +---------+------------------------------------------------------------------------------------------------------+
  56. | void | `engine.setStderrFunc <#engine.setStderrFunc>`__ **(** function callback **)** |
  57. +---------+------------------------------------------------------------------------------------------------------+
  58. Static Method Descriptions
  59. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  60. .. js:method:: Engine.load(basePath)
  61. Load the engine from the specified base path.
  62. :param string basePath:
  63. Base path of the engine to load.
  64. :returns:
  65. Promise which resolves once the engine is loaded.
  66. .. js:method:: Engine.unload()
  67. Unload the engine to free memory.
  68. This method is called automatically once the engine is started unless
  69. explicitly disabled using :js:meth:`engine.setUnloadAfterInit`.
  70. .. js:method:: Engine.isWebGLAvailable([majorVersion = 1])
  71. Check whether WebGL is available. Optionally, specify a particular version of WebGL to check for.
  72. :param number majorVersion:
  73. The major WebGL version to check for. Defaults to ``1`` for *WebGL 1.0*.
  74. :returns:
  75. ``true`` if the given major version of WebGL is available, ``false``
  76. otherwise.
  77. .. js:method:: Engine.setWebAssemblyFilenameExtension(extension)
  78. Set an alternative filename extension for the WebAssembly module. By default
  79. it is assumed to be ``wasm``.
  80. :param string extension:
  81. Filename extension without preceding dot.
  82. Instance Property Descriptions
  83. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  84. .. js:attribute:: engine.rtenv
  85. The runtime environment provided by Emscripten's ``Module``. For more information
  86. refer to the `official documentation <https://emscripten.org/docs/api_reference/module.html>`__ on Emscripten.
  87. Instance Method Descriptions
  88. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  89. .. js:class:: Engine
  90. Create a new instance of the ``Engine`` class.
  91. .. js:method:: engine.init([basePath])
  92. Initialize the engine instance. Optionally, pass the base path to the engine to load it,
  93. if it hasn't been loaded yet. See :js:meth:`Engine.load`.
  94. :param string basePath:
  95. Base path of the engine to load.
  96. :returns:
  97. Promise that resolves once the engine is loaded and initialized.
  98. .. js:method:: engine.preloadFile(file[, path])
  99. Load a file so it is available in the instance's file system once it runs. Must
  100. be called **before** starting the instance.
  101. :param string|ArrayBuffer file:
  102. If type is ``string``, the file will be loaded from that path.
  103. If type is ``ArrayBuffer`` or a view on one, the buffer will used as
  104. the content of the file.
  105. :param string path:
  106. Path by which the file will be accessible. Required, if ``file`` is not
  107. a string. If not passed, the path is derived from the URL of the loaded
  108. file.
  109. :returns:
  110. Promise that resolves once the file is loaded.
  111. .. js:method:: engine.start([arg1, arg2, …])
  112. Start the instance of the engine, using the passed strings as
  113. command line arguments. :js:meth:`engine.startGame` can be used
  114. in typical cases instead.
  115. This will initialize the instance if it is not initialized. For manual
  116. initialization, see :js:meth:`engine.init`. The engine must be loaded beforehand.
  117. Fails if a canvas cannot be found on the page.
  118. :param string variadic:
  119. Command line argument.
  120. :returns:
  121. Promise that resolves once the engine started.
  122. .. js:method:: engine.startGame(execName, mainPack)
  123. Start the game instance using the given executable URL and main pack URL.
  124. This will initialize the instance if it is not initialized. For manual
  125. initialization, see :js:meth:`engine.init`.
  126. This will load the engine if it is not loaded. The base path of the
  127. executable URL will be used as the engine base path.
  128. :param string execName:
  129. Executable name in a form of URL, omitting filename extension.
  130. :param string mainPack:
  131. URL of the main pack to start the game.
  132. :returns:
  133. Promise that resolves once the game started.
  134. .. js:method:: engine.setUnloadAfterInit(enabled)
  135. Specify whether the engine will be unloaded automatically after the
  136. instance is initialized. Enabled by default.
  137. :param boolean enabled:
  138. ``true`` if the engine shall be unloaded after initializing,
  139. ``false`` otherwise.
  140. .. js:method:: engine.setCanvas(canvasElem)
  141. Specify a canvas HTML element to use. By default, the first canvas element
  142. on the page is used for rendering.
  143. :param HTMLCanvasElement canvasElem:
  144. The canvas element to use.
  145. .. js:method:: engine.setCanvasResizedOnStart(enabled)
  146. Specifies whether the canvas will be resized to the width and height
  147. specified in the project settings on start. Enabled by default.
  148. :param boolean enabled:
  149. ``true`` if the canvas shall be resized on start, ``false`` otherwise.
  150. .. js:method:: engine.setLocale(locale)
  151. Specify a language code to select the proper localization for the game.
  152. .. seealso:: Complete list of :ref:`supported locales <doc_locales>`.
  153. :param string locale:
  154. Language code.
  155. .. js:method:: engine.setExecutableName(execName)
  156. Specify the virtual filename of the executable. By default, the base name
  157. of the loaded engine files is used.
  158. This affects the output of :ref:`OS.get_executable_path() <class_OS_method_get_executable_path>`
  159. and sets the automatically started main pack to :file:`{ExecutableName}.pck`.
  160. :param string execName:
  161. Executable name.
  162. .. js:method:: engine.setProgressFunc(callback)
  163. Specify a callback function for displaying download progress. The callback function is
  164. called once per frame, so that the usage of ``requestAnimationFrame()`` is not necessary.
  165. If the callback function receives a total amount of bytes as 0, this means that
  166. it is impossible to calculate. Possible reasons include:
  167. - Files are delivered with server-side chunked compression
  168. - Files are delivered with server-side compression on Chromium
  169. - Not all file downloads have started yet (usually on servers without
  170. multi-threading)
  171. :param function callback:
  172. The callback function must accept two numeric arguments: the amount of bytes
  173. loaded so far, and the total number of bytes to load.
  174. .. js:method:: engine.setStdoutFunc(callback)
  175. Specify a callback function for handling the standard output stream. This method
  176. should usually only be used in debug pages. By default, ``console.log()`` is used.
  177. :param function callback:
  178. The callback function must accept one string argument: the message to print.
  179. .. js:method:: engine.setStderrFunc(callback)
  180. Specify a callback function for handling the standard error stream. This method
  181. should usually only be used in debug pages. By default, ``console.warn()`` is used.
  182. :param function callback:
  183. The callback function must accept one string argument: the message to print.