comparison dwt/opengl/GLCanvas.d @ 116:580596d83ac4

Ported dwt.opengl.GLCanvas
author Jacob Carlborg <doob@me.com>
date Wed, 31 Dec 2008 15:46:19 +0100
parents d8635bb48c7c
children 04928add86e6
comparison
equal deleted inserted replaced
115:a2d82e6fd054 116:580596d83ac4
5 * which accompanies this distribution, and is available at 5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html 6 * http://www.eclipse.org/legal/epl-v10.html
7 * 7 *
8 * Contributors: 8 * Contributors:
9 * IBM Corporation - initial API and implementation 9 * IBM Corporation - initial API and implementation
10 *
11 * Port to the D programming language:
12 * Jacob Carlborg <doob@me.com>
10 *******************************************************************************/ 13 *******************************************************************************/
11 module dwt.opengl.GLCanvas; 14 module dwt.opengl.GLCanvas;
12 15
13 import dwt.dwthelper.utils; 16 import dwt.dwthelper.utils;
14 17
21 import dwt.widgets.Canvas; 24 import dwt.widgets.Canvas;
22 import dwt.widgets.Composite; 25 import dwt.widgets.Composite;
23 import dwt.widgets.Event; 26 import dwt.widgets.Event;
24 import dwt.widgets.Listener; 27 import dwt.widgets.Listener;
25 28
29 import dwt.internal.objc.cocoa.Cocoa;
30 import dwt.opengl.GLData;
31
26 /** 32 /**
27 * GLCanvas is a widget capable of displaying OpenGL content. 33 * GLCanvas is a widget capable of displaying OpenGL content.
28 * 34 *
29 * @see GLData 35 * @see GLData
30 * @see <a href="http://www.eclipse.org/swt/snippets/#opengl">OpenGL snippets</a> 36 * @see <a href="http://www.eclipse.org/swt/snippets/#opengl">OpenGL snippets</a>
34 */ 40 */
35 41
36 public class GLCanvas : Canvas { 42 public class GLCanvas : Canvas {
37 NSOpenGLView glView; 43 NSOpenGLView glView;
38 NSOpenGLPixelFormat pixelFormat; 44 NSOpenGLPixelFormat pixelFormat;
39 static final int MAX_ATTRIBUTES = 32; 45 static const int MAX_ATTRIBUTES = 32;
40 46
41 /** 47 /**
42 * Create a GLCanvas widget using the attributes described in the GLData 48 * Create a GLCanvas widget using the attributes described in the GLData
43 * object provided. 49 * object provided.
44 * 50 *
107 if (data.samples > 0) { 113 if (data.samples > 0) {
108 attrib [pos++] = OS.NSOpenGLPFASamples; 114 attrib [pos++] = OS.NSOpenGLPFASamples;
109 attrib [pos++] = data.samples; 115 attrib [pos++] = data.samples;
110 } 116 }
111 attrib [pos++] = 0; 117 attrib [pos++] = 0;
112 pixelFormat = cast(NSOpenGLPixelFormat)new NSOpenGLPixelFormat().alloc(); 118 pixelFormat = cast(NSOpenGLPixelFormat)(new NSOpenGLPixelFormat()).alloc();
113 if (pixelFormat is null) { 119 if (pixelFormat is null) {
114 dispose (); 120 dispose ();
115 DWT.error (DWT.ERROR_UNSUPPORTED_DEPTH); 121 DWT.error (DWT.ERROR_UNSUPPORTED_DEPTH);
116 } 122 }
117 pixelFormat.initWithAttributes(attrib); 123 pixelFormat.initWithAttributes((cast(NSOpenGLPixelFormatAttribute[])attrib).ptr);
118 124
119 glView = cast(NSOpenGLView)new NSOpenGLView().alloc(); 125 glView = cast(NSOpenGLView)(new NSOpenGLView()).alloc();
120 if (glView is null) { 126 if (glView is null) {
121 dispose (); 127 dispose ();
122 DWT.error (DWT.ERROR_UNSUPPORTED_DEPTH); 128 DWT.error (DWT.ERROR_UNSUPPORTED_DEPTH);
123 } 129 }
124 glView.initWithFrame(parent.view.bounds(), pixelFormat); 130 glView.initWithFrame(parent.view.bounds(), pixelFormat);
125 glView.setAutoresizingMask(OS.NSViewWidthSizable | OS.NSViewHeightSizable); 131 glView.setAutoresizingMask(OS.NSViewWidthSizable | OS.NSViewHeightSizable);
126 parent.view.addSubview(glView); 132 parent.view.addSubview(glView);
127 133
128 Listener listener = new Listener () { 134 Listener listener = new class () Listener {
129 public void handleEvent (Event event) { 135 public void handleEvent (Event event) {
130 switch (event.type) { 136 switch (event.type) {
131 case DWT.Dispose: 137 case DWT.Dispose:
132 if (glView !is null) { 138 if (glView !is null) {
133 glView.clearGLContext(); 139 glView.clearGLContext();
135 } 141 }
136 glView = null; 142 glView = null;
137 if (pixelFormat !is null) pixelFormat.release(); 143 if (pixelFormat !is null) pixelFormat.release();
138 pixelFormat = null; 144 pixelFormat = null;
139 break; 145 break;
146 default:
140 } 147 }
141 } 148 }
142 }; 149 };
143 addListener (DWT.Dispose, listener); 150 addListener (DWT.Dispose, listener);
144 } 151 }