summaryrefslogtreecommitdiffstats
path: root/game/code/debug/section.cpp
blob: d87379e52040fb21b61f372db46407925efe11fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <debug/section.h>
#include <p3d/matrixstack.hpp>
#include <p3d/utility.hpp>

Section::Section(tFont* pFont, const char* szName) :
	_pDebugFont(pFont),
	_pLineShader(NULL),
    _autoReset(true)
{
	assert(szName);

	int i;

	Reset(szName);

	_pLineShader = p3d::device->NewShader(/*p3d::pddi, */"simple");
	_pLineShader->AddRef();
	_pLineShader->SetInt(PDDI_SP_BLENDMODE, PDDI_BLEND_ALPHA);

	//Allocate string primitives for the 3d strings
	for(i=0;i<MaxText;i++)
	{
		if(_pDebugFont)
		{
            _pDebugFont->AddRef();

            P3D_UNICODE string[256];
            char emptyString[] = "Empty String";
            p3d::AsciiToUnicode(emptyString, string, strlen(emptyString));

			_pText[i]._pText = new tTextString(_pDebugFont, string);
			_pText[i]._pText->AddRef();
		}
		else
		{
			_pText[i]._pText = NULL;
		}
	}
}
Section::~Section()
{
	for(int i=0;i<MaxText;i++)
	{
		tRefCounted::Release(_pText[i]._pText);
	}

	tRefCounted::Release(_pLineShader);
	tRefCounted::Release(_pDebugFont);
}

void Section::Reset(const char *szName)
{
	assert(szName);
	strncpy(_szName, szName, sizeof(_szName));

	_NumLines = _NumText = _NumScreenLines = _NumScreenText = 0;
	ClearScreenText();
}

const char* Section::GetName() const
{

	return _szName;
}

void Section::AddLine(const rmt::Vector& a, const rmt::Vector& b, tColour colour/*, float time*/)
{
	if(_NumLines < MaxLines)
	{
		_Lines[_NumLines]._a = a;
		_Lines[_NumLines]._b = b;
		_Lines[_NumLines]._Colour = colour;
		_NumLines++;
	}
}

void Section::AddText(const char *szName, const rmt::Vector &pos, tColour colour/*, float time = 0*/)
{
	assert(szName);
	if(_NumText < MaxText && _pText[_NumText]._pText)
	{
		tTextString* p = _pText[_NumText]._pText;

        P3D_UNICODE string[256];
        p3d::AsciiToUnicode(szName, string, strlen(szName));

		p->SetString(string);
		p->SetColour(colour);
		p->SetScale(20.0f); //Fudge factor to make font appear about 1 meter high
		_pText[_NumText]._Pos = pos;
		_NumText++;
	}
}

void Section::AddScreenLine(const rmt::Vector &a, const rmt::Vector &b, tColour colour)
{
	if(_NumScreenLines < MaxScreenLines)
	{
		_ScreenLines[_NumScreenLines]._a = a;
		_ScreenLines[_NumScreenLines]._b = b;
		_ScreenLines[_NumScreenLines]._Colour = colour;
		_NumScreenLines++;
	}
}

void Section::AddScreenText(const char* szName, tColour colour )
{
	assert(szName);
	ScreenText &pST =  _pScreenText[_NumScreenText];

	if(_NumScreenText<MaxScreenText && pST._pText)
	{
		strncpy(pST._pText, szName, sizeof(pST._pText));
		pST._Colour = colour;
		pST._Pos = rmt::Vector(0.05f,_ScreenTextPos, 0);

		//TODO:get correct debug font height!
		_ScreenTextPos += 0.03f;
		_NumScreenText++;
	}
}
void Section::AddScreenText(const char* szName, const rmt::Vector &a, tColour colour)
{
	assert(szName);
	ScreenText &pST =  _pScreenText[_NumScreenText];

	if(_NumScreenText<MaxScreenText && pST._pText)
	{
		if(pST._pText)
			strncpy(pST._pText, szName, sizeof(pST._pText));
		pST._Colour = colour;
		pST._Pos = a;
		_NumScreenText++;
	}
}

void Section::ClearScreenText()
{
	_NumScreenText = 0;
	_ScreenTextPos = 0.15f;
}

void Section::Render()
{
	pddiPrimStream* pStream;
	int i;
	assert(_pLineShader);

	//Special case to get the pddi debugging information
	p3d::pddi->EnableStatsOverlay(0==strcmp(_szName, "pddi"));

	//Stream out world space lines
	if(_NumLines > 0)
    { 
        pStream = p3d::pddi->BeginPrims(_pLineShader, PDDI_PRIM_LINES, PDDI_V_C, _NumLines*2);
        if(pStream)
        {
		    for(i=0; i<_NumLines; i++)
		    {
			    Line &l = _Lines[i];

			    pddiVector A(l._a.x, l._a.y, l._a.z);
		   	    pddiVector B(l._b.x, l._b.y, l._b.z);
			    pStream->Vertex(&A,l._Colour);
			    pStream->Vertex(&B,l._Colour);
		    }
	    }
	    p3d::pddi->EndPrims(pStream);
    }

	//Draw all the world space vector text
	for(i=0;i<_NumText;i++)
	{
		p3d::stack->Push();
		p3d::stack->Translate(_pText[i]._Pos);
		_pText[i]._pText->Display();
		p3d::stack->Pop();
	}

	//Draw screen space lines
	{
		//Setup the matrix stack
		p3d::stack->Push();
		p3d::pddi->EnableZBuffer(false);
		p3d::stack->LoadIdentity();
		p3d::stack->Scale((float)p3d::display->GetWidth(),(float)p3d::display->GetHeight(),1);
		p3d::pddi->SetProjectionMode(PDDI_PROJECTION_DEVICE);

		//Stream out lines
	    if(_NumScreenLines > 0)
		{
            pStream = p3d::pddi->BeginPrims(_pLineShader, PDDI_PRIM_LINES, PDDI_V_C, _NumScreenLines*2);
		    if(pStream)
		    {
			    for(i=0; i<_NumScreenLines; i++)
			    {
				    Line &l = _ScreenLines[i];

				    pddiVector A(l._a.x, l._a.y, 1);
				    pddiVector B(l._b.x, l._b.y, 1);
				    pStream->Vertex(&A,l._Colour);
				    pStream->Vertex(&B,l._Colour);
			    }
		    }
		    p3d::pddi->EndPrims(pStream);
        }

		//Restore the old stack
		p3d::pddi->SetProjectionMode(PDDI_PROJECTION_PERSPECTIVE);
		p3d::pddi->EnableZBuffer(true);
		p3d::stack->Pop();
	}

	//Draw all the screen text
	p3d::stack->Push();
	p3d::stack->LoadIdentity();
	int height = p3d::display->GetHeight();
	int width = p3d::display->GetWidth();

	for(i=0;i<_NumScreenText;i++)
	{
		ScreenText &pST =  _pScreenText[i];
		if(pST._pText)
		{

			p3d::pddi->DrawString(pST._pText, 
				(int)(pST._Pos.x*width),
				(int)(pST._Pos.y*height), 
				pST._Colour);

		}
	}

	p3d::stack->Pop();
}