Advertisement
Guest User

GameTime.java

a guest
Oct 5th, 2012
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 KB | None | 0 0
  1. import org.lwjgl.Sys;
  2.  
  3.  
  4. public class GameTime {
  5.    
  6.     /**
  7.      * time at last frame
  8.      */
  9.     private static long lastFrame;
  10.    
  11.     /**
  12.      * frames per second
  13.      */
  14.     private static int fps, frames;
  15.    
  16.     /**
  17.      * last fps time
  18.      */
  19.     private static long lastFPS;
  20.    
  21.     private static int delta;
  22.    
  23.     /**
  24.      * initialize delta time and frame time
  25.      */
  26.     public static void init() {
  27.         updateDelta();
  28.         lastFPS = getTime();
  29.     }
  30.    
  31.     /**
  32.      *  update delta timer and FPS counter. Call once per loop.
  33.      */
  34.     public static void update() {
  35.         updateDelta();
  36.         updateFPS();
  37.     }
  38.    
  39.     /**
  40.      * returns current FPS value
  41.      * @return: fps
  42.      */
  43.     public static int getFPS() {
  44.         return fps;
  45.     }
  46.    
  47.    
  48.     /**
  49.      * Get the accurate system time
  50.      * @return: Zeit
  51.      */
  52.     public static long getTime() {
  53.         return (Sys.getTime() * 1000) / Sys.getTimerResolution();
  54.     }
  55.    
  56.    
  57.     /**
  58.      * return the delta value for this frame
  59.      * @return: delta
  60.      */
  61.     public static int getDelta() {
  62.         return delta;
  63.     }
  64.    
  65.    
  66.     /**
  67.      * Calculate how many milliseconds have passed since last frame and update delta
  68.      */
  69.     private static void updateDelta() {
  70.         long time = getTime();
  71.         delta = (int) (time - lastFrame);
  72.         lastFrame = time;
  73.     }
  74.    
  75.    
  76.     /**
  77.      * increment or reset FPS counter
  78.      */
  79.     private static void updateFPS() {
  80.         if (getTime() - lastFPS > 1000) {
  81.             fps = frames;
  82.             frames = 0;
  83.             lastFPS += 1000;
  84.         }
  85.         frames++;
  86.     }
  87.    
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement