1 /* 2 * Copyright (c) 2009-2010 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.lwjgl; 34 35 import com.jme3.system.Timer; 36 import java.util.logging.Level; 37 import java.util.logging.Logger; 38 import org.lwjgl.Sys; 39 40 /** 41 * <code>Timer</code> handles the system's time related functionality. This 42 * allows the calculation of the framerate. To keep the framerate calculation 43 * accurate, a call to update each frame is required. <code>Timer</code> is a 44 * singleton object and must be created via the <code>getTimer</code> method. 45 * 46 * @author Mark Powell 47 * @version $Id: LWJGLTimer.java,v 1.21 2007/09/22 16:46:35 irrisor Exp $ 48 */ 49 public class LwjglTimer extends Timer { 50 private static final Logger logger = Logger.getLogger(LwjglTimer.class 51 .getName()); 52 53 //frame rate parameters. 54 private long oldTime; 55 private long startTime; 56 57 private float lastTPF, lastFPS; 58 59 private final static long LWJGL_TIMER_RES = Sys.getTimerResolution(); 60 private final static float INV_LWJGL_TIMER_RES = ( 1f / LWJGL_TIMER_RES ); 61 62 public final static long LWJGL_TIME_TO_NANOS = (1000000000 / LWJGL_TIMER_RES); 63 64 /** 65 * Constructor builds a <code>Timer</code> object. All values will be 66 * initialized to it's default values. 67 */ LwjglTimer()68 public LwjglTimer() { 69 reset(); 70 logger.log(Level.INFO, "Timer resolution: {0} ticks per second", LWJGL_TIMER_RES); 71 } 72 reset()73 public void reset() { 74 startTime = Sys.getTime(); 75 oldTime = getTime(); 76 } 77 78 @Override getTimeInSeconds()79 public float getTimeInSeconds() { 80 return getTime() * INV_LWJGL_TIMER_RES; 81 } 82 83 /** 84 * @see com.jme.util.Timer#getTime() 85 */ getTime()86 public long getTime() { 87 return Sys.getTime() - startTime; 88 } 89 90 /** 91 * @see com.jme.util.Timer#getResolution() 92 */ getResolution()93 public long getResolution() { 94 return LWJGL_TIMER_RES; 95 } 96 97 /** 98 * <code>getFrameRate</code> returns the current frame rate since the last 99 * call to <code>update</code>. 100 * 101 * @return the current frame rate. 102 */ getFrameRate()103 public float getFrameRate() { 104 return lastFPS; 105 } 106 getTimePerFrame()107 public float getTimePerFrame() { 108 return lastTPF; 109 } 110 111 /** 112 * <code>update</code> recalulates the frame rate based on the previous 113 * call to update. It is assumed that update is called each frame. 114 */ update()115 public void update() { 116 long curTime = getTime(); 117 lastTPF = (curTime - oldTime) * (1.0f / LWJGL_TIMER_RES); 118 lastFPS = 1.0f / lastTPF; 119 oldTime = curTime; 120 } 121 122 /** 123 * <code>toString</code> returns the string representation of this timer 124 * in the format: <br> 125 * <br> 126 * jme.utility.Timer@1db699b <br> 127 * Time: {LONG} <br> 128 * FPS: {LONG} <br> 129 * 130 * @return the string representation of this object. 131 */ 132 @Override toString()133 public String toString() { 134 String string = super.toString(); 135 string += "\nTime: " + oldTime; 136 string += "\nFPS: " + getFrameRate(); 137 return string; 138 } 139 }