Timer.cpp
上传用户:royluo
上传日期:2007-01-05
资源大小:1584k
文件大小:5k
源码类别:

游戏

开发平台:

Visual C++

  1. /*****************************************************************************
  2. *                                                                             
  3. *   Timer.cpp                                                            
  4. *                                                                             
  5. *   Electrical Engineering Faculty - Software Lab                             
  6. *   Spring semester 1998                                                      
  7. *                                                                             
  8. *   Tanks game                                                                
  9. *                                                                             
  10. *   Module description: Calculates the time delta between the local time and 
  11. *                       the host machine local time.
  12. *                       
  13. *                                                                             
  14. *   Authors: Eran Yariv - 28484475                                           
  15. *            Moshe Zur  - 24070856                                           
  16. *                                                                            
  17. *                                                                            
  18. *   Date: 23/09/98                                                           
  19. *                                                                            
  20. ******************************************************************************/
  21. #include <stdafx.h>
  22. #include <Timer.h>
  23. #include <math.h>
  24. /****************** CTimer **********************/
  25. CTimer::CTimer (BOOL bForceLowResTimers) : m_bFirstSample (TRUE)
  26. {
  27.     LARGE_INTEGER   liFreq;
  28.     if (!bForceLowResTimers && QueryPerformanceFrequency( &liFreq ))
  29.     {   // User didn't force us to use low resoultion timer and 
  30.         // the high res timer is supported by the system
  31.         ASSERT (liFreq.QuadPart > 0);
  32.         m_llFreq = liFreq.QuadPart / LONGLONG (1000);
  33.         m_pActualSampleFunction = this->SampleLocalTimeWithPerfCounters;
  34.     }
  35.     else
  36.     {
  37.         m_pActualSampleFunction = GetTickCount;
  38.     }
  39. }
  40. DWORD CALLBACK
  41. CTimer::SampleLocalTimeWithPerfCounters()
  42. {
  43.     LARGE_INTEGER li;    
  44.     QueryPerformanceCounter (&li);
  45.     return DWORD (li.QuadPart / m_llFreq);
  46. }
  47. LONGLONG CTimer::m_llFreq;
  48. /****************** CVanJacobsonTimer **********************/
  49. /*------------------------------------------------------------------------------
  50.   Function: UpdateDelta
  51.   Purpose:  Updates the delta between the local timer (on this machine) and the 
  52.             host machine timer, using the Van-Jacobson algo. (used in TCP protocol)
  53.             based on former samples. This algo. gives more damped results than the
  54.             smoothed average algo..
  55.   Input:    dwRemoteTime - a new delta sample calculated from last message.
  56.   Output:   Returns last sample of local time.
  57.   Remarks:  
  58. ------------------------------------------------------------------------------*/
  59. DWORD
  60. CVanJacobsonTimer::UpdateDelta (DWORD dwRemoteTime)
  61. {
  62.     DWORD dwCurLocalTime = GetLocalTime ();
  63.     LONG lCurDelta = LONG(dwCurLocalTime) - LONG (dwRemoteTime);
  64.     if (m_bFirstSample)
  65.     {
  66.         m_lLastDelta = lCurDelta;
  67.         m_dPrevSRTT = double(lCurDelta);
  68.         m_dPrevSDEV = 0.0;
  69.     }
  70.     else
  71.     {
  72.         double dSRTT = (1.0 - VJ_g) * double(m_dPrevSRTT) + VJ_g * double(lCurDelta);
  73.         double dERR  = double(lCurDelta) - double(m_dPrevSRTT);
  74.         double dSDEV = (1.0 - VJ_h) * m_dPrevSDEV + VJ_h * fabs(dERR);
  75.         double dRTO  = dSRTT + VJ_f * dSDEV;
  76.         m_lLastDelta = LONG(dRTO);
  77.         // Update previous values:
  78.         m_dPrevSRTT = dSRTT;
  79.         m_dPrevSDEV = dSDEV;
  80.     }
  81.     return dwCurLocalTime;   // Return sampled local time
  82. }
  83. /****************** CSmoothedAverageTimer **********************/
  84. CSmoothedAverageTimer::CSmoothedAverageTimer  (BOOL bForceLowResTimers) :
  85.     CTimer (bForceLowResTimers),
  86.     m_dPrevDelta (0.0)
  87. {}
  88. /*------------------------------------------------------------------------------
  89.   Function: UpdateDelta
  90.   Purpose:  Updates the delta between the local timer (on this machine) and the 
  91.             host machine timer, using the smoothed average algo..
  92.   Input:    dwRemoteTime - a new delta sample calculated from last message.
  93.   Output:   Returns last sample of local time.
  94.   Remarks:  
  95. ------------------------------------------------------------------------------*/
  96. DWORD
  97. CSmoothedAverageTimer::UpdateDelta (DWORD dwRemoteTime)
  98. // Returns last sample of local time, updates m_lLastDelta 
  99. {
  100.     DWORD dwCurLocalTime = GetLocalTime ();
  101.     LONG lCurDelta = LONG(dwCurLocalTime) - LONG (dwRemoteTime);
  102.     if (!m_bFirstSample)
  103.     {   // Not first time we get a sample
  104.         m_dPrevDelta = ALPHA * m_dPrevDelta + ONE_MINUS_ALPHA * double(lCurDelta);
  105.     }
  106.     else 
  107.     {   // First time we get a sample
  108.         m_bFirstSample = FALSE; // Turn it off
  109.         m_dPrevDelta = lCurDelta;
  110.     }
  111.     m_lLastDelta = LONG (m_dPrevDelta);
  112.     return dwCurLocalTime;   // Return sampled local time
  113. }