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:
Here's what it looks like when I run it in the 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 <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