Skip to content

Commit

Permalink
Fix fps issue when certain animation intervals are set, such as (1 / …
Browse files Browse the repository at this point in the history
…30). (#2162)
  • Loading branch information
rh101 authored Sep 18, 2024
1 parent 1de0641 commit ef52043
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class AxmolRenderer implements GLSurfaceView.Renderer {

// The final animation interval which is used in 'onDrawFrame'
private static long sAnimationInterval = (long) (1.0f / 60f * AxmolRenderer.NANOSECONDSPERSECOND);
private static float FPS_CONTROL_THRESHOLD = 1.0f / 1200.0f * AxmolRenderer.NANOSECONDSPERSECOND;

// ===========================================================
// Fields
Expand Down Expand Up @@ -91,26 +92,24 @@ public void onSurfaceChanged(final GL10 GL10, final int width, final int height)
@Override
public void onDrawFrame(final GL10 gl) {
/*
* Fix 60fps limiting doesn't work when high-end device is working in 120fps mode.
* Render time MUST be counted in, or the FPS will slower than appointed.
*/
if (AxmolRenderer.sAnimationInterval <= 1.0f / 1200.0f * AxmolRenderer.NANOSECONDSPERSECOND) {
AxmolRenderer.nativeRender();
} else {
final long now = System.nanoTime();
final long interval = now - this.mLastTickInNanoSeconds;

/*
* Render time MUST be counted in, or the FPS will slower than appointed.
*/
this.mLastTickInNanoSeconds = now;
AxmolRenderer.nativeRender();
AxmolRenderer.nativeRender();
/*
* No need to use algorithm in default(60,90,120... FPS) situation,
* since onDrawFrame() was called by system 60 times per second by default.
*/
if (AxmolRenderer.sAnimationInterval > AxmolRenderer.FPS_CONTROL_THRESHOLD) {
final long interval = System.nanoTime() - this.mLastTickInNanoSeconds;

if (interval < AxmolRenderer.sAnimationInterval) {
try {
Thread.sleep((AxmolRenderer.sAnimationInterval - interval) / AxmolRenderer.NANOSECONDSPERMICROSECOND);
} catch (final Exception e) {
}
}

this.mLastTickInNanoSeconds = System.nanoTime();
}
}

Expand Down

0 comments on commit ef52043

Please sign in to comment.