I have an iPhone app that mainly uses standard controls. There is one view that appears in OpenGL (fancy graphics). In the interest of saving a lot of work, I looked around and found Cocos2d. It looks like it has exactly what I need (OpenGL ES, relatively easy to use), but I ran into a problem.
I do not know how to limit Cocos2d to existing in only one view. I used to implement my OpenGL stuff in a presentation and used a simple ViewController to present it in a navigation controller. Here Cocos2d seems to need to do a lot more, and I'm not sure how to do it in the same ViewController / View approach that I did with raw OpenGL.
UPDATE: This is what I did. Flip to the next in 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;
- (void)loadView {
CC_DIRECTOR_INIT();
CCDirector *director = [CCDirector sharedDirector];
[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
[director setDisplayFPS:true];
EAGLView *cocosView = [director openGLView];
[cocosView setMultipleTouchEnabled:true];
self.view = cocosView;
[CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];
[[CCDirector sharedDirector] runWithScene:[SomeScene scene]];
}
- (void)didReceiveMemoryWarning {
[[CCDirector sharedDirector] purgeCachedData];
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[[CCDirector sharedDirector] release];
[window release];
[super dealloc];
}
@end
And finally, as I try to present it (at the click of a button):
CocosController *cocosController = [[CocosController alloc] init];
[[self navigationController] pushViewController:cocosController animated:true];
This is a new approach, but nonetheless.
ApplicationDidFinish part of my AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:kCCDirectorTypeThreadMainLoop];
CCDirector *director = [CCDirector sharedDirector];
[director setDisplayFPS:YES];
state = kStateEnd;
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
return YES;
}
CocosController, which I use to represent the view:
#import "CocosController.h"
#import "cocos2d.h"
#import "SomeScene.h"
@implementation CocosController
@synthesize mainView;
- (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 {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[super dealloc];
}
@end
And this bit from the click button in the ViewController detail that I have that should do all of this:
CocosController *cc = [[CocosController alloc]init];
cc.mainView = self.view;
[[self navigationController] pushViewController:cc animated:false];