package com.abraham.gl.streamingtextures;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLDrawableFactory;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import com.sun.opengl.utils.Animator;

/**
 * @author Matthias Abraham
 *
 */
public class StreamingTextures
{
   /**
    * @param args
    */
   public static void main( String[] args )
   {
      new StreamingTextures();
   }
   
   /**
    * Constructs a new <code>StreamingTextures</code>.
    */
   public StreamingTextures()
   {
      JFrame frame = new JFrame( Messages.getString( "StreamingTextures.FRAME_TITLE" ) ); //$NON-NLS-1$
      frame.setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE );
      
      Container contentPane = frame.getContentPane();
      contentPane.setLayout( new BorderLayout() );
      GLCanvas canvas = createGLCanvas();
      contentPane.add( canvas );
      
      frame.pack();
      frame.setLocationByPlatform( true );
      frame.setVisible( true );
      // no more UI work beyond this point, as this is not the AWT/Swing thread
      
      final Animator animator = new Animator( canvas );
      animator.setPrintExceptions( true );
      animator.setRunAsFastAsPossible( true );
      animator.start();
      
      frame.addWindowListener( new WindowAdapter()
      {
         @Override
         public void windowClosing( WindowEvent e )
         {
            animator.stop();
            SwingUtilities.invokeLater( new Runnable()
            {
               public void run()
               {
                  System.exit( 0 );
               }
            } );
         }
      } );
   }

   private GLCanvas createGLCanvas()
   {
      GLCanvas canvas = new GLCanvas();
      canvas.setPreferredSize( new Dimension( 1024, 1024 ) );
      canvas.addGLEventListener( new GLEventHandler() );
      return canvas;
   }
}
