Does anyone want to share some simple and eficient code for a basic game loop using JOGL ? No more than 2 pages was the ideal for me.
Um thats not exaclty what JOGL is for, as far as I know. You are still responsible for the game loop logic yourself. To get a better idea I suggest you look at Kevin Glass’s tutorials on his site http://www.cokeandcode.com/tutorials. SpaceINvaders 103 is specific about using JOGL with his game.
That’s exactly what i was asking above. The specifics of making a small and effcient game loop in JOGL. I know it’s possible to do something very complex in this area but i am more interested in something small, fast and elegant like tutorial code.
That may do. Thank you.
Here is a efficient game loop using jogl api (my preferred way to use it).
You can control exactly each render frame with this code.
You need to create a loop that call the update method of the BasicGLFrame to render each GL frame…
You need to call close() method to close this view and release GL context…
It works great!
package fr.gjini.basic.view;
import java.awt.Dimension;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLContext;
import javax.media.opengl.GLDrawable;
import javax.media.opengl.GLDrawableFactory;
import javax.swing.JFrame;
/**
-
@author Augustin Gjini
*/
public class BasicGLFrame extends JFrame {/**
*
*/
public BasicGLFrame() {
this.setSize(DEFAULT_SIZE);
GLCapabilities glCapabilities = new GLCapabilities();
this.glDrawable = GLDrawableFactory.getFactory().getGLDrawable(this, glCapabilities, null);
this.glContext = this.glDrawable.createContext(null);
this.setVisible(true);
this.glContext.makeCurrent();
}/**
-
@see fr.gjini.mvcgame.generic.commons.Updatable#update(long)
/
public void update(long elapsedTime) {
/ Do some GL stuff…
GL gl = this.glContext.getGL();
// init
gl.glViewport(0, 0, this.glDrawable.getWidth(), this.glDrawable.getHeight());
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluOrtho2D(0, GL_WIDTH, 0, GL_HEIGHT);
// draw
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
…
*/
this.glDrawable.swapBuffers();
}
/**
-
@see java.awt.Frame#addNotify()
*/
@Override
public void addNotify() {
super.addNotify();
this.glDrawable.setRealized(true);
}
/**
-
@see java.awt.Frame#removeNotify()
*/
@Override
public void removeNotify() {
this.glDrawable.setRealized(false);
super.removeNotify();
}
/**
-
@see fr.gjini.mvcgame.generic.view.View#close()
*/
public void close() {
this.glContext.release();
this.glContext.destroy();
this.glDrawable.setRealized(false);
}
private static final Dimension DEFAULT_SIZE = new Dimension(350, 275);
private GLDrawable glDrawable;
private GLContext glContext;
-
@see fr.gjini.mvcgame.generic.commons.Updatable#update(long)
}
Hi gust. I haven’t tested the code yet because this is the latest api but it looks exactly what i was looking for. Thanks.
About the timing stuff i was thinking of something very simple like this for the main method. Everything should fit well in less than two pages of code. Any sugestion ?
...
void sleep(long nanos) {
try { Thread.sleep(nanos/1000000, nanos%1000000); }
catch (Exception e) {};
}
public static void main(String[] args) {
BasicGLFrame f = new BasicGLFrame();
long delta=0;
long time=System.nanoTime();
boolean done=false;
while (!done) {
// save time frame
time = System.nanoTime();
f.update();
... other stuff...
delta = System.nanoTime() - time;
sleep(NANOS_PER_FRAME-delta);
}
f.close();
}
...
I had no idea you could use an arbitrary (non-Canvas) heavyweight with the GLDrawableFactory.getGLDrawable() API – very cool that you’ve found an unintended way to use the new APIs.
I’m not sure of it, but it could mean we can now have cheap HUDs :
- use an awt.Panel as a drawable
- and layout transparent awt labels into it …
I don’t know if these labels will really be transparent, but it’s worth trying
Lilian
Unintended ?
Perhaps but It makes all sense. Why use an extra useless gui component when we only need the window to draw in. I hope you guys don’t think of removing this stuff because it’s very usefull.
We’ve only internally tested using Canvases with GLDrawableFactory.getGLDrawable(). It’s good to know that it works with arbitrary heavyweights.
Agreed. Don’t worry, we won’t add more strict checks to the implementation.
It might be nice to make things a little clearer in the docs. The JAWT docs are not very good about what “works” when you want to get a native surface (e.g. On windows an HWND). It seems the only documented thing is the Cavas, but it seems to work for any heavyweight. I would like to see something official from Sun that says what will work, so we can be sure that it always must work.
Do you mean the JOGL / JSR-231 docs or the JAWT docs? We could try to clear things up more in the JSR-231 spec but I’d be a little concerned about over-specifying the behavior. You should feel free to file an RFE on the JAWT via the bug database about this; category J2SE, subcategory classes_awt.
I meant JAWT more than JOGL… I assume that JOGL is using JAWT to make the “connection” between OpenGL and the native surface. I had to use JAWT to efficiently render live video within my Java UI and came across the incomplete documentation for JAWT.
Yes, JOGL uses the JAWT to interact with the AWT. I’d suggest you file the RFE. The docs could certainly use some work.
Turns out the request has laredy been made:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4922198
Give it a vote if you have one to spare. Or just add some comments. (Do Sun dudes ever look at the comments?)
We do look at the comments although admittedly they don’t show up in the default tool we use internally for bug tracking. We need to use another tool. However, the responsible engineer gets email every time a comment is added by an end user. FYI, please leave a link with your email address in the future, since without one there is no way for us to follow up to get more information (this is more relevant for bug reports, not RFEs), and also you seem to have posted the same comment four times although I’m sure you’re aware of that.
Actually I wasn’t aware that ANY comment was posted. It timed out every time I tried. And I specificly DID choose to include a link with my email, every time. It seemed to time-out when I entered my login information. The bug databse was severly hosed last night I guess.
It’s broken still today. If you don’t login BEFORE you put in the comment, it asks you for your login info after you submit it. That login screen always times out, yet the comment was included without the link.
The instructions on the timeout screen say "Please use the back button on your browser and resubmit. " which is what I did to get multiple posts
It sounds like somebody needs to file a bug about the bug database. I’ll ask around about this after the holidays.