-
Notifications
You must be signed in to change notification settings - Fork 0
/
D3DFont.cs
715 lines (573 loc) · 25.4 KB
/
D3DFont.cs
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
//-----------------------------------------------------------------------------
// File: D3DFont.cs
//
// Desc: Shortcut functions for using DX objects
//
// Copyright (c) Microsoft Corporation. All rights reserved
//-----------------------------------------------------------------------------
using System;
using System.Drawing;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Direct3D = Microsoft.DirectX.Direct3D;
/// <summary>
/// A generic font class
/// </summary>
public class GraphicsFont
{
public const int MaxNumfontVertices = 50*6;
// Font rendering flags
[System.Flags]
public enum RenderFlags
{
Centered = 0x0001,
TwoSided = 0x0002,
Filtered = 0x0004,
}
private System.Drawing.Font systemFont;
private bool isZEnable = false;
public bool ZBufferEnable { get { return isZEnable; } set { isZEnable = value; } }
string ourFontName; // Font properties
int ourFontHeight;
private Direct3D.Device device;
private TextureState textureState0;
private TextureState textureState1;
private Sampler samplerState0;
private RenderStates renderState;
private Direct3D.Texture fontTexture;
private Direct3D.VertexBuffer vertexBuffer;
private CustomVertex.TransformedColoredTextured[] fontVertices = new CustomVertex.TransformedColoredTextured[MaxNumfontVertices];
private int textureWidth; // Texture dimensions
private int textureHeight;
private float textureScale;
private int spacingPerChar;
private float[,] textureCoords = new float[128-32,4];
// Stateblocks for setting and restoring render states
private StateBlock savedStateBlock;
private StateBlock drawTextStateBlock;
/// <summary>
/// Create a new font object
/// </summary>
/// <param name="f">The font to use</param>
public GraphicsFont(System.Drawing.Font f)
{
ourFontName = f.Name;
ourFontHeight = (int)f.Size;
systemFont = f;
}
/// <summary>
/// Create a new font object
/// </summary>
public GraphicsFont(string fontName) : this(fontName, FontStyle.Regular, 12)
{
}
/// <summary>
/// Create a new font object
/// </summary>
public GraphicsFont(string fontName, FontStyle style) : this(fontName, style, 12)
{
}
/// <summary>
/// Create a new font object
/// </summary>
/// <param name="fontName">The name of the font</param>
/// <param name="style">The style</param>
/// <param name="size">Size of the font</param>
public GraphicsFont(string fontName, FontStyle style, int size)
{
ourFontName = fontName;
ourFontHeight = size;
systemFont = new System.Drawing.Font(fontName, ourFontHeight, style);
}
/// <summary>
/// Attempt to draw the systemFont alphabet onto the provided texture
/// graphics.
/// </summary>
/// <param name="g"></param>Graphics object on which to draw and measure the letters</param>
/// <param name="measureOnly">If set, the method will test to see if the alphabet will fit without actually drawing</param>
public void PaintAlphabet(Graphics g, bool measureOnly)
{
string str;
float x = 0;
float y = 0;
Point p = new Point(0, 0);
Size size = new Size(0,0);
// Calculate the spacing between characters based on line height
size = g.MeasureString(" ", systemFont).ToSize();
x = spacingPerChar = (int) Math.Ceiling(size.Height * 0.3);
for (char c = (char)32; c < (char)127; c++)
{
str = c.ToString();
// We need to do some things here to get the right sizes. The default implemententation of MeasureString
// will return a resolution independant size. For our height, this is what we want. However, for our width, we
// want a resolution dependant size.
Size resSize = g.MeasureString(str, systemFont).ToSize();
size.Height = resSize.Height + 1;
// Now the Resolution independent width
if (c != ' ') // We need the special case here because a space has a 0 width in GenericTypoGraphic stringformats
{
resSize = g.MeasureString(str, systemFont, p, StringFormat.GenericTypographic).ToSize();
size.Width = resSize.Width;
}
else
size.Width = resSize.Width;
if ((x + size.Width + spacingPerChar) > textureWidth)
{
x = spacingPerChar;
y += size.Height;
}
// Make sure we have room for the current character
if ((y + size.Height) > textureHeight)
throw new System.InvalidOperationException("Texture too small for alphabet");
if (!measureOnly)
{
if (c != ' ') // We need the special case here because a space has a 0 width in GenericTypoGraphic stringformats
g.DrawString(str, systemFont, Brushes.White, new Point((int)x, (int)y), StringFormat.GenericTypographic);
else
g.DrawString(str, systemFont, Brushes.White, new Point((int)x, (int)y));
textureCoords[c-32,0] = ((float) (x + 0 - spacingPerChar)) / textureWidth;
textureCoords[c-32,1] = ((float) (y + 0 + 0)) / textureHeight;
textureCoords[c-32,2] = ((float) (x + size.Width + spacingPerChar)) / textureWidth;
textureCoords[c-32,3] = ((float) (y + size.Height + 0)) / textureHeight;
}
x += size.Width + (2 * spacingPerChar);
}
}
/// <summary>
/// Initialize the device objects
/// </summary>
/// <param name="dev">The grpahics device used to initialize</param>
public void InitializeDeviceObjects(Device dev)
{
if (dev != null)
{
// Set up our events
dev.DeviceReset += new System.EventHandler(this.RestoreDeviceObjects);
}
// Keep a local copy of the device
device = dev;
textureState0 = device.TextureState[0];
textureState1 = device.TextureState[1];
samplerState0 = device.SamplerState[0];
renderState = device.RenderState;
// Create a bitmap on which to measure the alphabet
Bitmap bmp = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmp);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
g.TextContrast = 0;
// Establish the font and texture size
textureScale = 1.0f; // Draw fonts into texture without scaling
// Calculate the dimensions for the smallest power-of-two texture which
// can hold all the printable characters
textureWidth = textureHeight = 128;
for (;;)
{
try
{
// Measure the alphabet
PaintAlphabet(g, true);
}
catch (System.InvalidOperationException)
{
// Scale up the texture size and try again
textureWidth *= 2;
textureHeight *= 2;
continue;
}
break;
}
// If requested texture is too big, use a smaller texture and smaller font,
// and scale up when rendering.
Direct3D.Caps d3dCaps = device.DeviceCaps;
// If the needed texture is too large for the video card...
if (textureWidth > d3dCaps.MaxTextureWidth)
{
// Scale the font size down to fit on the largest possible texture
textureScale = (float)d3dCaps.MaxTextureWidth / (float)textureWidth;
textureWidth = textureHeight = d3dCaps.MaxTextureWidth;
for(;;)
{
// Create a new, smaller font
ourFontHeight = (int) Math.Floor(ourFontHeight * textureScale);
systemFont = new System.Drawing.Font(systemFont.Name, ourFontHeight, systemFont.Style);
try
{
// Measure the alphabet
PaintAlphabet(g, true);
}
catch (System.InvalidOperationException)
{
// If that still doesn't fit, scale down again and continue
textureScale *= 0.9F;
continue;
}
break;
}
}
// Release the bitmap used for measuring and create one for drawing
bmp.Dispose();
bmp = new Bitmap(textureWidth, textureHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
g = Graphics.FromImage(bmp);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
g.TextContrast = 0;
// Draw the alphabet
PaintAlphabet(g, false);
// Create a new texture for the font from the bitmap we just created
fontTexture = Texture.FromBitmap(device, bmp, 0, Pool.Managed);
RestoreDeviceObjects(null, null);
}
/// <summary>
/// Restore the font after a device has been reset
/// </summary>
public void RestoreDeviceObjects(object sender, EventArgs e)
{
vertexBuffer = new VertexBuffer(typeof(CustomVertex.TransformedColoredTextured), MaxNumfontVertices,
device, Usage.WriteOnly | Usage.Dynamic, 0, Pool.Default);
Surface surf = device.GetRenderTarget( 0 );
bool bSupportsAlphaBlend = Manager.CheckDeviceFormat(device.DeviceCaps.AdapterOrdinal,
device.DeviceCaps.DeviceType, device.DisplayMode.Format,
Usage.RenderTarget | Usage.QueryPostPixelShaderBlending, ResourceType.Surface,
surf.Description.Format );
// Create the state blocks for rendering text
for (int which=0; which < 2; which++)
{
device.BeginStateBlock();
device.SetTexture(0, fontTexture);
if (isZEnable)
renderState.ZBufferEnable = true;
else
renderState.ZBufferEnable = false;
if( bSupportsAlphaBlend )
{
renderState.AlphaBlendEnable = true;
renderState.SourceBlend = Blend.SourceAlpha;
renderState.DestinationBlend = Blend.InvSourceAlpha;
}
else
{
renderState.AlphaBlendEnable = false;
}
renderState.AlphaTestEnable = true;
renderState.ReferenceAlpha = 0x08;
renderState.AlphaFunction = Compare.GreaterEqual;
renderState.FillMode = FillMode.Solid;
renderState.CullMode = Cull.CounterClockwise;
renderState.StencilEnable = false;
renderState.Clipping = true;
device.ClipPlanes.DisableAll();
renderState.VertexBlend = VertexBlend.Disable;
renderState.IndexedVertexBlendEnable = false;
renderState.FogEnable = false;
renderState.ColorWriteEnable = ColorWriteEnable.RedGreenBlueAlpha;
textureState0.ColorOperation = TextureOperation.Modulate;
textureState0.ColorArgument1 = TextureArgument.TextureColor;
textureState0.ColorArgument2 = TextureArgument.Diffuse;
textureState0.AlphaOperation = TextureOperation.Modulate;
textureState0.AlphaArgument1 = TextureArgument.TextureColor;
textureState0.AlphaArgument2 = TextureArgument.Diffuse;
textureState0.TextureCoordinateIndex = 0;
textureState0.TextureTransform = TextureTransform.Disable; // REVIEW
textureState1.ColorOperation = TextureOperation.Disable;
textureState1.AlphaOperation = TextureOperation.Disable;
samplerState0.MinFilter = TextureFilter.Point;
samplerState0.MagFilter = TextureFilter.Point;
samplerState0.MipFilter = TextureFilter.None;
if (which==0)
savedStateBlock = device.EndStateBlock();
else
drawTextStateBlock = device.EndStateBlock();
}
}
/// <summary>
/// Draw some text on the screen
/// </summary>
public void DrawText(float xpos, float ypos, Color color, string text)
{
DrawText(xpos, ypos, color, text, RenderFlags.Filtered);
}
/// <summary>
/// Draw some text on the screen
/// </summary>
public void DrawText(float xpos, float ypos, Color color, string text, RenderFlags flags)
{
if (text == null)
return;
// Setup renderstate
savedStateBlock.Capture();
drawTextStateBlock.Apply();
device.SetTexture(0, fontTexture);
device.VertexFormat = CustomVertex.TransformedColoredTextured.Format;
device.PixelShader = null;
device.SetStreamSource(0, vertexBuffer, 0);
// Set filter states
if ((flags & RenderFlags.Filtered) != 0)
{
samplerState0.MinFilter = TextureFilter.Linear;
samplerState0.MagFilter = TextureFilter.Linear;
}
// Adjust for character spacing
xpos -= spacingPerChar;
float fStartX = xpos;
// Fill vertex buffer
int iv = 0;
int dwNumTriangles = 0;
foreach (char c in text)
{
if (c == '\n')
{
xpos = fStartX;
ypos += (textureCoords[0,3]-textureCoords[0,1])*textureHeight;
}
if ((c-32) < 0 || (c-32) >= 128-32)
continue;
float tx1 = textureCoords[c-32,0];
float ty1 = textureCoords[c-32,1];
float tx2 = textureCoords[c-32,2];
float ty2 = textureCoords[c-32,3];
float w = (tx2-tx1) * textureWidth / textureScale;
float h = (ty2-ty1) * textureHeight / textureScale;
int intColor = color.ToArgb();
if (c != ' ')
{
fontVertices[iv++] = new CustomVertex.TransformedColoredTextured(new Vector4(xpos+0-0.5f,ypos+h-0.5f,0.9f,1.0f), intColor, tx1, ty2);
fontVertices[iv++] = new CustomVertex.TransformedColoredTextured(new Vector4(xpos+0-0.5f,ypos+0-0.5f,0.9f,1.0f), intColor, tx1, ty1);
fontVertices[iv++] = new CustomVertex.TransformedColoredTextured(new Vector4(xpos+w-0.5f,ypos+h-0.5f,0.9f,1.0f), intColor, tx2, ty2);
fontVertices[iv++] = new CustomVertex.TransformedColoredTextured(new Vector4(xpos+w-0.5f,ypos+0-0.5f,0.9f,1.0f), intColor, tx2, ty1);
fontVertices[iv++] = new CustomVertex.TransformedColoredTextured(new Vector4(xpos+w-0.5f,ypos+h-0.5f,0.9f,1.0f), intColor, tx2, ty2);
fontVertices[iv++] = new CustomVertex.TransformedColoredTextured(new Vector4(xpos+0-0.5f,ypos+0-0.5f,0.9f,1.0f), intColor, tx1, ty1);
dwNumTriangles += 2;
if (dwNumTriangles*3 > (MaxNumfontVertices-6))
{
// Set the data for the vertexbuffer
vertexBuffer.SetData(fontVertices, 0, LockFlags.Discard);
device.DrawPrimitives(PrimitiveType.TriangleList, 0, dwNumTriangles);
dwNumTriangles = 0;
iv = 0;
}
}
xpos += w - (2 * spacingPerChar);
}
// Set the data for the vertex buffer
vertexBuffer.SetData(fontVertices, 0, LockFlags.Discard);
if (dwNumTriangles > 0)
device.DrawPrimitives(PrimitiveType.TriangleList, 0, dwNumTriangles);
// Restore the modified renderstates
savedStateBlock.Apply();
}
/// <summary>
/// Draws scaled 2D text. Note that x and y are in viewport coordinates
/// (ranging from -1 to +1). fXScale and fYScale are the size fraction
/// relative to the entire viewport. For example, a fXScale of 0.25 is
/// 1/8th of the screen width. This allows you to output text at a fixed
/// fraction of the viewport, even if the screen or window size changes.
/// </summary>
public void DrawTextScaled(float x, float y, float z,
float fXScale, float fYScale,
System.Drawing.Color color,
string text, RenderFlags flags)
{
if (device == null)
throw new System.ArgumentNullException();
// Set up renderstate
savedStateBlock.Capture();
drawTextStateBlock.Apply();
device.VertexFormat = CustomVertex.TransformedColoredTextured.Format;
device.PixelShader = null;
device.SetStreamSource(0, vertexBuffer, 0);
// Set filter states
if ((flags & RenderFlags.Filtered) != 0)
{
samplerState0.MinFilter = TextureFilter.Linear;
samplerState0.MagFilter = TextureFilter.Linear;
}
Viewport vp = device.Viewport;
float xpos = (x+1.0f)*vp.Width/2;
float ypos = (y+1.0f)*vp.Height/2;
float sz = z;
float rhw = 1.0f;
float fLineHeight = (textureCoords[0,3] - textureCoords[0,1]) * textureHeight;
// Adjust for character spacing
xpos -= spacingPerChar * (fXScale*vp.Height)/fLineHeight;
float fStartX = xpos;
// Fill vertex buffer
int numTriangles = 0;
int realColor = color.ToArgb();
int iv = 0;
foreach (char c in text)
{
if (c == '\n')
{
xpos = fStartX;
ypos += fYScale*vp.Height;
}
if ((c-32) < 0 || (c-32) >= 128-32)
continue;
float tx1 = textureCoords[c-32,0];
float ty1 = textureCoords[c-32,1];
float tx2 = textureCoords[c-32,2];
float ty2 = textureCoords[c-32,3];
float w = (tx2-tx1)*textureWidth;
float h = (ty2-ty1)*textureHeight;
w *= (fXScale*vp.Height)/fLineHeight;
h *= (fYScale*vp.Height)/fLineHeight;
if (c != ' ')
{
fontVertices[iv++] = new CustomVertex.TransformedColoredTextured(new Vector4(xpos+0-0.5f,ypos+h-0.5f,sz,rhw), realColor, tx1, ty2);
fontVertices[iv++] = new CustomVertex.TransformedColoredTextured(new Vector4(xpos+0-0.5f,ypos+0-0.5f,sz,rhw), realColor, tx1, ty1);
fontVertices[iv++] = new CustomVertex.TransformedColoredTextured(new Vector4(xpos+w-0.5f,ypos+h-0.5f,sz,rhw), realColor, tx2, ty2);
fontVertices[iv++] = new CustomVertex.TransformedColoredTextured(new Vector4(xpos+w-0.5f,ypos+0-0.5f,sz,rhw), realColor, tx2, ty1);
fontVertices[iv++] = new CustomVertex.TransformedColoredTextured(new Vector4(xpos+w-0.5f,ypos+h-0.5f,sz,rhw), realColor, tx2, ty2);
fontVertices[iv++] = new CustomVertex.TransformedColoredTextured(new Vector4(xpos+0-0.5f,ypos+0-0.5f,sz,rhw), realColor, tx1, ty1);
numTriangles += 2;
if (numTriangles*3 > (MaxNumfontVertices-6))
{
// Unlock, render, and relock the vertex buffer
vertexBuffer.SetData(fontVertices, 0, LockFlags.Discard);
device.DrawPrimitives(PrimitiveType.TriangleList , 0, numTriangles);
numTriangles = 0;
iv = 0;
}
}
xpos += w - (2 * spacingPerChar) * (fXScale*vp.Height)/fLineHeight;
}
// Unlock and render the vertex buffer
vertexBuffer.SetData(fontVertices, 0, LockFlags.Discard);
if (numTriangles > 0)
device.DrawPrimitives(PrimitiveType.TriangleList , 0, numTriangles);
// Restore the modified renderstates
savedStateBlock.Apply();
}
/// <summary>
/// Draws scaled 2D text. Note that x and y are in viewport coordinates
/// (ranging from -1 to +1). fXScale and fYScale are the size fraction
/// relative to the entire viewport. For example, a fXScale of 0.25 is
/// 1/8th of the screen width. This allows you to output text at a fixed
/// fraction of the viewport, even if the screen or window size changes.
/// </summary>
public void DrawTextScaled(float x, float y, float z,
float fXScale, float fYScale,
System.Drawing.Color color,
string text)
{
this.DrawTextScaled(x,y,z,fXScale, fYScale, color, text, 0);
}
/// <summary>
/// Renders 3D text
/// </summary>
public void Render3DText(string text, RenderFlags flags)
{
if (device == null)
throw new System.ArgumentNullException();
// Set up renderstate
savedStateBlock.Capture();
drawTextStateBlock.Apply();
device.VertexFormat = CustomVertex.PositionNormalTextured.Format;
device.PixelShader = null;
device.SetStreamSource(0, vertexBuffer, 0, VertexInformation.GetFormatSize(CustomVertex.PositionNormalTextured.Format));
// Set filter states
if ((flags & RenderFlags.Filtered) != 0)
{
samplerState0.MinFilter = TextureFilter.Linear;
samplerState0.MagFilter = TextureFilter.Linear;
}
// Position for each text element
float x = 0.0f;
float y = 0.0f;
// Center the text block at the origin
if ((flags & RenderFlags.Centered) != 0)
{
System.Drawing.SizeF sz = GetTextExtent(text);
x = -(((float)sz.Width)/10.0f)/2.0f;
y = -(((float)sz.Height)/10.0f)/2.0f;
}
// Turn off culling for two-sided text
if ((flags & RenderFlags.TwoSided) != 0)
renderState.CullMode = Cull.None;
// Adjust for character spacing
x -= spacingPerChar / 10.0f;
float fStartX = x;
// Fill vertex buffer
GraphicsStream strm = vertexBuffer.Lock(0, 0, LockFlags.Discard);
int numTriangles = 0;
foreach (char c in text)
{
if (c == '\n')
{
x = fStartX;
y -= (textureCoords[0,3]-textureCoords[0,1])*textureHeight/10.0f;
}
if ((c-32) < 0 || (c-32) >= 128-32)
continue;
float tx1 = textureCoords[c-32,0];
float ty1 = textureCoords[c-32,1];
float tx2 = textureCoords[c-32,2];
float ty2 = textureCoords[c-32,3];
float w = (tx2-tx1) * textureWidth / (10.0f * textureScale);
float h = (ty2-ty1) * textureHeight / (10.0f * textureScale);
if (c != ' ')
{
strm.Write(new CustomVertex.PositionNormalTextured(new Vector3(x+0,y+0,0), new Vector3(0,0,-1), tx1, ty2));
strm.Write(new CustomVertex.PositionNormalTextured(new Vector3(x+0,y+h,0), new Vector3(0,0,-1), tx1, ty1));
strm.Write(new CustomVertex.PositionNormalTextured(new Vector3(x+w,y+0,0), new Vector3(0,0,-1), tx2, ty2));
strm.Write(new CustomVertex.PositionNormalTextured(new Vector3(x+w,y+h,0), new Vector3(0,0,-1), tx2, ty1));
strm.Write(new CustomVertex.PositionNormalTextured(new Vector3(x+w,y+0,0), new Vector3(0,0,-1), tx2, ty2));
strm.Write(new CustomVertex.PositionNormalTextured(new Vector3(x+0,y+h,0), new Vector3(0,0,-1), tx1, ty1));
numTriangles += 2;
if (numTriangles*3 > (MaxNumfontVertices-6))
{
// Unlock, render, and relock the vertex buffer
vertexBuffer.Unlock();
device.DrawPrimitives(PrimitiveType.TriangleList , 0, numTriangles);
strm = vertexBuffer.Lock(0, 0, LockFlags.Discard);
numTriangles = 0;
}
}
x += w - (2 * spacingPerChar) / 10.0f;
}
// Unlock and render the vertex buffer
vertexBuffer.Unlock();
if (numTriangles > 0)
device.DrawPrimitives(PrimitiveType.TriangleList , 0, numTriangles);
// Restore the modified renderstates
savedStateBlock.Apply();
}
/// <summary>
/// Get the dimensions of a text string
/// </summary>
private System.Drawing.SizeF GetTextExtent(string text)
{
if (null == text || text == string.Empty)
throw new System.ArgumentNullException();
float fRowWidth = 0.0f;
float fRowHeight = (textureCoords[0,3]-textureCoords[0,1])*textureHeight;
float fWidth = 0.0f;
float fHeight = fRowHeight;
foreach (char c in text)
{
if (c == '\n')
{
fRowWidth = 0.0f;
fHeight += fRowHeight;
}
if ((c-32) < 0 || (c-32) >= 128-32)
continue;
float tx1 = textureCoords[c-32,0];
float tx2 = textureCoords[c-32,2];
fRowWidth += (tx2-tx1)*textureWidth - 2*spacingPerChar;
if (fRowWidth > fWidth)
fWidth = fRowWidth;
}
return new System.Drawing.SizeF(fWidth, fHeight);
}
/// <summary>
/// Cleanup any resources being used
/// </summary>
public void Dispose(object sender, EventArgs e)
{
if (systemFont != null)
systemFont.Dispose();
systemFont = null;
}
}