This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
/
DrawPrimitivesTest.cs
1027 lines (779 loc) · 31.7 KB
/
DrawPrimitivesTest.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CocosSharp;
namespace tests
{
public class BaseDrawNodeTest : TestNavigationLayer
{
#region Properties
public override string Title
{
get { return "Draw Demo"; }
}
#endregion Properties
#region Constructors
public BaseDrawNodeTest()
{
}
#endregion Constructors
#region Setup content
public virtual void Setup()
{
}
#endregion Setup content
#region Callbacks
public override void RestartCallback(object sender)
{
CCScene s = new DrawPrimitivesTestScene();
s.AddChild(DrawPrimitivesTestScene.restartTestAction());
Director.ReplaceScene(s);
}
public override void NextCallback(object sender)
{
CCScene s = new DrawPrimitivesTestScene();
s.AddChild(DrawPrimitivesTestScene.nextTestAction());
Director.ReplaceScene(s);
}
public override void BackCallback(object sender)
{
CCScene s = new DrawPrimitivesTestScene();
s.AddChild(DrawPrimitivesTestScene.backTestAction());
Director.ReplaceScene(s);
}
#endregion Callbacks
}
public class DrawNodeWithRenderTextureTest : BaseDrawNodeTest
{
#region Setup content
public override string Subtitle
{
get
{
return "RenderTarget CCDrawNode Visit";
}
}
public override void OnEnter()
{
base.OnEnter();
var windowSize = Layer.VisibleBoundsWorldspace.Size;
CCRenderTexture text = new CCRenderTexture(windowSize,windowSize);
text.Sprite.Position = windowSize.Center;
AddChild(text.Sprite, 24);
CCDrawNode draw = new CCDrawNode();
// Draw polygons
CCPoint[] points = new CCPoint[]
{
new CCPoint(windowSize.Height / 4, 0),
new CCPoint(windowSize.Width, windowSize.Height / 5),
new CCPoint(windowSize.Width / 3 * 2, windowSize.Height)
};
draw.DrawPolygon(points, points.Length, new CCColor4F(1, 0, 0, 0.5f), 4, new CCColor4F(0, 0, 1, 1));
draw.AnchorPoint = CCPoint.AnchorLowerLeft;
text.Begin();
draw.Visit();
text.End();
}
#endregion Setup content
}
public class DrawNodeWithRenderTextureTest1 : BaseDrawNodeTest
{
#region Setup content
public override string Subtitle
{
get
{
return "Test RenderTarget Clipping with CCDrawNode as Child";
}
}
public override void OnEnter()
{
base.OnEnter();
CCDrawNode circle = new CCDrawNode();
circle.DrawSolidCircle(new CCPoint(150.0f, 150.0f), 75.0f, new CCColor4B(255, 255, 255, 255));
CCRenderTexture rtm = new CCRenderTexture(new CCSize(200.0f, 200.0f), new CCSize(200.0f, 200.0f), CCSurfaceFormat.Color, CCDepthFormat.Depth24Stencil8);
rtm.BeginWithClear(CCColor4B.Orange);
circle.Visit();
rtm.End();
// Make sure our children nodes get visited
rtm.Sprite.Position = VisibleBoundsWorldspace.Center;
rtm.Sprite.AnchorPoint = CCPoint.AnchorMiddle;
AddChild(rtm.Sprite);
}
#endregion Setup content
}
public class DrawNodeWithRenderTextureTest2 : BaseDrawNodeTest
{
#region Setup content
public override string Subtitle
{
get
{
return "Test RenderTarget Clipping with CCDrawNode Visit";
}
}
public override void OnEnter()
{
base.OnEnter();
CCDrawNode circle = new CCDrawNode();
circle.DrawSolidCircle(new CCPoint(150.0f, 150.0f), 75.0f, new CCColor4B(255, 255, 255, 255));
CCRenderTexture rtm = new CCRenderTexture(new CCSize(200.0f, 200.0f), new CCSize(200.0f, 200.0f), CCSurfaceFormat.Color, CCDepthFormat.Depth24Stencil8);
rtm.BeginWithClear(CCColor4B.Orange);
circle.Visit(); // Draw to rendertarget
rtm.End();
rtm.Sprite.Position = VisibleBoundsWorldspace.Center;
rtm.Sprite.AnchorPoint = CCPoint.AnchorMiddle;
var sprite = rtm.Sprite;
sprite.AnchorPoint = CCPoint.AnchorMiddle;
sprite.Position = VisibleBoundsWorldspace.Center;
AddChild(sprite);
}
#endregion Setup content
}
public class DrawNodeWithRenderTextureTest3 : BaseDrawNodeTest
{
#region Setup content
public override string Subtitle
{
get
{
return "Test RenderTarget Clipping with CCDrawNode Visit to Sprite";
}
}
public override void OnEnter()
{
base.OnEnter();
CCDrawNode circle = new CCDrawNode();
circle.DrawSolidCircle(new CCPoint(150.0f, 150.0f), 75.0f, new CCColor4B(255, 255, 255, 255));
CCRenderTexture rtm = new CCRenderTexture(new CCSize(200.0f, 200.0f), new CCSize(200.0f, 200.0f), CCSurfaceFormat.Color, CCDepthFormat.Depth24Stencil8);
rtm.BeginWithClear(CCColor4B.Orange);
circle.Visit(); // Draw to rendertarget
rtm.End();
// Create a new sprite from the render target texture
var sprite = new CCSprite(rtm.Texture);
sprite.Position = VisibleBoundsWorldspace.Center;
AddChild(sprite);
}
#endregion Setup content
}
public class DrawPrimitivesTest : BaseDrawNodeTest
{
public DrawPrimitivesTest() : base()
{
//RenderDrawPrimTest ();
}
public override void OnEnter()
{
base.OnEnter();
RenderDrawPrimTest();
}
void RenderDrawPrimTest()
{
CCSize size = Layer.VisibleBoundsWorldspace.Size;
var visibleRect = VisibleBoundsWorldspace;
CCDrawNode drawNode = new CCDrawNode ();
// *NOTE* Using the Director.ContentScaleFactor for now until we work something out with the initialization
// CCDrawPriitives should be able to do this converstion themselves.
// draw a simple line
// The default state is:
// Line Width: 1
// color: 255,255,255,255 (white, non-transparent)
// Anti-Aliased
// glEnable(GL_LINE_SMOOTH);
float lineWidth = 1.0f;
CCColor4B lineColor = CCColor4B.White;
drawNode.DrawLine(visibleRect.LeftBottom(), visibleRect.RightTop(), lineWidth);
// line: color, width, aliased
lineWidth = 5.0f ;
lineColor = CCColor4B.Red;
drawNode.DrawLine(visibleRect.LeftTop (), visibleRect.RightBottom (), lineWidth, lineColor);
// TIP:
// If you are going to use always thde same color or width, you don't
// need to call it before every draw
//
// draw big point in the center
drawNode.DrawSolidCircle(visibleRect.Center(), 64.0f, new CCColor4B(0, 0, 255, 128));
// draw 4 small points
CCPoint[] points = {new CCPoint(60, 60), new CCPoint(70, 70), new CCPoint(60, 70), new CCPoint(70, 60)};
foreach (CCPoint point in points)
{
drawNode.DrawSolidCircle (point, 8.0f, new CCColor4B (0, 255, 255, 255));
}
// draw a green circle with 10 segments
drawNode.DrawCircle(visibleRect.Center, 100.0f, 10, CCColor4B.Green);
// draw an arc
drawNode.DrawSolidArc(visibleRect.Center, 10.0f, 0, 180, CCColor4B.AliceBlue);
// draw an ellipse within rectangular region
drawNode.DrawEllipse(new CCRect(100, 100, 100, 200), 10.0f, CCColor4B.Red);
// open yellow poly
CCPoint[] vertices =
{
new CCPoint(0, 0), new CCPoint(50, 50), new CCPoint(100, 50), new CCPoint(100, 100),
new CCPoint(50, 100)
};
// draw quad bezier path
drawNode.DrawQuadBezier(
new CCPoint(0, size.Height),
visibleRect.Center,
(CCPoint)visibleRect.Size,
50,
10.0f,
new CCColor4B(255, 0, 255, 255));
// draw cubic bezier path
drawNode.DrawCubicBezier(
visibleRect.Center,
new CCPoint(size.Width / 2 + 30, size.Height / 2 + 50),
new CCPoint(size.Width / 2 + 60, size.Height / 2 - 50),
new CCPoint(size.Width, size.Height / 2), 100, 10.0f,
new CCColor4B(255, 0, 255, 255));
//draw a solid polygon
CCPoint[] vertices3 =
{
new CCPoint(60, 160), new CCPoint(70, 190), new CCPoint(100, 190),
new CCPoint(90, 160)
};
drawNode.DrawPolygon(vertices3, 4, new CCColor4F(1,1,0,1), 1.0f, new CCColor4F(1,1,0,1));
AddChild(drawNode);
}
public override string Title
{
get
{
return "Draw Primitives";
}
}
public override string Subtitle
{
get
{
return "Drawing Primitives. Use DrawNode instead";
}
}
}
public class GeometryBatchTest1 : BaseDrawNodeTest
{
CCTexture2D texture;
CCGeometryNode geoBatch;
public GeometryBatchTest1 () : base()
{
texture = CCTextureCache.SharedTextureCache.AddImage("Images/CyanSquare.png");
//texture = CCTextureCache.SharedTextureCache.AddImage("Images/BackGround.png");
}
protected override void AddedToScene()
{
base.AddedToScene();
CreateGeom();
}
void CreateGeom()
{
geoBatch = new CCGeometryNode();
AddChild(geoBatch);
var visibleRect = VisibleBoundsWorldspace;
var item = geoBatch.CreateGeometryInstance(3, 3);
var vertices = item.GeometryPacket.Vertices;
var windowSize = VisibleBoundsWorldspace.Size;
var packet = item.GeometryPacket;
packet.Texture = texture;
item.GeometryPacket = packet;
// Draw polygons
vertices[0].Colors = CCColor4B.White;
vertices[1].Colors = CCColor4B.White;
vertices[2].Colors = CCColor4B.White;
// Texture coordinates use a normalzed value 0 to 1
vertices[0].TexCoords.U = 0;
vertices[0].TexCoords.V = 0;
vertices[1].TexCoords.U = 1;
vertices[1].TexCoords.V = 0;
vertices[2].TexCoords.U = 1;
vertices[2].TexCoords.V = 1;
vertices[0].Vertices.X = 0;
vertices[0].Vertices.Y = 0;
vertices[1].Vertices.X = texture.PixelsWide;
vertices[1].Vertices.Y = 0;
vertices[2].Vertices.X = texture.PixelsWide;
vertices[2].Vertices.Y = texture.PixelsHigh;
item.GeometryPacket.Indicies = new int[] { 2, 1, 0 };
var rotation = CCAffineTransform.Identity;
rotation.Rotation = (float)Math.PI / 4.0f;
rotation.Tx = windowSize.Center.X - texture.PixelsWide / 2;
rotation.Ty = windowSize.Center.Y - texture.PixelsHigh / 2;
item.InstanceAttributes.AdditionalTransform = rotation;
}
public override string Title
{
get
{
return "Geometry Node";
}
}
public override string Subtitle
{
get
{
return "Auto Clear of instances and transform";
}
}
}
public class GeometryBatchTest2 : BaseDrawNodeTest
{
CCTexture2D texture;
CCGeometryNode geoBatch = new CCGeometryNode();
public GeometryBatchTest2 () : base()
{
texture = CCTextureCache.SharedTextureCache.AddImage("Images/CyanSquare.png");
//texture = CCTextureCache.SharedTextureCache.AddImage("Images/BackGround.png");
AddChild(geoBatch);
}
protected override void AddedToScene()
{
base.AddedToScene();
var visibleRect = VisibleBoundsWorldspace;
var item = geoBatch.CreateGeometryInstance(3, 3);
var vertices = item.GeometryPacket.Vertices;
var windowSize = VisibleBoundsWorldspace.Size;
var packet = item.GeometryPacket;
packet.Texture = texture;
item.GeometryPacket = packet;
// Draw polygons
vertices[0].Colors = CCColor4B.White;
vertices[1].Colors = CCColor4B.White;
vertices[2].Colors = CCColor4B.White;
// Texture coordinates use a normalzed value 0 to 1
vertices[0].TexCoords.U = 0;
vertices[0].TexCoords.V = 0;
vertices[1].TexCoords.U = 1;
vertices[1].TexCoords.V = 0;
vertices[2].TexCoords.U = 1;
vertices[2].TexCoords.V = 1;
vertices[0].Vertices.X = 0;
vertices[0].Vertices.Y = 0;
vertices[1].Vertices.X = texture.PixelsWide;
vertices[1].Vertices.Y = 0;
vertices[2].Vertices.X = texture.PixelsWide;
vertices[2].Vertices.Y = texture.PixelsHigh;
item.GeometryPacket.Indicies = new int[] { 2, 1, 0 };
var rotation = CCAffineTransform.Identity;
rotation.Rotation = (float)Math.PI / 4.0f;
rotation.Tx = windowSize.Center.X - texture.PixelsWide / 2;
rotation.Ty = windowSize.Center.Y - texture.PixelsHigh / 2;
item.InstanceAttributes.AdditionalTransform = rotation;
}
public override void OnExit()
{
geoBatch.ClearInstances();
base.OnExit();
}
public override string Title
{
get
{
return "Geometry Batch";
}
}
public override string Subtitle
{
get
{
return "No Auto Clear of instances";
}
}
}
public class DrawNodeTest : BaseDrawNodeTest
{
#region Setup content
CCDrawNode draw;
public DrawNodeTest()
{
draw = new CCDrawNode();
draw.BlendFunc = CCBlendFunc.NonPremultiplied;
AddChild(draw, 10);
}
public override void OnEnter()
{
base.OnEnter();
CCSize windowSize = Layer.VisibleBoundsWorldspace.Size;
CCDrawNode draw = new CCDrawNode();
AddChild(draw, 10);
var s = windowSize;
// Draw 10 circles
for (int i = 0; i < 10; i++)
{
draw.DrawSolidCircle(s.Center, 10 * (10 - i),
new CCColor4F(CCRandom.Float_0_1(), CCRandom.Float_0_1(), CCRandom.Float_0_1(), 1));
}
// Draw polygons
CCPoint[] points = new CCPoint[]
{
new CCPoint(windowSize.Height / 4, 0),
new CCPoint(windowSize.Width, windowSize.Height / 5),
new CCPoint(windowSize.Width / 3 * 2, windowSize.Height)
};
draw.DrawPolygon(points, points.Length, new CCColor4F(1.0f, 0, 0, 0.5f), 4, new CCColor4F(0, 0, 1, 1));
// star poly (triggers buggs)
{
const float o = 80;
const float w = 20;
const float h = 50;
CCPoint[] star = new CCPoint[]
{
new CCPoint(o + w, o - h), new CCPoint(o + w * 2, o), // lower spike
new CCPoint(o + w * 2 + h, o + w), new CCPoint(o + w * 2, o + w * 2), // right spike
};
draw.DrawPolygon(star, star.Length, new CCColor4F(1, 0, 0, 0.5f), 1, new CCColor4F(0, 0, 1, 1));
}
// star poly (doesn't trigger bug... order is important un tesselation is supported.
{
const float o = 180;
const float w = 20;
const float h = 50;
var star = new CCPoint[]
{
new CCPoint(o, o), new CCPoint(o + w, o - h), new CCPoint(o + w * 2, o), // lower spike
new CCPoint(o + w * 2 + h, o + w), new CCPoint(o + w * 2, o + w * 2), // right spike
new CCPoint(o + w, o + w * 2 + h), new CCPoint(o, o + w * 2), // top spike
new CCPoint(o - h, o + w), // left spike
};
draw.DrawPolygon(star, star.Length, new CCColor4F(1, 0, 0, 0.5f), 1, new CCColor4F(0, 0, 1, 1));
}
// Draw segment
draw.DrawLine(new CCPoint(20, windowSize.Height), new CCPoint(20, windowSize.Height / 2), 10, new CCColor4F(0, 1, 0, 1), CCLineCap.Round);
draw.DrawLine(new CCPoint(10, windowSize.Height / 2), new CCPoint(windowSize.Width / 2, windowSize.Height / 2), 40,
new CCColor4F(1, 0, 1, 0.5f), CCLineCap.Round);
CCSize size = Layer.VisibleBoundsWorldspace.Size;
var visibleRect = VisibleBoundsWorldspace;
// draw quad bezier path
draw.DrawQuadBezier(new CCPoint(0, size.Height),
visibleRect.Center,
(CCPoint)visibleRect.Size,
50, 3,
new CCColor4B(255, 0, 255, 255));
// draw cubic bezier path
draw.DrawCubicBezier(visibleRect.Center,
new CCPoint(size.Width / 2 + 30, size.Height / 2 + 50),
new CCPoint(size.Width / 2 + 60, size.Height / 2 - 50),
new CCPoint(size.Width, size.Height / 2), 100, 2, CCColor4B.Green);
// draw an ellipse within rectangular region
draw.DrawEllipse(new CCRect(100, 300, 100, 200), 8, CCColor4B.AliceBlue);
var splinePoints = new List<CCPoint>();
splinePoints.Add(new CCPoint(0, 0));
splinePoints.Add(new CCPoint(50, 70));
splinePoints.Add(new CCPoint(0, 140));
splinePoints.Add(new CCPoint(100, 210));
splinePoints.Add(new CCPoint(0, 280));
splinePoints.Add(new CCPoint(150, 350));
int numberOfSegments = 64;
float tension = .05f;
draw.DrawCardinalSpline(splinePoints, tension, numberOfSegments);
draw.DrawSolidArc(
pos: new CCPoint(350, windowSize.Height * 0.75f),
radius: 100,
startAngle: CCMathHelper.ToRadians(45),
sweepAngle: CCMathHelper.Pi / 2, // this is in radians, clockwise
color: CCColor4B.Aquamarine);
}
#endregion Setup content
}
public class DrawNodeTest1 : BaseDrawNodeTest
{
#region Setup content
CCDrawNode draw;
public DrawNodeTest1()
{
draw = new CCDrawNode();
draw.BlendFunc = CCBlendFunc.NonPremultiplied;
AddChild(draw, 10);
}
public override string Subtitle
{
get
{
return "Using DrawTriangleList user defined geometry";
}
}
public override void OnEnter()
{
base.OnEnter();
CCSize windowSize = Layer.VisibleBoundsWorldspace.Size;
CCDrawNode draw = new CCDrawNode();
AddChild(draw, 10);
var s = windowSize;
// Draw 10 circles
for (int i = 0; i < 10; i++)
{
draw.DrawSolidCircle(s.Center, 10 * (10 - i),
new CCColor4F(CCRandom.Float_0_1(), CCRandom.Float_0_1(), CCRandom.Float_0_1(), 1));
}
// Draw polygons
CCV3F_C4B[] points = new CCV3F_C4B[3];
points[0].Colors = CCColor4B.Red;
points[0].Colors.A = 127;
points[1].Colors = CCColor4B.Green;
points[1].Colors.A = 127;
points[2].Colors = CCColor4B.Blue;
points[2].Colors.A = 127;
points[0].Vertices.X = windowSize.Height / 4;
points[0].Vertices.Y = 0;
points[1].Vertices.X = windowSize.Width;
points[1].Vertices.Y = windowSize.Height / 5;
points[2].Vertices.X = windowSize.Width / 3 * 2;
points[2].Vertices.Y = windowSize.Height;
draw.DrawTriangleList(points);
// star poly (triggers buggs)
{
const float o = 80;
const float w = 20;
const float h = 50;
CCPoint[] star = new CCPoint[]
{
new CCPoint(o + w, o - h), new CCPoint(o + w * 2, o), // lower spike
new CCPoint(o + w * 2 + h, o + w), new CCPoint(o + w * 2, o + w * 2), // right spike
};
draw.DrawPolygon(star, star.Length, new CCColor4F(1, 0, 0, 0.5f), 1, new CCColor4F(0, 0, 1, 1));
}
// star poly (doesn't trigger bug... order is important un tesselation is supported.
{
const float o = 180;
const float w = 20;
const float h = 50;
var star = new CCPoint[]
{
new CCPoint(o, o), new CCPoint(o + w, o - h), new CCPoint(o + w * 2, o), // lower spike
new CCPoint(o + w * 2 + h, o + w), new CCPoint(o + w * 2, o + w * 2), // right spike
new CCPoint(o + w, o + w * 2 + h), new CCPoint(o, o + w * 2), // top spike
new CCPoint(o - h, o + w), // left spike
};
draw.DrawPolygon(star, star.Length, new CCColor4F(1, 0, 0, 0.5f), 1, new CCColor4F(0, 0, 1, 1));
}
// Draw segment
draw.DrawLine(new CCPoint(20, windowSize.Height), new CCPoint(20, windowSize.Height / 2), 10, new CCColor4F(0, 1, 0, 1), CCLineCap.Round);
draw.DrawLine(new CCPoint(10, windowSize.Height / 2), new CCPoint(windowSize.Width / 2, windowSize.Height / 2), 40,
new CCColor4F(1, 0, 1, 0.5f), CCLineCap.Round);
}
#endregion Setup content
}
public class DrawNodeTestBlend : BaseDrawNodeTest
{
#region Setup content
CCDrawNode triangleList1;
CCDrawNode triangleList2;
public DrawNodeTestBlend()
{
triangleList1 = new CCDrawNode();
triangleList1.BlendFunc = CCBlendFunc.NonPremultiplied;
AddChild(triangleList1, 10);
triangleList2 = new CCDrawNode();
// The default BlendFunc is CCBlendFunc.AlphaBlend
//triangleList2.BlendFunc = CCBlendFunc.AlphaBlend;
AddChild(triangleList2, 10);
}
public override string Subtitle
{
get
{
return "Using DrawTriangleList With BlendFunc";
}
}
public override void OnEnter()
{
base.OnEnter();
CCSize windowSize = Layer.VisibleBoundsWorldspace.Size;
var line = new CCDrawNode();
line.DrawLine(new CCPoint(0, windowSize.Height / 2), new CCPoint(windowSize.Width, windowSize.Height / 2), 10);
AddChild(line, 0);
byte alpha = 100; // 255 is full opacity
var green = new CCColor4B(0, 255, 0, alpha);
CCV3F_C4B[] verts = new CCV3F_C4B[] {
new CCV3F_C4B( new CCPoint(0,0), green),
new CCV3F_C4B( new CCPoint(30,60), green),
new CCV3F_C4B( new CCPoint(60,0), green)
};
triangleList1.DrawTriangleList (verts);
triangleList1.Position = windowSize.Center;
triangleList1.PositionX -= windowSize.Width / 4;
triangleList1.PositionY -= triangleList1.ContentSize.Center.Y;
// Because the default BlendFunc of our DrawNode is AlphaBlend we need
// to pass the colors as pre multiplied alpha.
var greenPreMultiplied = green;
greenPreMultiplied.G = (byte)(greenPreMultiplied.G * alpha / 255.0f);
verts[0].Colors = greenPreMultiplied;
verts[1].Colors = greenPreMultiplied;
verts[2].Colors = greenPreMultiplied;
triangleList2.DrawTriangleList (verts);
triangleList2.Position = windowSize.Center;
triangleList2.PositionX += windowSize.Width / 4;
triangleList2.PositionY -= triangleList2.BoundingRect.Size.Height / 2;
}
#endregion Setup content
}
// Forum Post https://forums.xamarin.com/discussion/comment/145878/#Comment_145878
public class DrawNodeTriangleVertex : BaseDrawNodeTest
{
CCDrawNode drawTriangles;
CCDrawNode backGround;
public DrawNodeTriangleVertex()
{
Color = new CCColor3B(127, 127, 127);
Opacity = 255;
drawTriangles = new CCDrawNode();
drawTriangles.BlendFunc = CCBlendFunc.NonPremultiplied;
AddChild(drawTriangles, 10);
backGround = new CCDrawNode();
}
public override string Subtitle
{
get
{
return "Using AddTriangleVertex for Custom Geometry";
}
}
#region Setup content
public override void OnEnter()
{
base.OnEnter();
CCSize windowSize = VisibleBoundsWorldspace.Size;
var move = new CCMoveBy(4, new CCPoint(windowSize.Width / 2, 0));
backGround.Position = VisibleBoundsWorldspace.Left();
backGround.PositionX += windowSize.Width / 4;
// Run background animation
backGround.RepeatForever(move, move.Reverse());
backGround.DrawSolidCircle(CCPoint.Zero, 220, CCColor4B.White);
AddChild(backGround);
var color = CCColor4B.Red;
color.A = (byte)(255 * 0.3f);
// Draw polygons
//P1: 380:-160 P2: 200:-240 P3: 160:-420
CCPoint[] points = new CCPoint[]
{
//P1: 380:-160 P2: 200:-240 P3: 160:-420
new CCPoint(380,-160),
new CCPoint(200,-240),
new CCPoint(160,-420),
};
//P1: 160:-420 P2: 200:-520 P3: 360:-540
CCPoint[] pointss = new CCPoint[]
{
new CCPoint(160,-420),
new CCPoint(200,-520),
new CCPoint(360,-540)
};
//P1: 360:-540 P2: 420:-600 P3: 520:-520
CCPoint[] pointss2 = new CCPoint[]
{
new CCPoint(360,-540),
new CCPoint(420,-600),
new CCPoint(530,-520)
};
//P1: 520:-520 P2: 380:-160 P3: 160:-420
CCPoint[] pointss3 = new CCPoint[]
{
new CCPoint(520,-520),
new CCPoint(380,-160),
new CCPoint(160,-420)
};
// P1: 160:-420 P2: 360:-540 P3: 520:-520
CCPoint[] pointss4 = new CCPoint[]
{
new CCPoint(160,-420),
new CCPoint(360,-540),
new CCPoint(520,-520)
};
DrawSolidPolygon(points, color);
DrawSolidPolygon(pointss, color);
DrawSolidPolygon(pointss2, color);
DrawSolidPolygon(pointss3, color);
DrawSolidPolygon(pointss4, color);
drawTriangles.Position = windowSize.Center;
// Offset by the bounds of the polygons to more or less center it
drawTriangles.PositionX -= 370;
drawTriangles.PositionY += 440;
}
#endregion Setup content
void DrawSolidPolygon(CCPoint[] points, CCColor4B color)
{
for (int i = 0; i < points.Length - 2; i++)
{
drawTriangles.AddTriangleVertex(new CCV3F_C4B(points[0], color));
drawTriangles.AddTriangleVertex(new CCV3F_C4B(points[i + 1], color));
drawTriangles.AddTriangleVertex(new CCV3F_C4B(points[i + 2], color));
}
}
}
// Forum Post https://forums.xamarin.com/discussion/comment/145878/#Comment_145878
// Issue #287 : https://github.com/mono/CocosSharp/issues/287
public class DrawNodeDrawPolygon : BaseDrawNodeTest
{
CCDrawNode drawTriangles;
CCDrawNode backGround;
public DrawNodeDrawPolygon()
{
Color = new CCColor3B(127, 127, 127);
Opacity = 255;
drawTriangles = new CCDrawNode();
AddChild(drawTriangles, 10);
backGround = new CCDrawNode();
}
public override string Subtitle
{
get
{
return "Using DrawPolygon - Issue #287";
}
}
#region Setup content
public override void OnEnter()
{
base.OnEnter();
CCSize windowSize = VisibleBoundsWorldspace.Size;
var move = new CCMoveBy(4, new CCPoint(windowSize.Width / 2, 0));
backGround.Position = VisibleBoundsWorldspace.Left();
backGround.PositionX += windowSize.Width / 4;
// Run background animation
backGround.RepeatForever(move, move.Reverse());
backGround.DrawSolidCircle(CCPoint.Zero, 220, CCColor4B.White);
AddChild(backGround);
var color = CCColor4B.Red;
var alpha = (byte)(255 * 0.3f);
color.A = alpha;
// Draw polygons
//P1: 380:-160 P2: 200:-240 P3: 160:-420
CCPoint[] points = new CCPoint[]
{
//P1: 380:-160 P2: 200:-240 P3: 160:-420
new CCPoint(380,-160),
new CCPoint(200,-240),
new CCPoint(160,-420),
};
DrawSolidPolygon(points, color);
//P1: 160:-420 P2: 200:-520 P3: 360:-540
points[0] = new CCPoint(160, -420);
points[1] = new CCPoint(200, -520);
points[2] = new CCPoint(360, -540);
DrawSolidPolygon(points, color);
//P1: 360:-540 P2: 420:-600 P3: 520:-520
points[0] = new CCPoint(360, -540);
points[1] = new CCPoint(420, -600);
points[2] = new CCPoint(530, -520);
DrawSolidPolygon(points, color);
//P1: 520:-520 P2: 380:-160 P3: 160:-420
points[0] = new CCPoint(520, -520);