custom_platform_ports.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. .. _doc_custom_platform_ports:
  2. Custom platform ports
  3. =====================
  4. Similar to :ref:`doc_custom_modules_in_cpp`, Godot's multi-platform architecture
  5. is designed in a way that allows creating platform ports without modifying any
  6. existing source code.
  7. An example of a custom platform port distributed independently from the engine
  8. is `FRT <https://github.com/efornara/frt>`__, which targets single-board
  9. computers. Note that this platform port currently targets Godot 3.x; therefore,
  10. it does not use the :ref:`class_DisplayServer` abstraction that is new in Godot 4.
  11. Some reasons to create custom platform ports might be:
  12. - You want to port your game to consoles
  13. (see also the `Godot website on console support <https://godotengine.org/consoles/>`_),
  14. but wish to write the platform layer yourself. This is a long and arduous process, as it
  15. requires signing NDAs with console manufacturers, but it allows you to have
  16. full control over the console porting process.
  17. - You want to port Godot to an exotic platform that isn't currently supported.
  18. If you have questions about creating a custom platform port, feel free to ask in
  19. the ``#platforms`` channel of the
  20. `Godot Contributors Chat <https://chat.godotengine.org/channel/platforms>`__.
  21. .. note::
  22. Godot is a modern engine with modern requirements. Even if you only
  23. intend to run simple 2D projects on the target platform, it still requires
  24. an amount of memory that makes it unviable to run on most retro consoles.
  25. For reference, in Godot 4, an empty project with nothing visible requires
  26. about 100 MB of RAM to run on Linux (50 MB in headless mode).
  27. If you want to run Godot on heavily memory-constrained platforms, older
  28. Godot versions have lower memory requirements. The porting process is
  29. similar, with the exception of :ref:`class_DisplayServer` not being split
  30. from the :ref:`class_OS` singleton.
  31. Official platform ports
  32. -----------------------
  33. The official platform ports can be used as a reference when creating a custom platform port:
  34. - `Windows <https://github.com/godotengine/godot/tree/master/platform/windows>`__
  35. - `macOS <https://github.com/godotengine/godot/tree/master/platform/macos>`__
  36. - `Linux/\*BSD <https://github.com/godotengine/godot/tree/master/platform/linuxbsd>`__
  37. - `Android <https://github.com/godotengine/godot/tree/master/platform/android>`__
  38. - `iOS <https://github.com/godotengine/godot/tree/master/platform/ios>`__
  39. - `Web <https://github.com/godotengine/godot/tree/master/platform/web>`__
  40. While platform code is usually self-contained, there are exceptions to this
  41. rule. For instance, audio drivers that are shared across several platforms and
  42. rendering drivers are located in the
  43. `drivers/ folder <https://github.com/godotengine/godot/tree/master/drivers>`__
  44. of the Godot source code.
  45. Creating a custom platform port
  46. -------------------------------
  47. Creating a custom platform port is a large undertaking which requires prior
  48. knowledge of the platform's SDKs. Depending on what features you need, the
  49. amount of work needed varies:
  50. Required features of a platform port
  51. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  52. At the very least, a platform port must have methods from the :ref:`class_OS`
  53. singleton implemented to be buildable and usable for headless operation.
  54. A ``logo.svg`` (32×32) vector image must also be present within the platform
  55. folder. This logo is displayed in the Export dialog for each export preset
  56. targeting the platform in question.
  57. See `this implementation <https://github.com/godotengine/godot/blob/master/platform/linuxbsd/os_linuxbsd.cpp>`__
  58. for the Linux/\*BSD platform as an example. See also the
  59. `OS singleton header <https://github.com/godotengine/godot/blob/master/core/os/os.h>`__
  60. for reference.
  61. .. note::
  62. If your target platform is UNIX-like, consider inheriting from the ``OS_Unix``
  63. class to get much of the work done automatically.
  64. If the platform is not UNIX-like, you might use the
  65. `Windows port <https://github.com/godotengine/godot/blob/master/platform/windows/os_windows.cpp>`__
  66. as a reference.
  67. **detect.py file**
  68. A ``detect.py`` file must be created within the platform's folder with all
  69. methods implemented. This file is required for SCons to detect the platform as a
  70. valid option for compiling. See the
  71. `detect.py file <https://github.com/godotengine/godot/blob/master/platform/linuxbsd/detect.py>`__
  72. for the Linux/\*BSD platform as an example.
  73. All methods should be implemented within ``detect.py`` as follows:
  74. - ``is_active()``: Can be used to temporarily disable building for a platform.
  75. This should generally always return ``True``.
  76. - ``get_name()``: Returns the platform's user-visible name as a string.
  77. - ``can_build()``: Return ``True`` if the host system is able to build for the
  78. target platform, ``False`` otherwise. Do not put slow checks here, as this is
  79. queried when the list of platforms is requested by the user. Use
  80. ``configure()`` for extensive dependency checks instead.
  81. - ``get_opts()``: Returns the list of SCons build options that can be defined by
  82. the user for this platform.
  83. - ``get_flags()``: Returns the list of overridden SCons flags for this platform.
  84. - ``configure()``: Perform build configuration, such as selecting compiler
  85. options depending on SCons options chosen.
  86. Optional features of a platform port
  87. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  88. In practice, headless operation doesn't suffice if you want to see anything on
  89. screen and handle input devices. You may also want audio output for most
  90. games.
  91. *Some links on this list point to the Linux/\*BSD platform implementation as a reference.*
  92. - One or more `DisplayServers <https://github.com/godotengine/godot/blob/master/platform/linuxbsd/x11/display_server_x11.cpp>`__,
  93. with the windowing methods implemented. DisplayServer also covers features such
  94. as mouse support, touchscreen support and tablet driver (for pen input).
  95. See the
  96. `DisplayServer singleton header <https://github.com/godotengine/godot/blob/master/servers/display_server.h>`__
  97. for reference.
  98. - For platforms not featuring full windowing support (or if it's not relevant
  99. for the port you are making), most windowing functions can be left mostly
  100. unimplemented. These functions can be made to only check if the window ID is
  101. ``MAIN_WINDOW_ID`` and specific operations like resizing may be tied to the
  102. platform's screen resolution feature (if relevant). Any attempt to create
  103. or manipulate other window IDs can be rejected.
  104. - *If the target platform supports the graphics APIs in question:* Rendering
  105. context for `Vulkan <https://github.com/godotengine/godot/blob/master/platform/linuxbsd/x11/rendering_context_driver_vulkan_x11.cpp>`__,
  106. `Direct3D 12 <https://github.com/godotengine/godot/blob/master/drivers/d3d12/rendering_context_driver_d3d12.cpp>`__
  107. `OpenGL 3.3 or OpenGL ES 3.0 <https://github.com/godotengine/godot/blob/master/platform/linuxbsd/x11/gl_manager_x11.cpp>`__.
  108. - Input handlers for `keyboard <https://github.com/godotengine/godot/blob/master/platform/linuxbsd/x11/key_mapping_x11.cpp>`__
  109. and `controller <https://github.com/godotengine/godot/blob/master/platform/linuxbsd/joypad_linux.cpp>`__.
  110. - One or more `audio drivers <https://github.com/godotengine/godot/blob/master/drivers/pulseaudio/audio_driver_pulseaudio.cpp>`__.
  111. The audio driver can be located in the ``platform/`` folder (this is done for
  112. the Android and Web platforms), or in the ``drivers/`` folder if multiple
  113. platforms may be using this audio driver. See the
  114. `AudioServer singleton header <https://github.com/godotengine/godot/blob/master/servers/audio_server.h>`__
  115. for reference.
  116. - `Crash handler <https://github.com/godotengine/godot/blob/master/platform/linuxbsd/crash_handler_linuxbsd.cpp>`__,
  117. for printing crash backtraces when the game crashes. This allows for easier
  118. troubleshooting on platforms where logs aren't readily accessible.
  119. - `Text-to-speech driver <https://github.com/godotengine/godot/blob/master/platform/linuxbsd/tts_linux.cpp>`__
  120. (for accessibility).
  121. - `Export handler <https://github.com/godotengine/godot/tree/master/platform/linuxbsd/export>`__
  122. (for exporting from the editor, including :ref:`doc_one-click_deploy`).
  123. Not required if you intend to export only a PCK from the editor, then run the
  124. export template binary directly by renaming it to match the PCK file. See the
  125. `EditorExportPlatform header <https://github.com/godotengine/godot/blob/master/editor/export/editor_export_platform.h>`__
  126. for reference.
  127. ``run_icon.svg`` (16×16) should be present within the platform folder if
  128. :ref:`doc_one-click_deploy` is implemented for the target platform. This icon
  129. is displayed at the top of the editor when one-click deploy is set up for the
  130. target platform.
  131. If the target platform doesn't support running Vulkan, Direct3D 12, OpenGL 3.3,
  132. or OpenGL ES 3.0, you have two options:
  133. - Use a library at runtime to translate Vulkan or OpenGL calls to another graphics API.
  134. For example, `MoltenVK <https://moltengl.com/moltenvk/>`__ is used on macOS
  135. to translate Vulkan to Metal at runtime.
  136. - Create a new renderer from scratch. This is a large undertaking, especially if
  137. you want to support both 2D and 3D rendering with advanced features.
  138. Distributing a custom platform port
  139. -----------------------------------
  140. .. danger::
  141. Before distributing a custom platform port, make sure you're allowed to
  142. distribute all the code that is being linked against. Console SDKs are
  143. typically under NDAs which prevent redistribution to the public.
  144. Platform ports are designed to be as self-contained as possible. Most of the
  145. code can be kept within a single folder located in ``platform/``. Like
  146. :ref:`doc_custom_modules_in_cpp`, this allows for streamlining the build process
  147. by making it possible to ``git clone`` a platform folder within a Godot repository
  148. clone's ``platform/`` folder, then run ``scons platform=<name>``. No other steps are
  149. necessary for building, unless third-party platform-specific dependencies need
  150. to be installed first.
  151. However, when a custom rendering driver is needed, another folder must be added
  152. in ``drivers/``. In this case, the platform port can be distributed as a fork of
  153. the Godot repository, or as a collection of several folders that can be added
  154. over a Godot Git repository clone.