mport net.java.games.jogl.util.GLUT;
import display.DisplayOptions;
import net.java.games.jogl.*;
import java.awt.event.*;
import sun.misc.Perf;
import javax.swing.*;
import java.awt.*;
public class JoGLFrame implements KeyListener
{
private boolean fullScreen = false,
keys[] = new boolean[KeyEvent.KEY_LAST];
private Renderer renderer = null;
private Animator animationLoop = null;
private GLCanvas canvasGL = null;
private boolean updated = false;
private JFrame parentFrame = new JFrame();
private float timeSinceLastUpdate = .0f,
timePrecision = .02f,
frameInterval = 0;
private Perf timer = sun.misc.Perf.getPerf();
private long updateFrequency = timer.highResFrequency()>>1,
currentTime = 0,
frameTime = timer.highResCounter(),
lastTime = timer.highResCounter();
private int fps = 0;
public static void main(String []args) {
JoGLFrame demo = new JoGLFrame();
}
JoGLFrame(){
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception a){}
int parameters[] = new int[7];
//screenWidth = parameters[0];
//screenHeight = parameters[1];
//canvasWidth = parameters[2];
//canvasHeight = parameters[3];
//refreshRate = parameters[4];
//bitDepth = parameters[5];
//fullScreen = parameters[6]== 1;
DisplayOptions.showSettingsSelector("Select Screen Mode",
DisplayOptions.USE_JOGL3D_ENGINE,
parameters);
canvasGL = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
canvasGL.setSize(new Dimension(parameters[2], parameters[1]));
canvasGL.addGLEventListener((renderer = new Renderer()));
canvasGL.addKeyListener(this);
parentFrame.getContentPane().add(canvasGL,BorderLayout.CENTER);
parentFrame.addKeyListener(this);
parentFrame.requestFocus();
if(fullScreen = (parameters[6] == 1)){
parentFrame.setUndecorated(true);
GraphicsEnvironment.getLocalGraphicsEnvironment().
getDefaultScreenDevice().setFullScreenWindow(parentFrame);
GraphicsEnvironment.getLocalGraphicsEnvironment().
getDefaultScreenDevice().setDisplayMode((new DisplayMode(parameters[2], parameters[3],
parameters[5], parameters[4])));
}
else{
int xLocation = (parameters[0] - parameters[2])/2,
yLocation = (parameters[1] - parameters[3])/2;
xLocation = (xLocation < 0)? 0: xLocation;
yLocation = (yLocation < 0)? 0: yLocation;
parentFrame.setSize(parameters[2],parameters[3]);
parentFrame.setLocation(xLocation, yLocation );
parentFrame.addWindowListener(new shutDownWindow());
}
parentFrame.setVisible(true);
}
private class Renderer
implements GLEventListener
{
public void init(GLDrawable drawable){
animationLoop = new Animator(drawable);
animationLoop.start();
}
public void display(GLDrawable drawable){
currentTime = timer.highResCounter();
fps++;
if(currentTime - lastTime >=updateFrequency){
if(!fullScreen)
parentFrame.setTitle("Basic JoGL Frame: FPS " + String.valueOf(fps<<1) );
lastTime = currentTime;
fps = 0;
}
frameInterval = ((float)(currentTime - frameTime))/updateFrequency;
frameTime = currentTime;
updated = false;
timeSinceLastUpdate += frameInterval;
while(timeSinceLastUpdate>timePrecision){
timeSinceLastUpdate -= timePrecision;
updated = true;
}
if(updated){
//do some game logic stuff here
}
GL gl = drawable.getGL();
GLU glu = drawable.getGLU();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT );
processKeyboard();
}
public void reshape(GLDrawable drawable,
int xstart,int ystart,
int width, int height){
height = (height == 0) ? 1 : height;
}
public void displayChanged(GLDrawable drawable,
boolean modeChanged,
boolean deviceChanged){}
}
public void processKeyboard(){
//if(keys[KeyEvent.VK_LEFT])
}
public void keyReleased(KeyEvent evt){
keys[evt.getKeyCode()] = false;
}
public void keyPressed (KeyEvent evt){
keys[evt.getKeyCode()] = true;
if(keys[KeyEvent.VK_ESCAPE])
killApplication();
}
public void keyTyped (KeyEvent evt){}
public void killApplication(){
animationLoop.stop();
System.exit(0);
}
public class shutDownWindow extends WindowAdapter {
public void windowClosing(WindowEvent e) {
killApplication();
}
}
}