TimerClass.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace EQ2ModelViewer
  8. {
  9. public class TimerClass
  10. {
  11. [DllImport("kernel32.dll", SetLastError = true)]
  12. private static extern bool QueryPerformanceFrequency(out long lpFrequency);
  13. [DllImport("kernel32.dll", SetLastError = true)]
  14. private static extern bool QueryPerformanceCounter(out long lpFrequency);
  15. private long m_Frequency = 0;
  16. private long m_StartTime = 0;
  17. private float m_TicksPerMS = 0;
  18. private float m_FrameTime = 0;
  19. public bool Initialize()
  20. {
  21. if (!QueryPerformanceFrequency(out m_Frequency))
  22. {
  23. Console.WriteLine("TimerClass: Initialize failed.");
  24. return false;
  25. }
  26. m_TicksPerMS = (float)(m_Frequency / 1000);
  27. QueryPerformanceCounter(out m_StartTime);
  28. return true;
  29. }
  30. public void Frame()
  31. {
  32. long CurrentTime;
  33. float TimeDif;
  34. QueryPerformanceCounter(out CurrentTime);
  35. TimeDif = (float)(CurrentTime - m_StartTime);
  36. m_FrameTime = TimeDif / m_TicksPerMS;
  37. m_StartTime = CurrentTime;
  38. }
  39. public float GetTime()
  40. {
  41. return m_FrameTime;
  42. }
  43. }
  44. }