1 11 package org.eclipse.swt.opengl; 12 13 import org.eclipse.swt.*; 14 import org.eclipse.swt.widgets.*; 15 import org.eclipse.swt.internal.win32.*; 16 import org.eclipse.swt.internal.opengl.win32.*; 17 18 23 24 public class GLCanvas extends Canvas { 25 int context; 26 int pixelFormat; 27 28 41 public GLCanvas (Composite parent, int style, GLData data) { 42 super (parent, style); 43 if (data == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 44 PIXELFORMATDESCRIPTOR pfd = new PIXELFORMATDESCRIPTOR (); 45 pfd.nSize = (short) PIXELFORMATDESCRIPTOR.sizeof; 46 pfd.nVersion = 1; 47 pfd.dwFlags = WGL.PFD_DRAW_TO_WINDOW | WGL.PFD_SUPPORT_OPENGL; 48 pfd.dwLayerMask = WGL.PFD_MAIN_PLANE; 49 pfd.iPixelType = (byte) WGL.PFD_TYPE_RGBA; 50 if (data.doubleBuffer) pfd.dwFlags |= WGL.PFD_DOUBLEBUFFER; 51 if (data.stereo) pfd.dwFlags |= WGL.PFD_STEREO; 52 pfd.cRedBits = (byte) data.redSize; 53 pfd.cGreenBits = (byte) data.greenSize; 54 pfd.cBlueBits = (byte) data.blueSize; 55 pfd.cAlphaBits = (byte) data.alphaSize; 56 pfd.cDepthBits = (byte) data.depthSize; 57 pfd.cStencilBits = (byte) data.stencilSize; 58 pfd.cAccumRedBits = (byte) data.accumRedSize; 59 pfd.cAccumGreenBits = (byte) data.accumGreenSize; 60 pfd.cAccumBlueBits = (byte) data.accumBlueSize; 61 pfd.cAccumAlphaBits = (byte) data.accumAlphaSize; 62 pfd.cAccumBits = (byte) (pfd.cAccumRedBits + pfd.cAccumGreenBits + pfd.cAccumBlueBits + pfd.cAccumAlphaBits); 63 64 74 int hDC = OS.GetDC (handle); 75 pixelFormat = WGL.ChoosePixelFormat (hDC, pfd); 76 if (pixelFormat == 0 || !WGL.SetPixelFormat (hDC, pixelFormat, pfd)) { 77 OS.ReleaseDC (handle, hDC); 78 dispose (); 79 SWT.error (SWT.ERROR_UNSUPPORTED_DEPTH); 80 } 81 context = WGL.wglCreateContext (hDC); 82 if (context == 0) { 83 OS.ReleaseDC (handle, hDC); 84 SWT.error (SWT.ERROR_NO_HANDLES); 85 } 86 OS.ReleaseDC (handle, hDC); 87 90 Listener listener = new Listener () { 91 public void handleEvent (Event event) { 92 switch (event.type) { 93 case SWT.Dispose: 94 WGL.wglDeleteContext (context); 95 break; 96 } 97 } 98 }; 99 addListener (SWT.Dispose, listener); 100 } 101 102 111 public GLData getGLData () { 112 checkWidget (); 113 GLData data = new GLData (); 114 PIXELFORMATDESCRIPTOR pfd = new PIXELFORMATDESCRIPTOR (); 115 pfd.nSize = (short) PIXELFORMATDESCRIPTOR.sizeof; 116 int hDC = OS.GetDC (handle); 117 WGL.DescribePixelFormat (hDC, pixelFormat, PIXELFORMATDESCRIPTOR.sizeof, pfd); 118 OS.ReleaseDC (handle, hDC); 119 data.doubleBuffer = (pfd.dwFlags & WGL.PFD_DOUBLEBUFFER) != 0; 120 data.stereo = (pfd.dwFlags & WGL.PFD_STEREO) != 0; 121 data.redSize = pfd.cRedBits; 122 data.greenSize = pfd.cGreenBits; 123 data.blueSize = pfd.cBlueBits; 124 data.alphaSize = pfd.cAlphaBits; 125 data.depthSize = pfd.cDepthBits; 126 data.stencilSize = pfd.cStencilBits; 127 data.accumRedSize = pfd.cAccumRedBits; 128 data.accumGreenSize = pfd.cAccumGreenBits; 129 data.accumBlueSize = pfd.cAccumBlueBits; 130 data.accumAlphaSize = pfd.cAccumAlphaBits; 131 return data; 132 } 133 134 145 public boolean isCurrent () { 146 checkWidget (); 147 return WGL.wglGetCurrentContext () == context; 148 } 149 150 159 public void setCurrent () { 160 checkWidget (); 161 if (WGL.wglGetCurrentContext () == context) return; 162 int hDC = OS.GetDC (handle); 163 WGL.wglMakeCurrent (hDC, context); 164 OS.ReleaseDC (handle, hDC); 165 } 166 167 175 public void swapBuffers () { 176 checkWidget (); 177 int hDC = OS.GetDC (handle); 178 WGL.SwapBuffers (hDC); 179 OS.ReleaseDC (handle, hDC); 180 } 181 } 182 | Popular Tags |