need help understanding cocoa code

Member
Posts: 63
Joined: 2005.04
Post: #1
fullScreenGLContext =
[[NSOpenGLContext alloc] initWithFormat:fullScreenPixelFormat
shareContext:[self openGLContext]];

lisa "can I sit here"
comic store owner "yes, but only if you answer me these questions three!"

If I'm understanding this correctly, this first allocates memory for a new NSOpenGLContext object, then it is initiated with the pixelformat defined by fullScreenPixelFormat and finally is shared with the current context?

I'm still trying to wrap my head around the concept (or rather implementing) of objects, classes, allocating memory, messaging.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
basically, you're right.

-initWithPixelFormat:shareContext: is one operation.
it's shared with the current object's GL context, rather than the current context. I'd guess that the current object is an NSOpenGLView of some kind.
Quote this message in a reply
Member
Posts: 63
Joined: 2005.04
Post: #3
I get tons of errors when I try to compile this, How do I gain access to CG methods? Common sense says that their should be a framework to import, but I can't find any CG headers.

-(IBAction)fadeOut:(id)sender
{
CGSetDisplayTransferByFormula(gCGDisplayID,
&gGammaRedMin,
&gGammaRedMax * 0.5f,
&gGammaRedGamma,
&gGammaGreenMin,
&gGammaGreenMax * 0.5f,
&gGammaGreenGamma,
&gGammaBlueMin,
&gGammaBlueMax * 0.5f,
&gGammaBlueGamma);

}
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #4
Import the Quartz.framework. And by importing I mean add it to your project and use the #import thingy

"When you dream, there are no rules..."
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
wrong.

Just #include <ApplicationServices/ApplicationServices.h>

no need to add any additional frameworks if you've already got Cocoa.
Quote this message in a reply
Member
Posts: 63
Joined: 2005.04
Post: #6
Implementation of Step1View


Code:
#import "Step1View.h"
#import <OpenGL/gl.h>
#import <ApplicationServices/ApplicationServices.h>

@implementation Step1View

-(id)initWithFrame:(NSRect)frameRect
{  
    CGGetDisplayTransferByFormula(gCGDisplayID,
        &gGammaRedMin,
        &gGammaRedMax,
        &gGammaRedGamma,
        &gGammaGreenMin,
        &gGammaGreenMax,
        &gGammaGreenGamma,
        &gGammaBlueMin,
        &gGammaBlueMax,
        &gGammaBlueGamma);
    gGammaBrightness = 1.0;
    
    NSOpenGLPixelFormatAttribute MyAttributes[] =
    {
        NSOpenGLPFAWindow,
        NSOpenGLPFASingleRenderer,
        NSOpenGLPFANoRecovery,
        NSOpenGLPFAScreenMask,
        CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay),
        NSOpenGLPFADoubleBuffer,
        0
    };
    
    NSOpenGLPixelFormat* pixelFormat =
        [[NSOpenGLPixelFormat alloc] initWithAttributes:MyAttributes];
    if (pixelFormat == nil)
    {
        return nil;
    }
    [pixelFormat autorelease];
    
    MyAttributes[0] = NSOpenGLPFAFullScreen;
    NSOpenGLPixelFormat* fullScreenPixelFormat =
        [[NSOpenGLPixelFormat alloc] initWithAttributes:MyAttributes];
    if (fullScreenPixelFormat == nil)
    {
        return nil;
    }
    [fullScreenPixelFormat autorelease];
    
    
    self = [super initWithFrame:frameRect pixelFormat:pixelFormat];
    if (self == nil)
    {
        return nil;
    }
    
    fullScreenGLContext =
        [[NSOpenGLContext alloc] initWithFormat:fullScreenPixelFormat
                                    shareContext:[self openGLContext]];
        if (fullScreenGLContext == nil)
        {
            [self dealloc];
            return nil;
        }
        
        fullScreen = NO;
        
        [NSTimer scheduledTimerWithTimeInterval:0.001
                                     target:self
                                   selector:@selector(drawRect:)
                                   userInfo:nil
                                    repeats:YES];
    return self;
}

- (void)dealloc
{
    [fullScreenGLContext release];
}


-(void)drawRect:(NSRect)rect
{
    glClear(GL_COLOR_BUFFER_BIT);
    
    glRotatef(0.1f, 0.0f, 0.0f, 1.0f);
    
    glBegin(GL_QUADS);
    glVertex2f(-0.75f, -0.75f);
    glVertex2f( 0.75f, -0.75f);
    glVertex2f( 0.75f,  0.75f);
    glVertex2f(-0.75f,  0.75f);
    glEnd();
    
    [[NSOpenGLContext currentContext] flushBuffer];
}

-(IBAction)toggleFullScreen:(id)sender
{
    if(fullScreen)
    {
        [fullScreenGLContext clearDrawable];
        [[self openGLContext] makeCurrentContext];
        CGReleaseAllDisplays();
        fullScreen = NO;
    }
    else
    {
        CGCaptureAllDisplays();
        [fullScreenGLContext setFullScreen];
        [fullScreenGLContext makeCurrentContext];
        fullScreen = YES;
    }
}

-(IBAction)fadeOut:(id)sender
{
        CGSetDisplayTransferByFormula(gCGDisplayID,
        &gGammaRedMin,
        &gGammaRedMax * 0.5f,
        &gGammaRedGamma,
        &gGammaGreenMin,
        &gGammaGreenMax * 0.5f,
        &gGammaGreenGamma,
        &gGammaBlueMin,
        &gGammaBlueMax * 0.5f,
        &gGammaBlueGamma);
    
}



@end
Quote this message in a reply
Post Reply