I have an iPhone application that is mostly using standard controls. There is one view that is rendered in OpenGL (fancy graphs). In the interest of saving myself a lot of work, I looked around and found Cocos2d. It looks like it has exactly what I need (OpenGL ES, relatively simple to work with), but I've run into a problem.
I don't know how to limit Cocos2d to exist in just a single view. Before, I implemented my OpenGL stuff in a view and used a simple ViewController to present it within a navigation controller. Here, Cocos2d seems to need to do a lot more, and I'm unsure how to stuff it into the same ViewController/View approach I did with raw OpenGL.
UPDATE: This is what I was doing. Flip to the next bold to see what I'm doing now.
#import <UIKit/UIKit.h>
@interface CocosController : UIViewController {
UIWindow *window;
}
@property (nonatomic,retain) UIWindow *window;
@end
And my CocosController.m:
#import "CocosController.h"
#import "SomeScene.h"
#import "cocos2d.h"
@implementation CocosController
@synthesize window;
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
// Initialization code
CC_DIRECTOR_INIT();
CCDirector *director = [CCDirector sharedDirector];
//landscape
[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
[director setDisplayFPS:true];
//turn on multi-touch
EAGLView *cocosView = [director openGLView];
[cocosView setMultipleTouchEnabled:true];
self.view = cocosView;
//default texture formats...
[CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];
[[CCDirector sharedDirector] runWithScene:[SomeScene scene]];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[[CCDirector sharedDirector] purgeCachedData];
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[[CCDirector sharedDirector] release];
[window release];
[super dealloc];
}
@end
And, finally, how I'm trying to present it (in a button click):
CocosController *cocosController = [[CocosController alloc] init];
[[self navigationController] pushViewController:cocosController animated:true];
This is the new approach, same results though.
ApplicationDidFinish portion of my AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:kCCDirectorTypeThreadMainLoop];
CCDirector *director = [CCDirector sharedDirector];
[director setDisplayFPS:YES];
state = kStateEnd;
// Add the navigation controller's view to the window and display.
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
return YES;
}
CocosController I'm using to present the view:
#import "CocosController.h"
#import "cocos2d.h"
#import "SomeScene.h"
@implementation CocosController
@synthesize mainView;
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
EAGLView *glview = [EAGLView viewWithFrame:CGRectMake(0, 0, 250,350)];
[mainView addSubview:glview];
CCDirector *director = [CCDirector sharedDirector];
[director setOpenGLView:glview];
CCScene *scene = [CCScene node];
id node = [SomeScene node];
[scene addChild: node];
[director runWithScene:scene];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
And this bit is from a button click in a details ViewController I have that is supposed to make this whole thing appear:
CocosController *cc = [[CocosController alloc]init];
cc.mainView = self.view;
[[self navigationController] pushViewController:cc animated:false];