Hi guys, I’m new to cocos and just wonder what is the guideline in adding platform specific code.
Let say that I created a game -> adding something on the Android project-> change the game and recompile.
Will the change on the Android project reserved and how can I make sure that it always reserved?
Thanks!
Use CC_TARGET_PLATFORM like this:
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
// android specific code here
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
// iOS specific code here
#endif
Possible values for CC_TARGET_PLATFORM are:
CC_PLATFORM_UNKNOWNCC_PLATFORM_IOSCC_PLATFORM_ANDROIDCC_PLATFORM_WIN32CC_PLATFORM_MARMALADECC_PLATFORM_LINUXCC_PLATFORM_BADACC_PLATFORM_BLACKBERRYCC_PLATFORM_MACCC_PLATFORM_NACLCC_PLATFORM_EMSCRIPTENCC_PLATFORM_TIZENCC_PLATFORM_QT5CC_PLATFORM_WINRT
See CCPlatformConfig.h for more information
You should also consider the amount of code that will be different in each platform specific implementation.
If it is only a few lines of code, then Mausmausgames solution can work good.
But if it is a lot of code that is different, then you should consider using platform specific .cpp implementation files.
For example, if there will be lots of code in your implementation that is different for each platform, then consider using a single header file and separate .cpp implementations for each platform where the implementation differs.
I find that having a bunch of preprocessor #if, #elif, #endif … can make code ugly and hard to read and support, if the whole class if full of #if, #elif, #endif.
If you have a lot of code that differs in the implementation, then separate implementation files are the way to go.
Outside of the Classes folder I create a folder called Classes-Android, Classes-iOS, Classes-WP, etc. to hold the platform specific implementations. Then I can have a single .h header file to define the interface for the desired functionality that is located in my Classes folder. Then add the platform specific files only to the necessary platforms.
You can also use this technique to encapsulate away complete platform specific classes.
Then your game code can be clean and free of the nuances of different platforms.
And you probably will be left with some nice reusable code for future projects.
Thank a lot guys!
I just noticed that we have two project folder for android which are proj.android and proj.android-studio. I wonder is the first one for Eclipse and the second one for Android Studio? So to add platform specific code for Android we should just go with one specific project folder?
In a normal use case you wouldn’t add the platform specific code to the project folders. You would add it to the Classes folder or create new folders as @Heyalda suggested.
Like all other .cpp files the new files have to be included into an android project. For example in this file:
proj.android/jni/Android.mk
or here:
proj.android_studio/app/jni/Android.mk
But yes, proj.android can be used with Eclipse (or just use any IDE you like and compile with the cocos command line tool)
And yes, proj.android-studio is intended for android studio.
Thank @mausmausgames!
What if I want to add an Android activity or handle Android push notification. The Android project folders are for that situation, aren’t they?
Yes, you should add java source files into the proj.android/src folder and configure extra activities in the AndroidManifest.xml as necessary.
I haven’t used proj.andrid-studio yet, but for that it would probably be proj.android-studio/app/src.
You can then organize your .java files into package folders within the src folder like you would do for any other Android app.
Then use the Java Native Interface (JNI) to communicate between your Classes-Android/MyClass.cpp and your .java classes.