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

游戏

开发平台:

Visual C++

  1. /*****************************************************************************
  2. *                                                                             
  3. *   Timer.h                                                            
  4. *                                                                             
  5. *   Electrical Engineering Faculty - Software Lab                             
  6. *   Spring semester 1998                                                      
  7. *                                                                             
  8. *   Tanks game                                                                
  9. *                                                                             
  10. *   Module description: Implements the time calculation used to synch the 
  11. *                       local machine time with the host machine time.
  12. *                       
  13. *                                                                             
  14. *   Authors: Eran Yariv - 28484475                                           
  15. *            Moshe Zur  - 24070856                                           
  16. *                                                                            
  17. *                                                                            
  18. *   Date: 23/09/98                                                           
  19. *                                                                            
  20. ******************************************************************************/
  21. #ifndef TIMER_H
  22. #define TIMER_H
  23. #include "stdafx.h"
  24. /*  If VAN_JACOBSON is defined, the Van-Jacobon technique is used to calc the delta
  25.     between the server time (remote) and the client time (local).
  26.     (as in TCP round-trip delay estimation algorithms)
  27.     Otherwise, regular smoothed average is used to calc the delta
  28.     between the server time (remote) and the client time (local).
  29.     Refer to Dr. Dobbs #283, March 98, pp. 127-130 for more details.
  30. */
  31. class CTimer
  32. {
  33. public:
  34.     CTimer              (BOOL bForceLowResTimers = FALSE);   // If bForceLowResTimers, GetTickCount is used
  35.     virtual             ~CTimer () {}
  36.     DWORD               GetRemoteTime ();
  37.     DWORD               GetLocalTime (DWORD dwRemoteTime);
  38.     DWORD               GetLocalTime ();
  39. protected:
  40.     virtual DWORD       UpdateDelta (DWORD) = 0;
  41.     LONG                m_lLastDelta;       // Delta = LocalTime - RemoteTime
  42.     BOOL                m_bFirstSample;
  43. private:
  44.     static DWORD CALLBACK   SampleLocalTimeWithPerfCounters();
  45.     static LONGLONG     m_llFreq;
  46.     DWORD               (CALLBACK* m_pActualSampleFunction) ();
  47. };
  48. class CVanJacobsonTimer : public CTimer
  49. {
  50. public:
  51.     CVanJacobsonTimer   (BOOL bForceLowResTimers = FALSE);   // If bForceLowResTimers, GetTickCount is used
  52.     virtual             ~CVanJacobsonTimer() {}
  53. private:
  54.     virtual DWORD       UpdateDelta (DWORD);
  55.     #define VJ_g        double(0.125)
  56.     #define VJ_h        double(0.250)
  57.     #define VJ_f        double(4.000)
  58.     double              m_dPrevSRTT;        // Previous Smoothed Round-Trip Time
  59.     double              m_dPrevSDEV;        // Previous standard deviation
  60. };
  61. class CSmoothedAverageTimer : public CTimer
  62. {
  63. public:
  64.     CSmoothedAverageTimer (BOOL bForceLowResTimers = FALSE);   // If bForceLowResTimers, GetTickCount is used
  65.     virtual             ~CSmoothedAverageTimer() {}
  66. private:
  67.     virtual DWORD       UpdateDelta (DWORD);
  68.     #define ALPHA           double(0.5)
  69.     #define ONE_MINUS_ALPHA double (1.0 - ALPHA)
  70.     double                          m_dPrevDelta;
  71. };
  72. // Inline sections:
  73. #include <Timer.inl>
  74. // The TIMER_CLASS macro is used to specify the type of timer in use throughout
  75. // the program. It can be either CSmoothedAverageTimer or CVanJacobsonTimer.
  76. #define TIMER_CLASS         CSmoothedAverageTimer     
  77. #endif