Mercurial > projects > dwt-mac
comparison dwt/opengl/GLCanvas.d @ 0:380af2bdd8e5
Upload of whole dwt tree
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sat, 09 Aug 2008 17:00:02 +0200 |
parents | |
children | 649b8e223d5a |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:380af2bdd8e5 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2007 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 *******************************************************************************/ | |
11 module dwt.opengl.GLCanvas; | |
12 | |
13 import dwt.dwthelper.utils; | |
14 | |
15 import dwt.DWT; | |
16 import dwt.DWTException; | |
17 import dwt.internal.cocoa.NSOpenGLContext; | |
18 import dwt.internal.cocoa.NSOpenGLPixelFormat; | |
19 import dwt.internal.cocoa.NSOpenGLView; | |
20 import dwt.internal.cocoa.OS; | |
21 import dwt.widgets.Canvas; | |
22 import dwt.widgets.Composite; | |
23 import dwt.widgets.Event; | |
24 import dwt.widgets.Listener; | |
25 | |
26 /** | |
27 * GLCanvas is a widget capable of displaying OpenGL content. | |
28 * | |
29 * @since 3.2 | |
30 */ | |
31 | |
32 public class GLCanvas extends Canvas { | |
33 NSOpenGLView glView; | |
34 NSOpenGLPixelFormat pixelFormat; | |
35 static final int MAX_ATTRIBUTES = 32; | |
36 | |
37 /** | |
38 * Create a GLCanvas widget using the attributes described in the GLData | |
39 * object provided. | |
40 * | |
41 * @param parent a composite widget | |
42 * @param style the bitwise OR'ing of widget styles | |
43 * @param data the requested attributes of the GLCanvas | |
44 * | |
45 * @exception IllegalArgumentException | |
46 * <ul><li>ERROR_NULL_ARGUMENT when the data is null | |
47 * <li>ERROR_UNSUPPORTED_DEPTH when the requested attributes cannot be provided</ul> | |
48 * </ul> | |
49 */ | |
50 public GLCanvas (Composite parent, int style, GLData data) { | |
51 super (parent, style); | |
52 if (data is null) DWT.error (DWT.ERROR_NULL_ARGUMENT); | |
53 int attrib [] = new int [MAX_ATTRIBUTES]; | |
54 int pos = 0; | |
55 //TODO this is not working | |
56 // attrib [pos++] = OS.AGL_RGBA; | |
57 if (data.doubleBuffer) attrib [pos++] = OS.NSOpenGLPFADoubleBuffer; | |
58 if (data.stereo) attrib [pos++] = OS.NSOpenGLPFAStereo; | |
59 // if (data.redSize > 0) { | |
60 // attrib [pos++] = OS.AGL_RED_SIZE; | |
61 // attrib [pos++] = data.redSize; | |
62 // } | |
63 // if (data.greenSize > 0) { | |
64 // attrib [pos++] = OS.AGL_GREEN_SIZE; | |
65 // attrib [pos++] = data.greenSize; | |
66 // } | |
67 // if (data.blueSize > 0) { | |
68 // attrib [pos++] = OS.AGL_BLUE_SIZE; | |
69 // attrib [pos++] = data.blueSize; | |
70 // } | |
71 if (data.alphaSize > 0) { | |
72 attrib [pos++] = OS.NSOpenGLPFAAlphaSize; | |
73 attrib [pos++] = data.alphaSize; | |
74 } | |
75 if (data.depthSize > 0) { | |
76 attrib [pos++] = OS.NSOpenGLPFADepthSize; | |
77 attrib [pos++] = data.depthSize; | |
78 } | |
79 if (data.stencilSize > 0) { | |
80 attrib [pos++] = OS.NSOpenGLPFAStencilSize; | |
81 attrib [pos++] = data.stencilSize; | |
82 } | |
83 // if (data.accumRedSize > 0) { | |
84 // attrib [pos++] = OS.AGL_ACCUM_RED_SIZE; | |
85 // attrib [pos++] = data.accumRedSize; | |
86 // } | |
87 // if (data.accumGreenSize > 0) { | |
88 // attrib [pos++] = OS.AGL_ACCUM_GREEN_SIZE; | |
89 // attrib [pos++] = data.accumGreenSize; | |
90 // } | |
91 // if (data.accumBlueSize > 0) { | |
92 // attrib [pos++] = OS.AGL_ACCUM_BLUE_SIZE; | |
93 // attrib [pos++] = data.accumBlueSize; | |
94 // } | |
95 // if (data.accumAlphaSize > 0) { | |
96 // attrib [pos++] = OS.AGL_ACCUM_ALPHA_SIZE; | |
97 // attrib [pos++] = data.accumAlphaSize; | |
98 // } | |
99 if (data.sampleBuffers > 0) { | |
100 attrib [pos++] = OS.NSOpenGLPFASampleBuffers; | |
101 attrib [pos++] = data.sampleBuffers; | |
102 } | |
103 if (data.samples > 0) { | |
104 attrib [pos++] = OS.NSOpenGLPFASamples; | |
105 attrib [pos++] = data.samples; | |
106 } | |
107 attrib [pos++] = 0; | |
108 pixelFormat = (NSOpenGLPixelFormat)new NSOpenGLPixelFormat().alloc(); | |
109 if (pixelFormat is null) { | |
110 dispose (); | |
111 DWT.error (DWT.ERROR_UNSUPPORTED_DEPTH); | |
112 } | |
113 pixelFormat.initWithAttributes(attrib); | |
114 | |
115 glView = (NSOpenGLView)new NSOpenGLView().alloc(); | |
116 if (glView is null) { | |
117 dispose (); | |
118 DWT.error (DWT.ERROR_UNSUPPORTED_DEPTH); | |
119 } | |
120 glView.initWithFrame(parent.view.frame(), pixelFormat); | |
121 glView.setAutoresizingMask(OS.NSViewWidthSizable | OS.NSViewHeightSizable); | |
122 | |
123 Listener listener = new Listener () { | |
124 public void handleEvent (Event event) { | |
125 switch (event.type) { | |
126 case DWT.Dispose: | |
127 if (glView !is null) glView.release(); | |
128 glView = null; | |
129 if (pixelFormat !is null) pixelFormat.release(); | |
130 pixelFormat = null; | |
131 break; | |
132 } | |
133 } | |
134 }; | |
135 addListener (DWT.Dispose, listener); | |
136 } | |
137 | |
138 /** | |
139 * Returns a GLData object describing the created context. | |
140 * | |
141 * @return GLData description of the OpenGL context attributes | |
142 * @exception DWTException <ul> | |
143 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
144 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
145 * </ul> | |
146 */ | |
147 public GLData getGLData () { | |
148 checkWidget (); | |
149 GLData data = new GLData (); | |
150 int [] value = new int [1]; | |
151 // AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_DOUBLEBUFFER, value); | |
152 // data.doubleBuffer = value [0] !is 0; | |
153 // AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_STEREO, value); | |
154 // data.stereo = value [0] !is 0; | |
155 // AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_RED_SIZE, value); | |
156 // data.redSize = value [0]; | |
157 // AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_GREEN_SIZE, value); | |
158 // data.greenSize = value [0]; | |
159 // AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_BLUE_SIZE, value); | |
160 // data.blueSize = value [0]; | |
161 // AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_ALPHA_SIZE, value); | |
162 // data.alphaSize = value [0]; | |
163 // AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_DEPTH_SIZE, value); | |
164 // data.depthSize = value [0]; | |
165 // AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_STENCIL_SIZE, value); | |
166 // data.stencilSize = value [0]; | |
167 // AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_ACCUM_RED_SIZE, value); | |
168 // data.accumRedSize = value [0]; | |
169 // AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_ACCUM_GREEN_SIZE, value); | |
170 // data.accumGreenSize = value [0]; | |
171 // AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_ACCUM_BLUE_SIZE, value); | |
172 // data.accumBlueSize = value [0]; | |
173 // AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_ACCUM_ALPHA_SIZE, value); | |
174 // data.accumAlphaSize = value [0]; | |
175 // AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_SAMPLE_BUFFERS_ARB, value); | |
176 // data.sampleBuffers = value [0]; | |
177 // AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_SAMPLES_ARB, value); | |
178 data.samples = value [0]; | |
179 return data; | |
180 } | |
181 | |
182 /** | |
183 * Returns a bool indicating whether the receiver's OpenGL context | |
184 * is the current context. | |
185 * | |
186 * @return true if the receiver holds the current OpenGL context, | |
187 * false otherwise | |
188 * @exception DWTException <ul> | |
189 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
190 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
191 * </ul> | |
192 */ | |
193 public bool isCurrent () { | |
194 checkWidget (); | |
195 return NSOpenGLContext.currentContext().id is glView.openGLContext().id; | |
196 } | |
197 | |
198 /** | |
199 * Sets the OpenGL context associated with this GLCanvas to be the | |
200 * current GL context. | |
201 * | |
202 * @exception DWTException <ul> | |
203 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
204 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
205 * </ul> | |
206 */ | |
207 public void setCurrent () { | |
208 checkWidget (); | |
209 glView.openGLContext().makeCurrentContext(); | |
210 } | |
211 | |
212 /** | |
213 * Swaps the front and back color buffers. | |
214 * | |
215 * @exception DWTException <ul> | |
216 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
217 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
218 * </ul> | |
219 */ | |
220 public void swapBuffers () { | |
221 checkWidget (); | |
222 glView.openGLContext().flushBuffer(); | |
223 } | |
224 } |