After some trying here my version, contains parts of the tutors #4 and #34 from gametutorials.com to show something and to get the fps…
import java.awt.*;
import java.awt.event.*;
import gl4java.awt.*;
import gl4java.utils.glut.*;
import gl4java.utils.glut.fonts.*;
public class Test01 extends Frame implements Runnable
{
renderCanvas canvas = null;
GraphicsDevice myDevice;
DisplayMode oldMode, newMode;
boolean fullscreen = false;
int ScreenWidth = 800, ScreenHeight = 600, BitDepth = 32;
public static void main( String args[] )
{
new Test01();
}
public Test01()
{
super("Lalala");
addWindowListener( new FensterAdapter() );
setSize(ScreenWidth, ScreenHeight);
setResizable(false);
myDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
oldMode = myDevice.getDisplayMode();
newMode = new DisplayMode(ScreenWidth, ScreenHeight, BitDepth, oldMode.getRefreshRate());
start();
}
public void start()
{
Thread th = new Thread (this);
th.start ();
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
canvas = new renderCanvas(getSize().width, getSize().height);
add("Center", canvas);
if (fullscreen) {
if(myDevice.isFullScreenSupported()) {
try {
this.setUndecorated(true);
myDevice.setFullScreenWindow(this);
myDevice.setDisplayMode(newMode);
}
catch (Exception e) {
myDevice.setDisplayMode(oldMode);
}
}
else {
System.out.print("Vollbildmodus wird nicht unterstützt");
System.exit(0);
}
}
else {
setVisible(true);
}
}
public void run()
{
while (true) {
repaint();
try { Thread.sleep(0); }
catch (InterruptedException ex) { }
}
}
}
class renderCanvas extends GLAnimCanvas implements KeyListener
{
boolean[] keys=new boolean[256]; // holds information on which keys are held down.
static float framesPerSecond = 0.0f; // This will store our fps
static long lastTime = 0; // This will hold the time from the last frame
static String strFrameRate; // We will store the string here for the window title
static float rotY = 0; // This is used for the rotation degree
int FONT_HEIGHT = 24;
protected GLUTFunc glut = null;
public renderCanvas(int w, int h)
{
super(w,h);
addKeyListener(this);
glut = new GLUTFuncLightImplWithFonts(gl, glu);
}
void PositionText( int x, int y )
{
gl.glPushAttrib( GL_TRANSFORM_BIT | GL_VIEWPORT_BIT );
gl.glMatrixMode( GL_PROJECTION ); // Set our matrix to our projection matrix
gl.glPushMatrix(); // Push on a new matrix to work with
gl.glLoadIdentity(); // reset the matrix
gl.glMatrixMode( GL_MODELVIEW ); // Set our matrix to our model view matrix
gl.glPushMatrix(); // Push on a new matrix to work with
gl.glLoadIdentity(); // Reset that matrix
y = this.getHeight() - FONT_HEIGHT - y; // Calculate the weird screen position
gl.glViewport( x - 1, y - 1, 0, 0 ); // Create a new viewport to draw into
gl.glRasterPos4f( 0, 0, 0, 1 ); // Set the drawing position
gl.glPopMatrix(); // Pop the current modelview matrix off the stack
gl.glMatrixMode( GL_PROJECTION ); // Go back into projection mode
gl.glPopMatrix(); // Pop the projection matrix off the stack
gl.glPopAttrib(); // This restores our TRANSFORM and VIEWPORT attributes
}
void glDrawText(int x, int y, String strString)
{
PositionText(x, y);
glut.glutBitmapString(glut.GLUT_BITMAP_TIMES_ROMAN_24,strString);
}
void CalculateFrameRate()
{
long currentTime = System.currentTimeMillis()/1000;
++framesPerSecond;
if( currentTime - lastTime > 1.0f )
{
lastTime = currentTime;
strFrameRate = "FPS: "+ framesPerSecond;
framesPerSecond = 0;
}
gl.glColor3f(0f, 1f, 0f);
glDrawText(0, 0, strFrameRate);
}
public void preInit()
{
setUseFpsSleep(false); //We want to run this animation at full speed. :)
}
public void init()
{
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //This will clear the background color to Black
gl.glEnable(GL_DEPTH_TEST); //Enables Depth Testing
reshape(getSize().width, getSize().height); //Reshape the drawing canvas
start(); //Start the animation
}
public void doCleanup()
{
stop(); //Stop the animation
}
public void reshape(int width, int height)
{
gl.glViewport(0,0,width,height); // Make our viewport the whole window
gl.glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
gl.glLoadIdentity(); // Reset The Projection Matrix
glu.gluPerspective(45.0f,(float)width/(float)height, 1 ,150.0f);
gl.glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
gl.glLoadIdentity(); // Reset The Modelview Matrix
}
public void display()
{
//Ensure GL is initialized correctly
if (glj.gljMakeCurrent() == false) return;
gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
gl.glLoadIdentity(); // Reset The matrix
// Position View Up Vector
glu.gluLookAt(0, 0, 6, 0, 0, 0, 0, 1, 0); // This determines where the camera's position and view is
gl.glRotatef(rotY, 0, 1, 0);
rotY += 2;
gl.glBegin (GL_TRIANGLES); // This is our BEGIN to draw
gl.glColor3f(1, 0, 0); // Make the top vertex RED
gl.glVertex3f(0, 1, 0); // Here is the top point of the triangle
gl.glColor3f(0, 1, 0); // Make the left vertex GREEN
gl.glVertex3f(-1, 0, 0); // Here is the left point of the triangle
gl.glColor3f(0, 0, 1); // Make the right vertex BLUE
gl.glVertex3f(1, 0, 0); // Here is the right point of the triangle
gl.glEnd(); // This is the END of drawing
CalculateFrameRate();
ProcessKeyboard(); // Process Keyboard Results
//Swap buffers, check for errors, and release the drawing context
glj.gljSwap();
glj.gljCheckGL();
glj.gljFree();
}
// Process Keyboard Results
void ProcessKeyboard()
{
if(keys[KeyEvent.VK_ESCAPE]) {
keys[KeyEvent.VK_ESCAPE] = false;
doCleanup();
System.exit(0);
}
}
// These Methods Override the default implementation of MouseListener in GLAnimCanvas
public void mouseEntered( MouseEvent evt )
{
Component comp = evt.getComponent();
if( comp.equals(this ) )
{
requestFocus();
}
}
public void mouseClicked( MouseEvent evt )
{
Component comp = evt.getComponent();
if( comp.equals(this ) )
{
requestFocus();
}
}
// Invoked when a key has been typed. This event occurs when a key press is followed by a key release.
public void keyTyped(KeyEvent e)
{
}
// Invoked when a key has been pressed.
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()<250) // only interested in first 250 key codes
keys[e.getKeyCode()]=true;
}
// Invoked when a key has been released.
public void keyReleased(KeyEvent e)
{
if(e.getKeyCode()<250) // only interested in first 250 key codes
keys[e.getKeyCode()]=false;
}
}
class FensterAdapter extends WindowAdapter
{
public void windowClosing ( WindowEvent e)
{
System.exit(0);
}
}
The fps on my xp1700+ with gf4 ti4200 are horrible ~180 @80060032 windowed and ~200 in fullscreen.
(Q3 timedemo gives me more then 300fps^^)
Is java so slow? are there any big mistakes that slows everthing down?