Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Using Cocoa to Create a Full-Screen Context

When you set up an attributes array, you need to include the attribute NSOpenGLPFAFullScreen to specify that only renderers that are capable of rendering to the full screen should be considered when the system creates a pixel format object. You also need to include the attribute NSOpenGLPFAScreenMask along with the appropriate OpenGL display mask.

Listing 3-1 is a code fragment that shows how to use the NSOpenGLPixelFormat and NSOpenGLContext classes along with calls from Quartz Display Services to set up full-screen drawing in a Cocoa application. A detailed explanation for each numbered line of code appears following the listing.

Listing 3-1  Using Cocoa to set up full-screen drawing

CGDisplayErr err;
NSOpenGLContext *fullScreenContext;
NSOpenGLPixelFormatAttribute attrs[] = { // 1
    NSOpenGLPFAFullScreen,
    NSOpenGLPFAScreenMask,
                CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay),
    NSOpenGLPFAColorSize, 24,  // 2
    NSOpenGLPFADepthSize, 16,
    NSOpenGLPFADoubleBuffer,
    NSOpenGLPFAAccelerated,
    0
};
NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc]
                                initWithAttributes:attrs];
fullScreenContext = [[NSOpenGLContext alloc] initWithFormat:pixelFormat
                         shareContext:NULL];
[pixelFormat release];
pixelFormat = nil;
if (fullScreenContext == nil) {
        NSLog(@"Failed to create fullScreenContext");
        return;
}
err = CGCaptureAllDisplays(); // 3
if (err != CGDisplayNoErr) {
        [fullScreenContext release];
        fullScreenContext = nil;
        return;
}
[fullScreenContext setFullScreen]; // 4
[fullScreenContext makeCurrentContext]; // 5

Here's what the code does:

  1. Sets up an array of renderer and buffer attributes, including the appropriate attributes to specify full-screen mode and the display ID for the main display. This example also supplies a number of other attributes. You would supply the attributes that are appropriate for your application.

  2. Supplies a color size that matches the current display depth. Note that this value must match the current display depth.

  3. Calls the Quartz Display Services function that captures all displays. If you want to capture only one display, you can call the function CGDisplayCapture, passing the ID of the display that you want to capture.

  4. Attaches the full-screen drawable object to the rendering context.

  5. Makes the full-screen context the current context that will receive OpenGL commands. If you fail to perform this step, you won't see any content drawn to the screen.

When you no longer need to draw full-screen OpenGL content, you must release resources and release the captured display (or displays).



< Previous PageNext Page > Hide TOC


Last updated: 2008-06-09




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice