Autorotation in v0.99.5 templates

In order to be iPad compliant, v0.99.5 will include updated templates that support autorotation.

Basically the new templates will have 3 new files:

  • GameConfig.h
  • RootViewController.m (and .h)

The RootViewController.* files contain the code to handle the autorotation.

And in GameConfig.h you will be able to configure how to handle the autorotation:

  • No autorotation
  • Autorotation handled by cocos2d
  • Autorotation handled by UIViewController (default)

No Autorotation: It means just that. The EAGLView won’t be autorotated

Autorotation by cocos2d: The EAGLView will be “rotated” using CCDirector#setDeviceOrientation.

  • [+]: It is the fatest way to “rotate” the EAGLView since it uses one glRotatef() command (technically the EAGLView is not rotated).
  • [-]: If you are going to add UIKit objects to the EAGLView, then you will need to rotate them manually.

Autorotation by UIViewController: The EAGLView will be rotated by the UIViewController

  • [-]: The UIViewController will rotate the EAGLView and it is bit slower than using a single glRotate() command.
  • [+]: There is no need to rotate any UIKit object contained by the EAGLView.

If you want to understand how to implement autorotation manually, or if you are interested in understanding the technical details, I suggest reading this article:

And if you want to try the new templates now, then you should download the latest code from the “develop” branch, and reinstall the templates.

Update: Bill Hollings wrote an interesting article regarding autorotation where he describes another approach. Includes sample code: cocos2d an UIViewControllers

8 Responses to “Autorotation in v0.99.5 templates”


  • @riq – tried out the new template and autorotate works great, however I lost my hires scaling. If I use 2.0 for the CCDirector scale it’s like 4 times too big on the iPhone 4 simulator. What am I doing wrong? The same code worked fine before implementing the RootViewController (i.e. The graphics scaled fine and you could easily tell the iPhone 4 was using the larger images). Does the RootViewController mess with the scaling of CCDirector?
    Thanks

  • I did try the develop branch and I noticed a frame drop when using kGameAutorotationUIViewController.

    I thought the “bit slower” part would only affect the rotation.. but it affects the app all the time.

    I added following code to a new template project and tested on a iphone 2g (1st gen)

    CCSprite *background = [CCSprite spriteWithFile:@"Default.png"];
    background.anchorPoint = CGPointZero;
    [self addChild:background];

    result:
    kGameAutorotationNone 60fps
    kGameAutorotationCCDirector 60fps
    kGameAutorotationUIViewController 40fps

  • Yes, on older devices the performance of the UIViewController is not so good. This article explains a little bit how it works.

    It also mentions that UIViewController autorotation doesn’t perform OK on older devices.

  • Been using the example code from Bug 914 for a few weeks. The performance drop is there but not unacceptable in my case. Only problem I ran into was the return of the ‘flicker’ at startup. I haven’t spent too much time on it, but the old workaround doesn’t seem to work.

  • @wefiends: could you open a bug attaching the way to reproduce it ? Thanks.

  • Hi Riq…

    I just noticed your movement towards auto-rotation here. I thought I’d bring to your attention a similar framework that we developed at Brenwill:

    http://brenwill.com/2010/cocos2d-and-uiviewcontrollers/

    I’d be happy to have you pick out and incorporate any features that you might find useful as you develop your framework for this. From a quick review, our controller framework has the following characteristics relative to what you seem to be developing:

    - we introduced a layer protocol and base class to allow layers to participate in the rotation. You could move this directly into the CCNode or CCLayer base classes to remove the need for a separate protocol or base class.

    - the layer is aware of the controller so that it can turn rotation on or off (and device camera overlay control…see further down) from say a child user button.

    - the controller is aware of the active layer and notifies it of changes in orientation. The layer automatically handles its own adjustments in contentSize to transpose it when moving from portrait to landscape or back again.

    - game layer subclasses are provided with a callback hook on change in contentSize so that they can move children around if needed on rotation (say to keep something centered or in the bottom right corner).

    - auto-rotation can be dynamically toggled at any time (say via a user-controlled button…or scene to scene) with a single UIViewController instance

    - the orientation does not need to be forced to portrait at the start. The framework figures out the orientation automatically and marks a homeContentSize. Actually, in keeping with the nature of dynamic rotation, the application need not even be aware of device orientation parameters.

    - our controller generally does not need customization per application (except in rare occurances)

    - seems to require less coding for each application to set up auto-rotation

    - bypasses shouldAutorotateToInterfaceOrientation: and uses NSNotifications, so that the controller does not attempt to rotate the view

    - does NOT support UIView rotations…so UIKit overlays are not auto-rotated. We do not use UIKit objects with GL because of the degradation in performance…but I can understand why you are attempting this.

    - supports device camera overlay (augmented reality). This is the other big reason for introducing UIViewControllers into the cocos2d mix. This can be toggled on or off easily via a user button since the layer knows about the controller.

    I don’t want to interfere with the direction you’re taking…but I would like to post our framework to the cocos2d forum if you have no objections. At the very least it provides a reasonable alternative for developers that could use device camera overlay support, or do not require UIKit auto-rotation.

    And as I mentioned above, feel free to pick it apart and take anything useful for your auto-rotation development in the core cocos2d code.

    Thanks…

    …Bill

  • @Bill
    Interesting approach. Yes, please, feel free to post it on the forum. I think many people will find it useful. Thanks.

  • @riq

    Thanks for the nod in your post update above, riq.

    …Bill

Leave a Reply




Social Widgets powered by AB-WebLog.com.