forked from electronicarts/CnC_Generals_Zero_Hour
-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathW3DDisplayString.cpp
More file actions
399 lines (330 loc) · 12.1 KB
/
W3DDisplayString.cpp
File metadata and controls
399 lines (330 loc) · 12.1 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
** Command & Conquer Generals(tm)
** Copyright 2025 Electronic Arts Inc.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
////////////////////////////////////////////////////////////////////////////////
// //
// (c) 2001-2003 Electronic Arts Inc. //
// //
////////////////////////////////////////////////////////////////////////////////
// FILE: W3DDisplayString.cpp /////////////////////////////////////////////////
//-----------------------------------------------------------------------------
//
// Westwood Studios Pacific.
//
// Confidential Information
// Copyright (C) 2001 - All Rights Reserved
//
//-----------------------------------------------------------------------------
//
// Project: RTS3
//
// File name: W3DDisplayString.cpp
//
// Created: Colin Day, July 2001
//
// Desc: Display string W3D implementation, display strings hold
// double byte characters and all the data we need to render
// those strings to the screen.
//
//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
// SYSTEM INCLUDES ////////////////////////////////////////////////////////////
#include <stdlib.h>
// USER INCLUDES //////////////////////////////////////////////////////////////
#include "GameClient/GameClient.h"
#include "W3DDevice/GameClient/W3DDisplayString.h"
#include "GameClient/HotKey.h"
#include "GameClient/GameFont.h"
#include "GameClient/GlobalLanguage.h"
// DEFINES ////////////////////////////////////////////////////////////////////
// PRIVATE TYPES //////////////////////////////////////////////////////////////
// PRIVATE DATA ///////////////////////////////////////////////////////////////
// PUBLIC DATA ////////////////////////////////////////////////////////////////
// PRIVATE PROTOTYPES /////////////////////////////////////////////////////////
// PRIVATE FUNCTIONS //////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS ///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// W3DDisplayString::W3DDisplayString =========================================
/** */
//=============================================================================
W3DDisplayString::W3DDisplayString()
{
m_textChanged = FALSE;
m_textPos.x = 0;
m_textPos.y = 0;
m_currTextColor = 0;
m_currDropColor = 0;
m_size.x = 0;
m_size.y = 0;
m_fontChanged = FALSE;
m_clipRegion.lo.x = 0;
m_clipRegion.lo.y = 0;
m_clipRegion.hi.x = 0;
m_clipRegion.hi.y = 0;
m_lastResourceFrame = 0;
m_useHotKey = FALSE;
m_hotKeyPos.x = 0;
m_hotKeyPos.y = 0;
m_hotKeyColor = GameMakeColor(255,255,255,255);
}
// W3DDisplayString::~W3DDisplayString ========================================
/** */
//=============================================================================
W3DDisplayString::~W3DDisplayString()
{
}
// W3DDisplayString::textChanged ==============================================
/** This method automatically gets called from some methods in the display
* class so that we can write our own code here to to appropriate things
* on the changing of string data */
//=============================================================================
void W3DDisplayString::notifyTextChanged()
{
// extend functionality
DisplayString::notifyTextChanged();
if(TheGlobalLanguageData)
{
if(TheGlobalLanguageData->m_useHardWrap == TRUE)
{
m_textRenderer.Set_Use_Hard_Word_Wrap(true);
m_textRendererHotKey.Set_Use_Hard_Word_Wrap(true);
}
else
{
m_textRenderer.Set_Use_Hard_Word_Wrap(false);
m_textRendererHotKey.Set_Use_Hard_Word_Wrap(false);
}
}
// get our new text extents
computeExtents();
//
// set a flag so that if it comes that we need to render this string
// we know we must first build the sentence
//
m_textChanged = TRUE;
// reset data for our text renderer
m_textRenderer.Reset();
m_textRendererHotKey.Reset();
}
// W3DDisplayString::Draw =====================================================
/** Draw the text at the specified location in in the specified colors
* in the parameters. Since we keep an instance of the rendered text
* texture around, this is the time to check to see if any of our
* properties have changed since the last render, like color, or
* position, or content. If they have, we need to rebuild the sentence
* texture for rendering */
//=============================================================================
void W3DDisplayString::draw( Int x, Int y, Color color, Color dropColor )
{
draw(x,y, color, dropColor, 1, 1);
}
void W3DDisplayString::draw( Int x, Int y, Color color, Color dropColor, Int xDrop, Int yDrop )
{
Bool needNewPolys = FALSE;
// sanity
if( getTextLength() == 0 )
return; // nothing to draw
// if our font or text has changed we need to build a new sentence
if( m_fontChanged || m_textChanged )
{
if(m_useHotKey)
{
m_textRenderer.Set_Hot_Key_Parse(TRUE);
m_textRenderer.Build_Sentence( getText().str(), &m_hotKeyPos.x, &m_hotKeyPos.y );
m_hotkey.translate(TheHotKeyManager->searchHotKey(getText()));
if(!m_hotkey.isEmpty())
m_textRendererHotKey.Build_Sentence(m_hotkey.str(), nullptr, nullptr);
else
{
m_useHotKey = FALSE;
m_textRendererHotKey.Reset();
}
}
else
m_textRenderer.Build_Sentence( getText().str(), nullptr, nullptr );
m_fontChanged = FALSE;
m_textChanged = FALSE;
needNewPolys = TRUE;
}
//
// if our position has changed, or our colors have changed, or our
// text data has changed, we need to redo the texture quads
//
if( needNewPolys ||
x != m_textPos.x ||
y != m_textPos.y ||
color != m_currTextColor ||
dropColor != m_currDropColor )
{
// save the new attributes of the text position and color
m_textPos.x = x;
m_textPos.y = y;
m_currTextColor = color;
m_currDropColor = dropColor;
// reset the quads
m_textRenderer.Reset_Polys();
// draw the shadow
m_textRenderer.Set_Location( Vector2( m_textPos.x + xDrop, m_textPos.y + yDrop) );
m_textRenderer.Draw_Sentence( m_currDropColor );
// draw the text
m_textRenderer.Set_Location( Vector2( m_textPos.x, m_textPos.y ) );
m_textRenderer.Draw_Sentence( m_currTextColor );
if(m_useHotKey)
{
m_textRendererHotKey.Reset_Polys();
m_textRendererHotKey.Set_Location( Vector2( m_textPos.x + m_hotKeyPos.x , m_textPos.y +m_hotKeyPos.y) );
m_textRendererHotKey.Draw_Sentence( m_hotKeyColor );
m_textRendererHotKey.Render();
}
}
// render the text
m_textRenderer.Render();
// we are for sure using display resources now
if( TheGameClient )
usingResources( TheGameClient->getFrame() );
}
// W3DDisplayString::getSize ==================================================
/** Get the render size width and height of the string in this instance
* with the font associated with it */
//=============================================================================
void W3DDisplayString::getSize( Int *width, Int *height )
{
// assign the width and height we have stored to parameters present
if( width )
*width = m_size.x;
if( height )
*height = m_size.y;
}
// DisplayString::appendChar ==================================================
/** Get text with up to charPos characters, -1 = all characters */
//=============================================================================
Int W3DDisplayString::getWidth( Int charPos )
{
FontCharsClass * font;
Int width = 0;
Int count = 0;
font = m_textRenderer.Peek_Font();
if ( font )
{
const WideChar *text = m_textString.str();
WideChar ch;
while ( (ch = *text++ ) != 0 && ( charPos == -1 || count < charPos ) )
{
if ( ch != (WideChar)'\n' )
{
width += font->Get_Char_Spacing( ch );
}
count++;
}
}
return width;
}
// W3DDisplayString::setFont ==================================================
/** Set the font for this particular display string */
//=============================================================================
void W3DDisplayString::setFont( GameFont *font )
{
// sanity
if( font == nullptr )
return;
// if the new font is the same as our existing font do nothing
if( m_font == font )
return;
// extending functionality
DisplayString::setFont( font );
// set the font in our renderer
m_textRenderer.Set_Font( static_cast<FontCharsClass *>(m_font->fontData) );
// TheSuperHackers @fix bobtista 30/01/2026 Check for null before dereferencing to prevent crash if font loading fails
GameFont *boldFont = TheFontLibrary->getFont(font->nameString, font->pointSize, TRUE);
if (boldFont != nullptr)
m_textRendererHotKey.Set_Font( static_cast<FontCharsClass *>(boldFont->fontData) );
// recompute extents for text with new font
computeExtents();
// set flag telling us the font has changed since last render
m_fontChanged = TRUE;
}
// W3DDisplayString::setClipRegion ============================================
/** Set the clipping region for the text */
//=============================================================================
void W3DDisplayString::setClipRegion( IRegion2D *region )
{
// extend functionality
DisplayString::setClipRegion( region );
// only consider regions that are actual changes
if( region->lo.x != m_clipRegion.lo.x ||
region->lo.y != m_clipRegion.lo.y ||
region->hi.x != m_clipRegion.hi.x ||
region->hi.y != m_clipRegion.hi.y )
{
// assign new region
m_clipRegion = *region;
// set new region in renderer
m_textRenderer.Set_Clipping_Rect( RectClass( m_clipRegion.lo.x,
m_clipRegion.lo.y,
m_clipRegion.hi.x,
m_clipRegion.hi.y ) );
m_textRendererHotKey.Set_Clipping_Rect( RectClass( m_clipRegion.lo.x,
m_clipRegion.lo.y,
m_clipRegion.hi.x,
m_clipRegion.hi.y ) );
}
}
// W3DDisplayString::computeExtents ===========================================
/** Update the width and height of our string */
//=============================================================================
void W3DDisplayString::computeExtents()
{
UnsignedInt len = getTextLength();
// if we have no string, or no font we don't have a size yet
if( len == 0 || m_font == nullptr )
{
m_size.x = 0;
m_size.y = 0;
}
else
{
Vector2 extents = m_textRenderer.Get_Formatted_Text_Extents(getText().str()); //Get_Text_Extents( getText().str() );
m_size.x = extents.X;
m_size.y = extents.Y;
}
}
// W3DDisplayString::setWordWrap ===========================================
/** Set the wordwrap of the m_textRenderer */
//=============================================================================
void W3DDisplayString::setWordWrap( Int wordWrap )
{
// set the Word Wrap
if(m_textRenderer.Set_Wrapping_Width(wordWrap))
notifyTextChanged();
}
void W3DDisplayString::setUseHotkey( Bool useHotkey, Color hotKeyColor )
{
m_useHotKey = useHotkey;
m_hotKeyColor = hotKeyColor;
m_textRenderer.Set_Hot_Key_Parse(useHotkey);
notifyTextChanged();
}
// W3DDisplayString::setWordWrapCentered ======================================
/** Set the whether or not we want to center each new line in a text string */
//=============================================================================
void W3DDisplayString::setWordWrapCentered( Bool isCentered )
{
// set the Word Wrap
if( m_textRenderer.Set_Word_Wrap_Centered(isCentered) )
notifyTextChanged();
}