0

私は Cocoa/Objective-C の悪意全体に慣れていません (ただし、*nix で何年もの C/C++ を使用しています)。

NSOpenGLView で満たされた基本的な Cocoa アプリケーションを作成しようとしています。私は持っている:

隙間.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」は、ウィンドウとファイルの所有者の委任として設定されます。

「無効な描画可能」エラーが発生します (アプリケーションの起動が完了する前であっても)。これは、ウィンドウの前に opengl ビューをインスタンス化しようとしていることを意味します (おそらく間違っています)。

私が欲しいのは、ウィンドウを作成し、その中にopenglビューを設定するというこの問題の解決策です。これが完了したら、OGL を書くことにまったく問題はありません (前述したように、私は C++ を知っています) が、この NS コールはすべて私にとって異質です。

「SDL/GLUTでやってみませんか」という回答を求めているわけではありません。ウィンドウを適切に作成するためのコードだけです。

4

1 に答える 1

0

さて、私はこのアプローチをやめました。

長い道のりをたどり、これに変更しました:

隙間.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

私のopenglビューをサブクラス化する標準のNSViewであることを除いて、.xibの同じ設定。

上記のように、よりシンプルでエレガントなセットアップを望んでいましたが、すべてを勝ち取ることはできません.

閉まっている。

于 2011-03-24T01:24:57.890 に答える