Horizontal orientation in both Android and iPhone builds

Hello

I came across a rather peculiar behavior and I am wondering if this is a bug or this is by design.

What I was trying to do is to get the project to run fullscreen on both iPhone and Android.

From what I gathered from the forums in order to have horizontal orientation on Android I figured out that I need to:
* set @android:screenOrientation="landscape" in the manifest file
* uncomment the view->create(480, 320); line in android/jni/project/main.cpp

In order to have horizontal orientation for iPhone I figured I needed to have pDirector->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft); in bool AppDelegate::applicationDidFinishLaunching() in the AppDelegate.

However, from what I see in the libs/cocos2dx/platform/android/CCApplication_android.cpp file the applicationDidFinishLaunching method is called by Android as well so the setDeviceOrientation call will mess everything up by rotating everything once more:

if (! initInstance() || ! applicationDidFinishLaunching())

So basically what I did was guard the setDeviceOrientation call in the app delegate with #if conditionals:

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    pDirector->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft);
#endif

I am wondering, is this the right thing to do? What throws me off is that the initInstance section that deals with iPhone is empty and looks like this:

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
        // OpenGLView initialized in testsAppDelegate.mm on ios platform, nothing need to do here.
#endif  // CC_PLATFORM_IOS

Why not just move the setDeviceOrientation call there?

I remember that since cocos2d-x v0.8.5, we use RootViewController to control the orientation on ios devices, abandon the approach of invoking setDeviceOrientation in AppDelegate.
If you create a new project from xcode templates, in yougame/ios/RootViewController.mm, there’s a method which can decide the orientation

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return UIInterfaceOrientationIsLandscape( interfaceOrientation );
}

This way can make your game easily integrate with GameCenter, OpenFeint and other frameworks using UIView.

Hmm… That makes sense.

However, the XCode 3 template does not have a RootViewController file, only projects created with XCode4 have that. I guess the XCode3 template needs to be updated.

I’ll give it a try and if I can update the templates I will send a pull request. Never tried that before, though. :slight_smile:

Nevermind, looks like the repo version of the XCode3 template has been updated with the RootViewController.