Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.68 KB | None | 0 0
  1. package engine.util;
  2.  
  3. import java.util.HashMap;
  4.  
  5. /*
  6.  * Copyright (c) 2002-2012 LWJGL Project
  7.  * All rights reserved.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions are
  11.  * met:
  12.  *
  13.  * * Redistributions of source code must retain the above copyright
  14.  *   notice, this list of conditions and the following disclaimer.
  15.  *
  16.  * * Redistributions in binary form must reproduce the above copyright
  17.  *   notice, this list of conditions and the following disclaimer in the
  18.  *   documentation and/or other materials provided with the distribution.
  19.  *
  20.  * * Neither the name of 'LWJGL' nor the names of
  21.  *   its contributors may be used to endorse or promote products derived
  22.  *   from this software without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  26.  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  27.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  30.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  31.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.  */
  36.  
  37. /**
  38. * A highly accurate sync method that continually adapts to the system
  39. * it runs on to provide reliable results.
  40. *
  41. * @author Riven
  42. * @author kappaOne
  43. * @author orange451
  44. */
  45. public class Sync {
  46.  
  47.     /** number of nano seconds in a second */
  48.     private static final long NANOS_IN_SECOND = 1000L * 1000L * 1000L;
  49.  
  50.     /** The time to sleep/yield until the next frame */
  51.     private static HashMap<Thread,Long> nextFrameMap;
  52.    
  53.     /** whether the initialisation code has run */
  54.     private static HashMap<Thread,Boolean> initialisedMap;
  55.    
  56.     /** for calculating the averages the previous sleep/yield times are stored */
  57.     private static HashMap<Thread,RunningAvg> sleepDurationsMap;
  58.     private static HashMap<Thread,RunningAvg> yieldDurationsMap;
  59.    
  60.     /** initialise above fields */
  61.     static {
  62.         nextFrameMap = new HashMap<Thread,Long>();
  63.         initialisedMap = new HashMap<Thread,Boolean>();
  64.         sleepDurationsMap = new HashMap<Thread,RunningAvg>();
  65.         yieldDurationsMap = new HashMap<Thread,RunningAvg>();
  66.     }
  67.    
  68.    
  69.     /**
  70.      * An accurate sync method that will attempt to run at a constant frame rate.
  71.      * It should be called once every frame.
  72.      *
  73.      * @param fps - the desired frame rate, in frames per second
  74.      */
  75.     public static void sync(int fps) {
  76.         Thread c = checkThread();
  77.         if (fps <= 0) return;
  78.         if (!initialisedMap.get(c)) initialise();
  79.        
  80.         Long nextFrame = nextFrameMap.get(c);
  81.         RunningAvg sleepDurations = sleepDurationsMap.get(c);
  82.         RunningAvg yieldDurations = yieldDurationsMap.get(c);
  83.        
  84.         try {
  85.             // sleep until the average sleep time is greater than the time remaining till nextFrame
  86.             for (long t0 = getTime(), t1; (nextFrame - t0) > sleepDurations.avg(); t0 = t1) {
  87.                 Thread.sleep(1);
  88.                 sleepDurations.add((t1 = getTime()) - t0); // update average sleep time
  89.             }
  90.    
  91.             // slowly dampen sleep average if too high to avoid yielding too much
  92.             sleepDurations.dampenForLowResTicker();
  93.    
  94.             // yield until the average yield time is greater than the time remaining till nextFrame
  95.             for (long t0 = getTime(), t1; (nextFrame - t0) > yieldDurations.avg(); t0 = t1) {
  96.                 Thread.yield();
  97.                 yieldDurations.add((t1 = getTime()) - t0); // update average yield time
  98.             }
  99.         } catch (InterruptedException e) {
  100.             //
  101.         }
  102.        
  103.         // schedule next frame, drop frame(s) if already too late for next frame
  104.         nextFrameMap.put(c, Math.max(nextFrame + NANOS_IN_SECOND / fps, getTime()));
  105.     }
  106.    
  107.     private static Thread checkThread() {
  108.         Thread c = Thread.currentThread();
  109.  
  110.         if ( !nextFrameMap.containsKey(c) )
  111.             nextFrameMap.put(c, (long) 0);
  112.        
  113.         if ( !initialisedMap.containsKey(c) )
  114.             initialisedMap.put(c, false);
  115.        
  116.         if ( !sleepDurationsMap.containsKey(c) )
  117.             sleepDurationsMap.put(c, new RunningAvg(10));
  118.        
  119.         if ( !yieldDurationsMap.containsKey(c) )
  120.             yieldDurationsMap.put(c, new RunningAvg(10));
  121.  
  122.         return c;
  123.     }
  124.  
  125.     /**
  126.      * This method will initialise the sync method by setting initial
  127.      * values for sleepDurations/yieldDurations and nextFrame.
  128.      *
  129.      * If running on windows it will start the sleep timer fix.
  130.      */
  131.     private static void initialise() {
  132.         Thread c = checkThread();
  133.        
  134.         initialisedMap.put(c,true);
  135.        
  136.         sleepDurationsMap.get(c).init(1000 * 1000);
  137.         yieldDurationsMap.get(c).init((int) (-(getTime() - getTime()) * 1.333));
  138.        
  139.         nextFrameMap.put(c, getTime());
  140.        
  141.         String osName = System.getProperty("os.name");
  142.        
  143.         if (osName.startsWith("Win")) {
  144.             // On windows the sleep functions can be highly inaccurate by
  145.             // over 10ms making in unusable. However it can be forced to
  146.             // be a bit more accurate by running a separate sleeping daemon
  147.             // thread.
  148.             Thread timerAccuracyThread = new Thread(new Runnable() {
  149.                 public void run() {
  150.                     try {
  151.                         Thread.sleep(Long.MAX_VALUE);
  152.                     } catch (Exception e) {}
  153.                 }
  154.             });
  155.            
  156.             timerAccuracyThread.setName("LWJGL Timer");
  157.             timerAccuracyThread.setDaemon(true);
  158.             timerAccuracyThread.start();
  159.         }
  160.     }
  161.  
  162.     /**
  163.      * Get the system time in nano seconds
  164.      *
  165.      * @return will return the current time in nano's
  166.      */
  167.     private static long getTime() {
  168.         return System.nanoTime();
  169.     }
  170.  
  171.     private static class RunningAvg {
  172.         private final long[] slots;
  173.         private int offset;
  174.        
  175.         private static final long DAMPEN_THRESHOLD = 10 * 1000L * 1000L; // 10ms in nanoseconds
  176.         private static final float DAMPEN_FACTOR = 0.9f; // don't change: 0.9f is exactly right!
  177.  
  178.         public RunningAvg(int slotCount) {
  179.             this.slots = new long[slotCount];
  180.             this.offset = 0;
  181.         }
  182.  
  183.         public void init(long value) {
  184.             while (this.offset < this.slots.length) {
  185.                 this.slots[this.offset++] = value;
  186.             }
  187.         }
  188.  
  189.         public void add(long value) {
  190.             this.slots[this.offset++ % this.slots.length] = value;
  191.             this.offset %= this.slots.length;
  192.         }
  193.  
  194.         public long avg() {
  195.             long sum = 0;
  196.             for (int i = 0; i < this.slots.length; i++) {
  197.                 sum += this.slots[i];
  198.             }
  199.             return sum / this.slots.length;
  200.         }
  201.        
  202.         public void dampenForLowResTicker() {
  203.             if (this.avg() > DAMPEN_THRESHOLD) {
  204.                 for (int i = 0; i < this.slots.length; i++) {
  205.                     this.slots[i] *= DAMPEN_FACTOR;
  206.                 }
  207.             }
  208.         }
  209.     }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement