-
Notifications
You must be signed in to change notification settings - Fork 2
/
ShapeBatch.cs
1181 lines (978 loc) · 46.4 KB
/
ShapeBatch.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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -----------------------------------------
// ShapeBatch
//
// Copyright (c) Chris Cascioli
// Licensed under the MIT License
//
// https://github.com/vixorien/shapebatch
// -----------------------------------------
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace ShapeUtils
{
/// <summary>
/// Allows simple shapes to be drawn in MonoGame. These shapes
/// are batched and sent to the GPU in groups for efficiency.
///
/// Ideally, do NOT use ShapeBatch while a SpriteBatch is currently active.
/// </summary>
static class ShapeBatch
{
// Internal initialization and drawing fields
private static bool initialized = false;
private static bool batchActive = false;
private static BasicEffect effect = null;
private static GraphicsDevice device = null;
// The batches of lines and polygons
private static List<VertexPositionColor> lines = new List<VertexPositionColor>();
private static List<VertexPositionColor> polygons = new List<VertexPositionColor>();
// Depth values necessary for interleaving lines and polygons,
// even though they're tracked as two separate batches.
private static float currentDepth = 1.0f;
private const float DepthStep = 0.0000001f;
// How many primitives can be batched before being drawn?
private const int PrimitivesPerBatch = 1000;
// The default length for the segments (lines or triangles) of a circle
private const float DefaultCircleSegmentLength = 3.0f;
// The maximum multisample count ShapeBatch supports. Note that while
// this probably SHOULD be 32, as of MonoGame 3.8.1, OpenGL projects
// don't support values higher than 16.
private const int MaxMultisampleCount = 16;
// The default multisample count for antialiasing. This can be adjusted
// when enabling antialiasing.
private static int MultisampleCount = MaxMultisampleCount;
/// <summary>
/// Internal initialize function that is called any time
/// a batch is started. Only performs initialization
/// once per program.
/// </summary>
/// <param name="device">The GraphicsDevice used for drawing</param>
private static void Initialize(GraphicsDevice device)
{
// Anything to do?
if (initialized)
return;
// Save the device so we can draw whenever we need to
ShapeBatch.device = device;
// Create an effect for raw GPU drawing
effect = new BasicEffect(device);
effect.VertexColorEnabled = true;
// Ready to go, batch isn't active yet
initialized = true;
batchActive = false;
}
/// <summary>
/// Enables antialiasing.
///
/// Note, this MUST be called from the Game class's constructor, since
/// antialiasing settings must be configured before the GraphicsDevice object is actually
/// created by the GraphicsDeviceManager.
/// </summary>
/// <param name="graphicsDeviceManager">Graphics Device Manager from the Game class</param>
/// <exception cref="InvalidOperationException">Thrown if called after the GraphicsDevice object has been created.</exception>
public static void EnableAntialiasing(GraphicsDeviceManager graphicsDeviceManager)
{
EnableAntialiasing(graphicsDeviceManager, 16);
}
/// <summary>
/// Enables antialiasing.
///
/// Note, this MUST be called from the Game class's constructor, since
/// antialiasing settings must be configured before the GraphicsDevice object is actually
/// created by the GraphicsDeviceManager.
/// </summary>
/// <param name="graphicsDeviceManager">Graphics Device Manager from the Game class</param>
/// <param name="multisampleCount">Quality of antialiasing. Valid values are 1 through 16.</param>
/// <exception cref="InvalidOperationException">Thrown if called after the GraphicsDevice object has been created.</exception>
/// <exception cref="ArgumentException">Thrown if the multisample count is invalid.</exception>
public static void EnableAntialiasing(GraphicsDeviceManager graphicsDeviceManager, int multisampleCount)
{
// Verify that the graphics device has not yet been created
if (initialized || graphicsDeviceManager.GraphicsDevice != null)
{
throw new InvalidOperationException("GraphicsDevice already created. EnableAntialiasing() must be called within the Game class's constructor");
}
// Validate multisample count
if (multisampleCount < 1 || multisampleCount > MaxMultisampleCount)
{
throw new ArgumentException($"Invalid MultisampleCount. Value must be between 1 and {MaxMultisampleCount}.");
}
// Tell the graphics device manager that we want multisample antialiasing
// and save the multisample count for later
graphicsDeviceManager.PreferMultiSampling = true;
MultisampleCount = multisampleCount;
// We can't actually set the multisample count until the graphics device
// is about to be created, so listen for that event.
graphicsDeviceManager.PreparingDeviceSettings += PreparingDeviceSettings;
}
/// <summary>
/// Event handler for the graphics device manager preparing to create the graphics device
/// </summary>
private static void PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
{
// Set the multisample count now
e.GraphicsDeviceInformation.PresentationParameters.MultiSampleCount = MultisampleCount;
}
/// <summary>
/// Begins a batch. Shapes can only be drawn during a batch.
/// </summary>
/// <param name="device">The GraphicsDevice used for drawing. Most likely Game1's GraphicsDevice property.</param>
public static void Begin(GraphicsDevice device)
{
if (batchActive)
throw new InvalidOperationException("Cannot call Begin() twice before calling End()");
// Initialize the draw helper if necessary
Initialize(device);
// We're actively batching
batchActive = true;
currentDepth = 1.0f;
}
/// <summary>
/// Ends a batch and immediately draws any remaining shapes.
/// </summary>
public static void End()
{
if (!batchActive)
throw new InvalidOperationException("Must call Begin() before End()");
// Draw any remaining shapes
FlushShapes();
// The batch has ended
batchActive = false;
}
/// <summary>
/// Draws a 1-pixel-wide line
/// </summary>
/// <param name="startPos">The starting position</param>
/// <param name="endPos">The ending position</param>
/// <param name="color">The color of the line</param>
public static void Line(Vector2 startPos, Vector2 endPos, Color color)
{
if (!batchActive)
throw new InvalidOperationException("Line() must be called between Begin() and End()");
// Use the width-checking helper to handle this line, then progress depth
BatchWideLine(startPos, endPos, 1.0f, color, color);
NextDepth();
}
/// <summary>
/// Draws a 1-pixel-wide line
/// </summary>
/// <param name="startPos">The starting position</param>
/// <param name="endPos">The ending position</param>
/// <param name="startColor">The color of the line at the starting position</param>
/// <param name="endColor">The color of the line at the ending position</param>
public static void Line(Vector2 startPos, Vector2 endPos, Color startColor, Color endColor)
{
if (!batchActive)
throw new InvalidOperationException("Line() must be called between Begin() and End()");
// Use the width-checking helper to handle this line, then progress depth
BatchWideLine(startPos, endPos, 1.0f, startColor, endColor);
NextDepth();
}
/// <summary>
/// Draws a line
/// </summary>
/// <param name="startPos">The starting position</param>
/// <param name="endPos">The ending position</param>
/// <param name="width">The width of the line (minimum 1 pixel)</param>
/// <param name="color">The color of the line</param>
public static void Line(Vector2 startPos, Vector2 endPos, float width, Color color)
{
if (!batchActive)
throw new InvalidOperationException("Line() must be called between Begin() and End()");
// Use the width-checking helper to handle this line, then progress depth
BatchWideLine(startPos, endPos, width, color, color);
NextDepth();
}
/// <summary>
/// Draws a line
/// </summary>
/// <param name="startPos">The starting position</param>
/// <param name="endPos">The ending position</param>
/// <param name="width">The width of the line (minimum 1 pixel)</param>
/// <param name="startColor">The color of the line at the starting position</param>
/// <param name="endColor">The color of the line at the ending position</param>
public static void Line(Vector2 startPos, Vector2 endPos, float width, Color startColor, Color endColor)
{
if (!batchActive)
throw new InvalidOperationException("Line() must be called between Begin() and End()");
// Use the width-checking helper to handle this line, then progress depth
BatchWideLine(startPos, endPos, width, startColor, endColor);
NextDepth();
}
/// <summary>
/// Draws a 1-pixel-wide line. This overload returns the end position of the line.
/// </summary>
/// <param name="startPos">The starting position of the line</param>
/// <param name="length">The length of the line</param>
/// <param name="angle">The angle of the line, in radians, measured from the positive x axis</param>
/// <param name="color">The color of the line</param>
/// <returns>The end position of the line.</returns>
public static Vector2 Line(Vector2 startPos, float length, float angle, Color color)
{
if (!batchActive)
throw new InvalidOperationException("Line() must be called between Begin() and End()");
// Use the overload that handles the width, assuming a width of 1.0
return Line(startPos, length, angle, 1.0f, color);
}
/// <summary>
/// Draws a 1-pixel-wide line. This overload returns the end position of the line.
/// </summary>
/// <param name="startPos">The starting position of the line</param>
/// <param name="length">The length of the line</param>
/// <param name="angle">The angle of the line, in radians, measured from the positive x axis</param>
/// <param name="startColor">The color of the line at the starting position</param>
/// <param name="endColor">The color of the line at the ending position</param>
/// <returns>The end position of the line.</returns>
public static Vector2 Line(Vector2 startPos, float length, float angle, Color startColor, Color endColor)
{
if (!batchActive)
throw new InvalidOperationException("Line() must be called between Begin() and End()");
// Use the overload that handles the width, assuming a width of 1.0
return Line(startPos, length, angle, 1.0f, startColor, endColor);
}
/// <summary>
/// Draws a line. This overload returns the end position of the line.
/// </summary>
/// <param name="startPos">The starting position of the line</param>
/// <param name="length">The length of the line</param>
/// <param name="angle">The angle of the line, in radians, measured from the positive x axis</param>
/// <param name="width">The width of the line (minimum 1 pixel)</param>
/// <param name="color">The color of the line</param>
/// <returns>The end position of the line.</returns>
public static Vector2 Line(Vector2 startPos, float length, float angle, float width, Color color)
{
if (!batchActive)
throw new InvalidOperationException("Line() must be called between Begin() and End()");
// Calculate the ending position as an offset from the starting position
Vector2 endPos = startPos;
endPos += new Vector2(MathF.Cos(-angle), MathF.Sin(-angle)) * length;
// Use the width-checking helper to handle this line, then progress depth
BatchWideLine(startPos, endPos, width, color, color);
NextDepth();
// Return the ending position in the event that's useful to the caller
return endPos;
}
/// <summary>
/// Draws a line. This overload returns the end position of the line.
/// </summary>
/// <param name="startPos">The starting position of the line</param>
/// <param name="length">The length of the line</param>
/// <param name="angle">The angle of the line, in radians, measured from the positive x axis</param>
/// <param name="width">The width of the line (minimum 1 pixel)</param>
/// <param name="startColor">The color of the line at the starting position</param>
/// <param name="endColor">The color of the line at the ending position</param>
/// <returns>The end position of the line.</returns>
public static Vector2 Line(Vector2 startPos, float length, float angle, float width, Color startColor, Color endColor)
{
if (!batchActive)
throw new InvalidOperationException("Line() must be called between Begin() and End()");
// Calculate the ending position as an offset from the starting position
Vector2 endPos = startPos;
endPos += new Vector2(MathF.Cos(-angle), MathF.Sin(-angle)) * length;
// Use the width-checking helper to handle this line, then progress depth
BatchWideLine(startPos, endPos, width, startColor, endColor);
NextDepth();
// Return the ending position in the event that's useful to the caller
return endPos;
}
/// <summary>
/// Draws a solid (filled-in) box
/// </summary>
/// <param name="x">The x position of the top left corner of the box</param>
/// <param name="y">The y position of the top left corner of the box</param>
/// <param name="width">The width of the box</param>
/// <param name="height">The height of the box</param>
/// <param name="color">The color of the box</param>
public static void Box(float x, float y, float width, float height, Color color)
{
if (!batchActive)
throw new InvalidOperationException("Box() must be called between Begin() and End()");
// Create the corners
Vector2 topLeft = new Vector2(x, y);
Vector2 topRight = new Vector2(x + width, y);
Vector2 bottomRight = new Vector2(x + width, y + height);
Vector2 bottomLeft = new Vector2(x, y + height);
// Create the polygons
BatchPolygon(topLeft, bottomRight, bottomLeft, color, color, color);
BatchPolygon(topLeft, topRight, bottomRight, color, color, color);
NextDepth();
}
/// <summary>
/// Draws a solid (filled-in) box
/// </summary>
/// <param name="x">The x position of the top left corner of the box</param>
/// <param name="y">The y position of the top left corner of the box</param>
/// <param name="width">The width of the box</param>
/// <param name="height">The height of the box</param>
/// <param name="colorTopLeft">The color of the top left corner of the box</param>
/// <param name="colorTopRight">The color of the top right corner of the box</param>
/// <param name="colorBottomRight">The color of the bottom right corner of the box</param>
/// <param name="colorBottomLeft">The color of the bottom left corner of the box</param>
public static void Box(float x, float y, float width, float height, Color colorTopLeft, Color colorTopRight, Color colorBottomRight, Color colorBottomLeft)
{
if (!batchActive)
throw new InvalidOperationException("Box() must be called between Begin() and End()");
// Create the corners
Vector2 topLeft = new Vector2(x, y);
Vector2 topRight = new Vector2(x + width, y);
Vector2 bottomRight = new Vector2(x + width, y + height);
Vector2 bottomLeft = new Vector2(x, y + height);
// Create the polygons
BatchPolygon(topLeft, bottomRight, bottomLeft, colorTopLeft, colorBottomRight, colorBottomLeft);
BatchPolygon(topLeft, topRight, bottomRight, colorTopLeft, colorTopRight, colorBottomRight);
NextDepth();
}
/// <summary>
/// Draws a solid (filled-in) box
/// </summary>
/// <param name="rect">The rectangle specifying the box's position and size</param>
/// <param name="color">The color of the outline</param>
public static void Box(Rectangle rect, Color color)
{
if (!batchActive)
throw new InvalidOperationException("Box() must be called between Begin() and End()");
Box(rect.X, rect.Y, rect.Width, rect.Height, color);
}
/// <summary>
/// Draws a solid (filled-in) box
/// </summary>
/// <param name="rect">The rectangle specifying the box's position and size</param>
/// <param name="colorTopLeft">The color of the top left corner of the box</param>
/// <param name="colorTopRight">The color of the top right corner of the box</param>
/// <param name="colorBottomRight">The color of the bottom right corner of the box</param>
/// <param name="colorBottomLeft">The color of the bottom left corner of the box</param>
public static void Box(Rectangle rect, Color colorTopLeft, Color colorTopRight, Color colorBottomRight, Color colorBottomLeft)
{
if (!batchActive)
throw new InvalidOperationException("Box() must be called between Begin() and End()");
Box(rect.X, rect.Y, rect.Width, rect.Height, colorTopLeft, colorTopRight, colorBottomRight, colorBottomLeft);
}
/// <summary>
/// Draws the outline of a box
/// </summary>
/// <param name="x">The x position of the top left corner of the box</param>
/// <param name="y">The y position of the top left corner of the box</param>
/// <param name="width">The width of the box</param>
/// <param name="height">The height of the box</param>
/// <param name="color">The color of the outline</param>
public static void BoxOutline(float x, float y, float width, float height, Color color)
{
if (!batchActive)
throw new InvalidOperationException("BoxOutline() must be called between Begin() and End()");
// Create the corners
Vector2 topLeft = new Vector2(x, y);
Vector2 topRight = new Vector2(x + width, y);
Vector2 bottomRight = new Vector2(x + width, y + height);
Vector2 bottomLeft = new Vector2(x, y + height + 1); // This corner always rasterizes incorrectly, so adjust by 1 pixel
// Draw the four lines that make up the box
BatchLine(topLeft, topRight, color, color); // Top
BatchLine(topRight, bottomRight, color, color); // Right
BatchLine(bottomRight, bottomLeft, color, color); // Bottom
BatchLine(bottomLeft, topLeft, color, color); // Left
// Progress to the next depth
NextDepth();
}
/// <summary>
/// Draws the outline of a box
/// </summary>
/// <param name="x">The x position of the top left corner of the box</param>
/// <param name="y">The y position of the top left corner of the box</param>
/// <param name="width">The width of the box</param>
/// <param name="height">The height of the box</param>
/// <param name="colorTopLeft">The color of the top left corner of the box</param>
/// <param name="colorTopRight">The color of the top right corner of the box</param>
/// <param name="colorBottomRight">The color of the bottom right corner of the box</param>
/// <param name="colorBottomLeft">The color of the bottom left corner of the box</param>
public static void BoxOutline(float x, float y, float width, float height, Color colorTopLeft, Color colorTopRight, Color colorBottomRight, Color colorBottomLeft)
{
if (!batchActive)
throw new InvalidOperationException("BoxOutline() must be called between Begin() and End()");
// Create the corners
Vector2 topLeft = new Vector2(x, y);
Vector2 topRight = new Vector2(x + width, y);
Vector2 bottomRight = new Vector2(x + width, y + height);
Vector2 bottomLeft = new Vector2(x, y + height + 1); // This corner always rasterizes incorrectly, so adjust by 1 pixel
// Draw the four lines that make up the box
BatchLine(topLeft, topRight, colorTopLeft, colorTopRight); // Top
BatchLine(topRight, bottomRight, colorTopRight, colorBottomRight); // Right
BatchLine(bottomRight, bottomLeft, colorBottomRight, colorBottomLeft); // Bottom
BatchLine(bottomLeft, topLeft, colorBottomLeft, colorTopLeft); // Left
// Progress to the next depth
NextDepth();
}
/// <summary>
/// Draws the outline of a box
/// </summary>
/// <param name="rect">The rectangle specifying the box's position and size</param>
/// <param name="color">The color of the outline</param>
public static void BoxOutline(Rectangle rect, Color color)
{
if (!batchActive)
throw new InvalidOperationException("BoxOutline() must be called between Begin() and End()");
// Decompose the rectangle and call the other overload
BoxOutline(rect.X, rect.Y, rect.Width, rect.Height, color);
}
/// <summary>
/// Draws the outline of a box
/// </summary>
/// <param name="rect">The rectangle specifying the box's position and size</param>
/// <param name="colorTopLeft">The color of the top left corner of the box</param>
/// <param name="colorTopRight">The color of the top right corner of the box</param>
/// <param name="colorBottomRight">The color of the bottom right corner of the box</param>
/// <param name="colorBottomLeft">The color of the bottom left corner of the box</param>
public static void BoxOutline(Rectangle rect, Color colorTopLeft, Color colorTopRight, Color colorBottomRight, Color colorBottomLeft)
{
if (!batchActive)
throw new InvalidOperationException("BoxOutline() must be called between Begin() and End()");
// Decompose the rectangle and call the other overload
BoxOutline(rect.X, rect.Y, rect.Width, rect.Height, colorTopLeft, colorTopRight, colorBottomRight, colorBottomLeft);
}
/// <summary>
/// Draws a solid (filled-in) circle
/// </summary>
/// <param name="center">The position of the circle's center</param>
/// <param name="radius">The radius of the circle</param>
/// <param name="segments">The number of segments (triangles) that are used to fill in the circle. Minimum of 3.</param>
/// <param name="rotation">The rotation of the circle (this is much more obvious with fewer segments)</param>
/// <param name="color">The color of the circle</param>
public static void Circle(Vector2 center, float radius, int segments, float rotation, Color color)
{
if (!batchActive)
throw new InvalidOperationException("Circle() must be called between Begin() and End()");
// Call the overload that takes two colors
Circle(center, radius, segments, rotation, color, color);
}
/// <summary>
/// Draws a solid (filled-in) circle
/// </summary>
/// <param name="center">The position of the circle's center</param>
/// <param name="radius">The radius of the circle</param>
/// <param name="segments">The number of segments (triangles) that are used to fill in the circle. Minimum of 3.</param>
/// <param name="rotation">The rotation of the circle (this is much more obvious with fewer segments)</param>
/// <param name="colorCenter">The color of the center of the circle</param>
/// <param name="colorEdge">The color of the edge of the circle</param>
public static void Circle(Vector2 center, float radius, int segments, float rotation, Color colorCenter, Color colorEdge)
{
if (!batchActive)
throw new InvalidOperationException("Circle() must be called between Begin() and End()");
// Minimum of three segments
segments = Math.Max(1, segments);
// How far does each segment extend, in radians?
float step = MathF.PI * 2.0f / segments;
// Batch a triangle for each segment
for (int i = 0; i < segments; i++)
{
// The angle of each side of the triangle
float a0 = rotation + i * step;
float a1 = rotation + (i + 1) * step;
// The positions of the far vertices of the triangle
Vector2 pos0 = center + new Vector2(MathF.Cos(a0), MathF.Sin(a0)) * radius;
Vector2 pos1 = center + new Vector2(MathF.Cos(a1), MathF.Sin(a1)) * radius;
// Each triangle shares the center position of the circle
BatchPolygon(center, pos0, pos1, colorCenter, colorEdge, colorEdge);
}
// Progress to the next depth
NextDepth();
}
/// <summary>
/// Draws a solid (filled-in) circle
/// </summary>
/// <param name="center">The position of the circle's center</param>
/// <param name="radius">The radius of the circle</param>
/// <param name="segments">The number of segments (triangles) that are used to fill in the circle. Minimum of 3.</param>
/// <param name="color">The color of the circle</param>
public static void Circle(Vector2 center, float radius, int segments, Color color)
{
if (!batchActive)
throw new InvalidOperationException("Circle() must be called between Begin() and End()");
// Call the overload that takes a rotation
Circle(center, radius, segments, 0.0f, color, color);
}
/// <summary>
/// Draws a solid (filled-in) circle
/// </summary>
/// <param name="center">The position of the circle's center</param>
/// <param name="radius">The radius of the circle</param>
/// <param name="segments">The number of segments (triangles) that are used to fill in the circle. Minimum of 3.</param>
/// <param name="colorCenter">The color of the center of the circle</param>
/// <param name="colorEdge">The color of the edge of the circle</param>
public static void Circle(Vector2 center, float radius, int segments, Color colorCenter, Color colorEdge)
{
if (!batchActive)
throw new InvalidOperationException("Circle() must be called between Begin() and End()");
// Call the overload that takes a rotation
Circle(center, radius, segments, 0.0f, colorCenter, colorEdge);
}
/// <summary>
/// Draws a solid (filled-in) circle
/// </summary>
/// <param name="center">The position of the circle's center</param>
/// <param name="radius">The radius of the circle</param>
/// <param name="rotation">The rotation of the circle (this is much more obvious with fewer segments)</param>
/// <param name="color">The color of the circle</param>
public static void Circle(Vector2 center, float radius, Color color)
{
if (!batchActive)
throw new InvalidOperationException("Circle() must be called between Begin() and End()");
// Call the overload that calculates the number of segments
Circle(center, radius, color, color);
}
/// <summary>
/// Draws a solid (filled-in) circle
/// </summary>
/// <param name="center">The position of the circle's center</param>
/// <param name="radius">The radius of the circle</param>
/// <param name="rotation">The rotation of the circle (this is much more obvious with fewer segments)</param>
/// <param name="colorCenter">The color of the center of the circle</param>
/// <param name="colorEdge">The color of the edge of the circle</param>
public static void Circle(Vector2 center, float radius, Color colorCenter, Color colorEdge)
{
if (!batchActive)
throw new InvalidOperationException("Circle() must be called between Begin() and End()");
// Calculate an appropriate number of segments based on radius
float angle = MathF.Asin(DefaultCircleSegmentLength / radius);
int segments = (int)(MathF.PI * 2.0f / angle);
Circle(center, radius, segments, 0.0f, colorCenter, colorEdge);
}
/// <summary>
/// Draws the outline of a circle
/// </summary>
/// <param name="center">The position of the circle's center</param>
/// <param name="radius">The radius of the circle</param>
/// <param name="segments">The number of segments (lines) that make up the circle. Minimum of 3.</param>
/// <param name="rotation">The rotation of the circle (this is much more obvious with fewer segments)</param>
/// <param name="color">The color of the circle</param>
public static void CircleOutline(Vector2 center, float radius, int segments, float rotation, Color color)
{
if (!batchActive)
throw new InvalidOperationException("CircleOutline() must be called between Begin() and End()");
// Minimum of three segments
segments = Math.Max(1, segments);
// How far does each segment extend, in radians?
float step = MathF.PI * 2.0f / segments;
// Batch a line for each segment
for (int i = 0; i < segments; i++)
{
// The angle of each line endpoint
float a0 = rotation + i * step;
float a1 = rotation + (i + 1) * step;
// The positions line endpoints
Vector2 pos0 = center + new Vector2(MathF.Cos(a0), MathF.Sin(a0)) * radius;
Vector2 pos1 = center + new Vector2(MathF.Cos(a1), MathF.Sin(a1)) * radius;
// Batch a single line
BatchLine(pos0, pos1, color, color);
}
// Progress to the next depth
NextDepth();
}
/// <summary>
/// Draws the outline of a circle
/// </summary>
/// <param name="center">The position of the circle's center</param>
/// <param name="radius">The radius of the circle</param>
/// <param name="segments">The number of segments (lines) that make up the circle. Minimum of 3.</param>
/// <param name="color">The color of the circle</param>
public static void CircleOutline(Vector2 center, float radius, int segments, Color color)
{
if (!batchActive)
throw new InvalidOperationException("CircleOutline() must be called between Begin() and End()");
CircleOutline(center, radius, segments, 0.0f, color);
}
/// <summary>
/// Draws the outline of a circle
/// </summary>
/// <param name="center">The position of the circle's center</param>
/// <param name="radius">The radius of the circle</param>
/// <param name="color">The color of the circle</param>
public static void CircleOutline(Vector2 center, float radius, Color color)
{
if (!batchActive)
throw new InvalidOperationException("CircleOutline() must be called between Begin() and End()");
// Calculate an appropriate number of segments based on radius
float angle = MathF.Asin(DefaultCircleSegmentLength / radius);
int segments = (int)(MathF.PI * 2.0f / angle);
CircleOutline(center, radius, segments, 0.0f, color);
}
/// <summary>
/// Draws a solid (filled-in) triangle
/// </summary>
/// <param name="p0">The position of the first vertex</param>
/// <param name="p1">The position of the second vertex</param>
/// <param name="p2">The position of the third vertex</param>
/// <param name="color">The color of the triangle</param>
public static void Triangle(Vector2 p0, Vector2 p1, Vector2 p2, Color color)
{
if (!batchActive)
throw new InvalidOperationException("Triangle() must be called between Begin() and End()");
// Call the overload that takes 3 colors
Triangle(p0, p1, p2, color, color, color);
}
/// <summary>
/// Draws a solid (filled-in) triangle
/// </summary>
/// <param name="p0">The position of the first vertex</param>
/// <param name="p1">The position of the second vertex</param>
/// <param name="p2">The position of the third vertex</param>
/// <param name="color0">The color of the first vertex</param>
/// <param name="color1">The color of the second vertex</param>
/// <param name="color2">The color of the third vertex</param>
public static void Triangle(Vector2 p0, Vector2 p1, Vector2 p2, Color color0, Color color1, Color color2)
{
if (!batchActive)
throw new InvalidOperationException("Triangle() must be called between Begin() and End()");
// Need to make sure we're in clockwise winding order!
// Calculate vectors running down the edges of the triangle
Vector3 edge0 = new Vector3(p1 - p0, 0);
Vector3 edge1 = new Vector3(p2 - p0, 0);
// The cross product of those edges is a vector perpendicular
// to the other two
Vector3 cross = Vector3.Cross(edge0, edge1);
// Check the Z value of the cross product to determine
// if we need to swap the winding order of the vertices
if (cross.Z >= 0)
{
BatchPolygon(p0, p1, p2, color0, color1, color2); // 0, 1, 2
}
else
{
BatchPolygon(p0, p2, p1, color0, color2, color1); // 0, 2, 1
}
// Progress to the next depth
NextDepth();
}
/// <summary>
/// Draws a solid (filled-in) equilateral triangle
/// </summary>
/// <param name="center">The center of the triangle (equidistant from all three vertices)</param>
/// <param name="height">The height of the triangle as measued from the top vertex to the base</param>
/// <param name="rotation">The rotation of the triangle</param>
/// <param name="color">The color of the triangle</param>
public static void Triangle(Vector2 center, float height, float rotation, Color color)
{
if (!batchActive)
throw new InvalidOperationException("Triangle() must be called between Begin() and End()");
// Call the overload that takes 3 colors
Triangle(center, height, rotation, color, color, color);
}
/// <summary>
/// Draws a solid (filled-in) equilateral triangle
/// </summary>
/// <param name="center">The center of the triangle (equidistant from all three vertices)</param>
/// <param name="height">The height of the triangle as measued from the top vertex to the base</param>
/// <param name="rotation">The rotation of the triangle</param>
/// <param name="color0">The color of the first vertex</param>
/// <param name="color1">The color of the second vertex</param>
/// <param name="color2">The color of the third vertex</param>
public static void Triangle(Vector2 center, float height, float rotation, Color color0, Color color1, Color color2)
{
if (!batchActive)
throw new InvalidOperationException("Triangle() must be called between Begin() and End()");
// Pre-calculations to speed things up
float rad30 = MathF.PI / 6.0f; // 30 degrees in radians
float tan30 = MathF.Tan(rad30); // Tangent of 30 degrees
// Calculate various lengths from the height
float halfBase = tan30 * height;
float centerToBase = tan30 * halfBase;
float centerToTop = height - centerToBase;
// Angles to vertices, including custom rotation
float topAngle = -MathF.PI / 2 + rotation;
float brAngle = rad30 + rotation;
float blAngle = rad30 * 5 + rotation;
// Offsets to vertices from center
Vector2 topOffset = new Vector2(MathF.Cos(topAngle), MathF.Sin(topAngle)) * centerToTop;
Vector2 brOffset = new Vector2(MathF.Cos(brAngle), MathF.Sin(brAngle)) * centerToTop;
Vector2 blOffset = new Vector2(MathF.Cos(blAngle), MathF.Sin(blAngle)) * centerToTop;
// Actual pixel vertex locations
Vector2 v0 = center + topOffset;
Vector2 v1 = center + brOffset;
Vector2 v2 = center + blOffset;
// Draw the triangle
Triangle(v0, v1, v2, color0, color1, color2);
}
/// <summary>
/// Draws a solid (filled-in) equilateral triangle
/// </summary>
/// <param name="center">The center of the triangle (equidistant from all three vertices)</param>
/// <param name="height">The height of the triangle as measued from the top vertex to the base</param>
/// <param name="color">The color of the triangle</param>
public static void Triangle(Vector2 center, float height, Color color)
{
if (!batchActive)
throw new InvalidOperationException("Triangle() must be called between Begin() and End()");
// Draw the triangle using a default rotation
Triangle(center, height, 0.0f, color, color, color);
}
/// <summary>
/// Draws a solid (filled-in) equilateral triangle
/// </summary>
/// <param name="center">The center of the triangle (equidistant from all three vertices)</param>
/// <param name="height">The height of the triangle as measued from the top vertex to the base</param>
/// <param name="color">The color of the triangle</param>
public static void Triangle(Vector2 center, float height, Color color0, Color color1, Color color2)
{
if (!batchActive)
throw new InvalidOperationException("Triangle() must be called between Begin() and End()");
// Draw the triangle using a default rotation
Triangle(center, height, 0.0f, color0, color1, color2);
}
/// <summary>
/// Draws the outline of a triangle
/// </summary>
/// <param name="p0">The position of the first vertex</param>
/// <param name="p1">The position of the second vertex</param>
/// <param name="p2">The position of the third vertex</param>
/// <param name="color">The color of the triangle</param>
public static void TriangleOutline(Vector2 p0, Vector2 p1, Vector2 p2, Color color)
{
if (!batchActive)
throw new InvalidOperationException("TriangleOutline() must be called between Begin() and End()");
// Call the overload that takes 3 colors
TriangleOutline(p0, p1, p2, color, color, color);
}
/// <summary>
/// Draws the outline of a triangle
/// </summary>
/// <param name="p0">The position of the first vertex</param>
/// <param name="p1">The position of the second vertex</param>
/// <param name="p2">The position of the third vertex</param>
/// <param name="color0">The color of the first vertex</param>
/// <param name="color1">The color of the second vertex</param>
/// <param name="color2">The color of the third vertex</param>
public static void TriangleOutline(Vector2 p0, Vector2 p1, Vector2 p2, Color color0, Color color1, Color color2)
{
if (!batchActive)
throw new InvalidOperationException("TriangleOutline() must be called between Begin() and End()");
// Batch lines between the vertices
BatchLine(p0, p1, color0, color1);
BatchLine(p1, p2, color1, color2);
BatchLine(p2, p0, color2, color0);
// Progress to the next depth
NextDepth();
}
/// <summary>
/// Draws the outline of a triangle
/// </summary>
/// <param name="center">The center of the triangle (equidistant from all three vertices)</param>
/// <param name="height">The height of the triangle as measued from the top vertex to the base</param>
/// <param name="rotation">The rotation of the triangle</param>
/// <param name="color">The color of the triangle</param>
public static void TriangleOutline(Vector2 center, float height, float rotation, Color color)
{
if (!batchActive)
throw new InvalidOperationException("TriangleOutline() must be called between Begin() and End()");
// Call the overload that takes 3 colors
TriangleOutline(center, height, rotation, color, color, color);
}
/// <summary>
/// Draws the outline of a triangle
/// </summary>
/// <param name="center">The center of the triangle (equidistant from all three vertices)</param>
/// <param name="height">The height of the triangle as measued from the top vertex to the base</param>
/// <param name="rotation">The rotation of the triangle</param>
/// <param name="color0">The color of the first vertex</param>
/// <param name="color1">The color of the second vertex</param>
/// <param name="color2">The color of the third vertex</param>
public static void TriangleOutline(Vector2 center, float height, float rotation, Color color0, Color color1, Color color2)
{
if (!batchActive)
throw new InvalidOperationException("TriangleOutline() must be called between Begin() and End()");
// Pre-calculations to speed things up
float rad30 = MathF.PI / 6.0f; // 30 degrees in radians
float tan30 = MathF.Tan(rad30); // Tangent of 30 degrees
// Calculate various lengths from the height
float halfBase = tan30 * height;
float centerToBase = tan30 * halfBase;
float centerToTop = height - centerToBase;
// Angles to vertices, including custom rotation
float topAngle = -MathF.PI / 2 + rotation;
float brAngle = rad30 + rotation;
float blAngle = rad30 * 5 + rotation;
// Offsets to vertices from center
Vector2 topOffset = new Vector2(MathF.Cos(topAngle), MathF.Sin(topAngle)) * centerToTop;
Vector2 brOffset = new Vector2(MathF.Cos(brAngle), MathF.Sin(brAngle)) * centerToTop;
Vector2 blOffset = new Vector2(MathF.Cos(blAngle), MathF.Sin(blAngle)) * centerToTop;
// Actual pixel vertex locations
Vector2 v0 = center + topOffset;
Vector2 v1 = center + brOffset;
Vector2 v2 = center + blOffset;
// Batch lines between the vertices
BatchLine(v0, v1, color0, color1);
BatchLine(v1, v2, color1, color2);
BatchLine(v2, v0, color2, color0);
// Progress to the next depth
NextDepth();
}
/// <summary>
/// Draws the outline of a triangle
/// </summary>
/// <param name="center">The center of the triangle (equidistant from all three vertices)</param>
/// <param name="height">The height of the triangle as measued from the top vertex to the base</param>
/// <param name="color">The color of the triangle</param>
public static void TriangleOutline(Vector2 center, float height, Color color)
{
if (!batchActive)
throw new InvalidOperationException("TriangleOutline() must be called between Begin() and End()");
// Draw the triangle using a default rotation
TriangleOutline(center, height, 0.0f, color, color, color);
}
/// <summary>
/// Draws the outline of a triangle