Fullscreen issue on KeyListener event

classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|

Fullscreen issue on KeyListener event

laani
Hi, I'm new to JOGL.

I'm trying to get full screen mode to work but I have an issue on a key event when I toggle between fullscreen and window mode.

The funny thing is when I place this code in simpleScene.animator.pause(); in the keyPressed(KeyEvent e) function
it works.

Can anyone explain why its doing this?

Thanks in advance!

ERROR
Exception in thread "Timer-0" javax.media.opengl.GLException: javax.media.opengl.GLException: Unable to create temp OpenGL context for device context 0xc013661
        at jogamp.opengl.awt.AWTThreadingPlugin.invokeOnOpenGLThread(AWTThreadingPlugin.java:98)
        at jogamp.opengl.ThreadingImpl.invokeOnOpenGLThread(ThreadingImpl.java:197)
        at javax.media.opengl.Threading.invokeOnOpenGLThread(Threading.java:164)
        at javax.media.opengl.awt.GLCanvas.maybeDoSingleThreadedWorkaround(GLCanvas.java:830)
        at javax.media.opengl.awt.GLCanvas.display(GLCanvas.java:419)
        at com.jogamp.opengl.util.AWTAnimatorImpl.display(AWTAnimatorImpl.java:74)
        at com.jogamp.opengl.util.AnimatorBase.display(AnimatorBase.java:142)
        at com.jogamp.opengl.util.FPSAnimator$1.run(FPSAnimator.java:128)
        at java.util.TimerThread.mainLoop(Unknown Source)
        at java.util.TimerThread.run(Unknown Source)
Caused by: javax.media.opengl.GLException: Unable to create temp OpenGL context for device context 0xc013661
        at jogamp.opengl.windows.wgl.WindowsWGLContext.createImpl(WindowsWGLContext.java:294)
        at jogamp.opengl.GLContextImpl.makeCurrentLocking(GLContextImpl.java:468)
        at jogamp.opengl.GLContextImpl.makeCurrent(GLContextImpl.java:399)
        at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:383)
        at javax.media.opengl.awt.GLCanvas$DisplayOnEventDispatchThreadAction.run(GLCanvas.java:956)
        at java.awt.event.InvocationEvent.dispatch(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$000(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

CODE

        package windows;
       
        import java.awt.Color;
        import java.awt.Dimension;
        import java.awt.Frame;
        import java.awt.GraphicsDevice;
        import java.awt.GraphicsEnvironment;
        import java.awt.Toolkit;
        import java.awt.event.KeyAdapter;
        import java.awt.event.KeyEvent;
        import java.awt.event.WindowAdapter;
        import java.awt.event.WindowEvent;
       
        public class App
        {
                private static boolean fullScreenMode= true;
                private static int winW, winH;
                private static int winX, winY;
                private static int winWCurrent, winHCurrent;
                private static String winTitle = "Window Title";
                private final SimpleScene simpleScene;
       
                public App()
                {
                        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
                       
                        winW = dim.width;
                        winH = dim.height - 40;
                        winX = winY = 0;
                       
                        simpleScene = new SimpleScene();
                       
                        final GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
                        final GraphicsDevice device = environment.getDefaultScreenDevice();
                        device.getDefaultConfiguration();
                       
                        final Frame frame = new Frame();

                        frame.setTitle(winTitle);
                        frame.setBackground(Color.BLUE);
                       
                        if(device.isFullScreenSupported())
                        {
                                frame.setUndecorated(true);
                                frame.setResizable(false);
                                device.setFullScreenWindow(frame);
                                fullScreenMode = true;
                        }
                        else
                        {
                                frame.setBounds(winX, winY, winW, winH);
                                frame.setResizable(true);
                                fullScreenMode = true;
                        }
                       
                        winWCurrent = frame.getWidth();
                        winHCurrent = frame.getHeight();

                        frame.add(simpleScene);
                        frame.setVisible(true);
                       
                        frame.addWindowListener(new WindowAdapter()
                        {
                                public void windowClosing(WindowEvent we)
                                {
                                        simpleScene.animator.stop();
                                        System.exit(0);
                                }
                        });
                       
                        frame.addKeyListener(new KeyAdapter()
                        {
                                @Override
                                public void keyPressed(KeyEvent e)
                                {
                                        simpleScene.animator.pause();
                                       
                                        int keyCode = e.getKeyCode();
                                        switch(keyCode)
                                        {
                                        case KeyEvent.VK_SPACE:
                                                fullScreenMode = !fullScreenMode;
                                                frame.setVisible(false);
                                                if(frame.isDisplayable())
                                                        frame.dispose();
                                                if(fullScreenMode)
                                                {
                                                        winW = frame.getWidth();
                                                        winH = frame.getHeight();
                                                        winX = frame.getX();
                                                        winY = frame.getY();
                                                       
                                                        if(device.isFullScreenSupported())
                                                        {
                                                                frame.setUndecorated(true);
                                                                frame.setResizable(false);
                                                                device.setFullScreenWindow(frame);
                                                        }
                                                }
                                                else
                                                {
                                                        frame.setUndecorated(false);
                                                        device.setFullScreenWindow(null);
                                                        frame.setBounds(winX, winY, winW, winH);
                                                        frame.setResizable(true);
                                                }
                                               
                                                winWCurrent = frame.getWidth();
                                                winHCurrent = frame.getHeight();
                                                frame.setVisible(true);
                                                break;
       
                                        case KeyEvent.VK_ESCAPE:
                                               
                                                simpleScene.animator.stop();
                                                System.exit(0);
                                                break;
                                        }
                                }
                        });
                        simpleScene.animator.start();

                }
               
            public static void main(String[] args)
            {
                new App();
            }
        }


        package windows;
       
        import java.awt.Panel;
        import javax.media.opengl.*;
        import javax.media.opengl.awt.GLCanvas;
// import javax.media.opengl.glu.GLU;
        import com.jogamp.opengl.util.FPSAnimator;
       
        public class SimpleScene extends Panel implements GLEventListener
        {
                /**
                 *
                 */
                private static final long serialVersionUID = 1L;
               
                private static final int REFRESH_FPS = 60;
// private GLU glu; // GL Utility
                final FPSAnimator animator;
               
                private double theta = 0;
                private double sine = 0;
                private double cosine = 0;
               
                public SimpleScene()
                {
                        GLProfile glp = GLProfile.getDefault();
                        GLCapabilities glc = new GLCapabilities(glp);
                        GLCanvas canvas = new GLCanvas(glc);
                        canvas.setSize(500, 500);
                       
                        this.add(canvas);
                        canvas.setVisible(true);
                        canvas.addGLEventListener(this);
                        animator = new FPSAnimator(canvas, REFRESH_FPS, true);
                }
                       
                @Override
                public void display(GLAutoDrawable drawable)
                {
                        System.out.println("DISPLAY");
                        update();
                        render(drawable);
                }
       
                private void render(GLAutoDrawable drawable)
                {
                        System.out.println("RENDER");
                        GL2 gl = drawable.getGL().getGL2();
                       
                        // clear the color buffer
                        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
                       
                        // draw triangle filling the window
                        gl.glBegin(GL.GL_TRIANGLES);
                        gl.glColor3f(1, 0, 0);
                        gl.glVertex2d(-cosine, -cosine);
                        gl.glColor3f(0, 1, 0);
                        gl.glVertex2d(0, cosine);
                        gl.glColor3f(0, 0, 1);
                        gl.glVertex2d(sine, -sine);
                        gl.glEnd();
                }
       
                private void update()
                {
                        System.out.println("UPDATE");

                        theta += 0.01;
                        sine = Math.sin(theta);
                        cosine = Math.cos(theta);
                }
       
                @Override
                public void dispose(GLAutoDrawable arg0) {
                        // TODO Auto-generated method stub
                }
       
                @Override
                public void init(GLAutoDrawable drawable)
                {
                        System.out.println("INIT");
                        GL gl = drawable.setGL(new DebugGL2(drawable.getGL().getGL2()));
                }
       
                @Override
                public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
                                int arg4) {
                        // TODO Auto-generated method stub
                }
        }



IMAGES: BEFORE/AFTER
Reply | Threaded
Open this post in threaded view
|

Re: Fullscreen issue on KeyListener event

robbiezl
my code cause the same same problem.

If u change the awt component to light weight component, that will be OK

 I hope the manager can solve this problem.

I usually use GLCanvas with light weight components.GLCanvas is more efficient than GLJPanel
Reply | Threaded
Open this post in threaded view
|

Re: Fullscreen issue on KeyListener event

gouessej
Administrator
I don't use AWT full screen mode anymore. As far as I know, disposing a frame to drive it not displayable is necessary to implement the toggle from/to full screen mode (to modify the decoration and/or to switch to exclusive full screen mode) but disposing an heavyweight frame destroys some native resources which might cause some troubles with GLCanvas. In my case, I did not succeed to get something fully working because I had to remove the GLCanvas from an AWT embedded frame, it called removeNotify(), the OpenGL context was destroyed :(

Maybe using a shared context would solve this problem but personally, I would switch to NewtCanvasAWT, I would put a GLWindow as a child and I would use NewtCanvasAWT instead of GLCanvas. To switch to full screen mode, I would directly use the NEWT methods of the GLWindow. I have never fully tested it. Finally, keep in mind that NEWT full screen mode is more reliable than AWT equivalent.

Do you plan to put only a JOGL canvas into a window? If so, you could fully switch to NEWT, it would be even easier.

Edit.: according to the screen capture, the context has been probably destroyed like in my case.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Fullscreen issue on KeyListener event

robbiezl
Thank u gouessej.
I will try to use NewtCanvasAWT.
If use GLCanvas cause the context destroyed ,how to use a shared context?
thank u
Reply | Threaded
Open this post in threaded view
|

Re: Fullscreen issue on KeyListener event

robbiezl
In reply to this post by gouessej
I tryed to USE NEWT,but it is not convinient.It dose not support the java mouse event,  I have to change all my AWTEventListener
Reply | Threaded
Open this post in threaded view
|

Re: Fullscreen issue on KeyListener event

Sven Gothel
Administrator
On 03/29/2012 04:13 AM, robbiezl [via jogamp] wrote:
>
>
> I tryed to USE NEWT,but it is not convinient.It dose not support the java
> mouse event, it noly support mouse listener and key listener,not support
> mouse wheel listener.

Mouse wheel events are delivered by the mouse listener in NEWT.

>
> And I have to change all my AWTEventListener

Well, we could write a factory/adapter translating NEWT to AWT events
and forwarding this to your AWT listener. We have such tools in place
for the inverse situation, Android/AWT -> NEWT events.

Look at com.jogamp.newt.event.awt.AWTAdapter and it's usage.

~Sven


signature.asc (910 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Fullscreen issue on KeyListener event

Sven Gothel
Administrator
In reply to this post by robbiezl
On 03/29/2012 02:49 AM, robbiezl [via jogamp] wrote:
>
>
> Thank u gouessej.
> I will try to use NewtCanvasAWT.
> If use GLCanvas cause the context destroyed ,how to use a shared context?

I have answered this here in the forum once ..

Search in forum, or better in the JOGL source code, unit test .. for *Shared*

~Sven

> thank u


signature.asc (910 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Fullscreen issue on KeyListener event

robbiezl
thank u for ur help!
Reply | Threaded
Open this post in threaded view
|

Re: Fullscreen issue on KeyListener event

gouessej
Administrator
robbiezl, you can put your AWT listeners on NewtCanvasAWT (which is an AWT component too) if you don't want to use NEWT listeners.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Fullscreen issue on KeyListener event

Sven Gothel
Administrator
On 03/29/2012 09:14 AM, gouessej [via jogamp] wrote:
>
>
> robbiezl, you can put your AWT listeners on NewtCanvasAWT (which is an AWT
> component too) if you don't want to use NEWT listeners.

That works on all platforms ? I would think our NEWTCanvas
hides the AWT Canvas component due to the native Window parenting
and hence AWT won't receive the events anymore.

But if this works, even better :)

~Sven


signature.asc (910 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Fullscreen issue on KeyListener event

gouessej
Administrator
Actually, I have never tested this suggestion.

Do you mean the bridge between NEWT and AWT relies on some native code? If so, it explains why I don't reproduce my bug under Windows, only under GNU Linux.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Fullscreen issue on KeyListener event

Sven Gothel
Administrator
On 03/29/2012 09:49 PM, gouessej [via jogamp] wrote:
>
>
> Actually, I have never tested this suggestion.
>
> Do you mean the bridge between NEWT and AWT relies on some native code?

NewtCanvasAWT is based on the native window parenting
using native platform code for each windowing system - yes.

A normal NEWT Window is being attached to the AWT Canvas's native Window
as it's parent. Hence native events may not be delivered from the underlying
parent, the AWT Canvas. That depends on the AWT native event processing ..

I would say .. don't rely on those AWT events in such case.

> If
> so, it explains why I don't reproduce my bug under Windows, only under GNU
> Linux.

~Sven


signature.asc (910 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Fullscreen issue on KeyListener event

robbiezl
In reply to this post by gouessej
I tryed to put awt listener on NewtCanvasAWT ,but it can not cause the awt event.
Now I still the GLCanvas ,it is better for me
Reply | Threaded
Open this post in threaded view
|

Re: Fullscreen issue on KeyListener event

gouessej
Administrator
Have you solved your problem that way? If it's not the case, you should really consider using NEWT, you can find all necessary methods in the Java documentation of NEWT and lots of them are very close to their AWT equivalents, it is not difficult.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Fullscreen issue on KeyListener event

robbiezl
still no solution to this samll example.
Reply | Threaded
Open this post in threaded view
|

Re: Fullscreen issue on KeyListener event

Sven Gothel
Administrator
On 03/31/2012 03:05 AM, robbiezl [via jogamp] wrote:
>
>
> still no solution to this samll example.

You may have a look at our NEWT unit tests / demos,
where fullscreen even works out of the browser Applet.

See our Applet test page, and TestGearsES2NEWT.java within our JOGL source
code. If you like to see fullscreen w/ screen-mode change in action,
pls have a look at TestScreenMode01NEWT.java.

I haven't even tried AWT fullscreen mode, sorry.

~Sven


signature.asc (910 bytes) Download Attachment