2 * Copyright (c) 2003-2009 jMonkeyEngine
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
33 package com.jme.util.lwjgl;
35 import java.util.logging.Logger;
37 import com.jme.math.FastMath;
38 import com.jme.util.Timer;
42 * <code>Timer</code> handles the system's time related functionality. This
43 * allows the calculation of the framerate. To keep the framerate calculation
44 * accurate, a call to update each frame is required. <code>Timer</code> is a
45 * singleton object and must be created via the <code>getTimer</code> method.
48 * @version $Id: LWJGLTimer.java,v 1.21 2007/09/22 16:46:35 irrisor Exp $
50 public class LWJGLTimer extends Timer {
51 private static final Logger logger = Logger.getLogger(LWJGLTimer.class
54 private long lastFrameDiff;
56 //frame rate parameters.
59 private float lastTPF, lastFPS;
61 public static int TIMER_SMOOTHNESS = 32;
65 private int smoothIndex;
67 private final static long timerRez = Sys.getTimerResolution();
68 private final static float invTimerRez = ( 1f / timerRez );
69 private static float invTimerRezSmooth;
71 private long startTime;
73 private boolean allSmooth = false;
76 * Constructor builds a <code>Timer</code> object. All values will be
77 * initialized to it's default values.
82 //print timer resolution info
83 logger.info("Timer resolution: " + timerRez + " ticks per second");
91 // init to -1 to indicate this is a new timer.
94 startTime = Sys.getTime();
96 tpf = new long[TIMER_SMOOTHNESS];
97 smoothIndex = TIMER_SMOOTHNESS - 1;
98 invTimerRezSmooth = ( 1f / (timerRez * TIMER_SMOOTHNESS));
100 // set tpf... -1 values will not be used for calculating the average in update()
101 for ( int i = tpf.length; --i >= 0; ) {
107 * @see com.jme.util.Timer#getTime()
109 public long getTime() {
110 return Sys.getTime() - startTime;
114 * @see com.jme.util.Timer#getResolution()
116 public long getResolution() {
121 * <code>getFrameRate</code> returns the current frame rate since the last
122 * call to <code>update</code>.
124 * @return the current frame rate.
126 public float getFrameRate() {
130 public float getTimePerFrame() {
135 * <code>update</code> recalulates the frame rate based on the previous
136 * call to update. It is assumed that update is called each frame.
138 public void update() {
139 long newTime = Sys.getTime();
140 long oldTime = this.oldTime;
141 this.oldTime = newTime;
142 if ( oldTime == -1 ) {
143 // For the first frame use 60 fps. This value will not be counted in further averages.
144 // This is done so initialization code between creating the timer and the first
145 // frame is not counted as a single frame on it's own.
147 lastFPS = 1f / lastTPF;
151 long frameDiff = newTime - oldTime;
152 long lastFrameDiff = this.lastFrameDiff;
153 if ( lastFrameDiff > 0 && frameDiff > lastFrameDiff *100 ) {
154 frameDiff = lastFrameDiff *100;
156 this.lastFrameDiff = frameDiff;
157 tpf[smoothIndex] = frameDiff;
159 if ( smoothIndex < 0 ) {
160 smoothIndex = tpf.length - 1;
166 for ( int i = tpf.length; --i >= 0; ) {
167 if ( tpf[i] != -1 ) {
172 if (smoothCount == tpf.length)
174 lastTPF *= ( invTimerRez / smoothCount );
176 for ( int i = tpf.length; --i >= 0; ) {
177 if ( tpf[i] != -1 ) {
181 lastTPF *= invTimerRezSmooth;
183 if ( lastTPF < FastMath.FLT_EPSILON ) {
184 lastTPF = FastMath.FLT_EPSILON;
187 lastFPS = 1f / lastTPF;
191 * <code>toString</code> returns the string representation of this timer
192 * in the format: <br>
194 * jme.utility.Timer@1db699b <br>
198 * @return the string representation of this object.
200 public String toString() {
201 String string = super.toString();
202 string += "\nTime: " + oldTime;
203 string += "\nFPS: " + getFrameRate();