Cocos2d在NSWindowController自定义视图中创建CCGLView

时间:2013-02-07 21:10:17

标签: objective-c opengl-es cocos2d-iphone nswindowcontroller

我希望能够让Cocos2d包含在它自己的NSWindowController中,以便AppDelegate能够管理在不同窗口中同时运行的Cocos2d的几个不同实例。

每次我在NSWindowController子类中加载Cocos2d时,我都会在OpenGl视图中看到一个空白视图。

Resulting OpenGL View subclassed to CCGLView

为什么不能正确渲染场景?

Cocos2dWindowController.h

#import <Cocoa/Cocoa.h>
#import "cocos2d.h"

@interface Cocos2dWindowController : NSWindowController <CCDirectorDelegate>{

    CCGLView    *glView_;

}

@property (strong) IBOutlet CCGLView    *glView;

@end

Cocos2dWindowController.mm

#import "Cocos2dWindowController.h"
#import "PuzzleWorld.h"

@interface Cocos2dWindowController ()

@end

@implementation Cocos2dWindowController

@synthesize glView=glView_;

- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {
        // Initialization code here.


    }

    return self;
}


- (void)windowDidLoad
{
    [super windowDidLoad];

    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.

    //Check to see if the view is empty prior to launching
    CCDirectorMac *director = (CCDirectorMac*) [CCDirector sharedDirector];
    // enable FPS and SPF
    [director setDisplayStats:YES];

    // connect the OpenGL view with the director
    [director setView:glView_];

    director.delegate = self;

    // EXPERIMENTAL stuff.
    // 'Effects' don't work correctly when autoscale is turned on.
    // Use kCCDirectorResize_NoScale if you don't want auto-scaling.

    [director setResizeMode:kCCDirectorResize_AutoScale];

    // Enable "moving" mouse event. Default no.
    [self.window setAcceptsMouseMovedEvents:NO];

    // Center main window
    [self.window center];

    CCScene *scene = [CCScene node];

    [scene addChild:[PuzzleWorld node]];


    // Run whatever scene we'd like to run here.
    if(director.runningScene)
        [director replaceScene:scene];
    else
        [director runWithScene:scene];


}

//Override the close action to kill off Cocos2d and OpenGl
- (void)close{

    [super close];

    //Check to see if the view is empty prior to launching
    CCDirectorMac *director = (CCDirectorMac*) [CCDirector sharedDirector];
    [director setDelegate:nil];
    [director end];

}

@end

Xib Configuration

这是在带有Xcode版本4.6(4H127)的Cocos2d 2.0上发生的

1 个答案:

答案 0 :(得分:0)

我无法告诉你为什么你的屏幕是白色的。如果您将其基于Cocos2D Mac模板,请检查MainWindow.xib。 Cocos2D视图横跨整个窗口,因此只需调整它并使用它。

可以告诉您的是,运行多个cocos2d视图的目标是徒劳的:cocos2d仅支持单个视图。

多年来,支持多种观点的路线图已经出现在路线图中,看起来不会很快到来。考虑到cocos2d对于许多核心类(Director,ActionManager,Scheduler,Caches等)的单身人士的依赖程度,这也是一个非常重要的变化。