From 3a82c9eca162bead16803fec08e618b2e16f050a Mon Sep 17 00:00:00 2001 From: iwgeric Date: Tue, 6 Jan 2015 08:46:19 -0500 Subject: [PATCH] Add frame rate limitiing on Android. If frameRate is set in MainActivity, the application will limit the frame rate to the value defined. This can be used to save battery life. --- .../java/com/jme3/app/AndroidHarness.java | 9 ++++++++ .../com/jme3/system/android/OGLESContext.java | 23 +++++++++++++------ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/jme3-android/src/main/java/com/jme3/app/AndroidHarness.java b/jme3-android/src/main/java/com/jme3/app/AndroidHarness.java index 92713c666..7ce66ae98 100644 --- a/jme3-android/src/main/java/com/jme3/app/AndroidHarness.java +++ b/jme3-android/src/main/java/com/jme3/app/AndroidHarness.java @@ -88,6 +88,13 @@ public class AndroidHarness extends Activity implements TouchListener, DialogInt */ protected int eglStencilBits = 0; + /** + * Set the desired frame rate. If frameRate > 0, the application + * will be capped at the desired frame rate. + * (default = -1, no frame rate cap) + */ + protected int frameRate = -1; + /** * Sets the type of Audio Renderer to be used. *

@@ -239,6 +246,8 @@ public class AndroidHarness extends Activity implements TouchListener, DialogInt settings.setResolution(disp.getWidth(), disp.getHeight()); settings.setAudioRenderer(audioRendererType); + settings.setFrameRate(frameRate); + // Create application instance try { if (app == null) { diff --git a/jme3-android/src/main/java/com/jme3/system/android/OGLESContext.java b/jme3-android/src/main/java/com/jme3/system/android/OGLESContext.java index 79ec34875..22893c1ce 100644 --- a/jme3-android/src/main/java/com/jme3/system/android/OGLESContext.java +++ b/jme3-android/src/main/java/com/jme3/system/android/OGLESContext.java @@ -75,7 +75,8 @@ public class OGLESContext implements JmeContext, GLSurfaceView.Renderer, SoftTex protected SystemListener listener; protected boolean autoFlush = true; protected AndroidInputHandler androidInput; - protected int minFrameDuration = 0; // No FPS cap + protected long minFrameDuration = 0; // No FPS cap + protected long lastUpdateTime = 0; protected JoyInput androidSensorJoyInput = null; public OGLESContext() { @@ -225,6 +226,12 @@ public class OGLESContext implements JmeContext, GLSurfaceView.Renderer, SoftTex androidInput.loadSettings(settings); } + if (settings.getFrameRate() > 0) { + minFrameDuration = (long)(1000d / (double)settings.getFrameRate()); // ms + logger.log(Level.FINE, "Setting min tpf: {0}ms", minFrameDuration); + } else { + minFrameDuration = 0; + } } @Override @@ -320,23 +327,25 @@ public class OGLESContext implements JmeContext, GLSurfaceView.Renderer, SoftTex throw new IllegalStateException("onDrawFrame without create"); } - long milliStart = System.currentTimeMillis(); - listener.update(); if (autoFlush) { renderer.onFrame(); } - long milliDelta = System.currentTimeMillis() - milliStart; + long updateDelta = System.currentTimeMillis() - lastUpdateTime; // Enforce a FPS cap - if (milliDelta < minFrameDuration) { - //logger.log(Level.FINE, "Time per frame {0}", milliDelta); + if (updateDelta < minFrameDuration) { +// logger.log(Level.INFO, "lastUpdateTime: {0}, updateDelta: {1}, minTimePerFrame: {2}", +// new Object[]{lastUpdateTime, updateDelta, minTimePerFrame}); try { - Thread.sleep(minFrameDuration - milliDelta); + Thread.sleep(minFrameDuration - updateDelta); } catch (InterruptedException e) { } } + + lastUpdateTime = System.currentTimeMillis(); + } }