2

I've got a cocos2d 2 view as the Detail View in a UISplitViewController. For some reason, it's sizing the view as if it had the entire screen, both initially and when I rotate the screen. Here's some made up background:

background

Here's what it looks like when I run it in the simulator:

simulator

Here are all the relevant files:

Game1ViewController.h

#import <UIKit/UIKit.h>
#import "cocos2d/cocos2d.h"

@class Game1Scene;

@interface Game1ViewController : UIViewController {
    //  Game1Scene *_scene;
}

@property (nonatomic, retain) Game1Scene *scene;

@end

Game1ViewController.m


#import "cocos2d/cocos2d.h"
#import "Game1Scene.h"
#import "Game1ViewController.h"
#import "CCSpriteExtensions.h"

@implementation Game1ViewController
@synthesize scene = _scene;

- (CGRect)getRotatedBounds:(UIInterfaceOrientation)toInterfaceOrientation  {
    CGRect screenRect = [self.view bounds];
//    NSLog(@"%f, %f\n", [UIScreen mainScreen].bounds.size.width, self.view.frame.size.height - 88);
    
    CGRect rect = CGRectZero;   
    
    if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        rect = screenRect;
    } else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        rect.size = CGSizeMake( screenRect.size.height, screenRect.size.width );
    }
    return rect;
}

- (void)startCocos2D {
    
    CCDirector *director = [CCDirector sharedDirector];
    CGSize screenSize = [[CCDirector sharedDirector] winSize];
    NSLog(@"\n===========Size=%f, %f\n\n",screenSize.height, screenSize.width);
    
    // Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
    if( ! [director enableRetinaDisplay:YES] )
        CCLOG(@"Retina Display Not supported");
        
    NSLog(@"self.view bounds width, height: %f %f", [self.view bounds].size.width, [self.view bounds].size.height);
    
    CGRect rotatedBounds = [self getRotatedBounds:[UIApplication sharedApplication].statusBarOrientation];
    
    CCGLView *glview  = [ CCGLView  viewWithFrame:rotatedBounds
                                    pixelFormat:kEAGLColorFormatRGB565  // kEAGLColorFormatRGBA8
                                    depthFormat:0                       // GL_DEPTH_COMPONENT16_OES
                        ];
        
    [self.view addSubview:glview];
        
    [director setView:glview];
    
    [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
    
    self.scene = [Game1Scene nodeWithBounds:rotatedBounds];
    CCScene *scene = [CCScene node];
    [scene addChild: self.scene];
    [director runWithScene: scene];
    
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    self.title = @"Play Game1";
//    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//    self.view.autoresizingMask = 1;
    [super viewDidLoad];
    
    [self startCocos2D];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    CGRect rotatedBounds = [self getRotatedBounds: toInterfaceOrientation];
    
    CCDirector *director = [CCDirector sharedDirector];
    CCGLView *glView = [director view];
    float contentScaleFactor = [director contentScaleFactor];
    
    if ( contentScaleFactor != 1 ) {
        rotatedBounds.size.width *= contentScaleFactor;
        rotatedBounds.size.height *= contentScaleFactor;
    }
    glView.frame = rotatedBounds;
    
    [self.scene setBounds:rotatedBounds];
}


- (void)stopCocos2D {
    
    CCDirector *director = [CCDirector sharedDirector];
    
    CCGLView *view = [director view];
    [view removeFromSuperview];
    //[director detach];
    
    [director stopAnimation];
    [director pause];
    //[director setOpenGLView:nil];
    
    // kill the director
    [director end];
}

- (void)viewDidDisappear:(BOOL)animated {
    [self stopCocos2D];
    [super viewDidDisappear:animated];
}
@end

Game1Scene.h

#import &lt;Foundation/Foundation.h>
#import "cocos2d/cocos2d.h"

@interface Game1Scene : CCLayer {
    CGRect bounds_;
}

+ (id) nodeWithBounds:(CGRect)bounds;
- (void)setBounds:(CGRect)bounds;
@end

Game1Scene.m

#import "Game1Scene.h"
#import "CCSpriteExtensions.h"
#import "cocos2d/CCMenuItem.h"

@interface Game1Scene (Private)

- (void)startGame;
- (void)createUI;
- (id)initWithBounds:(CGRect)bounds;

@end

@implementation Game1Scene

+(id) nodeWithBounds:(CGRect)bounds {
    return  [[self alloc] initWithBounds:bounds];
}

- (void) setBounds:(CGRect)bounds {
    NSLog(@"bounds.width, bounds.height: %f , %f", bounds.size.width, bounds.size.height);
    bounds_ = bounds;
    [self createUI];
}

- (id) initWithBounds:(CGRect)bounds {
    if( (self=[super init] )) {
        srand ( time(NULL) );
        
        bounds_ = bounds;
        
        [self startGame];
    }
    return self;
}

- (void) createUI {
    
    [self removeAllChildrenWithCleanup:TRUE];
    
    // Plain dark purple background
    CCSprite *background = [CCSprite spriteWithFile:@"game1-background.png"];
    background.position = ccp(0, 0);
    background.anchorPoint = ccp(0, 0);
    NSLog(@"\n=== bounds= %f, %f\n", bounds_.size.width, bounds_.size.height);
    [background resizeTo:CGSizeMake(bounds_.size.width, bounds_.size.height)];
    [self addChild:background];
    
}

- (void)startGame {
    [self createUI];
}

@end

CCSpriteExtensions.h

/*
 CCSpriteExtensions.h

 http://www.learn-cocos2d.com/tag/ccspriteextensions/
 */
#import "cocos2d/cocos2d.h"

@interface CCSprite (Xtensions)

// Resize to the specified size by setting the scale factors correctly.
- (void)resizeTo:(CGSize) theSize;

@end

CCSpriteExtension.m

/*
 CCSpriteExtensions.m
 
 Source: http://www.learn-cocos2d.com/tag/ccspriteextensions/
 */
#import "CCSpriteExtensions.h"

@implementation CCSprite (Xtensions)

- (void)resizeTo:(CGSize) theSize
{
    CGFloat newWidth = theSize.width;
    CGFloat newHeight = theSize.height;
    
        
    float startWidth = self.contentSize.width;
    float startHeight = self.contentSize.height;
    
    float newScaleX = newWidth/startWidth;
    float newScaleY = newHeight/startHeight;
    
    self.scaleX = newScaleX;
    self.scaleY = newScaleY;
    
}
@end

I took the idea as explained in this question: UISplitViewController on iPad with Storyboards by creating a couple of Cocos2D Scenes in the DetailViews.

Update

I updated to 2.0. My problem sounds like something similar to this: http://www.cocos2d-iphone.org/forum/topic/30797

McKinley
  • 1,123
  • 1
  • 8
  • 18
  • just double-checking: the code above is in the detail view controller and not in some sub-class of UISplitViewController, yes? any chance your storyboard for the view has "wants full screen checked"? – john.k.doe Jun 26 '12 at 22:10
  • It wasn't checked. It's a regular UISplitViewController with Cocos2d scenes for the different DetailViews. –  Jun 27 '12 at 01:28
  • First, in Cocos2D 2.0, use CCGLView. Then if you want to use Interface Builder, you can connect it that way and get rid of the CCGLView initialization code. Try that, and maybe post your CCDirector initialization code also. – SimplyKiwi Jun 27 '12 at 01:32
  • Also, you should update to Cocos2D 2.0 if I wasn't clear in my last comment, it will make things like this way easier. – SimplyKiwi Jun 27 '12 at 01:44
  • I included more source and provided images illustrating the problem. –  Jul 01 '12 at 13:51

2 Answers2

0

I'm using cocos2d v2, and I'm not sure about 1.1, but in v2, CCScene and CCLayer are initing with screen's size:

@implementation CCScene
-(id) init
{
    if( (self=[super init]) ) {
        CGSize s = [[CCDirector sharedDirector] winSize];
        self.ignoreAnchorPointForPosition = YES;
        anchorPoint_ = ccp(0.5f, 0.5f);
        [self setContentSize:s];
    }

    return self;
}
@end

...

 @implementation CCLayer

    #pragma mark Layer - Init

        -(id) init
        {
            if( (self=[super init]) ) {

                CGSize s = [[CCDirector sharedDirector] winSize];
                anchorPoint_ = ccp(0.5f, 0.5f);
                [self setContentSize:s];
                self.ignoreAnchorPointForPosition = YES;

                isTouchEnabled_ = NO;

        }

            return self;
        }

If you whant to change it, just set contentSize property to it.

pasawaya
  • 11,515
  • 7
  • 53
  • 92
SentineL
  • 4,682
  • 5
  • 19
  • 38
0

Have you tried using some standard UIViews just for comparison? Implement the same getRotatedBounds but without all the Cocos2D stuff. This way you could make sure whether the error lies with the SplitViewController/Storyboards or within your Cocos2D handling?

Here's a tutorial on Splitviewcontrollers with Storyboards.

Bersaelor
  • 2,517
  • 34
  • 58