From f6d5dd71c9c68d95ceab14e7a0e5ee917a0d6cd4 Mon Sep 17 00:00:00 2001 From: John Zhang Date: Wed, 24 Jul 2019 00:39:57 +0800 Subject: [PATCH] refactor-CCDrawNode * remove unecessary intermediate temp variables * remove redundant pointer cast * remove redundant memset, as the values are assigned immediately in a for loop. * minor formatting --- cocos/2d/CCDrawNode.cpp | 70 +++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 41 deletions(-) diff --git a/cocos/2d/CCDrawNode.cpp b/cocos/2d/CCDrawNode.cpp index 6dc5dbce2ef3..b97be299a63e 100644 --- a/cocos/2d/CCDrawNode.cpp +++ b/cocos/2d/CCDrawNode.cpp @@ -43,8 +43,7 @@ static Vec2 v2fzero(0.0f,0.0f); static inline Vec2 v2f(float x, float y) { - Vec2 ret(x, y); - return ret; + return {x, y}; } static inline Vec2 v2fadd(const Vec2 &v0, const Vec2 &v1) @@ -568,9 +567,8 @@ void DrawNode::drawPoint(const Vec2& position, const float pointSize, const Colo { ensureCapacityGLPoint(1); - V2F_C4B_T2F *point = (V2F_C4B_T2F*)(_bufferGLPoint + _bufferCountGLPoint); - V2F_C4B_T2F a = {position, Color4B(color), Tex2F(pointSize,0)}; - *point = a; + V2F_C4B_T2F *point = _bufferGLPoint + _bufferCountGLPoint; + *point = {position, Color4B(color), Tex2F(pointSize,0)}; _bufferCountGLPoint += 1; _dirtyGLPoint = true; @@ -585,12 +583,11 @@ void DrawNode::drawPoints(const Vec2 *position, unsigned int numberOfPoints, con { ensureCapacityGLPoint(numberOfPoints); - V2F_C4B_T2F *point = (V2F_C4B_T2F*)(_bufferGLPoint + _bufferCountGLPoint); + V2F_C4B_T2F *point = _bufferGLPoint + _bufferCountGLPoint; for(unsigned int i=0; i < numberOfPoints; i++,point++) { - V2F_C4B_T2F a = {position[i], Color4B(color), Tex2F(pointSize,0)}; - *point = a; + *point = {position[i], Color4B(color), Tex2F(pointSize,0)}; } _bufferCountGLPoint += numberOfPoints; @@ -601,13 +598,10 @@ void DrawNode::drawLine(const Vec2 &origin, const Vec2 &destination, const Color { ensureCapacityGLLine(2); - V2F_C4B_T2F *point = (V2F_C4B_T2F*)(_bufferGLLine + _bufferCountGLLine); - - V2F_C4B_T2F a = {origin, Color4B(color), Tex2F(0.0, 0.0)}; - V2F_C4B_T2F b = {destination, Color4B(color), Tex2F(0.0, 0.0)}; + V2F_C4B_T2F *point = _bufferGLLine + _bufferCountGLLine; - *point = a; - *(point+1) = b; + *point = {origin, Color4B(color), Tex2F(0.0, 0.0)}; + *(point + 1) = {destination, Color4B(color), Tex2F(0.0, 0.0)}; _bufferCountGLLine += 2; _dirtyGLLine = true; @@ -615,10 +609,10 @@ void DrawNode::drawLine(const Vec2 &origin, const Vec2 &destination, const Color void DrawNode::drawRect(const Vec2 &origin, const Vec2 &destination, const Color4F &color) { - drawLine(Vec2(origin.x, origin.y), Vec2(destination.x, origin.y), color); - drawLine(Vec2(destination.x, origin.y), Vec2(destination.x, destination.y), color); - drawLine(Vec2(destination.x, destination.y), Vec2(origin.x, destination.y), color); - drawLine(Vec2(origin.x, destination.y), Vec2(origin.x, origin.y), color); + drawLine(origin, Vec2(destination.x, origin.y), color); + drawLine(Vec2(destination.x, origin.y), destination, color); + drawLine(destination, Vec2(origin.x, destination.y), color); + drawLine(Vec2(origin.x, destination.y), origin, color); } void DrawNode::drawPoly(const Vec2 *poli, unsigned int numberOfPoints, bool closePolygon, const Color4F &color) @@ -635,24 +629,20 @@ void DrawNode::drawPoly(const Vec2 *poli, unsigned int numberOfPoints, bool clos ensureCapacityGLLine(vertex_count); } - V2F_C4B_T2F *point = (V2F_C4B_T2F*)(_bufferGLLine + _bufferCountGLLine); + V2F_C4B_T2F *point = _bufferGLLine + _bufferCountGLLine; unsigned int i = 0; - for(; i