I have some C++ classes that are only used on certain OS (hardware access). Since I am doing cross-platform development I need a way to “switch” between those classes depending on the current OS. What is the recommended way to do this? Are there certain defines that I should use like #ifdef OSX or something?
OK, I had a look at UE4’s engine sources and have an idea - do I have to dig into UE4’s build system for this? Do I have to use Target.Platform etc.?
I find those defintions in source code
PLATFORM_LINUX
PLATFORM_MAC
PLATFORM_WINDOWS
They should work also use UE4 core APIs and avoid using standard C++ function as much as possible, so you can avoid a lot of problems
Maybe you could define an interface to your functionality and just implement it as dummy for the other platforms, if that is what you meant.
UE4 does something like this quite often in headers:
#if PLATFORM_WINDOWS
#include "Windows/WindowsPlatformSplash.h"
#elif PLATFORM_PS4
#include "PS4/PS4Splash.h"
#elif PLATFORM_XBOXONE
#include "XboxOne/XboxOneSplash.h"
#elif PLATFORM_MAC
#include "Mac/MacPlatformSplash.h"
#elif PLATFORM_IOS
#include "IOS/IOSPlatformSplash.h"
#elif PLATFORM_ANDROID
#include "Android/AndroidSplash.h"
#elif PLATFORM_WINRT
#include "WinRT/WinRTSplash.h"
#elif PLATFORM_HTML5
#include "HTML5/HTML5PlatformSplash.h"
#elif PLATFORM_LINUX
#include "Linux/LinuxPlatformSplash.h"
#endif
How do you target “ANDROID” and “IOS” ? Is there “PLATFORM_MOBILE”? I can’t find anything…
If you are going to programm in C++ you should study the C++ preprocessor. At least #define #ifdef #if #elif #endif should be no black magic to you.
Take care the PLATFORM_ defines in UE4 are defined as 0 or 1 not as pure define, so do not use #ifdef in this case.
#if PLATFORM_IOS || PLATFORM_ANDROID
// do mobile specific stuff
#endif
If this gets too repetitive you can create your own define in a header file included in all your classes you put:
#define PLATFORM_MOBILE (PLATFORM_IOS || PLATFORM_ANDROID)