Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef CLOCKDRIFTMGR_H
  7. #define CLOCKDRIFTMGR_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. class CClockDriftMgr
  12. {
  13. friend class CBaseClientState;
  14. public:
  15. CClockDriftMgr();
  16. // Is clock correction even enabled right now?
  17. static bool IsClockCorrectionEnabled();
  18. // Clear our state.
  19. void Clear();
  20. // This is called each time a server packet comes in. It is used to correlate
  21. // where the server is in time compared to us.
  22. void SetServerTick( int iServerTick );
  23. // Pass in the frametime you would use, and it will drift it towards the server clock.
  24. float AdjustFrameTime( float inputFrameTime );
  25. // Returns how many ticks ahead of the server the client is.
  26. float GetCurrentClockDifference() const;
  27. private:
  28. void ShowDebugInfo( float flAdjustment );
  29. // This scales the offsets so the average produced is equal to the
  30. // current average + flAmount. This way, as we add corrections,
  31. // we lower the average accordingly so we don't keep responding
  32. // as much as we need to after we'd adjusted it a couple times.
  33. void AdjustAverageDifferenceBy( float flAmountInSeconds );
  34. private:
  35. enum
  36. {
  37. // This controls how much it smoothes out the samples from the server.
  38. NUM_CLOCKDRIFT_SAMPLES=16
  39. };
  40. // This holds how many ticks the client is ahead each time we get a server tick.
  41. // We average these together to get our estimate of how far ahead we are.
  42. float m_ClockOffsets[NUM_CLOCKDRIFT_SAMPLES];
  43. int m_iCurClockOffset;
  44. int m_nServerTick; // Last-received tick from the server.
  45. int m_nClientTick; // The client's own tick counter (specifically, for interpolation during rendering).
  46. // The server may be on a slightly different tick and the client will drift towards it.
  47. };
  48. #endif // CLOCKDRIFTMGR_H