compiling_for_web.rst 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. .. _doc_compiling_for_web:
  2. Compiling for the Web
  3. =====================
  4. .. highlight:: shell
  5. Requirements
  6. ------------
  7. To compile export templates for the Web, the following is required:
  8. - `Emscripten SDK <http://emscripten.org/>`__ (Install in a path without
  9. spaces, i.e. not on "Program Files")
  10. - `Python 2.7+ <https://www.python.org/>`__ (3.0 is
  11. untested as of now)
  12. - `SCons <http://www.scons.org>`__ build system
  13. Compiling
  14. ---------
  15. Start a terminal and set the environment variable ``EMSCRIPTEN_ROOT`` to the
  16. installation directory of Emscripten::
  17. export EMSCRIPTEN_ROOT=~/emsdk/emscripten/master
  18. If you are on Windows, start a regular prompt or the Emscripten Command Prompt.
  19. Do **not** use the Developer Command Prompt nor any of the ones that come with
  20. Visual Studio. You can set the environment variable in the system settings or
  21. in the prompt itself::
  22. set EMSCRIPTEN_ROOT=C:\emsdk\emscripten\master
  23. Now go to the root directory of the engine source code and instruct SCons to
  24. compile for JavaScript. Specify ``target`` as either ``release`` for a release
  25. build or ``release_debug`` for a debug build::
  26. scons platform=javascript tools=no target=release
  27. scons platform=javascript tools=no target=release_debug
  28. The engine will now be compiled to JavaScript by Emscripten. If all goes well,
  29. the resulting file will be placed in the ``bin`` subdirectory. Its name is
  30. ``godot.javascript.opt.asm.js`` for release or
  31. ``godot.javascript.opt.debug.asm.js`` for debug. Additionally, two files of
  32. the same name but with the extensions ``.js`` and ``.html.mem`` will be
  33. generated.
  34. Building export templates
  35. -------------------------
  36. After compiling, further steps are required to build the template.
  37. The actual web export template has the form of a zip file containing at least
  38. these 5 files:
  39. 1. ``godot.asm.js`` — This is the file that was just compiled, but under a
  40. different name.
  41. For the release template::
  42. cp bin/godot.javascript.opt.asm.js godot.asm.js
  43. For the debug template::
  44. cp bin/godot.javascript.opt.debug.asm.js godot.asm.js
  45. 2. ``godot.js``
  46. 3. ``godot.mem`` — other files created during compilation, initially with the
  47. same name as the ``.asm.js`` file, except ``.asm.js`` is replaced by
  48. ``.js`` for ``godot.js`` and ``.html.mem`` for
  49. ``godot.mem``.
  50. For the release template::
  51. cp bin/godot.javascript.opt.js godot.js
  52. cp bin/godot.javascript.opt.html.mem godot.mem
  53. For the debug template::
  54. cp bin/godot.javascript.opt.debug.js godot.js
  55. cp bin/godot.javascript.opt.debug.html.mem godot.mem
  56. 4. ``godot.html``
  57. 5. ``godotfs.js`` — Both of these files are located within the Godot Engine
  58. repository, under ``tools/dist/html_fs/``.
  59. ::
  60. cp tools/dist/html_fs/godot.html .
  61. cp tools/dist/html_fs/godotfs.js .
  62. Once these 5 files are assembled, zip them up and your export template is ready
  63. to go. The correct name for the template file is ``javascript_release.zip`` for
  64. the release template::
  65. zip javascript_release.zip godot.asm.js godot.js godot.mem godotfs.js godot.html
  66. And ``javascript_debug.zip`` for the debug template::
  67. zip javascript_debug.zip godot.asm.js godot.js godot.mem godotfs.js godot.html
  68. The resulting files must be placed in the ``templates`` directory in your Godot
  69. user directory::
  70. mv javascript_release.zip ~/.godot/templates
  71. mv javascript_debug.zip ~/.godot/templates
  72. If you are writing custom modules or using custom C++ code, you may want to
  73. configure your zip files as custom export templates. This can be done in the
  74. export GUI, using the "Custom Package" option.
  75. There's no need to copy the templates in this case — you can simply reference
  76. the resulting files in your Godot source folder, so the next time you build,
  77. the custom templates will already be referenced.
  78. Customizing the HTML page
  79. -------------------------
  80. Rather than the default ``godot.html`` file from the Godot Engine repository's
  81. ``tools/dist/html_fs/`` directory, it is also possible to use a custom HTML
  82. page. This allows drastic customization of the final web presentation.
  83. In the HTML page, the JavaScript object ``Module`` is the page's interface to
  84. Emscripten. Check the official documentation for information on how to use it:
  85. https://kripken.github.io/emscripten-site/docs/api_reference/module.html
  86. The default HTML page offers an example to start off with, separating the
  87. Emscripten interface logic in the JavaScript ``Module`` object from the page
  88. logic in the ``Presentation`` object. Emscripten's default ``shell.html`` file
  89. is another example, but does not use Godot's placeholders, listed below.
  90. When exporting a game, several placeholders in the ``godot.html`` file are
  91. substituted by values dependent on the export:
  92. +------------------------------+-----------------------------------------------+
  93. | Placeholder | substituted by |
  94. +==============================+===============================================+
  95. | ``$GODOT_JS`` | Name of the compiled Godot Engine JavaScript |
  96. | | file |
  97. +------------------------------+-----------------------------------------------+
  98. | ``$GODOT_FS`` | Name of the filesystem access JavaScript |
  99. | | file |
  100. +------------------------------+-----------------------------------------------+
  101. | ``$GODOT_MEM`` | Name of the memory initialization file |
  102. +------------------------------+-----------------------------------------------+
  103. | ``$GODOT_CANVAS_WIDTH`` | Integer specifying the initial display width |
  104. | | of the game |
  105. +------------------------------+-----------------------------------------------+
  106. | ``$GODOT_CANVAS_HEIGHT`` | Integer specifying the initial display height |
  107. | | of the game |
  108. +------------------------------+-----------------------------------------------+
  109. | ``$GODOT_DEBUG_ENABLED`` | String ``true`` if debugging, ``false`` |
  110. | | otherwise |
  111. +------------------------------+-----------------------------------------------+
  112. | ``$GODOT_CONTROLS_ENABLED`` | String ``true`` if ``html/controls_enabled`` |
  113. | | is enabled, ``false`` otherwise |
  114. +------------------------------+-----------------------------------------------+
  115. | ``$GODOT_HEAD_TITLE`` | Title of the page, normally used as content |
  116. | | of the HTML ``<title>`` element |
  117. +------------------------------+-----------------------------------------------+
  118. | ``$GODOT_HEAD_INCLUDE`` | Custom string to include just before the end |
  119. | | of the HTML ``<head>`` element |
  120. +------------------------------+-----------------------------------------------+
  121. | ``$GODOT_STYLE_FONT_FAMILY`` | CSS format ``font-family`` to use, without |
  122. | | terminating semicolon |
  123. +------------------------------+-----------------------------------------------+
  124. | ``$GODOT_STYLE_INCLUDE`` | Custom string to include just before the end |
  125. | | of the page's CSS |
  126. +------------------------------+-----------------------------------------------+
  127. The first five of the placeholders listed should always be implemented in the
  128. HTML page, since they are important for the correct presentation of the game.
  129. The other placeholders are optional.
  130. Finally, the custom HTML page is installed by replacing the existing
  131. ``godot.html`` file in the export template with the new one, retaining the name
  132. of the original.