37
37
38
38
NS_CC_BEGIN
39
39
40
- // Vec2 == CGPoint in 32-bits, but not in 64-bits (OS X)
41
- // that's why the "v2f" functions are needed
42
- static Vec2 v2fzero (0 .0f ,0 .0f );
43
-
44
- static inline Vec2 v2f (float x, float y)
45
- {
46
- Vec2 ret (x, y);
47
- return ret;
48
- }
49
-
50
- static inline Vec2 v2fadd (const Vec2 &v0, const Vec2 &v1)
51
- {
52
- return v2f (v0.x +v1.x , v0.y +v1.y );
53
- }
54
-
55
- static inline Vec2 v2fsub (const Vec2 &v0, const Vec2 &v1)
56
- {
57
- return v2f (v0.x -v1.x , v0.y -v1.y );
58
- }
59
-
60
- static inline Vec2 v2fmult (const Vec2 &v, float s)
40
+ static inline Tex2F v2ToTex2F (const Vec2 &v)
61
41
{
62
- return v2f (v.x * s, v.y * s);
63
- }
64
-
65
- static inline Vec2 v2fperp (const Vec2 &p0)
66
- {
67
- return v2f (-p0.y , p0.x );
68
- }
69
-
70
- static inline Vec2 v2fneg (const Vec2 &p0)
71
- {
72
- return v2f (-p0.x , - p0.y );
73
- }
74
-
75
- static inline float v2fdot (const Vec2 &p0, const Vec2 &p1)
76
- {
77
- return p0.x * p1.x + p0.y * p1.y ;
78
- }
79
-
80
- static inline Vec2 v2fnormalize (const Vec2 &p)
81
- {
82
- Vec2 r (p.x , p.y );
83
- r.normalize ();
84
- return v2f (r.x , r.y );
85
- }
86
-
87
- static inline Vec2 __v2f (const Vec2 &v)
88
- {
89
- // #ifdef __LP64__
90
- return v2f (v.x , v.y );
91
- // #else
92
- // return * ((Vec2*) &v);
93
- // #endif
94
- }
95
-
96
- static inline Tex2F __t (const Vec2 &v)
97
- {
98
- return *(Tex2F*)&v;
42
+ return {v.x , v.y };
99
43
}
100
44
101
45
// implementation of DrawNode
@@ -318,9 +262,8 @@ void DrawNode::drawPoint(const Vec2& position, const float pointSize, const Colo
318
262
{
319
263
ensureCapacityGLPoint (1 );
320
264
321
- V2F_C4B_T2F *point = (V2F_C4B_T2F*)(_bufferGLPoint + _bufferCountGLPoint);
322
- V2F_C4B_T2F a = {position, Color4B (color), Tex2F (pointSize,0 )};
323
- *point = a;
265
+ V2F_C4B_T2F *point = V2F_C4B_T2F *point = _bufferGLPoint + _bufferCountGLPoint;
266
+ *point = {position, Color4B (color), Tex2F (pointSize,0 )};
324
267
325
268
_customCommandGLPoint.updateVertexBuffer (point, _bufferCountGLPoint*sizeof (V2F_C4B_T2F), sizeof (V2F_C4B_T2F));
326
269
_bufferCountGLPoint += 1 ;
@@ -337,11 +280,10 @@ void DrawNode::drawPoints(const Vec2 *position, unsigned int numberOfPoints, con
337
280
{
338
281
ensureCapacityGLPoint (numberOfPoints);
339
282
340
- V2F_C4B_T2F *point = (V2F_C4B_T2F*)( _bufferGLPoint + _bufferCountGLPoint) ;
283
+ V2F_C4B_T2F *point = _bufferGLPoint + _bufferCountGLPoint;
341
284
for (unsigned int i=0 ; i < numberOfPoints; i++)
342
285
{
343
- V2F_C4B_T2F a = {position[i], Color4B (color), Tex2F (pointSize,0 )};
344
- *(point + i) = a;
286
+ *(point + i) = {position[i], Color4B (color), Tex2F (pointSize,0 )};
345
287
}
346
288
347
289
_customCommandGLPoint.updateVertexBuffer (point, _bufferCountGLPoint*sizeof (V2F_C4B_T2F), numberOfPoints*sizeof (V2F_C4B_T2F));
@@ -354,13 +296,10 @@ void DrawNode::drawLine(const Vec2 &origin, const Vec2 &destination, const Color
354
296
{
355
297
ensureCapacityGLLine (2 );
356
298
357
- V2F_C4B_T2F *point = (V2F_C4B_T2F*)( _bufferGLLine + _bufferCountGLLine) ;
299
+ V2F_C4B_T2F *point = _bufferGLLine + _bufferCountGLLine;
358
300
359
- V2F_C4B_T2F a = {origin, Color4B (color), Tex2F (0.0 , 0.0 )};
360
- V2F_C4B_T2F b = {destination, Color4B (color), Tex2F (0.0 , 0.0 )};
361
-
362
- *point = a;
363
- *(point+1 ) = b;
301
+ *point = {origin, Color4B (color), Tex2F (0.0 , 0.0 )};
302
+ *(point+1 ) = {destination, Color4B (color), Tex2F (0.0 , 0.0 )};
364
303
365
304
_customCommandGLLine.updateVertexBuffer (point, _bufferCountGLLine*sizeof (V2F_C4B_T2F), 2 *sizeof (V2F_C4B_T2F));
366
305
_bufferCountGLLine += 2 ;
@@ -370,10 +309,10 @@ void DrawNode::drawLine(const Vec2 &origin, const Vec2 &destination, const Color
370
309
371
310
void DrawNode::drawRect (const Vec2 &origin, const Vec2 &destination, const Color4F &color)
372
311
{
373
- drawLine (Vec2 ( origin. x , origin. y ) , Vec2 (destination.x , origin.y ), color);
374
- drawLine (Vec2 (destination.x , origin.y ), Vec2 ( destination. x , destination. y ) , color);
375
- drawLine (Vec2 ( destination. x , destination. y ) , Vec2 (origin.x , destination.y ), color);
376
- drawLine (Vec2 (origin.x , destination.y ), Vec2 ( origin. x , origin. y ) , color);
312
+ drawLine (origin, Vec2 (destination.x , origin.y ), color);
313
+ drawLine (Vec2 (destination.x , origin.y ), destination, color);
314
+ drawLine (destination, Vec2 (origin.x , destination.y ), color);
315
+ drawLine (Vec2 (origin.x , destination.y ), origin, color);
377
316
}
378
317
379
318
void DrawNode::drawPoly (const Vec2 *poli, unsigned int numberOfPoints, bool closePolygon, const Color4F &color)
@@ -390,25 +329,20 @@ void DrawNode::drawPoly(const Vec2 *poli, unsigned int numberOfPoints, bool clos
390
329
ensureCapacityGLLine (vertex_count);
391
330
}
392
331
393
- V2F_C4B_T2F *point = (V2F_C4B_T2F*)( _bufferGLLine + _bufferCountGLLine) ;
332
+ V2F_C4B_T2F *point = _bufferGLLine + _bufferCountGLLine;
394
333
V2F_C4B_T2F *cursor = point;
395
334
396
335
unsigned int i = 0 ;
397
- for (; i< numberOfPoints- 1 ; i++)
336
+ for (; i < numberOfPoints - 1 ; i++)
398
337
{
399
- V2F_C4B_T2F a = {poli[i], Color4B (color), Tex2F (0.0 , 0.0 )};
400
- V2F_C4B_T2F b = {poli[i+1 ], Color4B (color), Tex2F (0.0 , 0.0 )};
401
-
402
- *point = a;
403
- *(point+1 ) = b;
338
+ *point = {poli[i], Color4B (color), Tex2F (0.0 , 0.0 )};
339
+ *(point + 1 ) = {poli[i+1 ], Color4B (color), Tex2F (0.0 , 0.0 )};
404
340
point += 2 ;
405
341
}
406
342
if (closePolygon)
407
343
{
408
- V2F_C4B_T2F a = {poli[i], Color4B (color), Tex2F (0.0 , 0.0 )};
409
- V2F_C4B_T2F b = {poli[0 ], Color4B (color), Tex2F (0.0 , 0.0 )};
410
- *point = a;
411
- *(point+1 ) = b;
344
+ *point = {poli[i], Color4B (color), Tex2F (0.0 , 0.0 )};
345
+ *(point + 1 ) = {poli[0 ], Color4B (color), Tex2F (0.0 , 0.0 )};
412
346
}
413
347
414
348
_customCommandGLLine.updateVertexBuffer (cursor, _bufferCountGLLine*sizeof (V2F_C4B_T2F), vertex_count*sizeof (V2F_C4B_T2F));
@@ -559,77 +493,77 @@ void DrawNode::drawDot(const Vec2 &pos, float radius, const Color4F &color)
559
493
560
494
void DrawNode::drawRect (const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, const Vec2& p4, const Color4F &color)
561
495
{
562
- drawLine (Vec2 (p1. x , p1. y ), Vec2 (p2. x , p2. y ) , color);
563
- drawLine (Vec2 (p2. x , p2. y ), Vec2 (p3. x , p3. y ) , color);
564
- drawLine (Vec2 (p3. x , p3. y ), Vec2 (p4. x , p4. y ) , color);
565
- drawLine (Vec2 (p4. x , p4. y ), Vec2 (p1. x , p1. y ) , color);
496
+ drawLine (p1, p2 , color);
497
+ drawLine (p2, p3 , color);
498
+ drawLine (p3, p4 , color);
499
+ drawLine (p4, p1 , color);
566
500
}
567
501
568
502
void DrawNode::drawSegment (const Vec2 &from, const Vec2 &to, float radius, const Color4F &color)
569
503
{
570
504
unsigned int vertex_count = 6 *3 ;
571
505
ensureCapacity (vertex_count);
572
506
573
- Vec2 a = __v2f ( from) ;
574
- Vec2 b = __v2f (to) ;
575
-
576
-
577
- Vec2 n = v2fnormalize ( v2fperp ( v2fsub (b, a)) );
578
- Vec2 t = v2fperp (n );
579
-
580
- Vec2 nw = v2fmult (n, radius) ;
581
- Vec2 tw = v2fmult (t, radius) ;
582
- Vec2 v0 = v2fsub (b, v2fadd (nw, tw) );
583
- Vec2 v1 = v2fadd (b, v2fsub (nw, tw) );
584
- Vec2 v2 = v2fsub (b, nw) ;
585
- Vec2 v3 = v2fadd (b, nw) ;
586
- Vec2 v4 = v2fsub (a, nw) ;
587
- Vec2 v5 = v2fadd (a, nw) ;
588
- Vec2 v6 = v2fsub (a, v2fsub (nw, tw) );
589
- Vec2 v7 = v2fadd (a, v2fadd (nw, tw) );
507
+ Vec2 a = from;
508
+ Vec2 b = to ;
509
+
510
+
511
+ Vec2 n = ((b - a). getPerp ()). getNormalized ( );
512
+ Vec2 t = n. getPerp ( );
513
+
514
+ Vec2 nw = n * radius;
515
+ Vec2 tw = t * radius;
516
+ Vec2 v0 = b - (nw + tw );
517
+ Vec2 v1 = b + (nw - tw );
518
+ Vec2 v2 = b - nw ;
519
+ Vec2 v3 = b + nw ;
520
+ Vec2 v4 = a - nw ;
521
+ Vec2 v5 = a + nw ;
522
+ Vec2 v6 = a - (nw - tw );
523
+ Vec2 v7 = a + (nw + tw );
590
524
591
525
592
526
V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
593
527
594
528
V2F_C4B_T2F_Triangle triangles0 = {
595
- {v0, Color4B (color), __t ( v2fneg ( v2fadd (n, t) ))},
596
- {v1, Color4B (color), __t ( v2fsub (n, t) )},
597
- {v2, Color4B (color), __t ( v2fneg (n) )},
529
+ {v0, Color4B (color), v2ToTex2F (-(n + t ))},
530
+ {v1, Color4B (color), v2ToTex2F (n - t )},
531
+ {v2, Color4B (color), v2ToTex2F (-n )},
598
532
};
599
533
triangles[0 ] = triangles0;
600
-
534
+
601
535
V2F_C4B_T2F_Triangle triangles1 = {
602
- {v3, Color4B (color), __t (n)},
603
- {v1, Color4B (color), __t ( v2fsub (n, t) )},
604
- {v2, Color4B (color), __t ( v2fneg (n) )},
536
+ {v3, Color4B (color), v2ToTex2F (n)},
537
+ {v1, Color4B (color), v2ToTex2F (n - t )},
538
+ {v2, Color4B (color), v2ToTex2F (-n )},
605
539
};
606
540
triangles[1 ] = triangles1;
607
-
541
+
608
542
V2F_C4B_T2F_Triangle triangles2 = {
609
- {v3, Color4B (color), __t (n)},
610
- {v4, Color4B (color), __t ( v2fneg (n) )},
611
- {v2, Color4B (color), __t ( v2fneg (n) )},
543
+ {v3, Color4B (color), v2ToTex2F (n)},
544
+ {v4, Color4B (color), v2ToTex2F (-n )},
545
+ {v2, Color4B (color), v2ToTex2F (-n )},
612
546
};
613
547
triangles[2 ] = triangles2;
614
548
615
549
V2F_C4B_T2F_Triangle triangles3 = {
616
- {v3, Color4B (color), __t (n)},
617
- {v4, Color4B (color), __t ( v2fneg (n) )},
618
- {v5, Color4B (color), __t (n) },
550
+ {v3, Color4B (color), v2ToTex2F (n)},
551
+ {v4, Color4B (color), v2ToTex2F (-n )},
552
+ {v5, Color4B (color), v2ToTex2F (n) },
619
553
};
620
554
triangles[3 ] = triangles3;
621
555
622
556
V2F_C4B_T2F_Triangle triangles4 = {
623
- {v6, Color4B (color), __t ( v2fsub (t, n) )},
624
- {v4, Color4B (color), __t ( v2fneg (n) ) },
625
- {v5, Color4B (color), __t (n)},
557
+ {v6, Color4B (color), v2ToTex2F (t - n )},
558
+ {v4, Color4B (color), v2ToTex2F (-n ) },
559
+ {v5, Color4B (color), v2ToTex2F (n)},
626
560
};
627
561
triangles[4 ] = triangles4;
628
562
629
563
V2F_C4B_T2F_Triangle triangles5 = {
630
- {v6, Color4B (color), __t ( v2fsub (t, n) )},
631
- {v7, Color4B (color), __t ( v2fadd (n, t) )},
632
- {v5, Color4B (color), __t (n)},
564
+ {v6, Color4B (color), v2ToTex2F (t - n )},
565
+ {v7, Color4B (color), v2ToTex2F (t + n )},
566
+ {v5, Color4B (color), v2ToTex2F (n)},
633
567
};
634
568
triangles[5 ] = triangles5;
635
569
@@ -655,9 +589,9 @@ void DrawNode::drawPolygon(const Vec2 *verts, int count, const Color4F &fillColo
655
589
for (int i = 0 ; i < count-2 ; i++)
656
590
{
657
591
V2F_C4B_T2F_Triangle tmp = {
658
- {verts[0 ], Color4B (fillColor), __t (v2fzero)},
659
- {verts[i+1 ], Color4B (fillColor), __t (v2fzero)},
660
- {verts[i+2 ], Color4B (fillColor), __t (v2fzero)},
592
+ {verts[0 ], Color4B (fillColor), v2ToTex2F (v2fzero)},
593
+ {verts[i+1 ], Color4B (fillColor), v2ToTex2F (v2fzero)},
594
+ {verts[i+2 ], Color4B (fillColor), v2ToTex2F (v2fzero)},
661
595
};
662
596
663
597
*cursor++ = tmp;
@@ -666,50 +600,48 @@ void DrawNode::drawPolygon(const Vec2 *verts, int count, const Color4F &fillColo
666
600
if (outline )
667
601
{
668
602
struct ExtrudeVerts {Vec2 offset, n;};
669
- struct ExtrudeVerts * extrude = (struct ExtrudeVerts *)malloc (sizeof (struct ExtrudeVerts )*count);
670
- memset (extrude, 0 , sizeof (struct ExtrudeVerts )*count);
603
+ struct ExtrudeVerts * extrude = (struct ExtrudeVerts *)malloc (sizeof (struct ExtrudeVerts ) * count);
671
604
672
605
for (int i = 0 ; i < count; i++)
673
606
{
674
- Vec2 v0 = __v2f ( verts[(i-1 +count)%count]) ;
675
- Vec2 v1 = __v2f ( verts[i]) ;
676
- Vec2 v2 = __v2f ( verts[(i+1 )%count]) ;
607
+ Vec2 v0 = verts[(i-1 +count)%count];
608
+ Vec2 v1 = verts[i];
609
+ Vec2 v2 = verts[(i+1 )%count];
677
610
678
- Vec2 n1 = v2fnormalize ( v2fperp ( v2fsub (v1, v0)) );
679
- Vec2 n2 = v2fnormalize ( v2fperp ( v2fsub (v2, v1)) );
611
+ Vec2 n1 = ((v1 - v0). getPerp ()). getNormalized ( );
612
+ Vec2 n2 = ((v2 - v1). getPerp ()). getNormalized ( );
680
613
681
- Vec2 offset = v2fmult (v2fadd (n1, n2), 1 .0f / (v2fdot (n1, n2) + 1 .0f ));
682
- struct ExtrudeVerts tmp = {offset, n2};
683
- extrude[i] = tmp;
614
+ Vec2 offset = (n1 + n2) * (1 .0f / (Vec2::dot (n1, n2) + 1 .0f ));
615
+ extrude[i] = {offset, n2};
684
616
}
685
617
686
618
for (int i = 0 ; i < count; i++)
687
619
{
688
620
int j = (i+1 )%count;
689
- Vec2 v0 = __v2f ( verts[i]) ;
690
- Vec2 v1 = __v2f ( verts[j]) ;
621
+ Vec2 v0 = verts[i];
622
+ Vec2 v1 = verts[j];
691
623
692
624
Vec2 n0 = extrude[i].n ;
693
625
694
626
Vec2 offset0 = extrude[i].offset ;
695
627
Vec2 offset1 = extrude[j].offset ;
696
628
697
- Vec2 inner0 = v2fsub (v0, v2fmult ( offset0, borderWidth)) ;
698
- Vec2 inner1 = v2fsub (v1, v2fmult ( offset1, borderWidth)) ;
699
- Vec2 outer0 = v2fadd (v0, v2fmult ( offset0, borderWidth)) ;
700
- Vec2 outer1 = v2fadd (v1, v2fmult ( offset1, borderWidth)) ;
629
+ Vec2 inner0 = v0 - offset0 * borderWidth;
630
+ Vec2 inner1 = v1 - offset1 * borderWidth;
631
+ Vec2 outer0 = v0 + offset0 * borderWidth;
632
+ Vec2 outer1 = v1 + offset1 * borderWidth;
701
633
702
634
V2F_C4B_T2F_Triangle tmp1 = {
703
- {inner0, Color4B (borderColor), __t ( v2fneg (n0) )},
704
- {inner1, Color4B (borderColor), __t ( v2fneg (n0) )},
705
- {outer1, Color4B (borderColor), __t (n0)}
635
+ {inner0, Color4B (borderColor), v2ToTex2F (-n0 )},
636
+ {inner1, Color4B (borderColor), v2ToTex2F (-n0 )},
637
+ {outer1, Color4B (borderColor), v2ToTex2F (n0)}
706
638
};
707
639
*cursor++ = tmp1;
708
640
709
641
V2F_C4B_T2F_Triangle tmp2 = {
710
- {inner0, Color4B (borderColor), __t ( v2fneg (n0) )},
711
- {outer0, Color4B (borderColor), __t (n0)},
712
- {outer1, Color4B (borderColor), __t (n0)}
642
+ {inner0, Color4B (borderColor), v2ToTex2F (-n0 )},
643
+ {outer0, Color4B (borderColor), v2ToTex2F (n0)},
644
+ {outer1, Color4B (borderColor), v2ToTex2F (n0)}
713
645
};
714
646
*cursor++ = tmp2;
715
647
}
@@ -732,12 +664,12 @@ void DrawNode::drawSolidRect(const Vec2 &origin, const Vec2 &destination, const
732
664
Vec2 (origin.x , destination.y )
733
665
};
734
666
735
- drawSolidPoly (vertices, 4 , color );
667
+ drawSolidPoly (vertices, 4 , color);
736
668
}
737
669
738
670
void DrawNode::drawSolidPoly (const Vec2 *poli, unsigned int numberOfPoints, const Color4F &color)
739
671
{
740
- drawPolygon (poli, numberOfPoints, color, 0.0 , Color4F (0.0 , 0.0 , 0.0 , 0.0 ));
672
+ drawPolygon (poli, numberOfPoints, color, 0.0 , Color4F ());
741
673
}
742
674
743
675
void DrawNode::drawSolidCircle (const Vec2& center, float radius, float angle, unsigned int segments, float scaleX, float scaleY, const Color4F &color)
@@ -774,9 +706,9 @@ void DrawNode::drawTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, cons
774
706
ensureCapacity (vertex_count);
775
707
776
708
Color4B col = Color4B (color);
777
- V2F_C4B_T2F a = {Vec2 (p1. x , p1. y ) , col, Tex2F (0.0 , 0.0 ) };
778
- V2F_C4B_T2F b = {Vec2 (p2. x , p2. y ) , col, Tex2F (0.0 , 0.0 ) };
779
- V2F_C4B_T2F c = {Vec2 (p3. x , p3. y ) , col, Tex2F (0.0 , 0.0 ) };
709
+ V2F_C4B_T2F a = {p1 , col, Tex2F (0.0 , 0.0 ) };
710
+ V2F_C4B_T2F b = {p2 , col, Tex2F (0.0 , 0.0 ) };
711
+ V2F_C4B_T2F c = {p3 , col, Tex2F (0.0 , 0.0 ) };
780
712
781
713
V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
782
714
V2F_C4B_T2F_Triangle triangle = {a, b, c};
0 commit comments