从viewcontroller启动coco2d OpenGL视图

时间:2012-07-05 16:53:50

标签: ios opengl-es cocos2d-iphone

通常,coco2d OpenGL视图是从appdelegate启动的。

方法1:从appdelegate luanching

- (void) applicationDidFinishLaunching:(UIApplication*)application
 {

  // Init the window
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// Try to use CADisplayLink director
// if it fails (SDK < 3.1) use the default director
if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
    [CCDirector setDirectorType:kCCDirectorTypeDefault];


CCDirector *director = [CCDirector sharedDirector];

// Init the View Controller
viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
viewController.wantsFullScreenLayout = YES;

//
// Create the EAGLView manually
//  1. Create a RGB565 format. Alternative: RGBA8
//  2. depth format of 0 bit. Use 16 or 24 bit for 3d effects, like CCPageTurnTransition
//
//
EAGLView *glView = [EAGLView viewWithFrame:[window bounds]
                               pixelFormat:kEAGLColorFormatRGB565   // kEAGLColorFormatRGBA8
                               depthFormat:0                        // GL_DEPTH_COMPONENT16_OES
                    ];

// attach the openglView to the director
[director setOpenGLView:glView];

但我需要能够从viewcontroller启动OpenGL视图 所以,在我的viewcontroller的viewdidload函数中,我做了这个

方法2:从viewcontroller启动        - (void)viewDidLoad      {

 [super viewDidLoad];

self.view.frame = CGRectMake(0, 0, 320, 480);

// Create an CCGLView with a RGB565 color buffer, and a depth buffer of 0-bits
CCGLView *glView = [CCGLView viewWithFrame:CGRectMake(0,0,320,480)
                               pixelFormat:kEAGLColorFormatRGB565   //kEAGLColorFormatRGBA8
                               depthFormat:0    //GL_DEPTH_COMPONENT24_OES
                        preserveBackbuffer:NO
                                sharegroup:nil
                             multiSampling:NO
                           numberOfSamples:0];

director_ = (CCDirectorIOS*) [CCDirector sharedDirector];


director_.wantsFullScreenLayout = YES;

// Display FSP and SPF
[director_ setDisplayStats:YES];

// set FPS at 60
[director_ setAnimationInterval:1.0/60];

// attach the openglView to the director
[director_ setView:glView];

// for rotation and other messages
[director_ setDelegate:self];

// 2D projection
[director_ setProjection:kCCDirectorProjection2D];
// Run the intro Scene



[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];

// and add the scene to the stack. The director will run it when it automatically when the view is displayed.
[director_ pushScene: [helloworld scene]]; 

method2不工作我没有看到任何场景,没有任何显示。那么问题是如何从viewcontroller中提供OpenGL View?

2 个答案:

答案 0 :(得分:1)

您是否尝试过类引用attachInView:方法?

它的形式是

[director_ attachInView:self.view];

否则你可以通过

来完成
[self.view addSubview:director_.view];

希望这会对你有所帮助。

答案 1 :(得分:0)

因为CCDirectorIOS继承了CCDirector类,后者又继承了uiviewcontroller类,我假设你正在使用uinavigationcontroller,因此只需将director_类推到最后viewdidload

[self.navigationController pushViewController:director_ animated:YES];