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.

150 lines
3.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "toolutils/basestatusbar.h"
  7. #include "toolutils/ConsolePage.h"
  8. #include "vgui_controls/Label.h"
  9. #include "movieobjects/dmeclip.h"
  10. #include "tier1/KeyValues.h"
  11. #include "vgui/IVGui.h"
  12. #include "toolutils/enginetools_int.h"
  13. #include "toolframework/ienginetool.h"
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include "tier0/memdbgon.h"
  16. using namespace vgui;
  17. //-----------------------------------------------------------------------------
  18. // Purpose:
  19. //-----------------------------------------------------------------------------
  20. CBaseStatusBar::CBaseStatusBar( vgui::Panel *parent, char const *panelName )
  21. : BaseClass( parent, panelName ),
  22. m_flLastFPSSnapShot( -1.0f )
  23. {
  24. SetVisible( true );
  25. m_pConsole = new CConsolePage( this, true );
  26. m_pLabel = new Label( this, "Console", "#BxConsole" );
  27. m_pMemory = new Label( this, "Memory", "" );
  28. m_pFPS = new Label( this, "FPS", "" );
  29. m_pGameTime = new Label( this, "GameTime", "" );
  30. MakePopup( false );
  31. UpdateMemoryUsage( 9.999 );
  32. }
  33. //-----------------------------------------------------------------------------
  34. // Purpose: Forces console to take up full area except right edge
  35. // Input : -
  36. //-----------------------------------------------------------------------------
  37. void CBaseStatusBar::PerformLayout()
  38. {
  39. BaseClass::PerformLayout();
  40. int w, h;
  41. GetSize( w, h );
  42. int oldw = w;
  43. w *= 0.45f;
  44. int x = 8;
  45. int cw, ch;
  46. m_pLabel->GetContentSize( cw, ch );
  47. m_pLabel->SetBounds( x, 4, cw, h - 8 );
  48. x += cw + 4;
  49. int consoleWide = w - x - 8;
  50. m_pConsole->SetBounds( x, 2, consoleWide, h - 4 );
  51. x += consoleWide + 4;
  52. int infoW = 85;
  53. int rightx = oldw - infoW - 10;
  54. m_pFPS->SetBounds( rightx, 2, infoW - 2 - 10, h - 8 );
  55. rightx -= infoW;
  56. m_pGameTime->SetBounds( rightx, 2, infoW - 2, h - 8 );
  57. rightx -= infoW;
  58. m_pMemory->SetBounds( rightx, 2, infoW - 2, h - 8 );
  59. }
  60. void CBaseStatusBar::UpdateMemoryUsage( float mbUsed )
  61. {
  62. char mem[ 256 ];
  63. Q_snprintf( mem, sizeof( mem ), "[mem: %.2f Mb]", mbUsed );
  64. m_pMemory->SetText( mem );
  65. }
  66. //-----------------------------------------------------------------------------
  67. // Purpose: Message map
  68. //-----------------------------------------------------------------------------
  69. void CBaseStatusBar::ApplySchemeSettings(IScheme *pScheme)
  70. {
  71. BaseClass::ApplySchemeSettings(pScheme);
  72. // get the borders we need
  73. SetBorder(pScheme->GetBorder("ButtonBorder"));
  74. // get the background color
  75. SetBgColor(pScheme->GetColor( "StatusBar.BgColor", GetBgColor() ));
  76. m_pLabel->SetFont( pScheme->GetFont( "DefaultVerySmall" ) );
  77. m_pMemory->SetFont( pScheme->GetFont( "DefaultVerySmall" ) );
  78. m_pFPS->SetFont( pScheme->GetFont( "DefaultVerySmall" ) );
  79. m_pGameTime->SetFont( pScheme->GetFont( "DefaultVerySmall" ) );
  80. }
  81. static float GetMemoryUsage();
  82. void CBaseStatusBar::OnThink()
  83. {
  84. BaseClass::OnThink();
  85. float curtime = enginetools->GetRealTime();
  86. char gt[ 32 ];
  87. Q_snprintf( gt, sizeof( gt ), "[game: %.3f]", enginetools->ServerTime() );
  88. m_pGameTime->SetText( gt );
  89. float elapsed = curtime - m_flLastFPSSnapShot;
  90. if ( elapsed < 0.4f )
  91. return;
  92. m_flLastFPSSnapShot = curtime;
  93. float ft = enginetools->GetRealFrameTime();
  94. if ( ft <= 0.0f )
  95. {
  96. m_pFPS->SetText( "[fps: ??]" );
  97. }
  98. else
  99. {
  100. char fps[ 32 ];
  101. Q_snprintf( fps, sizeof( fps ), "[fps: %.1f]", 1.0f / ft );
  102. m_pFPS->SetText( fps );
  103. }
  104. UpdateMemoryUsage( GetMemoryUsage() );
  105. }
  106. #include <windows.h>
  107. #include <psapi.h>
  108. static float GetMemoryUsage()
  109. {
  110. PROCESS_MEMORY_COUNTERS counters;
  111. counters.cb = sizeof( counters );
  112. if ( GetProcessMemoryInfo( GetCurrentProcess(), &counters, sizeof( counters ) ) )
  113. {
  114. return (float)counters.WorkingSetSize / ( 1024.0f * 1024.0f );
  115. }
  116. return 0;
  117. }