我目前正在尝试以编程方式创建一个简单的可可 NSWindow,而不是使用 Interface builder(我有这样做的理由)。这是一个快速测试:
int main(int argc, char** argv){
NSWindow *mainwin;
CocoaGLView *mainview;
NSRect scr_frame;
unsigned int style_mask;
NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];
[NSApplication sharedApplication];
scr_frame= NSMakeRect(100, 100, 400, 400);
style_mask=NSClosableWindowMask|NSMiniaturizableWindowMask|
NSResizableWindowMask|NSTitledWindowMask;
scr_frame=[NSWindow contentRectForFrameRect:scr_frame
styleMask:style_mask];
mainwin=[[NSWindow alloc]
initWithContentRect:scr_frame
styleMask:style_mask
backing:NSBackingStoreBuffered
defer:NO];
[mainwin makeKeyAndOrderFront:nil];
[mainwin setTitle:@"Visible screen window"];
mainview=[[CocoaGLView alloc] initWithFrame:scr_frame];
[mainwin setContentView:mainview];
[mainview display];
[mainwin setReleasedWhenClosed:YES];
[pool drain];
[NSApp run];
return 0;
}
CocoaGLView 是从 NSOpenGLView 派生的,看起来像这样:
@interface CocoaGLView : NSOpenGLView {
//some stuff
}
- (id) initWithFrame: (NSRect) frameRect;
- (void)setFrameSize:(NSSize) aSize;
- (void)drawRect:(NSRect) aRect;
@end
它通常有效。我可以看到窗户。我什至可以看到我在 CocoaGLViews drawRect 函数中绘制的 openGL 东西,但不幸的是,该函数只被调用一次,我错过了什么?