Domanda

Sono nuovo per l'intero cacao/obiettivo-c malarky (ma ho anni di c/c ++ su *nix).

Sto cercando di creare un'applicazione di cacao di base che è piena di un NSOPENGLVIEW. Io ho:

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

e chink.h

@interface chinkView : NSOpenGLView {

    NSWindow *window;

}


@property (assign) NSWindow *window;


@end

Ho un singolo .xib impostato come

https://skitch.com/cront/ri625/chinkxib

dove "Chink View" è impostato come delegato per la finestra e il proprietario del file.

Sto ricevendo un errore "non valido disegnabile" (anche prima che l'applicazione finisca il lancio), il che per mia comprensione significa che sta cercando di istanziare la vista aperta prima della finestra (anche se probabilmente sbaglio).

Quello che voglio è la soluzione a questo problema di creare la finestra e di impostare la vista OpenGL al suo interno. Una volta fatto questo non ho assolutamente problemi a scrivere l'OGL (come ho detto, lo so C ++) ma tutto questo NS chiama Gunk è estraneo a me.

Non sto cercando "perché non lo fai con le risposte SDL/Glut" per favore. Solo il codice per farlo creare la finestra correttamente.

È stato utile?

Soluzione

Ok, quindi ho licenziato questo approccio.

Percorso lungo e cambiato in questo:

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

e chink.h

@interface chinkNS : NSOpenGLView
{

    NSTimer * animationTimer;
    NSTimeInterval lastTime;

}

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

@end

Le stesse impostazioni in .xib, tranne per il fatto che si tratta di un NSView standard che sottoclasse la mia vista OpenGL.

Speravo in un allestimento più semplice ed elegante come sopra, ma non puoi vincerli tutti.

Chiuso.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top