diff --git a/ChangeLog b/ChangeLog index 3d74e7913..9e51f9416 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2008-03-31 Fred Kiefer + + * Headers/AppKit/NSOpenGLView.h: Add MacOS 10.3 method. + * Source/NSOpenGLView.m: Clean up. Use canDraw instead of + lockFocusInRect: as the entry point for context attachment. + 2008-03-29 Fred Kiefer * Source/NSApplication.m (-terminate:): Initialize termination as diff --git a/Headers/AppKit/NSOpenGLView.h b/Headers/AppKit/NSOpenGLView.h index dee15ca39..981503cf8 100644 --- a/Headers/AppKit/NSOpenGLView.h +++ b/Headers/AppKit/NSOpenGLView.h @@ -38,16 +38,19 @@ NSOpenGLPixelFormat *pixel_format; BOOL attached; } -+ (NSOpenGLPixelFormat*)defaultPixelFormat; -- (void)clearGLContext; -- (void)setOpenGLContext:(NSOpenGLContext*)context; -- (NSOpenGLContext*)openGLContext; -- (id)initWithFrame:(NSRect)frameRect -pixelFormat:(NSOpenGLPixelFormat*)format; -- (void) dealloc; -- (NSOpenGLPixelFormat*)pixelFormat; -- (void)setPixelFormat:(NSOpenGLPixelFormat*)pixelFormat; + ++ (NSOpenGLPixelFormat*) defaultPixelFormat; +- (void) clearGLContext; +- (void) setOpenGLContext: (NSOpenGLContext*)context; +- (NSOpenGLContext*) openGLContext; +- (id) initWithFrame: (NSRect)frameRect + pixelFormat: (NSOpenGLPixelFormat*)format; +- (NSOpenGLPixelFormat*) pixelFormat; +- (void) setPixelFormat: (NSOpenGLPixelFormat*)pixelFormat; - (void) reshape; - (void) update; +#if OS_API_VERSION(MAC_OS_X_VERSION_10_3, GS_API_LATEST) +- (void) prepareOpenGL; +#endif @end #endif diff --git a/Source/NSOpenGLView.m b/Source/NSOpenGLView.m index e53290d28..6dd237e28 100644 --- a/Source/NSOpenGLView.m +++ b/Source/NSOpenGLView.m @@ -32,8 +32,6 @@ #include "AppKit/NSOpenGL.h" #include "AppKit/NSOpenGLView.h" - - /** NSOpenGLView @@ -51,15 +49,9 @@ */ -@implementation NSOpenGLView -/** - return a standard NSOpenGLPixelFormat you can pass to the - initWithFrame: pixelFormat: method - */ -+ (NSOpenGLPixelFormat*)defaultPixelFormat -{ - NSOpenGLPixelFormat *fmt; - NSOpenGLPixelFormatAttribute attrs[] = + +static NSOpenGLPixelFormat *fmt = nil; +static NSOpenGLPixelFormatAttribute attrs[] = { NSOpenGLPFADoubleBuffer, NSOpenGLPFADepthSize, 16, @@ -72,10 +64,20 @@ // NSOpenGLPFADepthSize, 32, // 0 // }; - - fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes: attrs]; + +@implementation NSOpenGLView +/** + return a standard NSOpenGLPixelFormat you can pass to the + initWithFrame: pixelFormat: method + */ ++ (NSOpenGLPixelFormat*) defaultPixelFormat +{ + // Initialize it once + if (!fmt) + fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes: attrs]; + if (fmt) - return AUTORELEASE(fmt); + return fmt; else { NSWarnMLog(@"could not find a reasonable pixel format..."); @@ -87,7 +89,7 @@ detach from the current context. You should call it before releasing this object. */ -- (void)clearGLContext +- (void) clearGLContext { if (glcontext) { @@ -96,42 +98,48 @@ } } -- (void)setOpenGLContext:(NSOpenGLContext*)context +- (void) setOpenGLContext: (NSOpenGLContext*)context { [self clearGLContext]; ASSIGN(glcontext, context); attached = NO; } +- (void) prepareOpenGL +{ +} + /** return the current gl context associated with this view */ -- (NSOpenGLContext*)openGLContext +- (NSOpenGLContext*) openGLContext { if (glcontext == nil) { glcontext = [[NSOpenGLContext alloc] initWithFormat: pixel_format - shareContext: nil]; + shareContext: nil]; attached = NO; } return glcontext; } - -(id) initWithFrame: (NSRect)frameRect { return [self initWithFrame: frameRect - pixelFormat: [[self class] defaultPixelFormat]]; + pixelFormat: [[self class] defaultPixelFormat]]; } /** default initializer. Can be passed [NSOpenGLContext defaultPixelFormat] as second argument */ -- (id)initWithFrame:(NSRect)frameRect - pixelFormat:(NSOpenGLPixelFormat*)format +- (id) initWithFrame: (NSRect)frameRect + pixelFormat: (NSOpenGLPixelFormat*)format { - [super initWithFrame: frameRect]; + self = [super initWithFrame: frameRect]; + if (!self) + return nil; + ASSIGN(pixel_format, format); [self setPostsFrameChangedNotifications: YES]; @@ -153,12 +161,12 @@ [super dealloc]; } -- (NSOpenGLPixelFormat*)pixelFormat +- (NSOpenGLPixelFormat*) pixelFormat { return pixel_format; } -- (void)setPixelFormat:(NSOpenGLPixelFormat*)pixelFormat +- (void) setPixelFormat: (NSOpenGLPixelFormat*)pixelFormat { ASSIGN(pixel_format, pixelFormat); } @@ -179,9 +187,12 @@ [self reshape]; } -- (void) lockFocusInRect: (NSRect) aRect +/* FIXME: This is a hack to get a chance to attach the current + context before any drawing happens. I think this code should + be moved to openGLContext. + */ +- (BOOL) canDraw { - [super lockFocusInRect: aRect]; if (!glcontext) { [self openGLContext]; @@ -192,8 +203,11 @@ NSDebugMLLog(@"GL", @"Attaching context to the view"); [glcontext setView: self]; attached = YES; + [self prepareOpenGL]; } + return [super canDraw]; } + @end