2011-03-22 47 views
0

我是新來的整個可可/ Objective-C malarky(但在* nix上有多年的C/C++)。NSOpenGLView可可(再次)

我想創建一個基本的可可應用程序,它是用NSOpenGLView填充的。我有:

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

其中「縫隙查看'被設置爲窗口和文件所有者的代理。

我得到一個'無效的drawable'錯誤(甚至在應用程序完成啓動之前),這對我的理解意味着它試圖在窗口之前實例化opengl視圖(儘管我可能是錯誤的)。

我想要的是解決這個問題的創建窗口並在其中設置opengl視圖。一旦完成,我完全沒有問題寫OGL(就像我說的,我知道C++),但所有這些NS調用gunk對我來說是陌生的。

我不是在尋找'你爲什麼不用SDL/GLUT'來解答它。只需使代碼正確創建窗口即可。

回答

0

好吧,我解僱了這個方法。

了遠路,改變這樣:

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中的.xib

@interface chinkNS : NSOpenGLView 
{ 

    NSTimer * animationTimer; 
    NSTimeInterval lastTime; 

} 

- (void)initGL; 
- (void)reshapeGL; 
- (void)drawGL; 
- (void)animate:(float)dt; 

@end 

相同的設置,除了它是一個標準NSView的子類我的opengl視圖。

我希望能有一個更簡單,更優雅的設置,但是你不能贏。

已關閉。

相關問題