[Solved] Quick LWJGL Question

Hi, I first started Java game programming with simple built in Java2D classes. I remember having to create a game loop that updates that game that re-draws the screen. It was a lot of code (relatively).

I’m starting to learn LWJGL and is this following code all I really need to do in terms of worrying about re-drawing/updating the screen?


//I've created the Display in earlier lines of code
while (!Display.isCloseRequested()){
			//Render
			
			Display.update();
			Display.sync(60);
		}
		
		Display.destroy();

No, it’s not really that simple. For most games you’ll probably want a variable or “semi-fixed” timestep; and use the delta-time variable to ensure smooth animation and movement across systems.

However, unlike Java2D, you won’t need to worry about buffer strategies, or repainting, or Thread.wait/sleep, or what have you. At the end of each frame, you simply call Display.update to “swap the buffers” (i.e. show what has been rendered), then Display.sync if you want to cap the frame rate.

You will also need to initialize GL:

public void initGL(){
		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();
      // Coordinate Placement, use glOrtho(0, Display.getWidth(), 0, Display.getHeight, 1, -1); for Cartesian Coordinates (better for mouse input and what I consider to be the standard :P)
		GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
		GL11.glMatrixMode(GL11.GL_MODELVIEW);
      // Enabling Textures to be drawn on the screen
		GL11.glEnable(GL11.GL_TEXTURE_2D);
		
      // Used for Transparency
		GL11.glEnable(GL11.GL_BLEND);
		GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
	}

Check out http://www.lwjgl.org/wiki/index.php?title=Main_Page for good tutorials on getting started.

I would also recommend Slick-util http://slick.cokeandcode.com/downloads/util/ in order to load textures/sounds easier. LWJGL site also has tutorials on this.

Other than that, coming back to updating the screen, no. You pretty much have everything that you need. ;D

Thanks davedes and Longarmx!

Longarmx some questions, about that code you posted, could I just include that in the constructor?

Is

GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);

any different than

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1200, 800, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);

?

You could put it in the constructor if you really wanted to, but if you ever need to access your main class variable, you will have to do it through inheritance. I would personally just make it an initGL() method that is called in your main init() method. Saves a lot of confusion.

About your code, it is fine to not have the GL11. before everything. You just need to statically import GL11.

About the glOrtho though, I would still use the Display.width(), and Display.height() just so that it you don’t have to worry about it later on if you want to change the size of the display.
The way you have it set up right now (glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1)), the coordinates are like they are in Java2D and such (with (0, 0) starting the the upper left hand corner). Unfortunately though, the mouse input doesn’t change depending on your coordinate system. You can easily fix this by having a mouseY = Display.getHeight() - Mouse.getY();

I personally use glOrtho the way you are doing it just because I am used to it. You can use whatever you like though.

~Longarmx

Thank you so much for the speedy replies, it really helps ;D.

In addition to the last poster… You actually should be doing this for mouse Y:

int y = Display.getHeight() - Mouse.getY() - 1;

I’d also suggest using Matthias’ image decoders instead of Slick-Util. :slight_smile: Matthias’ are not as well documented, but they are generally more flexible and more efficiently written. And you can extend the IO utilities to write your own texture classes (i.e. if you want to support compressed textures, Texture1D, texture arrays, etc).
http://hg.l33tlabs.org/TextureLoader/file/c89e0a92f9dc/src/de/matthiasmann/textureloader