Re: New tutorial on AWT/SWT/Swing/GLJPanel/GLCanvas
Posted by
Wade Walker on
Feb 11, 2011; 10:47pm
URL: https://forum.jogamp.org/New-tutorial-on-AWT-SWT-Swing-GLJPanel-GLCanvas-tp2363921p2477248.html
Sven Gothel wrote
On Friday, February 11, 2011 11:02:37 Sven Gothel wrote:
> 89 long hdc = WGL.wglGetCurrentDC();
> 90 if (0 == hdc) {
> 91 throw new GLException("Error: attempted to make an external GLDrawable without a drawable current, werr " + GDI.GetLastError());
> 92 }
> 93 int hdcType = GDI.GetObjectType(hdc);
> 94 if( GDI.OBJ_DC != hdcType ) {
> 95 // FIXME: Turns out in above use case (WinXP-32bit, GDI, SWT) the returned DC (not 0) is invalid!
I know why this fails now

If you wrap the external context creation like this, my SWT tutorial works:
int hDC = OS.GetDC( glcanvas.handle );
final GLContext glcontext = GLDrawableFactory.getFactory( glprofile ).createExternalGLContext();
OS.ReleaseDC(glcanvas.handle, hDC);
The reason is because SWT doesn't leave a current DC. In the GLCanvas() constructor and in GLCanvas.makeCurrent(), they create a temp DC like this and release it immediately after use.
So when you call WGL.wglGetCurrentDC() it returns a DC handle, but it's invalid because SWT released it

Looking at the JOGL 1.1.1.a code for GLDrawableFactory.createExternalGLContext(), you can see that they didn't use a DC anywhere inside, so they didn't have this problem.
I guess the solution is to remove the need for a DC inside the new JOGL 2 createExternalGLContext(). I'm not sure of the best way to do that -- I'd probably end up butchering your code

MS probably changed the behavior of DC release in 64-bit versions of Windows, so you can sometimes access a DC after release without crashing. But it's probably not good to count on this.