我刚接触整个Cocoa/Objective-C malarky (但在*nix上有多年的C/C++经验)。
我正在尝试创建一个填充了NSOpenGLView的基本Cocoa应用程序。我有:
chink.mm
#import <OpenGL/gl.h>
#import "chink.h"
@implementation chinkView
@synthesize window;
- (void) applicationDidFinishLaunching : (NSNotification *)aNotification {
NSLog(@"Danger, Will Robinson! Danger!");
}
- (void) dealloc
{
[window release];
[super dealloc];
}
@end和chink.h
@interface chinkView : NSOpenGLView {
NSWindow *window;
}
@property (assign) NSWindow *window;
@end我有一个.xib设置,如下所示
https://skitch.com/cront/ri625/chinkxib
其中“chink View”设置为Window和File Owner的委托。
我收到了一个“invalid drawable”错误(甚至在应用程序启动完成之前),据我所知,这意味着它试图在窗口之前实例化opengl视图(尽管我可能是错的)。
我想要的是创建窗口并在其中设置opengl视图这一问题的解决方案。一旦这样做了,我绝对没有问题写开放源码(就像我说的,我知道C++),但所有这些NS调用粘性对我来说是陌生的。
我不是在寻找‘为什么你不用SDL/GLUT来做这件事’的答案。只是让它正确创建窗口的代码。
发布于 2011-03-24 09:24:58
好的,我放弃了这种方法。
走了很长一段路,改成了这样:
chink.mm
#import <Cocoa/Cocoa.h>
#import <OpenGL/OpenGL.h>
#import <OpenGL/gl.h>
#import <OpenGL/glu.h>
#import "chink.h"
@implementation chinkNS
- (id)initWithFrame:(NSRect)frameRect
{
NSOpenGLPixelFormat * pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:(NSOpenGLPixelFormatAttribute[]) {
NSOpenGLPFAWindow,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFADepthSize, 32,
nil
}];
if(pixelFormat == NULL)
NSLog(@"pixelFormat == NULL");
[pixelFormat autorelease];
self = [super initWithFrame:frameRect pixelFormat:pixelFormat];
if(self == NULL) {
NSLog(@"Could not initialise self");
return NULL;
}
[[self openGLContext] makeCurrentContext];
[self initGL];
return self;
}
- (void)awakeFromNib
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowWillMiniaturize:) name:NSWindowWillMiniaturizeNotification object:[self window]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidMiniaturize:) name:NSWindowDidMiniaturizeNotification object:[self window]];
animationTimer = [NSTimer scheduledTimerWithTimeInterval:1/60.0 target:self selector:@selector(timerFired) userInfo:NULL repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:animationTimer forMode:NSEventTrackingRunLoopMode];
[[NSRunLoop currentRunLoop] addTimer:animationTimer forMode:NSModalPanelRunLoopMode];
lastTime = [[NSDate date] timeIntervalSince1970];
}
- (void)drawRect:(NSRect)frame
{
NSLog(@"Chink is up and running. Let's do this thing!");
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glFlush();
[self drawGL];
[[self openGLContext] flushBuffer];
}
#pragma mark OpenGL
- (void)initGL { }
- (void)reshapeGL { }
- (void)drawGL { }
- (void)animate:(float)dt { }
#pragma mark Event Handling
- (void)timerFired
{
NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
[self animate:currentTime-lastTime];
lastTime = currentTime;
}
#pragma mark Loose ends
- (void)setFrame:(NSRect)frame
{
[super setFrame:frame];
[self reshapeGL];
}
// To get key events
- (BOOL)acceptsFirstResponder
{
return YES;
}
@end和chink.h
@interface chinkNS : NSOpenGLView
{
NSTimer * animationTimer;
NSTimeInterval lastTime;
}
- (void)initGL;
- (void)reshapeGL;
- (void)drawGL;
- (void)animate:(float)dt;
@end.xib中的相同设置,除了它是我的opengl视图的子类的标准NSView。
我希望有一个更简单,更优雅的设置,但是你不可能赢得所有的设置。
关着的不营业的。
https://stackoverflow.com/questions/5390870
复制相似问题