I am totally new to Cocos2d, please forgive my ignorance.
I am trying to adapt to different screen sizes and orientations in my game. So far I am writing for iOS. I have added UISupportedInterfaceOrientations to Info.plist with both landscape and portrait modes. Then I modified RootViewController.mm and changed shouldAutorotateToInterfaceOrientation to return YES for both landscape and portrait:
now when I rotate the device (the simulator), it rotates the view, but the scale does not change, so in landscape mode the top portion of the scene is hidden.
my question is how to deal with this and how to change the size of my view while preserving the aspect ratio in response to orientation change?
I have attached screenshots. as you see the top image shows the landscape mode. I need the game to scale proportionally so that the whole scene is visible vertically.
I also have met the similar issue after updating from 2.0-rc0a to 2.0.2.
When I setup app orientation to landscape (in XCode project settings) the director is initialized in portrait mode.
From debugging I can say that director initializes m_obScreenSize and m_obDesignResolutionSize from UIWindow bounds (portrait orientation).
Would be thankful for right approach.
I had the exact same problem but finally figured it out after many trial and error attempts.
Let me share my workaround to achieve multi-orientation, multi-size.
For iOS:
Comment out assert(m_eResolutionPolicy == kResolutionUnKnown); in cocos2dx/platform/ios/CCEGLView.mm::setContentScaleFactor(...).
Comment out CCAssert(m_bIsRetinaEnabled == false, "can not enable retina while set design resolution size!"); in cocos2dx/platform/CCEGLViewProtocol.cpp::setDesignResolutionSize(...).
Return true in shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation.
In (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation, add:
CGSize s;
if (UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication.statusBarOrientation)) {
s = CGSizeMake(std::max(UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height),
std::min(UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height));
} else {
s = CGSizeMake(std::min(UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height),
std::max(UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height));
}
Add nativeInit(w, h); to cocos2dx/platform/android/java/src_common/org/cocos2dx/lib/Cocos2dxRenderer.java ~~> void onSurfaceChanged(GL10 gl, int w, int h).
Add this code to void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thiz, jint w, jint h):
<pre>
cocos2d::CCEGLView* view = cocos2d::CCDirector::sharedDirector~~>getOpenGLView;
if {
…
} else {
…
if .width != w || view~~>getFrameSize.height != h) {
view~~>setFrameSize(w, h);
view->setDesignResolutionSize(w, h, kResolutionShowAll);
}
}
**Request for cocos2d-x team:**
I see there are few of us that develop apps supporting multiple orientations. The workaround solution works great for me, however it required me to modify the library code.
It would be great if this works without any library code change. (iOS #1 and #2, Android #1) Please consider supporting this case in future releases.
I have tried this, and it seems to work, but is this how to handle orientation change events?
Implement PW H modifications
in main.cpp/void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit, add a postNotification for the orientation change so that PW H’s code looks like this
Add a handler that does whatever needs to be done (views don’t seem to resize by themselves)
// put the view fullscreen, should I be checking CCEGLView frame size instead?
void HelloWorld::orientationChanged(CCObject* obj) {
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
setContentSize(winSize);
}
is this wrong? Any more efficient ways to do it? Can this be applied to rotate a CCTableView and have it resize?
I preferred using separate function instead of nativeInit
as i am observing it is taking lot of time for reloading the textures which are not required for orientation change and i am seeing issues realated to memory leaks
so i modified the above step as
Add nativeSurfacechange(w, h); to cocos2dx/platform/android/java/src_common/org/cocos2dx/lib/Cocos2dxRenderer.java ~~> void onSurfaceChanged.
Add new method void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeSurfacechange;
<pre>
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeSurfacechange
{
cocos2d::CCEGLView* view = cocos2d::CCDirector::sharedDirector~~>getOpenGLView;
if
{
if .width != w || view~~>getFrameSize.height != h)
{
view~~>setFrameSize(w, h);
// other processing required
}
}
}