Skip to content

Platform Specific Code

Alex Miyamoto edited this page Apr 23, 2018 · 2 revisions

We separate out platform specific code into its own subdirectory. The build system knows which directories are used by each platform and adds or excludes them accordingly.

Usually Usagi uses composition, rather than interfaces for platform specific behaviour. This is because of the hardware we were running on; performance was more important than build times. Feel free to create interfaces and instantiate platform specific behaviour through a derived class if that better suits your needs.

Do not however simply redefine the a class for each platform as it becomes impossible to track what the "correct" interface is.

Sometimes you will need to include a platform specific header, to do so use one of the following defines which take in the path (excluding the platform specific directory) and the filename. They are defined in common_ps.h

  • API_HEADER - A header file in a graphics library specific directory (e.g. _ogl, _vulkan)
  • FRAGMENT_HEADER - Deprecated, was used when working with the 3DS which had no fragment shaders for code that could be shared on every platform but the 3DS. Do not add new files to _fragment
  • OS_HEADER - Operating specific files (e.g. _win or _switch)
  • AUDIO_HEADER - Files relating to a particular audio system (e.g. _xaudio2 or _dummy)

For example:

#include API_HEADER(Engine/Graphics/Device, GFXDevice_ps.h)
#include OS_HEADER(Engine/Graphics/Device, OpenGLContext.h)

Defines do exist for platform specific code, however PLEASE REFRAIN FROM USING THEM. Sometimes it's necessary, particularly on the PC - but usually it's a sign your code has not properly abstracted out the hardware.

On cleaning up the code it was a pleasant surprise to find these defines were only relatively sparingly; the exception was the networking code. This was the case from early on in the project - it serves as a reminder that and once code gets into that state it stays there.
If you want to ifdef some platform specific code out do so on a branch and fix it before merging into master. As it stands even in its reduced state the networking code may still break the build on other platforms.

There is a complete workflow for platform specific code, and there has been since the inception of Usagi, there is no excuse for more than a smattering of #ifdef PLATFORM_ commands.

Work to be done

  • Move all platform specific code into a seperate directory (Engine/Platform) so we can easily keep repositories for these platforms which are hidden from public view.
  • A physics specific define and set of directories should be added to allow us to switch between physics libraries.
  • The abstraction of the entire networking library needs fixing