package anim;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
import obj.Bullet;
import obj.Enemy;
import obj.Forme;
import world.World;
public class Scene implements GLEventListener {
private GLU glu = new GLU();
private float h = 1;
// le haut
public static float upX = 0.0f;
public static float upY = 10.0f;
public static float upZ = 0.0f;
//contient tous les elements du jeu
public World world;
/* ******** CONSTRUCTEUR *********** */
public Scene(){
}
public void setWorld(World w){
this.world = w;
}
public World getWorld(){
return world;
}
/* ******** INIT ******** */
// (appeler apres chaque pause)
public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
// reglages inconnus
gl.glShadeModel(GL2.GL_SMOOTH); // Enable Smooth Shading
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
gl.glClearDepth(1.0f); // Depth Buffer Setup
gl.glEnable(GL2.GL_DEPTH_TEST); // Enables Depth Testing
gl.glDepthFunc(GL2.GL_LEQUAL); // The Type Of Depth Testing To Do
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST); // Really Nice Perspective Calculations
gl.setSwapInterval(1);
// debut texture
gl.glEnable(GL2.GL_BLEND);
gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL2.GL_TEXTURE_2D);
//chargement des fichiers
Forme.init_texture(gl);
Enemy.init_texture(gl);
Bullet.init_texture(gl);
// fin texture
// avoir le focus
try {
Robot robot = new Robot();
robot.mousePress(InputEvent.BUTTON2_MASK );
robot.mouseRelease(InputEvent.BUTTON2_MASK );
} catch (AWTException e) {
e.printStackTrace();
}
}// fin init
/* ******** DISPLAY ******** */
/**
* display() sera appel�e en boucle tout au long de l'application
* par la classe Animator. C'est dans cette fonction qu'on fera
* tout ce qui doit �tre affich�
*/
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
//actualise la camera
this.repaint(gl);
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
// dessine les cube
this.render(gl);
// aucune id�e
gl.glFlush();
}// fin display
// dessine les formes
public void render(GL2 gl) {
// dessine le monde
world.render(gl);
}// fin render
/* ******** RESHAPE ******** */
/**
* reshape() sera appel�e si la fen�tre d'affichage est redimensionn�e
*/
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL2 gl = drawable.getGL().getGL2();
if (height <= 0) { // avoid a divide by zero error!
height = 1;
}
h = (float) width / (float) height;
gl.glViewport(0, 0, width, height);
// actualise la camera
repaint(gl);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
}// fin reshape
/* ******** DISPOSE ******** */
public void dispose(GLAutoDrawable arg0) {
// System.out.println("dispose");
// eventuellement tuer un thread...
}
/* ******** REPAINT ******** */
// actualise la camera
public void repaint(GL2 gl) {
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
// 1 - FOV
// 2 - h= ratio de l'�cran
// 3 - minimum range of view
// 4 - maximum range of view
glu.gluPerspective(70.0f, h, 0.001f, 200.0f);
world.getCamera().gluLookAt(glu);
}
}