Advertisement
Guest User

Untitled

a guest
Jun 30th, 2009
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.20 KB | None | 0 0
  1. /*****************************************************************
  2.  * CodeName: SceneFactory.Net
  3.  * Copyright: Virtual Media Systems, S.L. 2003
  4.  * Assembly: Core
  5.  * Type: Source Code (Class Library)
  6.  * Version: 0.1
  7.  * Description: Hi-Res Timer Library (Win32 Only)
  8.  *
  9.  * Revisions
  10.  * ------------------------------------------------
  11.  * [F] 20/10/2003, Jcl - Shaping the thing up
  12.  *   - Implemented Standard Timer using Kernel32 Performance Counters (Win32 Only)
  13.  * [F] 7/11/2003, Jcl
  14.  *   - Implemented SlideCounts, renamed CClass to .NET default
  15.  *   - Fixed a bug with timing offset (presumably)
  16.  *****************************************************************/
  17.  
  18. using System;
  19. using System.Runtime.InteropServices;
  20.  
  21. using SceneFactory.Core;
  22. using SceneFactory.Core.Utils;
  23.  
  24. namespace SceneFactory.Core.Utils
  25. {
  26.   public class Timer
  27.   {
  28.     [DllImport("kernel32", EntryPoint="QueryPerformanceCounter")]
  29.     protected static extern uint QueryPerformanceCounter(ref long t);
  30.  
  31.     [DllImport("kernel32", EntryPoint="QueryPerformanceFrequency")]
  32.     protected static extern uint QueryPerformanceFrequency(ref long t);
  33.  
  34.     public  const long TimerPrecision_Seconds = 1;
  35.     public  const long TimerPrecision_TenthSeconds = 10;
  36.     public  const long TimerPrecision_HundredthSeconds = 100;
  37.     public  const long TimerPrecision_Millisecond = 1000;
  38.     public  const long TimerPrecision_TenthMillisecond = 10000;
  39.     public  const long TimerPrecision_HundredthMillisecond = 100000;
  40.     public  const long TimerPrecision_MicroSecond = 1000000;
  41.     public  const long TimerPrecision_NanoSecond = 1000000000;
  42.  
  43.     protected long m_StartValue;
  44.     protected long m_ActualValue;
  45.     protected long m_CounterFrequency;
  46.     protected long m_Offset;
  47.     protected long m_OffsetPrecision;
  48.    
  49.     protected bool m_Initialized;
  50.  
  51.     public long Milliseconds { get { return GetMs(); } }
  52.  
  53.     public Timer()
  54.     {
  55.       Initialize(0);
  56.     }
  57.  
  58.     public Timer(long Offset)
  59.     {
  60.       Initialize(Offset);
  61.     }
  62.  
  63.     protected void Initialize(long Offset)
  64.     {
  65.       m_Initialized = false;
  66.       m_StartValue = 0;
  67.       m_ActualValue = 0;
  68.       m_Offset = Offset;
  69.       m_OffsetPrecision = TimerPrecision_Millisecond;
  70.       try
  71.       {
  72.         QueryPerformanceFrequency(ref m_CounterFrequency);
  73.         m_Initialized = true;
  74.       }
  75.       catch(System.EntryPointNotFoundException e)
  76.       {
  77.         SFLog.ExceptionLog(Lang.GetString("CantLoadTimerRoutine"),e);                
  78.       }
  79.     }
  80.    
  81.     public void Start()
  82.     {
  83.       if(!m_Initialized)
  84.         return;
  85.      
  86.       QueryPerformanceCounter(ref m_StartValue);
  87.     }
  88.  
  89.     public void ResetOffset()
  90.     {
  91.       m_Offset = 0;
  92.       m_OffsetPrecision = TimerPrecision_Millisecond;
  93.     }
  94.  
  95.     public void TimerOffset(long Offset)
  96.     {
  97.       if(!m_Initialized)
  98.         return;
  99.      
  100.       TimerOffset(Offset, m_OffsetPrecision);
  101.     }
  102.  
  103.     public void TimerOffset(long Offset, long Precision)
  104.     {
  105.       if(!m_Initialized)
  106.         return;
  107.  
  108.       if(Precision != m_OffsetPrecision)
  109.       {
  110.         // Convert Offset to requested precision before adding
  111.         m_Offset = m_Offset * Precision / m_OffsetPrecision;
  112.       }
  113.       m_OffsetPrecision = Precision;
  114.       m_Offset += Offset;
  115.     }
  116.  
  117.     public long GetPrecisionTimeCount(long Precision)
  118.     {
  119.       if(!m_Initialized)
  120.         return 0;
  121.      
  122.       QueryPerformanceCounter(ref m_ActualValue);
  123.       m_ActualValue -= m_StartValue;
  124.       return (m_ActualValue * Precision / m_CounterFrequency) + ((m_Offset * m_OffsetPrecision)/ Precision);
  125.     }
  126.  
  127.     public long GetSlidePosition(long StartValue, long EndValue, long StartMilliseconds, long EndMilliseconds)
  128.     {
  129.       long Time = GetMs();
  130.       if(EndMilliseconds-StartMilliseconds == 0)
  131.         return StartValue;
  132.       if(StartMilliseconds>EndMilliseconds)
  133.       {
  134.         long vSwap = EndMilliseconds;
  135.         EndMilliseconds = StartMilliseconds;
  136.         StartMilliseconds = vSwap;
  137.       }
  138.       if(Time<=StartMilliseconds)
  139.         return StartValue;
  140.       if(Time>=EndMilliseconds)
  141.         return EndValue;
  142.  
  143.       return StartValue + ((Time-StartMilliseconds)*(EndValue-StartValue)/(EndMilliseconds-StartMilliseconds));                                            
  144.     }
  145.  
  146.     public long GetCurrentFrame(long TotalFrames, long TotalMilliseconds)
  147.     {
  148.       if(!m_Initialized)
  149.         return 0;
  150.  
  151.       QueryPerformanceCounter(ref m_ActualValue);
  152.       m_ActualValue -= m_StartValue;
  153.       long t = (m_ActualValue * TimerPrecision_Millisecond / m_CounterFrequency) + ((m_Offset * m_OffsetPrecision)/ TimerPrecision_Millisecond);
  154.       return (TotalFrames * t)/TotalMilliseconds;
  155.     }
  156.    
  157.     public long GetMs()
  158.     {
  159.       if(!m_Initialized)
  160.         return 0;
  161.  
  162.       return GetPrecisionTimeCount(TimerPrecision_Millisecond);
  163.     }
  164.  
  165.     public long GetTimerTicks()
  166.     {
  167.       if(!m_Initialized)
  168.         return 0;
  169.  
  170.       QueryPerformanceCounter(ref m_ActualValue);
  171.       m_ActualValue -= m_StartValue;
  172.       return m_ActualValue;
  173.     }
  174.  
  175.     public long GetTicksPerSecond()
  176.     {
  177.       if(!m_Initialized)
  178.         return 0;
  179.  
  180.       return m_CounterFrequency/1000;
  181.     }
  182.   }
  183. }
  184.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement