compiling_for_web.rst 7.1 KB

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