princec
|
 |
«
Posted
2004-04-16 10:47:22 » |
|
With the advent of LWJGL 0.9 marking a freeze in the API I thought there might be some clubies who'd like to see how simple a LWJGL game can look. So here is a complete Game class. Fill in the blanks with your game  (This code is checked into LWJGL CVS too) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
package org.lwjgl.examples;
import org.lwjgl.Sys; import org.lwjgl.input.Keyboard; import org.lwjgl.openal.AL; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.Window;
public class Game { public static final String GAME_TITLE = "My Game"; private static final int FRAMERATE = 60; private static boolean finished; private Game() {} public static void main(String[] args) { try { init(); run(); } catch (Exception e) { e.printStackTrace(System.err); Sys.alert(GAME_TITLE, "An error occured and the game will exit."); } finally { cleanup(); } } private static void init() throws Exception { Window.create(GAME_TITLE); Window.setVSyncEnabled(true); AL.create(); } private static void run() { while (!finished) { Window.update(); if (Window.isCloseRequested()) { finished = true; } else if (Window.isActive()) { logic(); render(); org.lwjgl.Display.sync(FRAMERATE); } else { try { Thread.sleep(100); } catch (InterruptedException e) { } logic(); if (Window.isVisible() || Window.isDirty()) { render(); } } } } private static void cleanup() { AL.destroy(); Window.destroy(); } private static void logic() { if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { finished = true; } } private static void render() { GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT); }
}
|
<edit> fixed a bug in cleanup <edit2> New timing code! Cas 
|
|
|
|
cfmdobbie
|
 |
«
Reply #1 - Posted
2004-04-16 15:30:42 » |
|
1 2 3
| Window.create(GAME_TITLE) ; |
Sweet!  It's in things like that where LWJGL really shines.
|
Hellomynameis Charlie Dobbie.
|
|
|
MickeyB
|
 |
«
Reply #2 - Posted
2004-04-16 18:22:11 » |
|
as dobbie said....sweet! Been wanting to play with this for while....that being said, let me fall into my semi-permanent roll as a clubie! I found a statement that went something like this: must have good graphics card to use lwjgl. I don;t on my dev box and I am getting the follwoing error running any of the openGL demos: org.lwjgl.LWJGLException: Could not choose ARB pixel formats. I posted to this forum because I am getting a error: 1 2 3 4
| Exception in thread "main" java.lang.NoClassDefFoundError at org.lwjgl.openal.AL.<clinit>(Unknown Source) at org.lwjgl.examples.Game.cleanup(Game.java:147) at org.lwjgl.examples.Game.main(Game.java:77) |
for me line 147 is:
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Matzon
|
 |
«
Reply #3 - Posted
2004-04-16 19:16:32 » |
|
I found a statement that went something like this: must have good graphics card to use lwjgl. Hmm, no - but it does make sense to have an OGL capable card  - that said, some use mesa - and you have to specify allowSoftwareOpenGL to enable software gl mode. I don;t on my dev box and I am getting the follwoing error running any of the openGL demos: org.lwjgl.LWJGLException: Could not choose ARB pixel formats.
Hmm, strange - do normal OpenGL apps work at all? - it's a pretty fatal error. I posted to this forum because I am getting a error: 1 2 3 4
| Exception in thread "main" java.lang.NoClassDefFoundError at org.lwjgl.openal.AL.<clinit>(Unknown Source) at org.lwjgl.examples.Game.cleanup(Game.java:147) at org.lwjgl.examples.Game.main(Game.java:77) |
for me line 147 is: That error is a bit strange: 1 - the constructor has never been called in the init, which is why it's just now being called in the destroy 2 - It can't find the AL class - sure you have lwjgl.jar in the classpath ??
|
|
|
|
MickeyB
|
 |
«
Reply #4 - Posted
2004-04-16 19:45:46 » |
|
Hmm, strange - do normal OpenGL apps work at all? - it's a pretty fatal error. Well Java3D for OpenGL works fine. Classpath...it compiles fine, just does not run. i wil double check classpath for run command in JCreator.
|
|
|
|
K.I.L.E.R
Senior Member   
Java games rock!
|
 |
«
Reply #5 - Posted
2004-04-17 11:07:25 » |
|
Here is a texture loader: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| import org.lwjgl.*; import org.lwjgl.opengl.*; import org.lwjgl.opengl.glu.*;
import javax.swing.*; import java.awt.image.*; import java.io.*; import java.awt.*; import java.nio.*;
public class Main {
private void programExec() { buff = BufferUtils.createIntBuffer(MAX);
buff.put(getIntContents()).flip();
GL11.glGenTextures(buff); }
private int[] getIntContents() { int[] c = new int[MAX]; int i=0;
try { c[i] = fr.read();
while(c[i] != -1) { i++; c[i] = fr.read(); } }catch (Exception e) { JOptionPane.showMessageDialog(null, e.getMessage()); e.printStackTrace(); }
return c; }
public Main() { try { fr = new FileReader("c:\\texture.PNG"); } catch (Exception e) { JOptionPane.showMessageDialog(null, e.getMessage()); e.printStackTrace(); } }
public static void main(String[] args) { Main main = new Main();
main.programExec(); }
BufferedImage bufImg; Image img; ImageIcon imgIc; FileReader fr; IntBuffer buff;
final static int MAX = 25000; } |
|
Vorax: Is there a name for a "redneck" programmer? Jeff: Unemployed. 
|
|
|
D.t.O
Junior Member  
Psych'd about Java Games
|
 |
«
Reply #6 - Posted
2004-04-17 20:26:13 » |
|
With the advent of LWJGL 0.9 marking a freeze in the API I thought there might be some clubies who'd like to see how simple a LWJGL game can look. Are you trying to convince me that LWJGL is better than others, or that game development in general is simple?
|
Enjoy. Regards, - D.t.O
|
|
|
SpuTTer
|
 |
«
Reply #7 - Posted
2004-04-17 23:05:57 » |
|
I think he was spending is time trying to give out a simple game loop to help out newbies who might want to be able to get a game going up quickly with a solid framework. I think that's great. More people should be giving back to the community. We should encourage this, not be all 'mean' about it. 
|
|
|
|
nonnus29
Senior Member   
Giving Java a second chance after ludumdare fiasco
|
 |
«
Reply #8 - Posted
2004-04-17 23:14:55 » |
|
I could never run any lwjgl demos until I installed a newer driver for my POS intel onboard gfx. I didn't think drivers would be available for it but after about 30 minutes of searching I did find them on the intel site.
So you might try updating your drivers.
|
|
|
|
|
D.t.O
Junior Member  
Psych'd about Java Games
|
 |
«
Reply #9 - Posted
2004-04-18 16:10:31 » |
|
I think that's great. More people should be giving back to the community. We should encourage this, not be all 'mean' about it Oops...looks like I miscommunicated something  . Don't get me wrong here, I think this is a great idea! To me, all these relatively low-level APIs are just scary  . Thanks Cas. 
|
Enjoy. Regards, - D.t.O
|
|
|
Games published by our own members! Check 'em out!
|
|
Morgrog
Senior Newbie 
Rubber bands and cafeine, weeeee~
|
 |
«
Reply #10 - Posted
2004-04-23 19:38:07 » |
|
1) /bump 2) woot! thx a zillion, that's gonna motivate me to actually do something (heck I've been around for a while and have yet to start anything, gd time constraints) 3) private static final float FRAMETIME = 1.0f / 60.0f; *claps at the time based loop* what happens if the fps drops below 60? (i.e. if the graphic card/cpu can't handle it?) and huh if this works like I think it does, do you really need to put setVSyncEnable? 4) no mouse? tsk tsk  Thanks again from the most experienced in noobiness newless clubie 
|
|
|
|
|
princec
|
 |
«
Reply #11 - Posted
2004-04-24 12:35:48 » |
|
2) woot! thx a zillion, that's gonna motivate me to actually do something (heck I've been around for a while and have yet to start anything, gd time constraints)
I've just started my 80hr game project based on this code. 4 hrs down, 76 to go. 3) private static final float FRAMETIME = 1.0f / 60.0f; *claps at the time based loop* what happens if the fps drops below 60? (i.e. if the graphic card/cpu can't handle it?) and huh if this works like I think it does, do you really need to put setVSyncEnable?
Then it slows down  If you can use vsync then you'll not get any tearing, and that's all the difference it makes. 4) no mouse? tsk tsk
Mouse, controller, and keyboard are automatically created and polled by LWJGL for you now. Cas 
|
|
|
|
MickeyB
|
 |
«
Reply #12 - Posted
2004-04-28 20:26:39 » |
|
Found where I am dying when trying to run your simple game loop: Window.create(GAME_TITLE); I have a println in front of this and after. The after never gets called, nothing is caught, then it goes straight to the finally() and throws a nullpointer because the AL.create() was never called. (since I cant get past WIndow.create(GAME_TITLE)  hmmmm, I am cluieless and lost.
|
|
|
|
Matzon
|
 |
«
Reply #13 - Posted
2004-04-28 20:30:55 » |
|
Try running in debug mode: -Dorg.lwjgl.Sys.debug=true
|
|
|
|
MickeyB
|
 |
«
Reply #14 - Posted
2004-04-28 20:53:23 » |
|
1 2 3 4 5
| in the finally clause... Exception in thread "main" java.lang.NoClassDefFoundError at org.lwjgl.openal.AL.<clinit>(Unknown Source) at org.lwjgl.examples.Game.cleanup(Game.java:163) at org.lwjgl.examples.Game.main(Game.java:81) |
same as above...the AL is never created, debug mode showed nothing different
|
|
|
|
Matzon
|
 |
«
Reply #15 - Posted
2004-04-28 22:15:05 » |
|
hmm, you really sholdn't ever receive a NoClassDefFoundError, unless you have your project setup in some wierd way...
could I get a copy of your project to find the error - this is really strange...
another way would be to grab the source and start adding debug code to find the exact line thats failing. I just dont get why you don't get any output when running in debug mode... *ponder*
|
|
|
|
mezcal
Senior Newbie 
Java games rock!
|
 |
«
Reply #16 - Posted
2004-04-29 03:12:14 » |
|
I'm getting the same error... Exception in thread "main" java.lang.NoClassDefFoundError at org.lwjgl.openal.AL.<clinit>(Unknown Source) at org.lwjgl.examples.Game.cleanup(Game.java:147) at org.lwjgl.examples.Game.main(Game.java:77) Try running in debug mode: -Dorg.lwjgl.Sys.debug=true How do I do this? (I'm using JCreater freeware, by the way..)
|
|
|
|
|
Matzon
|
 |
«
Reply #17 - Posted
2004-04-29 05:46:19 » |
|
hmm this is getting odd... the debug switch is something you append to your commandline... so if you normally do: java org.lwjgl.examples.Game you should now do: java -Dorg.lwjgl.Sys.debug=true org.lwjgl.examples.Game if you don't get *any* output - something is way wrong... I get: Window registered Releasing DC Destroy window Could not locate symbol glPointParameteriNV NOTICE: GL_NV_point_sprite disabled because of missing driver symbols Could not locate symbol glLoadProgramNV Could not locate symbol glVertexStream1fATI NOTICE: GL_ATI_vertex_streams disabled because of missing driver symbols found X-axis found Y-axis found Wheel found Button 0 found Button 1 found Button 2 found Button 3 found Button 4 found Button 5 found Button 6 found Button 7 WARNING: Clamping to 4 mouse buttons found X Axis found Y Axis found Button 0 found Button 1 found Button 2 found Button 3 Adapter key: \Registry\Machine\System\CurrentControlSet\Control\Video\{70DC50FE- FB2D-4BF2-BEB0-EAB6417827E1}\0000 Adapter key: \Registry\Machine\System\CurrentControlSet\Control\Video\{70DC50FE- FB2D-4BF2-BEB0-EAB6417827E1}\0000 Adapter: ati2dvag Version: 6.14.10.6430 JWS Classloader looking for: lwjglaudio Failure locating OpenAL using classloader:java.lang.NoSuchMethodException: sun.m isc.Launcher$AppClassLoader.findLibrary(java.lang.String) Found 2 OpenAL paths Testing '..\libs\win32\' Found OpenAL at '..\libs\win32\' Deleting GL context Destroying directinput Releasing DC Destroy window
|
|
|
|
MickeyB
|
 |
«
Reply #18 - Posted
2004-04-29 13:08:01 » |
|
Interesting I am using JCreator Pro. Decided to try from command line with the following: 1
| C:\java\simple\lwjgl\Test\classes>java -Dorg.lwjgl.Sys.debug=true -classpath .;c:\java\simple\lwjgl9\lwjgl-win32-0.9\lwjgl.jar org.lwjgl.examples.Game |
and still get the exact same thing. My package http://www22.brinkster.com/mbowles/lwjgl_test.zip
|
|
|
|
Matzon
|
 |
«
Reply #19 - Posted
2004-04-29 13:37:51 » |
|
1
| C:\java\simple\lwjgl\Test\classes>java -Dorg.lwjgl.Sys.debug=true -classpath .;c:\java\simple\lwjgl9\lwjgl-win32-0.9\lwjgl.jar org.lwjgl.examples.Game |
Hmm, and where's the lwjgl.dll placed? It might look like you'd need a: 1
| java -Djava.library.path=c:\java\simple\lwjgl9\lwjgl-win32-0.9\ -Dorg.lwjgl.Sys.debug=true -classpath .;c:\java\simple\lwjgl9\lwjgl-win32-0.9\lwjgl.jar org.lwjgl.examples.Game |
?
|
|
|
|
MickeyB
|
 |
«
Reply #20 - Posted
2004-04-29 15:16:01 » |
|
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 -- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| 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? 
|
|
|
|
mezcal
Senior Newbie 
Java games rock!
|
 |
«
Reply #21 - Posted
2004-04-29 16:13:09 » |
|
Mine is working now. The "-Djava.library.path=c:\lwjgl-win32-0.9\" seemed to be what I was missing. 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 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  )
|
|
|
|
|
Matzon
|
 |
«
Reply #22 - Posted
2004-04-29 16:32:27 » |
|
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? 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. 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? Yes 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  ) 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.
|
|
|
|
Matzon
|
 |
«
Reply #23 - Posted
2004-04-29 16:38:14 » |
|
1 2
| Adapter: Version: null org.lwjgl.LWJGLException: Could not choose ARB pixel formats. |
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)
|
|
|
|
MickeyB
|
 |
«
Reply #24 - Posted
2004-04-29 18:18:29 » |
|
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?
|
|
|
|
M4g3
Senior Newbie 
Java games rock!
|
 |
«
Reply #25 - Posted
2004-04-29 18:31:08 » |
|
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  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  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  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. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| private static void render() { GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT); GL11.glLoadIdentity(); GL11.glTranslatef(-1.5f,0.0f,-6.0f); GL11.glBegin(GL11.GL_TRIANGLES); GL11.glVertex3f( 0.0f, 1.0f, 0.0f); GL11.glVertex3f(-1.0f,-1.0f, 0.0f); GL11.glVertex3f( 1.0f,-1.0f, 0.0f); GL11.glEnd(); GL11.glTranslatef(3.0f,0.0f,0.0f); GL11.glBegin(GL11.GL_QUADS); GL11.glVertex3f(-1.0f, 1.0f, 0.0f); GL11.glVertex3f( 1.0f, 1.0f, 0.0f); GL11.glVertex3f( 1.0f,-1.0f, 0.0f); GL11.glVertex3f(-1.0f,-1.0f, 0.0f); GL11.glEnd(); } |
|
|
|
|
|
Orangy Tang
|
 |
«
Reply #26 - Posted
2004-04-29 19:09:18 » |
|
NeHe demos usually have an init method which sets a bunch of default gl state. Did you remember to copy that as well...?
|
|
|
|
M4g3
Senior Newbie 
Java games rock!
|
 |
«
Reply #27 - Posted
2004-04-29 19:30:48 » |
|
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
|
|
|
|
|
Matzon
|
 |
«
Reply #28 - Posted
2004-04-29 20:14:41 » |
|
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? 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!)
|
|
|
|
MickeyB
|
 |
«
Reply #29 - Posted
2004-04-30 13:03:35 » |
|
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.
|
|
|
|
|