customizing_html5_shell.rst 8.0 KB

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