Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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  
  [Resolved] LWJGL Rendering Loop -- Stuttery Performance!  (Read 1526 times)
0 Members and 1 Guest are viewing this topic.
Offline Smarto

Senior Newbie




Hiya!


« Posted 2005-07-29 23:36:36 »

Hiya everyone! I am new to using LWJGL and OpenGL, and I am having a problem with my time-based game loop.
When I run the program, the graphics just seem to stutter occasionally, and I have a bad feeling that it is some oversight in my code causing the problem.
If anyone could be nice enough to look over these portions of my code and tell me what my problem is, I would greatly appreciate it!

I will explain the system so maybe from my explanation you could find a flaw and not even have to look in the code:

I have two base classes, GameWindow and GameControl.  GameWindow contains the main() method.  Each class has a reference to the active instance of the other class.
GameWindow begins by intializing Display, setting the title, display modes and everything and initializes OpenGL. In then sets the boolean programRunning to true and begins the game loop, contained in GameControl.

GameControl's game loop works like this:

1. set variable beforeLoop to System.nanoTime()
2. Yield the thread
3. Poll the keyboard for input
4. run method programLifeTime, passing the value of variable delta, which is a method that keeps track of how long the program has been running.
5. run method renderGame, which limits the frame rate to a preset value, according to a calculated time period based off of the preset value.  For example, if the Desired FPS is 60, the period of time to wait before rendering again would be 16666666 ns.  It only renders a maximum of once per loop, then resets the time counter  (code example below).
6. run method updateGame, which then passes the delta value to the requisite areas for game updating.
7. Update the Display
8. calculate the delta value according to System.nanotime() - beforeLoop

OpenGL Initialization
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
private void initGL() {
      // enable textures since we're going to use these for our sprites
     GL11.glEnable(GL11.GL_TEXTURE_2D);
           GL11.glShadeModel(GL11.GL_SMOOTH); // Enable Smooth Shading
          GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
          GL11.glClearDepth(1.0); // Depth Buffer Setup
     // disable the OpenGL depth test since we're rendering 2D graphics
     GL11.glDisable(GL11.GL_DEPTH_TEST);
     
      GL11.glMatrixMode(GL11.GL_PROJECTION);
      GL11.glLoadIdentity();
     
      GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
     
      GL11.glOrtho(0, windowWidth, windowHeight, 0, -1, 1);
   }


Game Loop
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  
public void gameLoop() {
     
      beforeLoop = System.nanoTime();
     
      Thread.yield();
      Keyboard.poll();
      processKeyboard();
     
      programLifeTime(delta);
      renderGame(delta);
      updateGame(delta);
     
      Display.update();
     
      delta = System.nanoTime() - beforeLoop;
     
   }

public void updateGame(long delta) {
      Display.setTitle(gameWindow.getWindowTitle() + " | Elapsed Time- " + lifeSpanMinutes + "m:" + lifeSpanSeconds + "s " + "FPS: " + currentFPS);
     
      for(int x = 0; x < 10; x++) {
         rect[x].updateEntity(delta);
      }
   }
   
   public void renderGame(long delta) {
      FPS_TIME_ELAPSED = FPS_TIME_ELAPSED + delta;
      fpsCounterTimer = fpsCounterTimer + delta;
      if(FPS_TIME_ELAPSED >= FPS_PERIOD) {
         FPS_TIME_ELAPSED = 0;
         try {
            // clear screen
           GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
            GL11.glMatrixMode(GL11.GL_MODELVIEW);
            GL11.glLoadIdentity();
            } catch(Exception e) {
               e.printStackTrace();
               System.exit(0);
            }
         render();

         fpsCounter++;
         if(fpsCounterTimer > 1000000000) {
            currentFPS = fpsCounter;
            fpsCounter = 0;
            fpsCounterTimer = 0;
         }
      }
   }


And here is my rendering code for rendering a quad (the code stutters even with just a single moving quad on the screen):
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
public void drawSprite(float x, float y) {
     
      // Set the color to draw geometry as
     GL11.glColor3f(1.0f, 0f, 0f);
     
      GL11.glBegin(GL11.GL_QUADS);
     
      // Upper-Left vertex
     GL11.glVertex2f(x - (width/2), y - (height/2));
      // Bottom-Left vertex
     GL11.glVertex2f(x - (width/2), y + (height/2));
      // Bottom-Right vertex
     GL11.glVertex2f(x + (width/2), y + (height/2));
      // Top-Right Vertex
     GL11.glVertex2f(x + (width/2), y - (height/2));
     
      GL11.glEnd();
     
   }



I know this is alot of code and a huge post to look over and maybe not even enough to find my problem, but if anyone could, I would be so happy!
I cannot figure out why my code stutters!
Thank you so much in advance!

Smarto
Anger is the equivalent of grasping a hot coal in the palm of your hand
- Unkown
Offline Jeff

JGO Coder




Got any cats?


« Reply #1 - Posted 2005-07-30 01:40:14 »

You may want to try the specific LWJGL forum.  Maybe post a  pointer to this message over there...

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline anarchotron

Junior Member




...precious bodily fluids.


« Reply #2 - Posted 2005-07-30 06:13:45 »

Two suggestions:

1. Since your render(long delta) method only does something some of the time, maybe you should only call Display.update() when something has been done in render(long delta).  So maybe put the Display.update() call inside the work block of render(long delta).   I don't think that should matter though.

2. Normally you want to limit the frame rate of the whole game loop, not just the rendering rate.  Besides your Thread.yield() call, the game loop is spinning as fast as it can in a fast loop and periodically rendering.  What you would prefer is if the loop slept in between frames.  Unfortunately it is somewhat difficult to get accurate sleeps on Windows with Java.  Thread.sleep() on Windows has a minimum sleep time of ~15ms no matter what you tell it to do, which automatically drops your frame rate to a _maximum_ of 66.   In my project I'm using the Display.sync(int framerate) method which is a very convient way to limit the framerate.  It isn't perfectly accurate either, but it lets you sync up framerates in excess of 60fps pretty easily.

Incidentally I'd point out that I've heard elsewhere on these boards that Thread.yield() is often pointless or counterproductive, particularly when all the threads of interest are running at the same priority.

Try Display.sync(int framerate) to sleep your whole loop and see if your situation improves.


public void gameLoop() {

Keyboard.poll();
processKeyboard();

programLifeTime(delta);
renderGame(delta);
updateGame(delta);

Display.update();

Display.sync(desiredFramerate);
}



And remove the framerate limiting code from renderGame();

PS.
It is also customary, when updating & rendering once per frame, do update first and render last, rather than vice-versa.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Smarto

Senior Newbie




Hiya!


« Reply #3 - Posted 2005-07-31 00:53:29 »

Thanks so much for your help anarchotron!
You really helped me clean up the loop -- I can now see how horribly inefficient it was before  Grin.
Now my animations move across the screen with much smoother graphics and the loop runs through much less often.

Thanks alot!!!

Smarto
Anger is the equivalent of grasping a hot coal in the palm of your hand
- Unkown
Offline Matzon

JGO Knight


Medals: 19
Projects: 1


I'm gonna wring your pants!


« Reply #4 - Posted 2005-07-31 06:56:51 »


public void gameLoop() {

// Keyboard.poll(); <--- no need to call Keyboard.poll, Display.update() does this for you
processKeyboard();

programLifeTime(delta);
renderGame(delta);
updateGame(delta);

Display.update();

Display.sync(desiredFramerate);
}


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.

TehJavaDev (11 views)
2014-08-28 18:26:30

CopyableCougar4 (24 views)
2014-08-22 19:31:30

atombrot (37 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (29 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56
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!