-(void)loadOpenglScene:(CGRect)rect { cocos2d::CCApplication::sharedApplication().run(); EAGLView *glView = [EAGLView viewWithFrame: rect pixelFormat: kEAGLColorFormatRGBA8 depthFormat: GL_DEPTH_COMPONENT16_OES preserveBackbuffer: NO sharegroup:nil multiSampling:NO numberOfSamples:0]; [self.view insertSubview:glView atIndex:0]; cocos2d::CCDirector *pDirector = cocos2d::CCDirector::sharedDirector(); pDirector->setOpenGLView(&cocos2d::CCEGLView::sharedOpenGLView()); // enable High Resource Mode(2x, such as iphone4) and maintains low resource on other devices. // pDirector->enableRetinaDisplay(true); // turn on display FPS //pDirector->setDisplayFPS(true); // pDirector->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft); // set FPS. the default value is 1.0/60 if you don't call this pDirector->setAnimationInterval(1.0 / 60);
// create a scene. it's an autorelease object cocos2d::CCScene *pScene_ = cocos2d::MyGraphicsEngine::scene(myparams); // run pDirector->runWithScene(pScene_);
// Retained because if not, we get an BAD ACCESS exception when we try to release the Cocos2d-x environment later [[EAGLView sharedEGLView] retain]; }
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view.
// In this example we use a navigation bar and a "close" button, the Cocos2d-x view will be added below the navigation bar UINavigationBar *bar=[[[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 49)]autorelease]; [self.view insertSubview:bar atIndex:0]; UIBarButtonItem *closeButton=[[[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStyleDonetarget:self action:@selector(close)] autorelease]; UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@""]; item.rightBarButtonItem = closeButton; item.hidesBackButton = YES; [bar pushNavigationItem:item animated:NO]; [item release]; [self loadOpenglScene:CGRectMake(0, bar.frame.size.height, self.view.frame.size.width, self.view.frame.size.height-bar.frame.size.height)]; }
-(void)close { //TODO }
-(void)viewDidDisappear:(BOOL)animated { //Release Cocos2d-x stack cocos2d::CCDirector *pDirector = cocos2d::CCDirector::sharedDirector(); //CCDirector::end() is asynchronous. That's why you can't release [EAGLView sharedEGLView] here after (you'll get a BAD ACCESS exception), and that's why we put it un viewDidUnload. pDirector->end(); [self checkWin]; }
- (void)viewDidUnload { [[EAGLView sharedEGLView] release]; [super viewDidUnload]; // Release any retained subviews of the main view. }