I’m writing a small game with Libgdx.
I have aRender[OpenGL] thread that keeps calling render() on all objects and an
Update thread that keeps calling update(double delta) on all objects.
Update thread is looping way faster. Should I try to use some kind of synchronization so Update thread could rest for a bit ?
Would there be any benefits from it ?
Update
public void run() {
while(true){
nano = System.nanoTime();
long delta = nano - timestamp;
timestamp = nano;
accumulator+=(double)delta/BILION;
while(accumulator >= step){
update(step);
accumulator-=step;
}
long loc = (long) ((step -accumulator)*1000)+1;
try {
Thread.sleep(loc);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I dont know if its a problem, but my current update is 2500-20000 fps. So Update speed is more than sufficient.
Your data Update and your Render should be independent and not
synchronized.Your Update function should work at a speed that you determine will be the most user-friendly for the user.
It should be independent from Render since the rendering rate may change due to the hardware used by the end-user.
And for example you don’t want your “monster” to walk ten times faster on the last generation tablet so it would become unplayable.
You should set your update rate to a constant you choose.
Then maybe you’ll want your update rate to slow down a bit if the framerate is really slow.
That’s my opinion hoper it’ll help.