Clock.cpp
上传用户:sz83729876
上传日期:2013-03-07
资源大小:4140k
文件大小:4k
源码类别:

OpenGL

开发平台:

Windows_Unix

  1. #include "Clock.h"
  2. //-------------------------------------------------------
  3. //  Static variables
  4. //-------------------------------------------------------
  5. LARGE_INTEGER   Timer::m_llPerformanceFreq;
  6. LARGE_INTEGER   Timer::m_llBeginTime;
  7. LARGE_INTEGER   Timer::m_llLastTime;
  8. LARGE_INTEGER   Timer::m_llCurTime;
  9. float           Timer::m_fTimeScale;
  10. float           Timer::m_fBeginTime;
  11. float           Timer::m_fCurrentTime;
  12. float           Timer::m_fSecSinceLastFrame;
  13. float           Timer::m_fCounter;
  14. float           Timer::m_fFPS, Timer::m_fTempFPS;
  15. long            Timer::m_dwFrameCount;
  16. //-------------------------------------------------------
  17. //  Initialize()
  18. //-------------------------------------------------------
  19. //      Checks performance counter for it's
  20. //      frequency (if it exists) and resets the 
  21. //      counters.
  22. //
  23. bool Timer::Initialize()
  24. {
  25.     if ( QueryPerformanceFrequency( &m_llPerformanceFreq ) )
  26.     {
  27.         QueryPerformanceCounter( &m_llBeginTime );
  28.         m_llLastTime.QuadPart = m_llBeginTime.QuadPart;
  29.         m_llCurTime.QuadPart = m_llBeginTime.QuadPart;
  30.         m_fTimeScale = 1.0f / m_llPerformanceFreq.QuadPart;
  31.         m_fBeginTime          = m_llBeginTime.QuadPart * m_fTimeScale;
  32.         m_fCurrentTime        = m_llBeginTime.QuadPart * m_fTimeScale;
  33.         m_fSecSinceLastFrame  = 0.0f;
  34.         m_fCounter            = 0.0f;
  35.         m_fFPS                = 0.0f;
  36.         m_fTempFPS            = 0.0f;
  37.         m_dwFrameCount        = 0;
  38.         return true;
  39.     }
  40.     return false;
  41. }
  42. //-------------------------------------------------------
  43. //  GetTimer()
  44. //-------------------------------------------------------
  45. //      Retrieves one of the four timer values.
  46. //
  47. //      TIMER_APPSTART
  48. //          Returns the time when the app started.
  49. //
  50. //      TIMER_SINCEAPPSTART
  51. //          Returns the time elapsed since the 
  52. //          app started. 
  53. //
  54. //      TIMER_SINCELASTFRAME
  55. //          Returns the time it took the to render
  56. //          the last frame.
  57. //
  58. //      TIMER_ACTUAL
  59. //          Returns the *actual* time at the moment
  60. //          GetTimer() is called.
  61. //
  62. float Timer::GetTimer( TIMER_TYPE enumType )
  63. {
  64.     LARGE_INTEGER ct;
  65.     switch( enumType )
  66.     {
  67.         case TIMER_APPSTART:
  68.             return m_fBeginTime;
  69.         case TIMER_SINCEAPPSTART:
  70.             return m_fCurrentTime;
  71.         case TIMER_SINCELASTFRAME:
  72.             return m_fSecSinceLastFrame;
  73.         case TIMER_ACTUAL:
  74.             QueryPerformanceCounter( &ct );
  75.             return ( (ct.QuadPart-m_llBeginTime.QuadPart) * m_fTimeScale );
  76.     }
  77.     return -1.0f;
  78. }
  79. //-------------------------------------------------------
  80. //  OncePerFrameQuery()
  81. //-------------------------------------------------------
  82. //      Called once per frame to get the current
  83. //      time and calculate the fps and other things
  84. //      from that.
  85. //
  86. bool Timer::OncePerFrameQuery( HWND hWnd )
  87. {
  88.     m_llLastTime = m_llCurTime;
  89.     if (!QueryPerformanceCounter( &m_llCurTime ))
  90.         return false;
  91.     m_fCurrentTime       = (float)( (m_llCurTime.QuadPart-m_llBeginTime.QuadPart) * m_fTimeScale );
  92.     m_fSecSinceLastFrame = (float)( (m_llCurTime.QuadPart-m_llLastTime.QuadPart) * m_fTimeScale );
  93.     m_fFPS = 1.0f / m_fSecSinceLastFrame;
  94.     m_dwFrameCount++;
  95.     m_fCounter += m_fSecSinceLastFrame;
  96.     if ( m_fCounter>= 1.0f )
  97.     {
  98.         char ss[128];
  99.         sprintf( ss, "The Hollow   %1.2f fps   running %1.2f seconds", m_fFPS, m_fCurrentTime );
  100.         SetWindowText( hWnd, ss );
  101.         m_fCounter = 0.0f;
  102.     }
  103.     return true;
  104. }
  105. //-------------------------------------------------------
  106. //  OncePerFrameQuery()
  107. //-------------------------------------------------------
  108. //      Returns the current frames per second.
  109. //
  110. float Timer::GetFPS()
  111. {
  112.     return m_fFPS;
  113. }