/*
 * Created on 02.08.2003
 */
package net.sf.scrapie.opengltest;

import java.awt.BorderLayout;
import java.awt.DisplayMode;
import java.awt.Frame;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import net.java.games.jogl.Animator;
import net.java.games.jogl.GLCanvas;
import net.java.games.jogl.GLCapabilities;
import net.java.games.jogl.GLDrawableFactory;

/**
 * @author bodo
 */
public class Test extends Frame {

	/**
	 * Gibt den Display-Mode zurück, der am besten passt
	 * @param bestmodes die Liste der gewünschten Modi
	 * @param device das GraphicsDevie
	 * @return der beste Modus
	 */
	private DisplayMode getBestDisplayMode(DisplayMode[] bestmodes, GraphicsDevice device) {
		for (int x = 0; x < bestmodes.length; x++) {
			DisplayMode[] modes = device.getDisplayModes();
			for (int i = 0; i < modes.length; i++) {
				if (modes[i].getWidth() == bestmodes[x].getWidth()
					&& modes[i].getHeight() == bestmodes[x].getHeight()
					&& modes[i].getBitDepth()
						== bestmodes[x].getBitDepth()) {
					return bestmodes[x];
				}
			}
		}

		return null;
	}

	/**
	 * Setzt den Display-Modus auf den passensten, den Java finden kann.
	 * @param bestmodes die Liste der Modes, die passen könnten
	 * @param device das Device
	 */
	public void setBestDisplayMode(DisplayMode[] bestmodes,GraphicsDevice device) {
		DisplayMode best = getBestDisplayMode(bestmodes, device);
		if (best != null) {
			device.setDisplayMode(best);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}
		} else {
			System.out.println("Unable to initialize graphics hardward.");
			System.out.println(
				"Try to reduce graphical hardware acceleration.");
			System.exit(11);
		}
	}

	public Test() {
		setTitle("JoGL Test");
		setSize( 800, 600 );
	}

	public void init() {
	/*	DisplayMode[] modes =
			new DisplayMode[] {
				new DisplayMode(800, 600, 32, 0),
				new DisplayMode(800, 600, 16, 0),
				};
		
		GraphicsDevice device =
						GraphicsEnvironment
							.getLocalGraphicsEnvironment()
							.getDefaultScreenDevice();		
		setUndecorated(true);
		setIgnoreRepaint(true);
		device.setFullScreenWindow(this);

		setBestDisplayMode(modes, device);
	*/	
		GLCapabilities glCaps = new GLCapabilities();
		glCaps.setRedBits(8);
		glCaps.setBlueBits(8);
		glCaps.setGreenBits(8);
		glCaps.setAlphaBits(8);
 
		GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas( glCaps );
 
 		this.setLayout(new BorderLayout());
		add( canvas , BorderLayout.CENTER);
		
		TestRenderer test = new TestRenderer(this);
		canvas.addGLEventListener(test);
 
		final Animator animator = new Animator( canvas);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
			  animator.stop();
			  System.exit(0);
			}
		  });

		show();

		animator.start();		
	}

	public static void main(String[] args) {
		Test test = new Test();
		test.init();
	}
}
