views:

43

answers:

1

Hi,

I am currently trying to create a simple cocoa NSWindow programmatically instead of using Interface builder (I have got my reasons to do so). this is a quick test:

   int main(int argc, char** argv){     
    NSWindow *mainwin;
    CocoaGLView *mainview;
    NSRect scr_frame;
    unsigned int style_mask;

    NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];

    [NSApplication sharedApplication];

    scr_frame= NSMakeRect(100, 100, 400, 400);
    style_mask=NSClosableWindowMask|NSMiniaturizableWindowMask|
    NSResizableWindowMask|NSTitledWindowMask;

    scr_frame=[NSWindow contentRectForFrameRect:scr_frame
                                      styleMask:style_mask];

    mainwin=[[NSWindow alloc]
             initWithContentRect:scr_frame
             styleMask:style_mask
             backing:NSBackingStoreBuffered
             defer:NO];
    [mainwin makeKeyAndOrderFront:nil];
    [mainwin setTitle:@"Visible screen window"];

    mainview=[[CocoaGLView alloc] initWithFrame:scr_frame];
    [mainwin setContentView:mainview];
    [mainview display];

    [mainwin setReleasedWhenClosed:YES];

    [pool drain];
    [NSApp run];
    return 0;
}

CocoaGLView is derived from NSOpenGLView and looks like this:

    @interface CocoaGLView : NSOpenGLView {
    //some stuff
}

- (id) initWithFrame: (NSRect) frameRect;

- (void)setFrameSize:(NSSize) aSize;
- (void)drawRect:(NSRect) aRect;

@end

it generally works. I can see the window. I can even see the openGL things I draw inside CocoaGLViews drawRect function, but that function unfortunatelly only gets called once, what am I missing?

+2  A: 

Why would you expect it to be called more than once? A view is asked to draw when the OS thinks that its content is no longer valid. If you want the OpenGL view to be drawn periodically, then you need to set up a timer that sends setNeedsDisplay: messages to your view.

JWWalker