customizing_html5_shell.rst 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. .. _doc_customizing_html5_shell:
  2. Customizing the Web export HTML page
  3. ====================================
  4. Rather than the default HTML page that comes with the export templates, it is
  5. also possible to use a custom HTML page. This allows drastic customization of
  6. the final web presentation and behavior. The path to the custom HTML page is
  7. specified in the export options as ``Html/Custom Html Shell``.
  8. The default HTML page is available in the Godot Engine repository at
  9. `/mist/dist/html/default.html <https://github.com/godotengine/godot/blob/master/misc/dist/html/default.html>`_.
  10. Some simple use-cases where customizing the default page is useful include:
  11. - Loading files from a different directory
  12. - Loading a ``.zip`` file instead of a ``.pck`` file as main pack
  13. - Loading engine files from a different directory than the main pack file
  14. - Loading some extra files before the engine starts, so they are available in
  15. the file system later
  16. - Passing custom "command line" arguments, e.g. ``-s`` to start a MainLoop script
  17. Placeholder substitution
  18. ------------------------
  19. When exporting the game, several placeholders in the HTML page are substituted
  20. by values dependening on the export:
  21. +------------------------------+-----------------------------------------------+
  22. | Placeholder | substituted by |
  23. +==============================+===============================================+
  24. | ``$GODOT_BASENAME`` | Basename of exported files without suffixes, |
  25. | | e.g. ``game`` when exporting ``game.html`` |
  26. +------------------------------+-----------------------------------------------+
  27. | ``$GODOT_DEBUG_ENABLED`` | ``true`` if debugging, ``false`` otherwise |
  28. +------------------------------+-----------------------------------------------+
  29. | ``$GODOT_HEAD_INCLUDE`` | Custom string to include just before the end |
  30. | | of the HTML ``<head>`` element |
  31. +------------------------------+-----------------------------------------------+
  32. The HTML file must evaluate the JavaScript file ``$GODOT_BASENAME.js``. This
  33. file defines a global ``Engine`` object used to start the engine, :ref:`see
  34. below <doc_javascript_engine_object>` for details.
  35. The boot splash image is exported as ``$GODOT_BASENAME.png`` and can be used
  36. e.g. in ``<img />`` elements.
  37. ``$GODOT_DEBUG_ENABLED`` can be useful to optionally display e.g. an output
  38. console or other debug tools.
  39. ``$GODOT_HEAD_INCLUDE`` is substituted with the string specified by the export
  40. option ``Html/Head Include``.
  41. .. _doc_javascript_engine_object:
  42. The ``Engine`` object
  43. ---------------------
  44. The JavaScript global object ``Engine`` is defined by ``$GODOT_BASENAME.js``
  45. and serves as an interface to the engine start-up process.
  46. The object itself has only two methods, ``load()`` and ``unload()``.
  47. ``Engine.load(basePath)``
  48. ~~~~~~~~~~~~~~~~~~~~~~~~~
  49. Loads the engine from the passed base path.
  50. Returns a promise that resolves once the engine is loaded.
  51. ``Engine.unload()``
  52. ~~~~~~~~~~~~~~~~~~~
  53. Unloads the module to free memory. This is called automatically once the
  54. module is instantiated unless explicitly disabled.
  55. ``Engine.isWebGLAvailable(majorVersion = 1)``
  56. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  57. Returns ``true`` if the given major version of WebGL is available,
  58. ``false`` otherwise. Defaults to ``1`` for WebGL 1.0.
  59. Starting an ``Engine`` instance
  60. -------------------------------
  61. The more interesting interface is accessed by instantiating ``Engine`` using
  62. the ``new`` operator:
  63. .. code-block:: js
  64. var engine = new Engine();
  65. This ``Engine`` instance, referred to as ``engine`` with a lower-case ``e``
  66. from here, is a startable instance of the engine, usually a game. To start such
  67. an instance, the global ``Engine`` object must be loaded, then the ``engine``
  68. instance must be initialized and started.
  69. ``engine.init(basePath)``
  70. ~~~~~~~~~~~~~~~~~~~~~~~~~
  71. Initializes the instance. If the engine wasn't loaded yet, a base path
  72. must be passed from which the engine will be loaded.
  73. Returns a promise that resolves once the engine is loaded and initialized.
  74. It can then be started with ``engine.startGame()``
  75. ``engine.preloadFile(file, path)``
  76. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  77. This loads a file so it is available in the file system once the instance
  78. is started. This must be called **before** starting the instance.
  79. If ``file`` is a string, the file will be loaded from that URL. If ``file`` is
  80. an ``ArrayBuffer`` or a view on one, the buffer will used as content of the
  81. file.
  82. If ``path`` is a string, it specifies the path by which the file will be
  83. available. This is mandatory if ``file`` is not a string.
  84. Otherwise, the path is derived from the URL of the loaded file.
  85. Returns a promise that resolves once the file is preloaded.
  86. ``engine.start(arg1, arg2, …)``
  87. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  88. Starts the instance of the engine, handing the passed strings as arguments
  89. to the ``main()`` function. This allows great control over how the engine
  90. is used, but usually the other methods whose names start with ``engine.start``
  91. are simpler to use.
  92. Returns a promise that resolves once the engine started.
  93. ``engine.startGame(mainPack)``
  94. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  95. Starts the game with the main pack loaded from the passed URL string and
  96. starts the engine with it.
  97. If the engine isn't loaded yet, the base path of the passed URL will be
  98. used to load the engine.
  99. Returns a promise that resolves once the game started.
  100. Configuring start-up behaviour
  101. ------------------------------
  102. Beside starting the engine, other methods of the engine instance allow
  103. configuring the behavior:
  104. ``engine.setUnloadAfterInit(enabled)``
  105. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  106. Sets whether the Engine will be unloaded automatically after the instance
  107. is initialized. This frees browser memory by unloading files that are no
  108. longer needed once the instance is initialized. However, if more instances of
  109. the engine will be started, the Engine will have to be loaded again.
  110. Defaults to ``true``.
  111. ``engine.setCanvas(canvasElem)``
  112. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  113. By default, the first canvas element on the page is used for rendering.
  114. By calling this method, another canvas can be specified.
  115. ``engine.setCanvasResizedOnStart(enabled)``
  116. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  117. Sets whether the canvas will be resized to the width and height specified
  118. in the project settings on start. Defaults to ``true``.
  119. ``engine.setLocale(locale)``
  120. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  121. By default, the engine will try to guess the locale to use from the
  122. JavaScript environment. It is usually preferable to use a server-side
  123. user-specified locale, or at least use the locale requested in the HTTP
  124. ``Accept-Language`` header. This method allows specifying such a custom locale
  125. string.
  126. ``engine.setExecutableName(execName)``
  127. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  128. By default, the base name of the loaded engine files is used for the
  129. executable name. This method allows specifying another name.
  130. Customizing the presentation
  131. ----------------------------
  132. The following methods are used to implement the presentation:
  133. ``engine.setProgressFunc(func)``
  134. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  135. This method is used to display download progress. The passed callback
  136. function is called with two number arguments, the first argument specifies
  137. bytes loaded so far, the second argument specifies the total number of bytes
  138. to load.
  139. .. code-block:: js
  140. function printProgress(current, total) {
  141. console.log("Loaded " + current + " of " + total + " bytes");
  142. }
  143. engine.setProgressFunc(printProgress);
  144. If the total is 0, it couldn't be calculated. Possible reasons
  145. include:
  146. - Files are delivered with server-side chunked compression
  147. - Files are delivered with server-side compression on Chromium
  148. - Not all file downloads have started yet (usually on servers without multi-threading)
  149. ``engine.setStdoutFunc(func)``, ``engine.setStderrFunc(func)``
  150. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  151. These methods allow implementing custom behavior for the ``stdout`` and
  152. ``stderr`` streams. The functions passed in will be called with one string
  153. argument specifying the string to print.
  154. .. code-block:: js
  155. function printStderr(text) {
  156. console.warn("Error: " + text);
  157. }
  158. engine.setStderrFunc(printStderr);
  159. These methods should usually only be used in debug pages. The
  160. ``$GODOT_DEBUG_ENABLED`` placeholder can be used to check for this.
  161. By default, ``console.log()`` and ``console.warn()`` are used respectively.
  162. Accessing the Emscripten ``Module``
  163. -----------------------------------
  164. If you know what you're doing, you can access the runtime environment
  165. (Emscripten's ``Module``) as ``engine.rtenv``. Check the official Emscripten
  166. documentation for information on how to use it:
  167. https://kripken.github.io/emscripten-site/docs/api_reference/module.html