Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (582)
games submitted by our members
Games in WIP (503)
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  
  My game loop is stuttering, any suggestions on making it smoother?  (Read 2250 times)
0 Members and 1 Guest are viewing this topic.
Offline Z-Man
« Posted 2011-06-19 05:53:54 »

So I'm trying out a new game loop that I recently wrote and it seems to stutter when simply moving a box across the screen. My last game loop which was a modified version of the game loop from KGPJ stuttered as well but I think that may have been me just messing with it. The loop should be running 30-31 fps and the cube moves 5 pixels every update so I don't think that's the problem.  Does anyone have any suggestions on making my loop smoother?

[CODE]
   public void run()
   {
     long start = System.nanoTime(),curr;
    
     while(canvas.isRunning())
     {
        if(!canvas.isPaused())
        {
           canvas.update();
           canvas.draw();
        }
       
        while((curr = System.nanoTime() - start) < period)
              Thread.yield();
       
        start = System.nanoTime();
     }
     System.exit(0);
   }
[/CODE]

Oh and this is how I draw, if it matters. The updating is just advance the cubeX 5 and resetting it to 0 - cubeWidth if cubeX > the width of the canvas. If there is any other code I need to post let me know.
[CODE]
   public void draw()
   {
      do
      {
         do
         {
            Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
            g.setColor(Color.black);
            g.fillRect(0,0,CWIDTH,CHEIGHT);
            
            
            g.setColor(Color.white);
            g.fillRect(cubeX,cubeY,cubeWidth,cubeHeight);

            g.dispose();
         }while(strategy.contentsRestored());

         strategy.show();
      }while(strategy.contentsLost());
   }
[/CODE]
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


« Reply #1 - Posted 2011-06-19 07:55:20 »

You'll get stuttering no matter what with Java2D, it's a limitation you can't get around without going the route of OpenGL.

You can have a look at this game loop tutorial, http://www.java-gaming.org/topics/game-loops/24220/view.html but I think yours looks more or less okay.

See my work:
OTC Software
Offline Z-Man
« Reply #2 - Posted 2011-06-19 19:06:55 »

Gah. That sucks. Is there a simple(ish) way to do 2D games with OpenGL? Or is OpenGL 3D only.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 70
Projects: 15


★★★★★


« Reply #3 - Posted 2011-06-19 19:53:30 »

Gah. That sucks. Is there a simple(ish) way to do 2D games with OpenGL? Or is OpenGL 3D only.

Yeh just use Slick2D, its api is just like Java2D.
Offline Z-Man
« Reply #4 - Posted 2011-06-19 19:59:40 »

Ah thank you both  Grin
Offline philfrei
« Reply #5 - Posted 2011-06-19 20:00:53 »

I am wondering what the stuttering you describe looks like. Is it roughly the equivalent to what I'm getting here?

http://www.hexara.com/gameNS.html

After loading a puzzle (from the file menu), you can click and drag an icon around. There is a slight stutter. I'm wondering if it is comparable to what you are complaining about. I'm using a Timer to trigger a repaint() call on a JPanel once every 33 msec.

I wouldn't be surprised if your stutter has more to do with limitations of the MouseMotionListener than anything else. I was recently trying to implement a JSlider and it was similarly no where near as smooth as it seems like it should have been. Here is a theory based on total speculation (hopefully someone will set it straight): code run via Listener calls execute on the EDT and are prone to "consolidation". It seems to me I ran a System.out.println() for the listener AND for the object scheduled to receive the info, and many of the listener outputs would never reach the target, which had code checking the field 44100 times a second (once per sound sample).

So, I'm guessing that a lot of the MouseMotionListener updates never reach your draw command, due to similar consolidation. Someone please shoot this down, as it doesn't totally make sense that a Listener would update at a certain rate and then not be able to pass the info on at the same rate.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #6 - Posted 2011-06-20 00:14:12 »

@philfrei
Not stuttering over here. Game runs very smoothly.

@Z-Man
Try using Thread.sleep(1) instead of Thread.yield().

Offline philfrei
« Reply #7 - Posted 2011-06-20 02:08:06 »

Yeah, that Thread.yield() construct looks fishy. Where in KGPJ does he do that? Usually he has one sleeping the duration between ending the draw and the time for the next iteration.

On page 18, very top, Davison gives the reasons one needs to call Thread.sleep(someValueOfMsecs) in a game loop. Thread.yield() could lead to any or all the problems he lists there, including giving time for garbage collection and the prevention of events being "coalesced" on the EDT.

Also, is the long "curr" really needed?

@ra4king - Thanks for the sanity check!

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline Z-Man
« Reply #8 - Posted 2011-06-20 04:41:37 »

Nope long curr isn't needed in that game loop. Thanks for pointing that out.

EDIT: Also the stuttering isn't terrible. It's just a small stutter every few seconds. Not really bad but noticeable all the same. So I'm trying to figure out how to work Slick2D in without making sweeping changes to any of my games xD.

EDIT 2: philfrei your right I don't think he uses yield. He sleeps the time you have left. Does in anyone know if this is better than the sleep(1) while loop?
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #9 - Posted 2011-06-20 05:02:43 »

Thread.sleep(timeToSleep) isn't reliable, Thread.yield() is smooth on most computers yet it eats the CPU like a hog, which leads to stuttering eventually. Thread.sleep(1) inside a while loop is the best way to do it and barely uses any CPU time.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


« Reply #10 - Posted 2011-06-20 06:38:32 »

The stuttering sounds like the typical Java2D stuttering. Also, Thread.yield() has never led to stuttering or any other issues, even though it does in fact max out a processor. Thread.sleep() can actually be much less reliable, but of course it makes your game use a much more realistic processor footprint. It really depends on what you're going for...

See my work:
OTC Software
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.

xsi3rr4x (72 views)
2014-04-15 18:08:23

BurntPizza (68 views)
2014-04-15 03:46:01

UprightPath (79 views)
2014-04-14 17:39:50

UprightPath (65 views)
2014-04-14 17:35:47

Porlus (80 views)
2014-04-14 15:48:38

tom_mai78101 (104 views)
2014-04-10 04:04:31

BurntPizza (164 views)
2014-04-08 23:06:04

tom_mai78101 (260 views)
2014-04-05 13:34:39

trollwarrior1 (210 views)
2014-04-04 12:06:45

CJLetsGame (220 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!