Better sync

This commit is contained in:
lax1dude 2024-12-11 21:32:51 -08:00
parent 290e775165
commit eb6131cacf

View file

@ -1614,26 +1614,32 @@ public class EaglerAdapterGL30 extends EaglerAdapterImpl2 {
} }
public static boolean sync(int limitFramerate, long[] timerPtr) { public static boolean sync(int limitFramerate, long[] timerPtr) {
boolean limitFPS = limitFramerate > 0 && limitFramerate < 1000; boolean limitFPS = limitFramerate > 0 && limitFramerate <= 1000;
boolean blocked = false; boolean blocked = false;
if(limitFPS) { if(limitFPS) {
long frameMillis = (1000l / limitFramerate);
if(timerPtr[0] == 0l) { if(timerPtr[0] == 0l) {
timerPtr[0] = steadyTimeMillis(); timerPtr[0] = steadyTimeMillis() + frameMillis;
}else { }else {
long millis = steadyTimeMillis(); long millis = steadyTimeMillis();
long frameMillis = (1000l / limitFramerate); long remaining = timerPtr[0] - millis;
long frameTime = millis - timerPtr[0]; if(remaining > 0) {
if(frameTime > 2000l || frameTime < 0l) { if(isWebGL) {
frameTime = frameMillis; immediateContinue(); // cannot stack setTimeouts, or it will throttle
timerPtr[0] = millis; millis = steadyTimeMillis();
}else { remaining = timerPtr[0] - millis;
timerPtr[0] += frameMillis; if(remaining > 0) {
sleep((int)remaining);
}
}else {
sleep((int)remaining);
} }
if(frameTime >= 0l && frameTime < frameMillis) {
sleep((int)(frameMillis - frameTime));
blocked = true; blocked = true;
} }
if((timerPtr[0] += frameMillis) < millis) {
timerPtr[0] = millis;
}
} }
}else { }else {
timerPtr[0] = 0l; timerPtr[0] = 0l;