首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSOpenGLView Cocoa (再一次)

NSOpenGLView Cocoa (再一次)
EN

Stack Overflow用户
提问于 2011-03-22 20:02:36
回答 1查看 1.6K关注 0票数 0

我刚接触整个Cocoa/Objective-C malarky (但在*nix上有多年的C/C++经验)。

我正在尝试创建一个填充了NSOpenGLView的基本Cocoa应用程序。我有:

chink.mm

代码语言:javascript
复制
#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

代码语言:javascript
复制
@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来做这件事’的答案。只是让它正确创建窗口的代码。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-03-24 09:24:58

好的,我放弃了这种方法。

走了很长一段路,改成了这样:

chink.mm

代码语言:javascript
复制
#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

代码语言:javascript
复制
@interface chinkNS : NSOpenGLView
{

    NSTimer * animationTimer;
    NSTimeInterval lastTime;

}

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

@end

.xib中的相同设置,除了它是我的opengl视图的子类的标准NSView。

我希望有一个更简单,更优雅的设置,但是你不可能赢得所有的设置。

关着的不营业的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5390870

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档