-
Notifications
You must be signed in to change notification settings - Fork 4
/
DisplayText.cpp
326 lines (277 loc) · 6.58 KB
/
DisplayText.cpp
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//
// DisplayText.cpp
//
// Definitions of classes needed to display text in an OpenGL window
//
// Create multiple instances to make multiple text areas
// OpenGL stack-safe
// Scrolling, Text color, background, bounding box, etc.
#include "JMDefs.h"
#include "DisplayText.h"
#include "Render.h"
#include "Item.h"
#include "EndGameState.h"
class CGame;
#define FONT_DRAW_W 6
#define FONT_DRAW_H 8
#define TEXT_MAXCHARS 2048
// Constructor
CDisplayText::CDisplayText( JRect in, uint8 inAlpha )
: m_Rect(in),
m_dwWidth(in.Width()),
m_dwHeight( in.Height() ),
m_dwUsedLines( m_dwHeight / FONT_DRAW_H ),
m_dwFreeLines( 0 ),
m_szText( NULL ),
m_szDrawPtr( NULL ),
m_vPos( m_Rect.Left(), m_Rect.Top() ),
m_dwFlags( FLAG_TEXT_NONE ),
m_rcViewport( 0, 480, 640, 0 )
{
m_szText = new char[TEXT_MAXCHARS];
memset( m_szText, 0, sizeof( *m_szText ) );
m_szDrawPtr = m_szText;
m_TileSet = new CTileset("Resources/SmallText6X8.png", 6,8);
m_Color.SetColor( 0, 0, 0, 255 );
m_BoundingBoxColor.SetColor( 128,170,192, inAlpha );
}
bool CDisplayText::Update( float fCurTime )
{
return true;
}
// Setup functions
void CDisplayText::PreDraw()
{
bool bInverse;
if( m_dwFlags & FLAG_TEXT_INVERSE )
{
bInverse = true;
}
else
{
bInverse = false;
}
g_pGame->GetRender()->PreDrawObjects( m_rcViewport, m_TileSet->Texture(), false, bInverse);
}
void CDisplayText::PostDraw()
{
g_pGame->GetRender()->PostDrawObjects();
}
// Unformatted Text Drawing functions
void CDisplayText::Draw()
{
Paginate();
PreDraw();
DrawBoundingBox();
DrawStr( m_Rect.Left(), m_Rect.Top(), true, m_Rect.Bottom(), m_szDrawPtr );
PostDraw();
}
void CDisplayText::DrawStr( int x, int y, char *szString )
{
PreDraw();
DrawStr( x, y, false, 0, szString );
PostDraw();
}
void CDisplayText::DrawStr(int x, int y, bool bBoundsCheck, int dwYMax, char *szString )
{
//printf("Trying to draw string: %s\n", szString);
JVector vScreen( (float)x, (float)y );
JVector vSize( (float)FONT_DRAW_W, (float)FONT_DRAW_H );
char *ptr = szString;
int index;
m_TileSet->PreDrawTile();
m_TileSet->SetTileColor( m_Color );
// Do that string parse, baby
while( *ptr != nul )
{
if( *ptr == ' ' )
{
vScreen.x += FONT_DRAW_W;
}
else if( *ptr > ' ' && *ptr <= '~' )
{
index = *ptr - ' ' - 1;
m_TileSet->DrawTile( index, vScreen, vSize, true );
vScreen.x += FONT_DRAW_W;
}
if( *ptr == '\n' )
{
vScreen.Init( (float)x, (float)(vScreen.y + FONT_DRAW_H) );
// Don't scroll off the bottom of the bounding box
if( bBoundsCheck && vScreen.y > dwYMax )
{
break;
}
}
ptr++;
}
m_TileSet->PostDrawTile();
}
void CDisplayText::DrawBoundingBox()
{
if( m_dwFlags & FLAG_TEXT_BOUNDING_BOX )
{
g_pGame->GetRender()->DrawTextBoundingBox( m_Rect, m_BoundingBoxColor );
}
else
{
g_pGame->GetRender()->DrawTextBoundingBox( m_Rect, JColor(51, 128, 102, 255) );
}
}
void CDisplayText::Paginate()
{
int dwAddLines = 0;
int dwAddLinesMax;
char *ptr;
dwAddLinesMax = m_dwUsedLines + m_dwFreeLines;
ptr = strchr( m_szText, nul );
while( ptr > m_szText )
{
if( *ptr == '\n' )
{
dwAddLines++;
}
if( dwAddLines == dwAddLinesMax )
{
// ran out of room; sorry.
ptr++;
break;
}
ptr--;
}
if( ptr == m_szText && dwAddLines > m_dwUsedLines )
{
m_dwFreeLines = dwAddLines - m_dwUsedLines + 1;
}
m_szDrawPtr = ptr;
}
// Formatted Text Drawing Functions
void CDisplayText::Printf( const char *fmt, ... )
{
va_list vList;
char szBuffer3[TEXT_MAXCHARS];
// Get resultant output string
va_start( vList, fmt );
vsprintf( szBuffer3, fmt, vList );
DrawFormattedStr(szBuffer3);
va_end(vList);
}
void CDisplayText::DrawFormattedStr(const char *szString)
{
char szBuffer[TEXT_MAXCHARS];
char szBuffer2[TEXT_MAXCHARS];
char *ptr;
char *ptr2;
JIVector vPos(m_Rect.Left(), m_Rect.Top());
strcpy( szBuffer, szString );
ptr = szBuffer;
while( *ptr )
{
if( *ptr >= ' ' && *ptr <= '~' )
{
vPos.x += FONT_DRAW_W;
}
else if( *ptr == '\n' )
{
vPos.x = m_Rect.Left();
}
if( vPos.x > m_Rect.Right() )
{
ptr++;
if( m_dwFlags & FLAG_TEXT_WRAP_WHITESPACE )
{
// Find the last space
while( ptr > szBuffer && *ptr != ' ' )
{
ptr--;
}
}
strcpy( szBuffer2, ptr );
*ptr++ = '\n';
*ptr = nul;
// Kill the leading space (if necessary)
if( m_dwFlags & FLAG_TEXT_WRAP_WHITESPACE && szBuffer2[0] == ' ' )
{
ptr2 = &szBuffer2[1];
}
else
{
ptr2 = szBuffer2;
}
strcat( ptr, ptr2 );
vPos.x = m_Rect.Left();
}
else
{
ptr++;
}
}
// Make sure it's going to fit in the bounding box by removing
// strings at the top of the bounding box
ptr = m_szText;
while( strlen( ptr ) + strlen( szBuffer ) > TEXT_MAXCHARS )
{
ptr = strchr( ptr, '\n' );
if( !ptr )
{
assert( !"CDisplayText::DrawFormattedString: string too long!" );
}
ptr++;
}
// If there are strings that need to be removed, do that now.
if( ptr != m_szText )
{
strcpy( szBuffer2, ptr );
strcpy( m_szText, szBuffer2 );
}
// Now that we have the space, concatenate the new string.
strcat( m_szText, szBuffer );
}
void CDisplayText::DisplayList( JLinkList<CItem> *pList, const CDisplayMeta *pMeta, const uint8 dwIndex )
{
// TODO: Use dwIndex to filter the
CLink<CItem> *pLink = pList->GetHead();
CItem *pItem;
Clear();
Printf(pMeta->header);
char cListId = 'a';
while(pLink != NULL)
{
pItem = pLink->m_lpData;
Printf("%c - %s\n", cListId, pItem->GetName());
if( cListId < pMeta->limit )
{
cListId++;
}
else
{
printf(pMeta->footer);
break;
}
pLink = pList->GetNext(pLink);
}
}
void CDisplayText::DisplayList( JLinkList<CScore> *pList, const CDisplayMeta *pMeta, const uint8 dwIndex )
{
// TODO: Use dwIndex to filter the
CLink<CScore> *pLink = pList->GetHead();
CScore *pItem;
Clear();
Printf(pMeta->header);
char cListId = 1;
while(pLink != NULL)
{
pItem = pLink->m_lpData;
Printf("%d - %s\n", cListId, pItem->GetName());
if( cListId < pMeta->limit )
{
cListId++;
}
else
{
printf(pMeta->footer);
break;
}
pLink = pList->GetNext(pLink);
}
}