Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (564)
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  
  [Solved] Quick LWJGL Question  (Read 843 times)
0 Members and 1 Guest are viewing this topic.
Offline Vladiedoo
« Posted 2012-06-13 02:54:45 »

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?

1  
2  
3  
4  
5  
6  
7  
8  
9  
//I've created the Display in earlier lines of code
while (!Display.isCloseRequested()){
         //Render
       
         Display.update();
         Display.sync(60);
      }
     
      Display.destroy();

Offline davedes
« Reply #1 - Posted 2012-06-13 03:00:46 »

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.

Offline Longarmx
« Reply #2 - Posted 2012-06-13 03:04:23 »

You will also need to initialize GL:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
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.  Grin

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Vladiedoo
« Reply #3 - Posted 2012-06-13 03:13:53 »

Thanks davedes and Longarmx!

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

Is
1  
2  
3  
4  
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
1  
2  
3  
4  
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1200, 800, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);

?
Offline Longarmx
« Reply #4 - Posted 2012-06-13 03:37:48 »

Thanks davedes and Longarmx!

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

Is
1  
2  
3  
4  
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
1  
2  
3  
4  
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1200, 800, 0, 1, -1);
glOrtho(0, 1200, 800, 0, 1, -1);

?

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

Offline Vladiedoo
« Reply #5 - Posted 2012-06-13 03:41:39 »

Thank you so much for the speedy replies, it really helps  Grin.
Offline davedes
« Reply #6 - Posted 2012-06-13 03:56:08 »

In addition to the last poster... You actually should be doing this for mouse Y:
1  
int y = Display.getHeight() - Mouse.getY() - 1;


I'd also suggest using Matthias' image decoders instead of Slick-Util. Smiley 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

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.

Grunnt (22 views)
2014-09-23 14:38:19

radar3301 (14 views)
2014-09-21 23:33:17

BurntPizza (31 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (29 views)
2014-09-20 20:14:06

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

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

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

TehJavaDev (108 views)
2014-09-10 06:39:09
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!