package anim;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import javax.imageio.ImageIO;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
import listener.Souris;
public class Scene implements GLEventListener {
Forme forme = new Forme();
private GLU glu = new GLU();
private int[][] pos;
private int nbCol = 0;
private int nbLig = 0;
private float h = 1;
// deplacement
private boolean haut = false;
private boolean bas = false;
private boolean gauche = false;
private boolean droite = false;
private float direction = 0.0f;
private Souris souris = new Souris();
// posDebut
public double eyeX = 12.0f;
public double eyeY = 0.5f;
public double eyeZ = -12.0f;
// le haut
public float upX = 0.0f;
public float upY = 10.0f;
public float upZ = 0.0f;
// pos debut
public double debutX;
public double debutZ;
/* ******** INIT ******** */
public void init(GLAutoDrawable drawable) {
// System.out.println("init");
//loadTextFile("laby1.txt");
loadImgFile("laby2.png");
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);
}
// lecture du fichier texte
public void loadTextFile(String path) {
InputStream link = null;
try {
// ouverture du fichier qui est dans les sources
link = getClass().getClassLoader().getResource(path).openStream();
Scanner sc = new Scanner(link);
String ligne;
String[] val;
boolean premier = true;
int id = 0;
int height = 0;
int width = 0;
// parcours de toutes les lignes du fichier
while ( sc.hasNext() ) {
ligne = sc.nextLine();
// on ignore les -- et les lignes vide
if ( ! (ligne.startsWith("--") || ligne.isEmpty()) ) {
// premiere ligne = taille du laby
if ( premier ) {
val = ligne.split(",");
width = Integer.parseInt(val[0]);
height = Integer.parseInt(val[1]);
pos = new int[height][width];
premier = false;
// le reste le type des blocs
}else {
val = ligne.split(",");
for ( int i = 0; i < width ; i++ ) {
pos[id][i] = Integer.parseInt(val[i]);
if ( pos[id][i] == 2 ){
eyeX = id+0.5;
eyeZ = i+0.6;
debutX = eyeX;
debutZ = eyeZ;
}
}
id++;
}
}
}
nbLig = height;
nbCol = width;
sc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// lecture de l'image
public void loadImgFile(String path) {
InputStream link = null;
try {
link = getClass().getClassLoader().getResource(path).openStream();
BufferedImage bi = ImageIO.read(link);
int height = bi.getHeight();
int width = bi.getWidth();
pos = new int[height][width];
for(int x = 0 ; x < height ; x++) {
for(int y = 0 ; y < width ; y++) {
int rgb = bi.getRGB(x, y);
@SuppressWarnings("unused")
int alpha = (rgb >> 24) & 0xFF;
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = (rgb ) & 0xFF;
pos[x][y] = getType(red, green, blue);
if ( pos[x][y] == 2 ){
eyeX = x+0.5;
eyeZ = y+0.6;
debutX = eyeX;
debutZ = eyeZ;
}
}
}
nbLig = height;
nbCol = width;
} catch (IOException e) {
e.printStackTrace();
}
}
private int getType(int R, int G, int B) {
if(R == 255 && G == 255 && B == 255) {
//blanc
return 1;
} else if(R == 0 && G == 0 && B == 0) {
//noir
return 0;
} else if(R == 255 && G == 0 && B == 0) {
//rouge
return 2;
} else if(R == 0 && G == 255 && B == 0) {
//vert
return 3;
}
return -1;
}
/* ******** 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();
// (met a jour des variables pour la rotation des cubes)
forme.actualise();
}
// dessine les formes
public void render(GL2 gl) {
// dessine le sol
forme.drawFloor(gl, 0.0f);
for ( int i = 0 ; i < nbLig ; i++ ){
for ( int j = 0 ; j < nbCol ; j++ ){
switch(pos[i][j]){
case 0:
forme.drawQuads(gl, i, j);
break;
case 1:
// un vide
break;
case 2:
forme.drawFlag(gl, i, j, 'g');
break;
case 3 :
forme.drawFlag(gl, i, j, 'r');
break;
default:
System.out.println("erreur");
}
}
}
}
/* ******** 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();
}
/* ******** DISPOSE ******** */
public void dispose(GLAutoDrawable arg0) {
// System.out.println("dispose");
// eventuellement tuer un thread...
}
/* ******** REPAINT ******** */
// actualise la camera
public void repaint(GL2 gl) {
actualise();
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
// h= ratio de l'�cran
glu.gluPerspective(45.0f, h, 0.001f, 200.0f);
glu.gluLookAt(
eyeX,
eyeY,
eyeZ,
eyeX + Math.cos( ( 0.5 * Math.PI ) - direction ),
eyeY,
eyeZ + Math.sin( ( 0.5 * Math.PI ) - direction ),
upX,
upY,
upZ
);
}
/* ******** AUTRES ******** */
public void actualise() {
float varX = 0;
float varZ = 0;
if ( haut ) {
varZ += Math.cos(direction) /10;
varX += Math.sin(direction) /10;
}
if ( bas) {
varZ -= Math.cos(direction) /10;
varX -= Math.sin(direction) /10;
}
if ( gauche ) {
varX += Math.cos(direction) /20;
varZ -= Math.sin(direction) /20;
}
if ( droite ) {
varX -= Math.cos(direction) /20;
varZ += Math.sin(direction) /20;
}
direction -= souris.getDelta()/1000.0;
// test de collision
int nextPosType = pos[(int)(eyeX+varX)][(int)(eyeZ+varZ)];
if ( nextPosType != 0 ){
eyeX += varX;
eyeZ += varZ;
if ( nextPosType == 3 ){
System.exit(0);
}
}
}
public void jump(){
eyeY++;
}
public void ground(){
eyeY--;
}
public void up(boolean b) {
haut = b;
}
public void down(boolean b) {
bas = b;
}
public void left(boolean b) {
gauche = b;
}
public void right(boolean b) {
droite = b;
}
public void reset(){
eyeX = debutX;
eyeZ = debutZ;
}
}