/* Instructions: Launch the application (BuggyFrame) and then select
 * "Open" from the File Menu.    Even with -DATI_WORKAROUND is set to
 * "true", this will still crash on ATI hardware as follows:
 * 
 * An unexpected exception has been detected in native code outside the VM.
 * Unexpected Signal : EXCEPTION_ACCESS_VIOLATION (0xc0000005) occurred at PC=0x24F8A703
 * Function=DrvSetContext+0x11313
 * Library=E:\WINNT\system32\atioglxx.dll
 * 
 * at net.java.games.jogl.impl.windows.WGL.wglCreateContext(Native Method)
 * at net.java.games.jogl.impl.windows.WindowsGLContext.
 *                            choosePixelFormatAndCreateContext(WindowsGLContext.java:495)
 */

import net.java.games.jogl.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class BuggyFrame extends JFrame
{
    private class BuggyPanel extends JPanel {
                    
        private class BuggyListener implements GLEventListener {
            private int width,height;
                                
            synchronized public void display(GLDrawable gLDrawable)
            {
                final GL gl = gLDrawable.getGL();
                gl.glViewport(0,0,this.width, this.height);
                gl.glClear( GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT );
                gl.glFlush(); 
            }

            public void displayChanged(GLDrawable gLDrawable, 
                                       boolean modeChanged, boolean deviceChanged) {}
                        
            public void init(GLDrawable gLDrawable) {}
                        
            public void reshape(GLDrawable gLDrawable, 
                                int x, int y, int width, int height)
            {    this.height = height; this.width = width; }

        } // BuggyListener
                
        public BuggyPanel()
        {
            BuggyListener listener = new BuggyListener();
            GLCanvas canvas =    
                GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
            canvas.addGLEventListener(listener);
            this.setLayout(new BorderLayout());
            this.add(canvas,BorderLayout.CENTER);
        }
    } // BuggyPanel

    class OpenAction extends AbstractAction {
        public OpenAction() { super("Open"); } 
        public void actionPerformed(ActionEvent e) { new BuggyFrame(); }
    }
    class QuitAction extends AbstractAction {
        public QuitAction() { super("Quit"); } 
        public void actionPerformed(ActionEvent e) { System.exit(0); }
    }
    public BuggyFrame() {
        super("ATI Buggy Demonstrator");

        // set up the menu
        JPopupMenu.setDefaultLightWeightPopupEnabled(false);
        JMenuBar menuBar = new JMenuBar();
        this.setJMenuBar(menuBar);
        JMenu fileMenu = new JMenu("File");
        menuBar.add(fileMenu);
        fileMenu.add(new OpenAction());
        fileMenu.add(new QuitAction());

        // load the panel
        Container cp = this.getContentPane();
        this.setSize(640,480);
        cp.add(new BuggyPanel());
        this.show();
    }
    public static void main(String[] args)
    {
        BuggyFrame app = new BuggyFrame();
    }
} // BuggyFrame

