Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
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  
  Help with smoothing game loop  (Read 1461 times)
0 Members and 1 Guest are viewing this topic.
Offline nspyro

Junior Newbie





« Posted 2012-05-03 23:45:12 »

Hello peeps,
First post here.

I have been working on a game loop and can't seem to get the frames/game to run smoothly
I was wondering if anyone could have a look at the code and see if they know why its laggy every so often.

http://pastebin.com/HPEa9dfV

Thanks in advanced!
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-05-04 00:29:13 »

It looks fine. In fact, that whole sleeping code looks like mine Wink

Maybe it's your computer?

Offline nspyro

Junior Newbie





« Reply #2 - Posted 2012-05-04 00:38:17 »

80% was made from you  Tongue
Hm, I just cant help but see it jump every so often.
Im OSX based? could it be the shitty graphics performance of OSX?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-05-04 00:46:49 »

Yup, I blame OS X.

Offline nspyro

Junior Newbie





« Reply #4 - Posted 2012-05-04 00:52:37 »

 Sad, despite the hate, +1 internets to you!
Thanks for the help + the code Wink
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 832
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2012-05-04 01:00:51 »

Try to turn the 'wait loop' into:
1  
2  
3  
while(System.nanoTime() < nextFrame) {
   Thread.yield();
}

If that smoothens the framerate, then the problem lies in the 'accuracy' of Thread.sleep(1).

If that doesn't solve it, try this:
1  
2  
3  
while(System.nanoTime() < nextFrame) {
   ; // do nothing
}

If that smoothens the framerate, then the problem lies in the 'accuracy' of Thread.yield().

If that doesn't solve it, you're screwed. Probably some background-process is grabbing CPU cycles, or your OS is swapping.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2012-05-04 01:16:34 »

If the problem is with the 'accuracy' of Thread.sleep(1), try using that sleeping trick everyone talks about:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
new Thread() {
    {
        setDaemon(true);
        start();
    }
   
    public void run() {
        while(true) {
            try { Thread.sleep(Long.MAX_VALUE); } catch(Exception exc) {}
        }
    }
};

Offline Nyhm

Senior Devvie


Medals: 3
Projects: 1


Island Forge


« Reply #7 - Posted 2012-05-04 01:41:36 »

I was just seeking advice about my graphics driver loop code. I use the nanosecond sleep call (but not sure if this would help your case):

1  
2  
3  
4  
5  
6  
if (wait > 0)
{
    long mill = wait / NS_PER_MS;
    long nano = wait - (mill * NS_PER_MS);
    Thread.currentThread().sleep(mill, (int)nano);
}



Island Forge: Create Islands with Stories for Others to Explore!
Free-to-Play with Membership and Upgrade options!
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 832
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2012-05-04 01:47:20 »

Thread.sleep(millis, nanos) simply calls Thread.sleep(millis).

That method shouldn't have existed. Cranky

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline nspyro

Junior Newbie





« Reply #9 - Posted 2012-05-04 01:58:06 »

Thanks for the help guys!
I have been playing around with the sleep call, but still seem to get the same results
Ive tested it on 2 Macs I have (MBA and iMac), I obviously get better frames on the iMac compared to the MBA
FYI, it looks like the BufferImage just stops for a split second then resume's, it doesnt really 'skip' a frame, it just pauses (kind of annoying)

Is there a more accurate way of calculating the sleep time?

I thought if i tried a simpler loop it may help? (that might have been stupid of me)
And it didn't Tongue
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
unprocessed += (now - lastTime)/nanosPerTick;
while(unprocessed >= 1){
   ticks++;
   update();
   unprocessed = 0;
   shouldRender = true;
}
   if(shouldRender){
      frames++;
      render();
}
         
try {
   Thread.sleep(1);
} catch (Exception e) {
   e.printStackTrace();
}


Like you guys mentioned it seems it has more to do with the thread then the loop/render - update

Thanks again!
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.

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (22 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (41 views)
2014-11-12 21:09:33

kovacsa (69 views)
2014-11-07 19:57:14
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!