Fullscreen Frustrations

Nibbie
Posts: 1
Joined: 2009.02
Post: #1
I'm having difficulties getting fullscreen mode to work right. At the moment it's set up as a windowed/fullscreen deal. Whenever I enter fullscreen whatever was drawn in the window disappears.

Here's my code:

Code:
@implementation View
- (void) makeFullScreen {
    NSOpenGLPixelFormatAttribute attributes[] = {
        NSOpenGLPFAFullScreen,
        NSOpenGLPFAScreenMask, CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay),
        NSOpenGLPFAColorSize, 24,
        NSOpenGLPFADepthSize, 16,
        NSOpenGLPFADoubleBuffer,
        NSOpenGLPFAAccelerated,
        0
    };
    
    NSOpenGLPixelFormat* pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes: attributes];
    
    NSOpenGLContext* context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:NULL];
    
    [pixelFormat release];
    
    pixelFormat = nil;
    
    CGCaptureAllDisplays();
    
    [context setFullScreen];
    [context makeCurrentContext];
}

- (BOOL) acceptsFirstResponder {
    return YES;
}

- (void) drawRect: (NSRect) bounds {
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_TRIANGLES);
    {
        glVertex3f(  0.0,  0.6, 0.0);
        glVertex3f( -0.2, -0.3, 0.0);
        glVertex3f(  0.2, -0.3 ,0.0);
    }
    glEnd();
    glFlush();
}

- (void) keyDown: (NSEvent*) event {
    NSString* chars = [event characters];
    unichar uniChar = [chars characterAtIndex: 0];
    
    switch(uniChar) {
        case 'a':
            [self makeFullScreen];
            break;
        case 27:
            exit(0);
            break;
    }
}
@end

My view extends NSOpenGLView. I'm new to this so I understand that whatever I have is probably completely wrong. I've looked at the NSOpenGL Fullscreen example provided by Apple but I'm a little lost. I've Googled my brains out and haven't really come up with anything that I've found useful. Any help would be appreciated!
Quote this message in a reply
Member
Posts: 245
Joined: 2005.11
Post: #2
Two things stand out immediately:
1 - you have to share your windowed OpenGL context with the fullscreen conext so that your textures and things are available to both.
2 - you have to make sure the correct context is current when you want to draw. You could either do this manually at the start of drawrect: or set your fullscreen context as the context for your NSOpenGLView (which automatically makes its own context current before calling drawrect). Watch out for memory leaks if you choose that method.
Quote this message in a reply
Post Reply