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.
Thanks in advanced!
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.
Thanks in advanced!
It looks fine. In fact, that whole sleeping code looks like mine
Maybe it’s your computer?
80% was made from you
Hm, I just cant help but see it jump every so often.
Im OSX based? could it be the shitty graphics performance of OSX?
Yup, I blame OS X.
:(, despite the hate, +1 internets to you!
Thanks for the help + the code
Try to turn the ‘wait loop’ into:
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:
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.
If the problem is with the ‘accuracy’ of Thread.sleep(1), try using that sleeping trick everyone talks about:
new Thread() {
{
setDaemon(true);
start();
}
public void run() {
while(true) {
try { Thread.sleep(Long.MAX_VALUE); } catch(Exception exc) {}
}
}
};
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):
if (wait > 0)
{
long mill = wait / NS_PER_MS;
long nano = wait - (mill * NS_PER_MS);
Thread.currentThread().sleep(mill, (int)nano);
}
Thread.sleep(millis, nanos) simply calls Thread.sleep(millis).
That method shouldn’t have existed. :cranky:
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
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!