// @author Fernando Hideki Mohara // UNICAMP-FT - Faculdade de Tecnologia // RA 091183 // Project 1 - Maze package f091183.mohara.jogl.maze; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.media.opengl.GL2; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLEventListener; import javax.media.opengl.awt.ComponentEvents; //import javax.media.opengl.glu.GLU; class Renderer implements GLEventListener, KeyListener { static final public char[] maze = {'w','w','w','w','w', 'w','w','w','w','w', 'w','w','w','w','w', 'w','w','w','w','w', 'w',' ',' ',' ',' ', ' ',' ',' ',' ',' ', ' ',' ',' ',' ','w', ' ',' ',' ',' ','S', 'w',' ','w','w','w', 'w','w','w','w',' ', 'w',' ','w',' ',' ', ' ','w',' ','w','w', 'w',' ',' ','w',' ', ' ',' ',' ','w',' ', ' ',' ','w',' ','w', ' ','w','w','w','w', 'w',' ','w','w','w', 'w',' ','w','w','w', 'w','w','w','w','w', 'w','w',' ',' ','w', /*ln 5*/ 'w',' ',' ',' ',' ', ' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ', ' ','w','w',' ','w', 'w','w','w','w','w', 'w','w','w','w','w', 'w','w','w','w','w', ' ','w',' ',' ','w', 'w',' ',' ',' ',' ', ' ',' ',' ',' ',' ', ' ',' ',' ',' ','w', ' ','w','w',' ','w', 'w',' ','w',' ','w', 'w','w','w','w','w', 'w','w','w',' ',' ', ' ',' ','w',' ','w', 'w',' ','w',' ',' ', ' ',' ',' ',' ',' ', ' ',' ','w','w','w', 'w','w','w',' ','w', /*ln 10*/ 'w',' ','w',' ','w', 'w','w','w','w','w', 'w',' ','w',' ',' ', ' ',' ',' ',' ','w', 'w',' ','w','w','w', ' ',' ',' ',' ',' ', 'w',' ','w',' ','w', 'w','w','w','w','w', 'w',' ',' ',' ',' ', ' ','w','w','w',' ', 'w',' ','w',' ',' ', ' ',' ',' ',' ','w', 'w','w','w','w','w', 'w','w',' ','w',' ', 'w',' ','w','w','w', ' ','w','w',' ','w', 'w',' ',' ',' ',' ', ' ',' ',' ','w',' ', 'w',' ','w',' ',' ', ' ','w',' ',' ','w', /*ln 15*/ 'w',' ','w','w',' ', 'w','w','w','w',' ', 'w',' ','w',' ','w', 'w','w','w','w','w', 'w',' ',' ','w',' ', 'w',' ',' ',' ',' ', 'w',' ','w',' ',' ', ' ','w',' ',' ','w', 'w','w',' ','w',' ', 'w','w','w','w',' ', 'w',' ','w','w','w', ' ','w','w',' ','w', 'w',' ',' ','w',' ', ' ',' ',' ',' ',' ', 'w',' ',' ',' ',' ', ' ',' ',' ',' ','w', 'w','E','w','w','w', 'w','w','w','w','w', 'w','w','w','w','w', 'w','w','w','w','w', }; // y 5 y 10 y 15 public static MoveControl player = new MoveControl(); // Canvas initialization public void init(GLAutoDrawable gLDrawable) { System.out.println("init() called"); GL2 gl = gLDrawable.getGL().getGL2(); gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); } // Canvas action public void display(GLAutoDrawable gLDrawable) { update(gLDrawable); render(gLDrawable); } // Reads the changes in the screen private void update(GLAutoDrawable gLDrawable) { double move = 0.00833333333; int mazeXY=0; /* player.setPlayerY(Math.round( (player.getPlayerY()-move) * 10000.0 ) / 10000.0); */ System.out.println("pY: " + player.getPlayerY()); // Position of player in maze. Checking for wall after >>move Y-<< for(double counterX=0.9; counterX >=-1; counterX=counterX-0.1) { for(double counterY=0.9; counterY >=-1; counterY=counterY-0.1, mazeXY++) { double XP = Math.round(player.getPlayerX()* 100.0 ) / 100.0; double XC = Math.round(counterX*100.0) / 100.0; double YP = Math.round(player.getPlayerY()* 100.0 ) / 100.0; double YC = Math.round(counterY*100.0) / 100.0; if( (XP == XC) && (YP == YC)) { if(maze[mazeXY+20]== 'w') { player.setPlayerY(Math.round( (player.getPlayerY()+move) * 10000.0 ) / 10000.0); } } } } } // Rends the canvas private void render(GLAutoDrawable gLDrawable) { GL2 gl = gLDrawable.getGL().getGL2(); int mazeXY=0; // CLear screen gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); // Draw the maze with walls for(double counterX=0.9; counterX >=-1; counterX=counterX-0.1) { for(double counterY=0.9; counterY >=-1; counterY=counterY-0.1, mazeXY++) { // Check if position is a wall if(maze[mazeXY]=='w') { //The wall gl.glTranslated(0.05, 0.05, 0); gl.glBegin(GL2.GL_QUADS); gl.glColor3f(1, 0, 0); gl.glVertex2d(counterX-0.05, counterY-0.05); // [ - ; - ] gl.glColor3f(0, 0, 1); gl.glVertex2d(counterX+0.05, counterY-0.05); // [ + ; - ] gl.glColor3f(1, 1, 0); gl.glVertex2d(counterX+0.05, counterY+0.05); // [ + ; + ] gl.glColor3f(0, 1, 0); gl.glVertex2d(counterX-0.05, counterY+0.05); // [ - ; + ] } } } //The wall gl.glTranslated(0.05, 0.05, 0); gl.glBegin(GL2.GL_QUADS); gl.glColor3f(1, 1, 1); gl.glVertex2d(player.getPlayerX()-0.033, player.getPlayerY()-0.033); // [ - ; - ] gl.glVertex2d(player.getPlayerX()+0.033, player.getPlayerY()-0.033); // [ + ; - ] gl.glVertex2d(player.getPlayerX()+0.033, player.getPlayerY()+0.033); // [ + ; + ] gl.glVertex2d(player.getPlayerX()-0.033, player.getPlayerY()+0.033); // [ - ; + ] gl.glFlush(); gl.glEnd(); } public void displayChanged(GLAutoDrawable gLDrawable, boolean modeChanged, boolean deviceChanged) {} // Reshape canvas public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height) { /*System.out.println("reshape() called: x = "+x+", y = "+y+", width = "+width+", height = "+height); final GL2 gl = gLDrawable.getGL().getGL2(); if (height <= 0) // avoid a divide by zero error! { height = 1; } final float h = (float) width / (float) height; gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL2.GL_PROJECTION); gl.glLoadIdentity(); gl.glPerspective(45.0f, h, 1.0, 20.0); gl.glMatrixMode(GL2.GL_MODELVIEW); gl.glLoadIdentity();*/ } public void dispose(GLAutoDrawable arg0) {} @Override public void keyTyped(KeyEvent paramKeyEvent) { } @Override public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_S) { double move=0.00833333333; player.setPlayerY(Math.round( (player.getPlayerY()-move) * 10000.0 ) / 10000.0); } } @Override public void keyReleased(KeyEvent paramKeyEvent) { // TODO Auto-generated method stub } }