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.

198 lines
6.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #if !defined( CLIENTSTATS_H )
  9. #define CLIENTSTATS_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "interface.h"
  14. #include <limits.h>
  15. #include "tier0/dbg.h"
  16. #define INTERFACEVERSION_CLIENTSTATS "ClientStats004"
  17. //-----------------------------------------------------------------------------
  18. // An interface used to help the client stats implementation tell time
  19. //-----------------------------------------------------------------------------
  20. struct IClientStatsTime
  21. {
  22. virtual float GetTime() = 0;
  23. };
  24. //-----------------------------------------------------------------------------
  25. // Allows clients to draw their own stats text, will be passed by the
  26. // engine into DisplayStats of the IClientStats interface.
  27. //-----------------------------------------------------------------------------
  28. struct IClientStatsTextDisplay
  29. {
  30. // Draws the stats
  31. virtual void DrawStatsText( PRINTF_FORMAT_STRING const char *fmt, ... ) = 0;
  32. virtual void SetDrawColor( unsigned char r, unsigned char g, unsigned char b ) = 0;
  33. // Sets a color based on a value and its max acceptable limit
  34. virtual void SetDrawColorFromStatValues( float limit, float value ) = 0;
  35. };
  36. //-----------------------------------------------------------------------------
  37. // This will exist as a singleton within the client DLL and will be hooked into
  38. // the engine to allow clients to render their own stats.
  39. //-----------------------------------------------------------------------------
  40. abstract_class IClientStats
  41. {
  42. public:
  43. // This is called at startup to tell the stats about time
  44. virtual void Init( IClientStatsTime* pTime ) = 0;
  45. // These methods are called at the beginning and the end of each run
  46. virtual void BeginRun() = 0;
  47. virtual void EndRun() = 0;
  48. // These methods are called at the beginning and the end of each frame
  49. virtual void BeginFrame() = 0;
  50. virtual void EndFrame() = 0;
  51. // ---------------------------------------------------------------
  52. // All this stuff is used to prop stats for gathering r_speeds data during timedemo.
  53. // ---------------------------------------------------------------
  54. virtual int GetNumTimesStats( void ) const = 0;
  55. // returns timed stats
  56. virtual double TimedStatInFrame( int statID ) const = 0;
  57. virtual double TotalTimedStat( int statID ) const = 0;
  58. };
  59. //-----------------------------------------------------------------------------
  60. // This is a templatized implementation which can be instantiated anywhere
  61. // Note that you still have to install it and display it though.
  62. //-----------------------------------------------------------------------------
  63. template <int timedStatCount, int countedStatCount>
  64. abstract_class CBaseClientStats : public IClientStats
  65. {
  66. public:
  67. void Init( IClientStatsTime* pTime );
  68. void BeginRun();
  69. void EndRun();
  70. void BeginFrame();
  71. void EndFrame();
  72. // Timed stat gathering
  73. void BeginTimedStat( int stat );
  74. void EndTimedStat( int stat );
  75. // ---------------------------------------------------------------
  76. // All this stuff is used to prop stats for gathering r_speeds data during timedemo.
  77. // ---------------------------------------------------------------
  78. // returns timed stats
  79. double TimedStatInFrame( int statID ) const
  80. {
  81. Assert( statID >= 0 && statID < timedStatCount );
  82. Assert( m_StatFrameTime[statID] >= 0.0 );
  83. return m_StatFrameTime[statID];
  84. }
  85. double TotalTimedStat( int statID ) const
  86. {
  87. Assert( statID >= 0 && statID < timedStatCount );
  88. return m_TotalStatTime[statID];
  89. }
  90. virtual const char *GetCountedStatName( int statID ) const = 0;
  91. virtual const char *GetTimedStatName( int statID ) const = 0;
  92. protected:
  93. // Timed statistics
  94. double m_StatFrameTime[timedStatCount];
  95. double m_StatStartTime[timedStatCount];
  96. double m_TotalStatTime[timedStatCount];
  97. private:
  98. IClientStatsTime* m_pTime;
  99. };
  100. //-----------------------------------------------------------------------------
  101. // Initializes client stats
  102. //-----------------------------------------------------------------------------
  103. template <int timedStatCount, int countedStatCount>
  104. void CBaseClientStats<timedStatCount, countedStatCount>::Init( IClientStatsTime* pTime )
  105. {
  106. Assert( pTime );
  107. m_pTime = pTime;
  108. }
  109. //-----------------------------------------------------------------------------
  110. // These methods are called at the beginning and the end of each run
  111. //-----------------------------------------------------------------------------
  112. template <int timedStatCount, int countedStatCount>
  113. void CBaseClientStats<timedStatCount, countedStatCount>::BeginRun()
  114. {
  115. int i;
  116. for (i = 0; i < timedStatCount; ++i)
  117. m_TotalStatTime[i] = 0.0;
  118. }
  119. template <int timedStatCount, int countedStatCount>
  120. void CBaseClientStats<timedStatCount, countedStatCount>::EndRun()
  121. {
  122. }
  123. //-----------------------------------------------------------------------------
  124. // These methods are called at the beginning and the end of each frame
  125. //-----------------------------------------------------------------------------
  126. template <int timedStatCount, int countedStatCount>
  127. void CBaseClientStats<timedStatCount, countedStatCount>::BeginFrame()
  128. {
  129. int i;
  130. for (i = 0; i < timedStatCount; ++i)
  131. m_StatFrameTime[i] = 0.0;
  132. }
  133. template <int timedStatCount, int countedStatCount>
  134. void CBaseClientStats<timedStatCount, countedStatCount>::EndFrame()
  135. {
  136. int i;
  137. for (i = 0; i < timedStatCount; ++i)
  138. m_TotalStatTime[i] += m_StatFrameTime[i];
  139. }
  140. //-----------------------------------------------------------------------------
  141. // Inlined stat gathering methods
  142. //-----------------------------------------------------------------------------
  143. template <int timedStatCount, int countedStatCount>
  144. void CBaseClientStats<timedStatCount, countedStatCount>::BeginTimedStat( int stat )
  145. {
  146. if (m_pTime)
  147. m_StatStartTime[stat] = m_pTime->GetTime();
  148. }
  149. template <int timedStatCount, int countedStatCount>
  150. void CBaseClientStats<timedStatCount, countedStatCount>::EndTimedStat( int stat )
  151. {
  152. if (m_pTime)
  153. m_StatFrameTime[stat] += m_pTime->GetTime() - m_StatStartTime[stat];
  154. }
  155. #endif // CLIENTSTATS_H