#import <Cocoa/Cocoa.h>
#import <Quartz/Quartz.h>
#import <OpenGL/CGLMacro.h>

static CGColorSpaceRef CreateSystemColorSpace()
{
	CMProfileRef sysprof = NULL;
	CGColorSpaceRef dispColorSpace = NULL;
	
	// Get the Systems Profile for the main display
	if (CMGetSystemProfile(&sysprof) == noErr)
	{
		// Create a colorspace with the systems profile
		dispColorSpace = CGColorSpaceCreateWithPlatformColorSpace(sysprof);
		
		// Close the profile
		CMCloseProfile(sysprof);
	}
	
	return dispColorSpace;
}

int main()
{
	NSAutoreleasePool *pool = [NSAutoreleasePool new];
	
	NSOpenGLPixelFormatAttribute attribs[] = {NSOpenGLPFAAccelerated, NSOpenGLPFADoubleBuffer, NSOpenGLPFADepthSize, 24,0};
	
	NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
	
	NSOpenGLContext *context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat
														  shareContext:nil];
	NSLog(@"context: %@", context);
	CGLContextObj cgl_ctx = [context CGLContextObj];
	
	QCComposition *composition = [QCComposition compositionWithFile:@"test.qtz"];
	QCRenderer *renderer = [[QCRenderer alloc] initWithCGLContext: [context CGLContextObj]
													  pixelFormat: [pixelFormat CGLPixelFormatObj]
													   colorSpace: CreateSystemColorSpace()
													  composition: composition];
	
	glEnable(GL_ALPHA_TEST);
	glAlphaFunc(GL_GREATER, 0.42);
	
	[renderer renderAtTime:0.0 arguments:nil];
	
	GLfloat ref;
	GLint func;
	
	glGetIntegerv(GL_ALPHA_TEST_FUNC, &func);
	glGetFloatv(GL_ALPHA_TEST_REF, &ref);
	
	// At this point, ref and func should be 0.42 and GL_GREATER (0x204) respectively
	// func appears to be OK, but ref is no 0.42
	printf("ALPHA_TEST enabled: %i (should be 1)  func: %x (was %x),    ref %f (was %f)\n",
		   glIsEnabled(GL_ALPHA_TEST),
		   func, GL_GREATER, 
		   ref, 0.42);
		
	[pool release];
	return 0;
}