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