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

import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

import net.java.games.jogl.GL;
import net.java.games.jogl.GLDrawable;
import net.java.games.jogl.GLEventListener;
import net.java.games.jogl.GLU;

/**
 * @author bodo
 */
public class TestRenderer implements GLEventListener, KeyListener {
	private long frameCount = 0;
	private long lastTime = 0;
	private long fps = 0;

	int _width;
	int _height;

	Random rnd = new Random();

	Texture tex;

	private Frame _parent;

	public TestRenderer(Frame frame) {
		_parent = frame;
		frame.addKeyListener(this);
	}

	/* (non-Javadoc)
	 * @see net.java.games.jogl.GLEventListener#init(net.java.games.jogl.GLDrawable)
	 */
	public void init(GLDrawable drawable) {
		GL gl = drawable.getGL();
		
		System.out.println(gl.glGetString(GL.GL_RENDERER));
		System.out.println(gl.glGetString(GL.GL_VENDOR));
		System.out.println(gl.glGetString(GL.GL_VERSION));
		
		gl.glEnable(GL.GL_BLEND);
		gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
		gl.glEnable(GL.GL_TEXTURE_2D);
		
		gl.glClearColor(0.0F, 0.0F, 0.0F, 0.0F);
		gl.glViewport(0, 0, _width, _height);

		gl.glShadeModel(GL.GL_FLAT);
		gl.glDisable(GL.GL_DEPTH_TEST);

		try {
			TextureLoader loader = new TextureLoader(gl, drawable.getGLU());
			tex = loader.getTexture("test", "1.png");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/* (non-Javadoc)
	 * @see net.java.games.jogl.GLEventListener#display(net.java.games.jogl.GLDrawable)
	 */
	public void display(GLDrawable drawable) {
		GL gl = drawable.getGL();

		gl.glClearColor(0.0F, 0.6F, 0.0F, 0.0F);
		gl.glClear(GL.GL_COLOR_BUFFER_BIT);

		for (int i = 0; i < 500; i++) {
			drawSprite(gl, tex, rnd.nextInt(_width), rnd.nextInt(_height));
		}

		gl.glFlush();
		
		frameCount++;
		if (System.currentTimeMillis() - lastTime > 1000L) {
			fps = frameCount;
			frameCount = 0;
			lastTime = System.currentTimeMillis();
			System.out.println(fps);
		}

	}

	public void drawSprite(GL gl, Texture tex, int x, int y) {
		gl.glLoadIdentity();
		tex.bind(gl);
		gl.glTranslatef((float) x, (float) y, 0.0F);

		gl.glBegin(GL.GL_QUADS);
			gl.glTexCoord2f(0.0F, 0.0F);
			gl.glVertex2f(0.0F, 0.0F);
			
			gl.glTexCoord2f(0.0F, tex.getHeight());
			gl.glVertex2f(0.0F, (float) tex.getImageHeight());
			
			gl.glTexCoord2f(tex.getWidth(), tex.getHeight());
			gl.glVertex2f((float) tex.getImageWidth(), (float) tex.getImageHeight());
			
			gl.glTexCoord2f(tex.getWidth(), 0.0F);
			gl.glVertex2f((float) tex.getImageWidth(), 0.0F);
		gl.glEnd();
	}

	/* (non-Javadoc)
	 * @see net.java.games.jogl.GLEventListener#reshape(net.java.games.jogl.GLDrawable, int, int, int, int)
	 */
	public void reshape(GLDrawable drawable,int x,int y,int width,int height) {
		GL gl = drawable.getGL();
		GLU glu = drawable.getGLU();
		
		_width = width;
		_height = height;
		
		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glLoadIdentity();
		gl.glOrtho(0.0, (double) width, (double) height, 0.0, -1.0, 1.0);		
		gl.glMatrixMode(GL.GL_MODELVIEW);
	}

	/* (non-Javadoc)
	 * @see net.java.games.jogl.GLEventListener#displayChanged(net.java.games.jogl.GLDrawable, boolean, boolean)
	 */
	public void displayChanged(
		GLDrawable drawable,
		boolean modeChanged,
		boolean deviceChanged) {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
	 */
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() ==  KeyEvent.VK_ESCAPE) {
			System.exit(0);
		}
	}

	/* (non-Javadoc)
	 * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
	 */
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
	 */
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
}