From c6487d0edca768d81c540dd210cb81c2f50fdeaa Mon Sep 17 00:00:00 2001 From: fedor Date: Sat, 16 Nov 2002 04:48:52 +0000 Subject: [PATCH] Add OpenGL support git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@14997 72102866-910b-0410-8b05-ffd578937521 --- Headers/gnustep/gui/GSDisplayServer.h | 4 + Headers/gnustep/gui/NSOpenGL.h | 139 +++++++++++++++++++ Headers/gnustep/gui/NSOpenGLView.h | 51 +++++++ Model/GMAppKit.m | 33 ++++- Source/GNUmakefile | 5 + Source/GSDisplayServer.m | 11 ++ Source/NSOpenGLContext.m | 171 +++++++++++++++++++++++ Source/NSOpenGLPixelFormat.m | 108 +++++++++++++++ Source/NSOpenGLView.m | 190 ++++++++++++++++++++++++++ 9 files changed, 708 insertions(+), 4 deletions(-) create mode 100644 Headers/gnustep/gui/NSOpenGL.h create mode 100644 Headers/gnustep/gui/NSOpenGLView.h create mode 100644 Source/NSOpenGLContext.m create mode 100644 Source/NSOpenGLPixelFormat.m create mode 100644 Source/NSOpenGLView.m diff --git a/Headers/gnustep/gui/GSDisplayServer.h b/Headers/gnustep/gui/GSDisplayServer.h index e9565387a..69c1efd2f 100644 --- a/Headers/gnustep/gui/GSDisplayServer.h +++ b/Headers/gnustep/gui/GSDisplayServer.h @@ -68,6 +68,10 @@ APPKIT_EXPORT NSString * GSScreenNumber; - (NSDictionary *) attributes; - (void) closeServer; +/* GL context */ +- glContextClass; +- glPixelFormatClass; + /* Drag and drop support. */ + (BOOL) addDragTypes: (NSArray*)types toWindow: (NSWindow *)win; + (BOOL) removeDragTypes: (NSArray*)types fromWindow: (NSWindow *)win; diff --git a/Headers/gnustep/gui/NSOpenGL.h b/Headers/gnustep/gui/NSOpenGL.h new file mode 100644 index 000000000..a058600f8 --- /dev/null +++ b/Headers/gnustep/gui/NSOpenGL.h @@ -0,0 +1,139 @@ +/* -*-ObjC-*- */ +/** + Copyright (C) 2002 Free Software Foundation, Inc. + + Author: Frederic De Jaeger + Date: Nov 2002 + + This file is part of the GNU Objective C User interface library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA. + */ + +#ifndef _NSOpenGL_h_INCLUDE +#define _NSOpenGL_h_INCLUDE + +#include +@class NSView; + +typedef enum { + NSOpenGLPFAAllRenderers = 1, + NSOpenGLPFADoubleBuffer = 5, + NSOpenGLPFAStereo = 6, + NSOpenGLPFAAuxBuffers = 7, + NSOpenGLPFAColorSize = 8, + NSOpenGLPFAAlphaSize = 11, + NSOpenGLPFADepthSize = 12, + NSOpenGLPFAStencilSize = 13, + NSOpenGLPFAAccumSize = 14, + NSOpenGLPFAMinimumPolicy = 51, + NSOpenGLPFAMaximumPolicy = 52, + NSOpenGLPFAOffScreen = 53, + NSOpenGLPFAFullScreen = 54, + NSOpenGLPFASampleBuffers = 55, + NSOpenGLPFASamples = 56, + NSOpenGLPFAAuxDepthStencil = 57, + NSOpenGLPFARendererID = 70, + NSOpenGLPFASingleRenderer = 71, + NSOpenGLPFANoRecovery = 72, + NSOpenGLPFAAccelerated = 73, + NSOpenGLPFAClosestPolicy = 74, + NSOpenGLPFARobust = 75, + NSOpenGLPFABackingStore = 76, + NSOpenGLPFAMPSafe = 78, + NSOpenGLPFAWindow = 80, + NSOpenGLPFAMultiScreen = 81, + NSOpenGLPFACompliant = 83, + NSOpenGLPFAScreenMask = 84, + NSOpenGLPFAVirtualScreenCount = 128 +} NSOpenGLPixelFormatAttribute; + +typedef enum { + NSOpenGLCPSwapRectangle = 200, + NSOpenGLCPSwapRectangleEnable = 201, + NSOpenGLCPRasterizationEnable = 221, + NSOpenGLCPSwapInterval = 222, + NSOpenGLCPSurfaceOrder = 235, + NSOpenGLCPSurfaceOpacity = 236, + NSOpenGLCPStateValidation = 301 +} NSOpenGLContextParameter; + +typedef enum { + NSOpenGLGOFormatCacheSize = 501, + NSOpenGLGOClearFormatCache = 502, + NSOpenGLGORetainRenderers = 503, + NSOpenGLGOResetLibrary = 504 +} NSOpenGLGlobalOption; + +@interface NSOpenGLPixelFormat : NSObject +{ +} +- (void)getValues:(long *)vals + forAttribute:(NSOpenGLPixelFormatAttribute)attrib + forVirtualScreen:(int)screen; +- (id)initWithAttributes:(NSOpenGLPixelFormatAttribute *)attribs; +- (int)numberOfVirtualScreens; +@end + +@interface NSOpenGLContext : NSObject +{ +} + ++ (void)clearCurrentContext; ++ (NSOpenGLContext *)currentContext; +- (void)clearDrawable; +- (void)copyAttributesFromContext:(NSOpenGLContext *)context + withMask:(unsigned long)mask; +- (void)createTexture:(unsigned long)target + fromView:(NSView*)view + internalFormat:(unsigned long)format; + +- (int)currentVirtualScreen; + +- (void)flushBuffer; + +- (void)getValues:(long *)vals + forParameter:(NSOpenGLContextParameter)param; + +- (id)initWithFormat:(NSOpenGLPixelFormat *)format + shareContext:(NSOpenGLContext *)share; + + +- (void)makeCurrentContext; + +- (void)setCurrentVirtualScreen:(int)screen; + +- (void)setFullScreen; + +- (void)setOffScreen:(void *)baseaddr + width:(long)width + height:(long)height + rowbytes:(long)rowbytes; + +- (void)setValues:(const long *)vals + forParameter:(NSOpenGLContextParameter)param; + +- (void)setView:(NSView *)view; + +- (void)update; + +- (NSView *)view; + + +@end + + + +#endif diff --git a/Headers/gnustep/gui/NSOpenGLView.h b/Headers/gnustep/gui/NSOpenGLView.h new file mode 100644 index 000000000..5fdc505ea --- /dev/null +++ b/Headers/gnustep/gui/NSOpenGLView.h @@ -0,0 +1,51 @@ +/* -*-ObjC-*- */ +/** + Copyright (C) 2002 Free Software Foundation, Inc. + + Author: Frederic De Jaeger + Date: Nov 2002 + + This file is part of the GNU Objective C User interface library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA. + */ + +#ifndef _NSOpenGLView_h +#define _NSOpenGLView_h + +#include + +@class NSOpenGLContext; +@class NSOpenGLPixelFormat; + +@interface NSOpenGLView : NSView +{ + NSOpenGLContext *glcontext; + 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; +- (void) reshape; +- (void) update; +@end +#endif diff --git a/Model/GMAppKit.m b/Model/GMAppKit.m index a62698cbe..d821e9f05 100644 --- a/Model/GMAppKit.m +++ b/Model/GMAppKit.m @@ -591,6 +591,9 @@ void __dummy_GMAppKit_functionForLinking() {} withName:@"catalogName"]; [archiver encodeString:[self colorNameComponent] withName:@"colorName"]; } + else if ([colorSpaceName isEqual:@"NSPatternColorSpace"]) { + [archiver encodeObject: [self patternImage] withName: @"patternImage"]; + } } + (id)createObjectForModelUnarchiver:(GMUnarchiver*)unarchiver @@ -644,6 +647,23 @@ void __dummy_GMAppKit_functionForLinking() {} NSString *colornm = [unarchiver decodeStringWithName: @"colorName"]; return [self colorWithCatalogName: catalog colorName: colornm]; } + else if ([colorSpaceName isEqual:@"NSPatternColorSpace"]) { + NSImage *image = [unarchiver decodeObjectWithName: @"patternImage"]; + if (image == nil) + { + NSLog(@"Internal: No can't decode colorspace %@", colorSpaceName); + NSLog(@" creating generic white color"); + return [NSColor colorWithDeviceWhite: 1.0 alpha: 1.0]; + } + else + return [NSColor colorWithPatternImage: image]; + } + else + { + NSLog(@"Internal: No decoder for colorspace %@", colorSpaceName); + NSLog(@" creating generic white color"); + return [NSColor colorWithDeviceWhite: 1.0 alpha: 1.0]; + } return nil; } @@ -983,7 +1003,7 @@ void __dummy_GMAppKit_functionForLinking() {} decodedItems = [unarchiver decodeArrayWithName: @"itemArray"]; - if (decodedItems) + if (decodedItems && [decodedItems count]) { count = [decodedItems count]; for (i = 0; i < count; i++) @@ -999,10 +1019,15 @@ void __dummy_GMAppKit_functionForLinking() {} [myItem setTag: [item tag]]; [myItem setKeyEquivalent: [item keyEquivalent]]; } + string = [unarchiver decodeStringWithName: @"selectedItem"]; + [self selectItemWithTitle: string]; + } + else + { + /* For old gmodels that didn't support popups */ + [self addItemWithTitle: @"Item 1"]; + [self selectItemAtIndex: 0]; } - - string = [unarchiver decodeStringWithName: @"selectedItem"]; - [self selectItemWithTitle: string]; [self setEnabled: [unarchiver decodeBOOLWithName: @"isEnabled"]]; [self setTag: [unarchiver decodeIntWithName: @"tag"]]; diff --git a/Source/GNUmakefile b/Source/GNUmakefile index 38a2fb37d..9edfabc19 100644 --- a/Source/GNUmakefile +++ b/Source/GNUmakefile @@ -101,6 +101,9 @@ NSMenu.m \ NSMenuView.m \ NSMenuItem.m \ NSMenuItemCell.m \ +NSOpenGLContext.m \ +NSOpenGLPixelFormat.m \ +NSOpenGLView.m \ NSOpenPanel.m \ NSOutlineView.m \ NSPageLayout.m \ @@ -254,6 +257,8 @@ NSMenuItem.h \ NSMenuItemCell.h \ NSMenuView.h \ NSOpenPanel.h \ +NSOpenGL.h \ +NSOpenGLView.h \ NSOutlineView.h \ NSPageLayout.h \ NSPanel.h \ diff --git a/Source/GSDisplayServer.m b/Source/GSDisplayServer.m index 5bbb256da..8543945f2 100644 --- a/Source/GSDisplayServer.m +++ b/Source/GSDisplayServer.m @@ -245,6 +245,17 @@ GSCurrentServer(void) [super dealloc]; } +- glContextClass +{ + return nil; +} + +- glPixelFormatClass +{ + return nil; +} + + /* Drag and drop support. */ /** Convienience method that calls -addDragTypes:toWindow: using the server that controls win. diff --git a/Source/NSOpenGLContext.m b/Source/NSOpenGLContext.m new file mode 100644 index 000000000..b11490bbb --- /dev/null +++ b/Source/NSOpenGLContext.m @@ -0,0 +1,171 @@ +/** NSOpenGLContext.m + + Context for openGL drawing + + Copyright (C) 1996 Free Software Foundation, Inc. + + Author: Frederic De Jaeger + Date: 2002 + + This file is part of the GNUstep GUI Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#include +#include +#include + + +@implementation NSOpenGLContext ++ _classContext +{ + Class glContextClass = [GSCurrentServer() glContextClass]; + + if ( glContextClass == nil ) + { + NSWarnMLog(@"Backend doesn't have any gl context"); + return nil; + } + else + { + NSDebugMLLog(@"GLX", @"found something"); + return glContextClass; + } +} + + ++ allocWithZone: (NSZone *) z +{ + Class c = [self _classContext]; + if ( c ) + return NSAllocateObject(c, 0, z); + else + return nil; +} + ++ (void)clearCurrentContext +{ + [[self _classContext] clearCurrentContext]; +} + ++ (NSOpenGLContext *)currentContext +{ + return [[self _classContext] currentContext]; +} + +- (void)clearDrawable +{ + [self subclassResponsibility: _cmd]; +} + +- (void)copyAttributesFromContext:(NSOpenGLContext *)context + withMask:(unsigned long)mask +{ + [self subclassResponsibility: _cmd]; +} + +- (void)createTexture:(unsigned long)target + fromView:(NSView*)view + internalFormat:(unsigned long)format +{ + [self subclassResponsibility: _cmd]; +} + + +- (int)currentVirtualScreen +{ + [self subclassResponsibility: _cmd]; + return 0; +} + + +- (void)flushBuffer +{ + [self subclassResponsibility: _cmd]; +} + + +- (void)getValues:(long *)vals + forParameter:(NSOpenGLContextParameter)param +{ + [self subclassResponsibility: _cmd]; +} + + +- (id)initWithFormat:(NSOpenGLPixelFormat *)format + shareContext:(NSOpenGLContext *)share +{ + [self subclassResponsibility: _cmd]; + return 0; +} + + + +- (void)makeCurrentContext +{ + [self subclassResponsibility: _cmd]; +} + + +- (void)setCurrentVirtualScreen:(int)screen +{ + [self subclassResponsibility: _cmd]; +} + + +- (void)setFullScreen +{ + [self subclassResponsibility: _cmd]; +} + + +- (void)setOffScreen:(void *)baseaddr + width:(long)width + height:(long)height + rowbytes:(long)rowbytes +{ + [self subclassResponsibility: _cmd]; +} + + +- (void)setValues:(const long *)vals + forParameter:(NSOpenGLContextParameter)param +{ + [self subclassResponsibility: _cmd]; +} + + +- (void)setView:(NSView *)view +{ + [self subclassResponsibility: _cmd]; +} + + +- (void)update +{ + [self subclassResponsibility: _cmd]; +} + + +- (NSView *)view +{ + [self subclassResponsibility: _cmd]; + return nil; +} +@end + + diff --git a/Source/NSOpenGLPixelFormat.m b/Source/NSOpenGLPixelFormat.m new file mode 100644 index 000000000..6c07ce0f1 --- /dev/null +++ b/Source/NSOpenGLPixelFormat.m @@ -0,0 +1,108 @@ +/** NSOpenGLPixelFormat.m + + use to choose the type of GL context + + Copyright (C) 1996 Free Software Foundation, Inc. + + Author: Frederic De Jaeger + Date: 2002 + + This file is part of the GNUstep GUI Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#include + +#include +#include +#include + + +// @interface GSGLPixelFormat : NSOpenGLPixelFormat +// {} +// + _classContext; +// @end + +// @implementation GSGLPixelFormat +// - (id)initWithAttributes:(NSOpenGLPixelFormatAttribute *)attribs +// { +// self = [[GSGLPixelFormat _classContext] alloc]; + +// return [self initWithAttributes: attribs]; +// } +// @end + +//static GSGLPixelFormat *temp; + +@implementation NSOpenGLPixelFormat ++ _classPixelFormat +{ + Class glPixelFormatClass = [GSCurrentServer() glPixelFormatClass]; + + if ( glPixelFormatClass == nil ) + { + NSWarnMLog(@"Backend doesn't have any glPixelFormatClass"); + return nil; + } + else + { + NSDebugMLLog(@"GLX", @"found a class %@", glPixelFormatClass); + return glPixelFormatClass; + } +} + +// + (void) initialize +// { +// if ( self == [NSOpenGLPixelFormat class] ) +// { +// temp = (GSGLPixelFormat *) NSAllocateObject([GSGLPixelFormat class], 0, +// NSDefaultMallocZone()); +// } +// } + ++ allocWithZone: (NSZone *) z +{ + Class c = [self _classPixelFormat]; + if ( c ) + return NSAllocateObject(c, 0, z); + else + return nil; +} + +- (void)getValues:(long *)vals + forAttribute:(NSOpenGLPixelFormatAttribute)attrib + forVirtualScreen:(int)screen +{ + [self subclassResponsibility: _cmd]; +} + +- (id)initWithAttributes:(NSOpenGLPixelFormatAttribute *)attribs +{ + [self subclassResponsibility: _cmd]; + return nil; +} + +- (int)numberOfVirtualScreens +{ + [self subclassResponsibility: _cmd]; + return 0; +} + +@end + + + diff --git a/Source/NSOpenGLView.m b/Source/NSOpenGLView.m new file mode 100644 index 000000000..c2a96a9f6 --- /dev/null +++ b/Source/NSOpenGLView.m @@ -0,0 +1,190 @@ +/** NSOpenGLView.m + + Context for openGL drawing + + Copyright (C) 1996 Free Software Foundation, Inc. + + Author: Frederic De Jaeger + Date: 2002 + + This file is part of the GNUstep GUI Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#include +#include +#include +#include +#include + + + +/** + + NSOpenGLView + +

+ This class is simply a view with a NSOpenGLContext attached to it. + This is the simplest way to initialize a GL context within GNUstep. +

+ + +

+ There is a mechanism to update the context when the view is moved + or resize. +

+ +
+*/ +@implementation NSOpenGLView +/** + return a standard NSOpenGLPixelFormat you can pass to the + initWithFrame: pixelFormat: method + */ ++ (NSOpenGLPixelFormat*)defaultPixelFormat +{ + NSOpenGLPixelFormat *fmt; + NSOpenGLPixelFormatAttribute attrs[] = + { + NSOpenGLPFADoubleBuffer, + NSOpenGLPFADepthSize, 16, + NSOpenGLPFAColorSize, 1, + 0 + }; +// NSOpenGLPixelFormatAttribute attrs[] = +// { +// NSOpenGLPFADoubleBuffer, +// NSOpenGLPFADepthSize, 32, +// 0 +// }; + + fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes: attrs]; + if (fmt) + return AUTORELEASE(fmt); + else + { + NSWarnMLog(@"could not find a reasonable pixel format..."); + return nil; + } +} + +/** + detach from the current context. You should call it before releasing this + object. + */ +- (void)clearGLContext +{ + if (glcontext) + { + [glcontext clearDrawable]; + DESTROY(glcontext); + } +} + +- (void)setOpenGLContext:(NSOpenGLContext*)context +{ + [self clearGLContext]; + ASSIGN(glcontext, context); + attached = NO; +} + +/** + return the current gl context associated with this view +*/ +- (NSOpenGLContext*)openGLContext +{ + if ( glcontext == nil ) + { + glcontext = [[NSOpenGLContext alloc] initWithFormat: pixel_format + shareContext: nil]; + attached = NO; + } + return glcontext; +} + +/** default initializer. Can be passed [NSOpenGLContext defaultPixelFormat] + as second argument +*/ +- (id)initWithFrame:(NSRect)frameRect + pixelFormat:(NSOpenGLPixelFormat*)format +{ + [super initWithFrame: frameRect]; + ASSIGN(pixel_format, format); + + [self setPostsFrameChangedNotifications: YES]; + [[NSNotificationCenter defaultCenter] + addObserver: self + selector: @selector(_frameChanged:) + name: NSViewFrameDidChangeNotification + object: self]; + + return self; +} + +- (void) dealloc +{ + [[NSNotificationCenter defaultCenter] removeObserver: self]; + RELEASE(pixel_format); + RELEASE(glcontext); + NSDebugMLLog(@"GL", @"deallocating"); + [super dealloc]; +} + +- (NSOpenGLPixelFormat*)pixelFormat +{ + return pixel_format; +} + +- (void)setPixelFormat:(NSOpenGLPixelFormat*)pixelFormat +{ + ASSIGN(pixel_format, pixelFormat); +} + +- (void) reshape +{ +} + +- (void) update +{ + [glcontext update]; +} + +- (void) _frameChanged: (NSNotification *) aNot +{ + NSDebugMLLog(@"GL", @"our frame has changed"); + [self update]; + [self reshape]; +} + +- (void) lockFocusInRect: (NSRect) aRect +{ + [super lockFocusInRect: aRect]; + if ( !glcontext ) + { + [self openGLContext]; + NSAssert(glcontext, NSInternalInconsistencyException); + } + if ( attached == NO && glcontext != nil ) + { + NSDebugMLLog(@"GL", @"Attaching context to the view"); + [glcontext setView: self]; + attached = YES; + } +} +@end + +