Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Basic Game Loop in JOGL  (Read 3798 times)
0 Members and 1 Guest are viewing this topic.
Offline zingbat

Senior Member




Java games rock!


« Posted 2005-12-20 19:10:28 »

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.
Offline Kommi

Junior Member




All opinions will be lined up and shot!


« Reply #1 - Posted 2005-12-20 20:30:52 »

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.

Kommi
Offline zingbat

Senior Member




Java games rock!


« Reply #2 - Posted 2005-12-20 22:55:32 »

You are still responsible for the game loop logic yourself.

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.

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 may do. Thank you.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline gust

Senior Newbie




Vive le Java !


« Reply #3 - Posted 2005-12-21 00:54:53 »


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;
   
}
Offline zingbat

Senior Member




Java games rock!


« Reply #4 - Posted 2005-12-21 02:13:51 »

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 ?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
...

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();
}

...

Offline Ken Russell

JGO Coder




Java games rock!


« Reply #5 - Posted 2005-12-21 08:34:17 »

Here is a efficient game loop using jogl api (my preferred way to use it).

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.
Offline c_lilian

Senior Member


Projects: 1


Java games will probably rock someday...


« Reply #6 - Posted 2005-12-21 10:35:19 »

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

Offline zingbat

Senior Member




Java games rock!


« Reply #7 - Posted 2005-12-21 15:24:00 »

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.

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.
Offline Ken Russell

JGO Coder




Java games rock!


« Reply #8 - Posted 2005-12-21 19:57:27 »

Unintended ?

We've only internally tested using Canvases with GLDrawableFactory.getGLDrawable(). It's good to know that it works with arbitrary heavyweights.

Quote
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.

Agreed. Don't worry, we won't add more strict checks to the implementation.
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #9 - Posted 2005-12-22 04:15:05 »

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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ken Russell

JGO Coder




Java games rock!


« Reply #10 - Posted 2005-12-22 07:19:59 »

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.
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #11 - Posted 2005-12-23 05:26:18 »

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.

Offline Ken Russell

JGO Coder




Java games rock!


« Reply #12 - Posted 2005-12-23 05:41:16 »

Yes, JOGL uses the JAWT to interact with the AWT. I'd suggest you file the RFE. The docs could certainly use some work.
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #13 - Posted 2005-12-24 01:26:08 »

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?)

Offline Ken Russell

JGO Coder




Java games rock!


« Reply #14 - Posted 2005-12-24 02:43:25 »

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.
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #15 - Posted 2005-12-24 19:03:09 »

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.

Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #16 - Posted 2005-12-24 19:06:20 »

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 Smiley

Offline Ken Russell

JGO Coder




Java games rock!


« Reply #17 - Posted 2005-12-25 23:19:55 »

It sounds like somebody needs to file a bug about the bug database. I'll ask around about this after the holidays.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (24 views)
2014-09-19 03:14:18

Dwinin (39 views)
2014-09-12 09:08:26

Norakomi (68 views)
2014-09-10 13:57:51

TehJavaDev (93 views)
2014-09-10 06:39:09

Tekkerue (47 views)
2014-09-09 02:24:56

mitcheeb (68 views)
2014-09-08 06:06:29

BurntPizza (51 views)
2014-09-07 01:13:42

Longarmx (38 views)
2014-09-07 01:12:14

Longarmx (44 views)
2014-09-07 01:11:22

Longarmx (40 views)
2014-09-07 01:10:19
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!