1 /* 2 * Copyright (c) 2003-2009 jMonkeyEngine 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 package com.jme3.system.android; 34 35 import com.jme3.system.Timer; 36 37 /** 38 * <code>AndroidTimer</code> is a System.nanoTime implementation of <code>Timer</code>. 39 */ 40 public class AndroidTimer extends Timer { 41 42 //private static final long TIMER_RESOLUTION = 1000L; 43 //private static final float INVERSE_TIMER_RESOLUTION = 1f/1000L; 44 private static final long TIMER_RESOLUTION = 1000000000L; 45 private static final float INVERSE_TIMER_RESOLUTION = 1f/1000000000L; 46 47 private long startTime; 48 private long previousTime; 49 private float tpf; 50 private float fps; 51 AndroidTimer()52 public AndroidTimer() { 53 //startTime = System.currentTimeMillis(); 54 startTime = System.nanoTime(); 55 } 56 57 /** 58 * Returns the time in seconds. The timer starts 59 * at 0.0 seconds. 60 * 61 * @return the current time in seconds 62 */ 63 @Override getTimeInSeconds()64 public float getTimeInSeconds() { 65 return getTime() * INVERSE_TIMER_RESOLUTION; 66 } 67 getTime()68 public long getTime() { 69 //return System.currentTimeMillis() - startTime; 70 return System.nanoTime() - startTime; 71 } 72 getResolution()73 public long getResolution() { 74 return TIMER_RESOLUTION; 75 } 76 getFrameRate()77 public float getFrameRate() { 78 return fps; 79 } 80 getTimePerFrame()81 public float getTimePerFrame() { 82 return tpf; 83 } 84 update()85 public void update() { 86 tpf = (getTime() - previousTime) * (1.0f / TIMER_RESOLUTION); 87 fps = 1.0f / tpf; 88 previousTime = getTime(); 89 } 90 reset()91 public void reset() { 92 //startTime = System.currentTimeMillis(); 93 startTime = System.nanoTime(); 94 previousTime = getTime(); 95 } 96 } 97