package main; import game.GameController; import java.awt.Point; import java.awt.geom.Point2D; import org.lwjgl.LWJGLException; import org.lwjgl.opengl.AWTGLCanvas; import org.lwjgl.opengl.GL11; /*** * @author Krunoslav Saho * 12/06/2005 * GameLoop.java */ public class GameLoop extends AWTGLCanvas { private int width, height; private boolean canExit, vsync; private GameController game; private Point2D.Float clickCoords; private boolean init; @Override protected void paintGL() { if(!init) { init(); init = true; } else { display(); } } public GameLoop(int width, int height, boolean vsync) throws LWJGLException { super(); game = new GameController(); clickCoords = new Point2D.Float(-1, -1); this.width = width; this.height = height; this.vsync = vsync; this.canExit = false; // this.setVSyncEnabled(vsync); } private void init() { GL11.glDisable(GL11.GL_DEPTH_BUFFER_BIT); GL11.glClearColor(.3f, .4f, .5f, 1f); GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY); GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); GL11.glOrtho(0, width, height, 0, -1, 1); GL11.glLoadIdentity(); GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glLoadIdentity(); } private void display() { GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); GL11.glLoadIdentity(); if(this.clickCoords.x != -1 && this.clickCoords.y != -1) convertScreenCoordsToWorld(); game.run(); canExit = game.isDone(); this.clickCoords.x = -1; this.clickCoords.y = -1; } private void convertScreenCoordsToWorld() { double[] modelView = new double[16]; double[] projection = new double[16]; int[] view = new int[4]; double[] pos = new double[] {this.clickCoords.x, this.clickCoords.y, 0}; //FIXME repair unproject // GL11.glGetDoublev(GL11.GL_MODELVIEW_MATRIX, modelView); // GL11.glGetDoublev(GL11.GL_PROJECTION_MATRIX, projection); // GL11.glGetIntegerv(GL11.GL_VIEWPORT, view); // // glu.gluUnProject // ( // this.clickCoords.x, view[3] - this.clickCoords.y, 0, // modelView, // projection, // view, // pos // ); this.clickCoords.x = (float)pos[0]; this.clickCoords.y = (float)pos[1]; game.setClickCoords(this.clickCoords); } public boolean canExit() { return this.canExit; } public void setClickCoords(Point clickCoords) { this.clickCoords.x = clickCoords.x; this.clickCoords.y = clickCoords.y; } }