3737
3838NS_CC_BEGIN
3939
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)
6141{
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 };
9943}
10044
10145// implementation of DrawNode
@@ -318,9 +262,8 @@ void DrawNode::drawPoint(const Vec2& position, const float pointSize, const Colo
318262{
319263 ensureCapacityGLPoint (1 );
320264
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 )};
324267
325268 _customCommandGLPoint.updateVertexBuffer (point, _bufferCountGLPoint*sizeof (V2F_C4B_T2F), sizeof (V2F_C4B_T2F));
326269 _bufferCountGLPoint += 1 ;
@@ -337,11 +280,10 @@ void DrawNode::drawPoints(const Vec2 *position, unsigned int numberOfPoints, con
337280{
338281 ensureCapacityGLPoint (numberOfPoints);
339282
340- V2F_C4B_T2F *point = (V2F_C4B_T2F*)( _bufferGLPoint + _bufferCountGLPoint) ;
283+ V2F_C4B_T2F *point = _bufferGLPoint + _bufferCountGLPoint;
341284 for (unsigned int i=0 ; i < numberOfPoints; i++)
342285 {
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 )};
345287 }
346288
347289 _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
354296{
355297 ensureCapacityGLLine (2 );
356298
357- V2F_C4B_T2F *point = (V2F_C4B_T2F*)( _bufferGLLine + _bufferCountGLLine) ;
299+ V2F_C4B_T2F *point = _bufferGLLine + _bufferCountGLLine;
358300
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 )};
364303
365304 _customCommandGLLine.updateVertexBuffer (point, _bufferCountGLLine*sizeof (V2F_C4B_T2F), 2 *sizeof (V2F_C4B_T2F));
366305 _bufferCountGLLine += 2 ;
@@ -370,10 +309,10 @@ void DrawNode::drawLine(const Vec2 &origin, const Vec2 &destination, const Color
370309
371310void DrawNode::drawRect (const Vec2 &origin, const Vec2 &destination, const Color4F &color)
372311{
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);
377316}
378317
379318void 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
390329 ensureCapacityGLLine (vertex_count);
391330 }
392331
393- V2F_C4B_T2F *point = (V2F_C4B_T2F*)( _bufferGLLine + _bufferCountGLLine) ;
332+ V2F_C4B_T2F *point = _bufferGLLine + _bufferCountGLLine;
394333 V2F_C4B_T2F *cursor = point;
395334
396335 unsigned int i = 0 ;
397- for (; i< numberOfPoints- 1 ; i++)
336+ for (; i < numberOfPoints - 1 ; i++)
398337 {
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 )};
404340 point += 2 ;
405341 }
406342 if (closePolygon)
407343 {
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 )};
412346 }
413347
414348 _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)
559493
560494void DrawNode::drawRect (const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, const Vec2& p4, const Color4F &color)
561495{
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);
566500}
567501
568502void DrawNode::drawSegment (const Vec2 &from, const Vec2 &to, float radius, const Color4F &color)
569503{
570504 unsigned int vertex_count = 6 *3 ;
571505 ensureCapacity (vertex_count);
572506
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 );
590524
591525
592526 V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
593527
594528 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 )},
598532 };
599533 triangles[0 ] = triangles0;
600-
534+
601535 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 )},
605539 };
606540 triangles[1 ] = triangles1;
607-
541+
608542 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 )},
612546 };
613547 triangles[2 ] = triangles2;
614548
615549 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) },
619553 };
620554 triangles[3 ] = triangles3;
621555
622556 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)},
626560 };
627561 triangles[4 ] = triangles4;
628562
629563 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)},
633567 };
634568 triangles[5 ] = triangles5;
635569
@@ -655,9 +589,9 @@ void DrawNode::drawPolygon(const Vec2 *verts, int count, const Color4F &fillColo
655589 for (int i = 0 ; i < count-2 ; i++)
656590 {
657591 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)},
661595 };
662596
663597 *cursor++ = tmp;
@@ -666,50 +600,48 @@ void DrawNode::drawPolygon(const Vec2 *verts, int count, const Color4F &fillColo
666600 if (outline)
667601 {
668602 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);
671604
672605 for (int i = 0 ; i < count; i++)
673606 {
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];
677610
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 ( );
680613
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};
684616 }
685617
686618 for (int i = 0 ; i < count; i++)
687619 {
688620 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];
691623
692624 Vec2 n0 = extrude[i].n ;
693625
694626 Vec2 offset0 = extrude[i].offset ;
695627 Vec2 offset1 = extrude[j].offset ;
696628
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;
701633
702634 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)}
706638 };
707639 *cursor++ = tmp1;
708640
709641 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)}
713645 };
714646 *cursor++ = tmp2;
715647 }
@@ -732,12 +664,12 @@ void DrawNode::drawSolidRect(const Vec2 &origin, const Vec2 &destination, const
732664 Vec2 (origin.x , destination.y )
733665 };
734666
735- drawSolidPoly (vertices, 4 , color );
667+ drawSolidPoly (vertices, 4 , color);
736668}
737669
738670void DrawNode::drawSolidPoly (const Vec2 *poli, unsigned int numberOfPoints, const Color4F &color)
739671{
740- drawPolygon (poli, numberOfPoints, color, 0.0 , Color4F (0.0 , 0.0 , 0.0 , 0.0 ));
672+ drawPolygon (poli, numberOfPoints, color, 0.0 , Color4F ());
741673}
742674
743675void 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
774706 ensureCapacity (vertex_count);
775707
776708 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 ) };
780712
781713 V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
782714 V2F_C4B_T2F_Triangle triangle = {a, b, c};
0 commit comments