Let’s get started! To create a window with a canvas to draw on (called a display) we use the following code:
import org.lwjgl.LWJGLException; import org.lwjgl.opengl.Display; public class HelloWorld { public static void main (String args[]){ try { Display.setTitle("Hello World"); Display.create(); } catch (LWJGLException e) { e.printStackTrace(); } } }
Easy! This creates a window the size of your current desktop with an area where the shapes we will later draw with OpenGL will appear. Unfortunately this window cannot be closed using the traditional ‘x’ button, you must shut down the java process itself. The next thing to do will be to add the close mechanism.
Add the following to the bottom of the main method:
while(!Display.isCloseRequested()){ Thread.sleep(100); } Display.destroy();
Again, what this code does is obvious. We can see that the developers of LWJGL have tried to ensure that getting to the stage where we can start drawing shapes is as painless as possible.
Advertisements
I’m getting an unhandled InterreuptedException error on the Thread.sleep(100) when I try this.
Any ideas?
Thread.sleep() throws an InterruptedException. Put that statement in
try{
Thread.sleep(100);
}
catch(Exception e){
e.printStackTrace();
}
Because he never initialized a thread via when you put the public class HelloWorld{
}
you are supposed to put implements Runnable and then inside the main thing you then put Thread whatEverTheNameIs = new Thread(mainGameLoop);
the take everything out of the main function and put it in another function called mainGameLooop. and you are done
Hi, i learned java. i started learning lwjgl with the with the aim to game development. i fell in love with lwjgl. Now i come to know that game engine( unity3d, udk etc) is required to develop games. A game engine abstracts out all of the OpenGL, low-level logic. It does 100% of the rendering for you (except for shaders). And developing a game engine is not one persons work. So why did/should i learn lwjgl? Now should i continue learning lwjgl? What are the advantages? Please reply at your earliest convenience.