LWJGL Game Init and Main Loop

ok, got a little further…

here is the out put from my app, I added a number of out.println()'s for my own testing, they all start with a –

Begin Main...
--Begin init...
--build window...
Adapter key: \REGISTRY\Machine\System\ControlSet001\Services\idisw2km\Device0
Adapter key: \REGISTRY\Machine\System\ControlSet001\Services\idisw2km\Device0
Adapter:  Version: null
Window registered
Releasing DC
Destroy window
Releasing DC
Destroy window
--beginning catch for an error...
org.lwjgl.LWJGLException: Could not choose ARB pixel formats.
        at org.lwjgl.opengl.Window.nCreate(Native Method)
        at org.lwjgl.opengl.Window.createWindow(Unknown Source)
        at org.lwjgl.opengl.Window.create(Unknown Source)
        at org.lwjgl.opengl.Window.create(Unknown Source)
        at org.lwjgl.examples.Game.init(Game.java:96)
        at org.lwjgl.examples.Game.main(Game.java:73)
*** Alert ***My Game
An error occured and the game will exit.
--in the finally clause...

C:\java\simple\lwjgl\Test\classes>

Your change helped, now what have I blown up? :slight_smile:

Mine is working now.

The “-Djava.library.path=c:\lwjgl-win32-0.9” seemed to be what I was missing.

[quote] 4. Should you wish to place the dll’s in some other directory than you’re class files (say /native as opposed to /classes) you could do so, by using the java.library.path property. For example

  java -cp classes; -Djava.library.path=native org.lwjgl.test.WindowCreationTest

[/quote]
That’s from the lwjgl page. I guess I should have listened to them, hehe. I didn’t know what they were talking about, though. Why do you have to set Djava.library.path if your classes are in a different directory than the dll’s? I was kindof under the impression that’s what classpath takes care of?

Am I correct when I assume that if there were no dll’s involved… (if I was just using someone else’s pure java classes, from a different package…) then changing the classpath would have been enough?

Also, I didn’t know that you had to set classpath both when you were compiling and executing. So I was doing something like “javac Game.java -classpath .;c:…\lwjgl.jar” but later I would try to run it using java org.lwjgl.examples.Game. (This was the first time I had to change the classpath for anything, so, if nothing else, I’ve learned something about it :slight_smile: )

[quote]Why do you have to set Djava.library.path if your classes are in a different directory than the dll’s? I was kindof under the impression that’s what classpath takes care of?
[/quote]
No, the classpath has to do with the classes loaded by the VM. The library path on the other hand, tells the VM where to locate it’s native libraries.

[quote]Am I correct when I assume that if there were no dll’s involved… (if I was just using someone else’s pure java classes, from a different package…) then changing the classpath would have been enough?
[/quote]
Yes

[quote]Also, I didn’t know that you had to set classpath both when you were compiling and executing. So I was doing something like “javac Game.java -classpath .;c:…\lwjgl.jar” but later I would try to run it using java org.lwjgl.examples.Game. (This was the first time I had to change the classpath for anything, so, if nothing else, I’ve learned something about it :slight_smile: )
[/quote]
Well, the classpath tells the VM where to find classes. When compiling, the compiler also needs to solve any dependencies which is why a classpath often also has to be set.

Hmm, seems to be some OpenGL issues…
What graphics card?
could you try to download OpenGL Extensions Viewer from:
http://www.realtech-vr.com/glview/

then try the OGL tests…

Finish off with choosing File -> Save and email me the generated xml file (info@lwjgl.org)

glview will not run. Missing dll. I am using the base graphics with my system, no additional video card. I was informed one was not required, is it?

Being the clueless newbie that I am (I read all the basics/essentials from the sun’s tutorial and am able to draw some stuff in java2d) I really thank you for putting this up :slight_smile:

I was struggling through some 2d tutorials which introduced me to a lot of methods for getting timers up and getting a gameloop to run at a proper FPS. And finding this was a pleasant surprise. Especially because all the test were working (except the grass, it gives me exceptions even while I have a nvidia card) and I got it into jcreator perfectly. Everything compiles perfectly and is working :slight_smile: So thanks a lot and hopefully I’ll be able to make a game in a month or something like that.

Now let’s grab some opengl book or some tutorials :slight_smile:

EDIT: I got NeHe’s tutorials for java, and I figured a lot of the code just does the window creating which can be done easier since version 0.X. So I copied this code into my render function, but I aint getting any polygons on my screen, no errors either.


      private static void render() { 
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT); 
            GL11.glLoadIdentity();                                          // Reset The Current Modelview Matrix
                  GL11.glTranslatef(-1.5f,0.0f,-6.0f);                        // Move Left 1.5 Units And Into The Screen 6.0
                  GL11.glBegin(GL11.GL_TRIANGLES);                              // Drawing Using Triangles
                        GL11.glVertex3f( 0.0f, 1.0f, 0.0f);                  // Top
                        GL11.glVertex3f(-1.0f,-1.0f, 0.0f);                  // Bottom Left
                        GL11.glVertex3f( 1.0f,-1.0f, 0.0f);                  // Bottom Right
                  GL11.glEnd();                                                            // Finished Drawing The Triangle
                  GL11.glTranslatef(3.0f,0.0f,0.0f);                        // Move Right 3 Units
                  GL11.glBegin(GL11.GL_QUADS);                                    // Draw A Quad
                        GL11.glVertex3f(-1.0f, 1.0f, 0.0f);                  // Top Left
                        GL11.glVertex3f( 1.0f, 1.0f, 0.0f);                  // Top Right
                        GL11.glVertex3f( 1.0f,-1.0f, 0.0f);                  // Bottom Right
                        GL11.glVertex3f(-1.0f,-1.0f, 0.0f);                  // Bottom Left
                  GL11.glEnd();                                                            // Done Drawing The Quad
      } 

NeHe demos usually have an init method which sets a bunch of default gl state. Did you remember to copy that as well…?

EDIT:
sorry for the confusion but everything is working correctly now, I did it all over and I must have made a stupid mistake in the init somewhere

[quote]glview will not run. Missing dll. I am using the base graphics with my system, no additional video card. I was informed one was not required, is it?
[/quote]
Well, you’ll need an OpenGL capable card… it sounds like you don’t have that ?`What card is it, or rather whats the name of the integrated graphics chip/ motherboard?

If you don’t have opengl drivers, you’re out of luck (and you have a very old graphics card/chip!)

Not sure of the chip, Dell Lattitude bought in OCt of 2002. Not sure it this is related, but I do some Java3D work on this box using the java3d install for opengl and it works fine.

I’ll poke around and see if I can determine the motherboard/chipset.

Edit: After running dxdiag, I found its a Radeon Mobility chipset with DirectDraw, Direct3D and AGP Texturing all accelerated and tested fine. doh! I still must be screwing something up.

VSync is based on the desktop’s refresh rate and that can range from 60 to 85 (normally). So when VSync isn’t enabled you have to use the hardcoded frame rate (60) right?

so huh won’t the game run faster on a 85Hz vsynced computer as opposed to a 60Hz app controlled computer? Will there be a big difference?

[quote]So when VSync isn’t enabled you have to use the hardcoded frame rate (60) right?
[/quote]
Right.

[quote]so huh won’t the game run faster on a 85Hz vsynced computer as opposed to a 60Hz app controlled computer? Will there be a big difference?
[/quote]
It depends on how you calculate the movement of your game objects. If you update all movement time based instead of frame based, the game will be equally fast on all refresh rates.
So think for movement speed in ‘units per second’ rather than ‘units per frame’ (units being pixels, meters, miles or whatever) and all will be well.

I have updated my video drivers and according to the ATI site, my Mobility Radeon is OpenGL ready for 2d and 3d accelerated graphics. I am at a loss. :frowning:

[quote]Out of curiosity, (when looking at Cas’ code) is it better to use a different thread for the logic() function and only call that thread at fixed amount of time? (time proc?)
[/quote]
No!

[quote]I mean, the thread sleeps for a given interval of time and only executes the logic every now and then (25ms or whatever is the standard)
Is that the way to do time based logic, thus isolating the rendering and the logic part of the game?
[/quote]
Your rendering will use the data updated in the logic right? The 2 threads can’t run at the same time or you’ll get synchronization bugs. So you get absolutely nothing from using 2 threads except some really complex logic that synchronizes the threads.

Btw. You can call the logic() function more than once per game loop if it the logic is lagging behind.

[s]ok, so how do I force time based logic?

create an application tick (i.e. something time based that is constantly updated, kinda like the frame rate I suppose) and if that application tick is greater than our update rate, we update our logic? is that how it should be implemented?

Anyways, thanks for the eye opener, I’ll see how I can turn my game into a time based one :D[/s]

edit: NM, just went through parts of Kevin Glass’ tutorial and it’s pretty straightforward :D, 2 thumbs up for Kevin

[quote]ok, so how do I force time based logic?
[/quote]
Time based logic would call .logic and .render once per loop, but the logic method would caculate the time elapsed since the last frame. Then all your game updates are done based on that elapsed time. Thats usually pretty easy and often just requires scaling your speed/velocity type calculations based on that frames delta time.

However it can make collision a real pain, since you can get varying steps. You often need to check the entire path an object moved though rather than just the end point (otherwise you ‘leapfrog’ over an object).

Time based is great for ‘proper’ 3d worlds where you can get something that runs smoother on faster machines. However for a 2d game you’ll get a much smoother scrolling with a fixed framerate rather than a variable one.

Hey y’all - the timing code has been revised in the CVS of LWJGL! Now in 0.91 (ahem) we’ve got the most super-groovy method in the universe to take care of all your frame-capped woes:


Display.sync(FPS);

Yes! It does exactly what you think it’s gonna do! Now you can just sit back and let our system-friendly bit of code take care of capping framerates while you get on and make the tea.

Cas :slight_smile:

ohh, this makes me think of a question to add to the wiki which I’m gonna do right now

that is, how to get the latest working binaries from CVS (if it’s even possible, if not, how to compile them)

so uh yah goes on the wiki

Thanks again!

Now if I can only finish my personnal SCM perl script
(it’s gonna do a bunch of fun things, all based on a config file, like huh backup the latest version of a project, package and sign the jars, copy the source to a showsrc directory and what not) :slight_smile:

Hm… just took a look and experimented a bit. As I expected (and as I already pointed out in the other thread) using the same cap rate as hz will lead to frame skips if vsync is actually enabled. Aiming for some fps more helps (eg +3).

I also checked the code and I think my version is a bit neater (shorter and no casts) :slight_smile:


public static void sync(long fps)
{
      long gapTo = Sys.getTimerResolution() / fps + timeThen;
      timeNow = Sys.getTime();
      while(gapTo>timeNow)
      {
            Thread.yield();
            timeNow = Sys.getTime();
      }
      timeThen=timeNow;
}

Smartass :stuck_out_tongue: I will change to CVS to reflect your nicer version.

I don’t get frame skips with vsync enabled, but I can see that it might happen. +3 is probably overkill, +1 ought to do the trick.

Cas :slight_smile: