It is easy to add orientation change to Cocos2dx on Android but it needs some very small changes within core cocos files.
It will be great if these 3 changes can be made in core files so that users don’t need to manually redo them after every new release.
-
Declare a new native function in Cocos2dxRenderer.java:
private static native void nativeSurfaceChanged(final int w, final int h);
-
Call that new function in onSurfaceChanged in Cocos2dxRenderer.java:
public void onSurfaceChanged(final GL10 pGL10, final int pWidth, final int pHeight) { nativeSurfaceChanged(pWidth, pHeight); }
-
Define that new native function in main.cpp that is generated when creating a new Android project:
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeSurfaceChanged(JNIEnv* env, jobject thiz, jint w, jint h)
{
cocos2d::CCEGLView* view = cocos2d::CCDirector::sharedDirector()->getOpenGLView();
if (view)
{
if (view->getFrameSize().width != w || view->getFrameSize().height != h)
{
// Do app specific orientation change coding here. One easy approach is to restart current scene.
// It is app’s responsibility to restore state of elements in their scene and reposition them for new size
// eg: CCDirector::sharedDirector()->replaceScene(currentScene);
}
}
}