DebugGraph.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #pragma hdrstop
  21. #include "../idlib/precompiled.h"
  22. /*
  23. ================================================================================================
  24. Contains the DebugGraph implementation.
  25. ================================================================================================
  26. */
  27. /*
  28. ========================
  29. idDebugGraph::idDebugGraph
  30. ========================
  31. */
  32. idDebugGraph::idDebugGraph( int numItems ) :
  33. bgColor( 0.0f, 0.0f, 0.0f, 0.5f ),
  34. fontColor( 1.0f, 1.0f, 1.0f, 1.0f ),
  35. enable( true ),
  36. mode( GRAPH_FILL ),
  37. sideways( false ),
  38. border( 0.0f ),
  39. position( 100.0f, 100.0f, 100.0f, 100.0f ) {
  40. Init( numItems );
  41. }
  42. /*
  43. ========================
  44. idDebugGraph::Init
  45. ========================
  46. */
  47. void idDebugGraph::Init( int numBars ) {
  48. bars.SetNum( numBars );
  49. labels.Clear();
  50. for ( int i = 0; i < numBars; i++ ) {
  51. bars[i].value = 0.0f;
  52. }
  53. }
  54. /*
  55. ========================
  56. idDebugGraph::AddGridLine
  57. ========================
  58. */
  59. void idDebugGraph::AddGridLine( float value, const idVec4 & color ) {
  60. graphPlot_t & line = grid.Alloc();
  61. line.value = value;
  62. line.color = color;
  63. }
  64. /*
  65. ========================
  66. idDebugGraph::SetValue
  67. ========================
  68. */
  69. void idDebugGraph::SetValue( int b, float value, const idVec4 & color ) {
  70. if ( !enable ) {
  71. return;
  72. }
  73. if ( b < 0 ) {
  74. bars.RemoveIndex( 0 );
  75. graphPlot_t & graph = bars.Alloc();
  76. graph.value = value;
  77. graph.color = color;
  78. } else {
  79. bars[b].value = value;
  80. bars[b].color = color;
  81. }
  82. }
  83. /*
  84. ========================
  85. idDebugGraph::SetLabel
  86. ========================
  87. */
  88. void idDebugGraph::SetLabel( int b, const char * text ) {
  89. if ( labels.Num() != bars.Num() ) {
  90. labels.SetNum( bars.Num() );
  91. }
  92. labels[b] = text;
  93. }
  94. /*
  95. ========================
  96. idDebugGraph::Render
  97. ========================
  98. */
  99. void idDebugGraph::Render( idRenderSystem * gui ) {
  100. if ( !enable ) {
  101. return;
  102. }
  103. gui->DrawFilled( bgColor, position.x, position.y, position.z, position.w );
  104. if ( bars.Num() == 0 ) {
  105. return;
  106. }
  107. if ( sideways ) {
  108. float barWidth = position.z - border * 2.0f;
  109. float barHeight = ( ( position.w - border ) / (float)bars.Num() );
  110. float barLeft = position.x + border;
  111. float barTop = position.y + border;
  112. for ( int i = 0; i < bars.Num(); i++ ) {
  113. idVec4 rect( vec4_zero );
  114. if ( mode == GRAPH_LINE ) {
  115. rect.Set( barLeft + barWidth * bars[i].value, barTop + i * barHeight, 1.0f, barHeight - border );
  116. } else if ( mode == GRAPH_FILL ) {
  117. rect.Set( barLeft, barTop + i * barHeight, barWidth * bars[i].value, barHeight - border );
  118. } else if ( mode == GRAPH_FILL_REVERSE ) {
  119. rect.Set( barLeft + barWidth, barTop + i * barHeight, barWidth - barWidth * bars[i].value, barHeight - border );
  120. }
  121. gui->DrawFilled( bars[i].color, rect.x, rect.y, rect.z, rect.w );
  122. }
  123. if ( labels.Num() > 0 ) {
  124. int maxLen = 0;
  125. for ( int i = 0; i < labels.Num(); i++ ) {
  126. maxLen = Max( maxLen, labels[i].Length() );
  127. }
  128. idVec4 rect( position );
  129. rect.x -= SMALLCHAR_WIDTH * maxLen;
  130. rect.z = SMALLCHAR_WIDTH * maxLen;
  131. gui->DrawFilled( bgColor, rect.x, rect.y, rect.z, rect.w );
  132. for ( int i = 0; i < labels.Num(); i++ ) {
  133. idVec2 pos( barLeft - SMALLCHAR_WIDTH * maxLen, barTop + i * barHeight );
  134. gui->DrawSmallStringExt( idMath::Ftoi( pos.x ), idMath::Ftoi( pos.y ), labels[i], fontColor, true );
  135. }
  136. }
  137. } else {
  138. float barWidth = ( ( position.z - border ) / (float)bars.Num() );
  139. float barHeight = position.w - border * 2.0f;
  140. float barLeft = position.x + border;
  141. float barTop = position.y + border;
  142. float barBottom = barTop + barHeight;
  143. for ( int i = 0; i < grid.Num(); i++ ) {
  144. idVec4 rect( position.x, barBottom - barHeight * grid[i].value, position.z, 1.0f );
  145. gui->DrawFilled( grid[i].color, rect.x, rect.y, rect.z, rect.w );
  146. }
  147. for ( int i = 0; i < bars.Num(); i++ ) {
  148. idVec4 rect;
  149. if ( mode == GRAPH_LINE ) {
  150. rect.Set( barLeft + i * barWidth, barBottom - barHeight * bars[i].value, barWidth - border, 1.0f );
  151. } else if ( mode == GRAPH_FILL ) {
  152. rect.Set( barLeft + i * barWidth, barBottom - barHeight * bars[i].value, barWidth - border, barHeight * bars[i].value );
  153. } else if ( mode == GRAPH_FILL_REVERSE ) {
  154. rect.Set( barLeft + i * barWidth, barTop, barWidth - border, barHeight * bars[i].value );
  155. }
  156. gui->DrawFilled( bars[i].color, rect.x, rect.y, rect.z, rect.w );
  157. }
  158. if ( labels.Num() > 0 ) {
  159. idVec4 rect( position );
  160. rect.y += barHeight;
  161. rect.w = SMALLCHAR_HEIGHT;
  162. gui->DrawFilled( bgColor, rect.x, rect.y, rect.z, rect.w );
  163. for ( int i = 0; i < labels.Num(); i++ ) {
  164. idVec2 pos( barLeft + i * barWidth, barBottom + border );
  165. gui->DrawSmallStringExt( idMath::Ftoi( pos.x ), idMath::Ftoi( pos.y ), labels[i], fontColor, true );
  166. }
  167. }
  168. }
  169. }