From: Patrik Gornicz Date: Thu, 22 Jan 2009 00:42:11 +0000 (-0500) Subject: refactored entityCreator X-Git-Tag: physics-premerge~53 X-Git-Url: http://gitweb.pgornicz.com/gitweb.cgi?a=commitdiff_plain;h=423931a317185a0904255c4bd6652404c6c1a1ae;p=libbear.git refactored entityCreator --- diff --git a/TODO b/TODO index 3b9d98a..8a2c497 100644 --- a/TODO +++ b/TODO @@ -26,6 +26,8 @@ and a * entry is something to remember when working in this area of the project. - replace the set in collisionHandler - replace the set in effectManager - create my own list + - create my own queue + - repleace queue in entityCreator ******************************************************************************* diff --git a/src/entityCreator.cpp b/src/entityCreator.cpp index 191d396..5e0edaa 100644 --- a/src/entityCreator.cpp +++ b/src/entityCreator.cpp @@ -30,84 +30,96 @@ /// ***** Private Variables ***** typedef std::queue queBall; +typedef std::queue quePoly; -queBall startBalls; -queBall mouseBalls; +static queBall s_startBalls; +static queBall s_mouseBalls; + +static quePoly s_startPolys; /// ***** Private Method Headers ***** -Ball* addBall(const Vector2& pos, - float radius, - const float* color, - queBall& que); +static Ball* addBall(const Vector2& vecPos, + float fRadius, + const float* color, + queBall& que); + +static Polygon* addPoly(const vector& vecPoints, + const float* color, + quePoly& que); -void removeBall(queBall& que); -void removeAllBalls(queBall& que); +static void removeBall(queBall& que); +static void removeAllBalls(queBall& que); + +static void removePoly(quePoly& que); +static void removeAllPolys(quePoly& que); /// ***** Initializers/Cleaners ***** void creator::init() { - Ball* ball; - float startMass = 5; + Ball* pBall; + float fStartMass = 5; + + pBall = addBall(Vector2(50, 50), 20, cWhite, s_startBalls); + pBall->mass = fStartMass; - ball = addBall(Vector2(50, 50), 20, cWhite, startBalls); - ball->mass = startMass; + pBall = addBall(Vector2(150, 50), 20, cGrey, s_startBalls); + pBall->mass = fStartMass; - ball = addBall(Vector2(150, 50), 20, cGrey, startBalls); - ball->mass = startMass; + pBall = addBall(Vector2(50, 100), 20, cRed, s_startBalls); + pBall->mass = fStartMass; - ball = addBall(Vector2(50, 100), 20, cRed, startBalls); - ball->mass = startMass; + pBall = addBall(Vector2(100, 100), 20, cGreen, s_startBalls); + pBall->mass = fStartMass; - ball = addBall(Vector2(100, 100), 20, cGreen, startBalls); - ball->mass = startMass; + pBall = addBall(Vector2(150, 100), 20, cBlue, s_startBalls); + pBall->mass = fStartMass; - ball = addBall(Vector2(150, 100), 20, cBlue, startBalls); - ball->mass = startMass; + pBall = addBall(Vector2(50, 150), 20, cYellow, s_startBalls); + pBall->mass = fStartMass; - ball = addBall(Vector2(50, 150), 20, cYellow, startBalls); - ball->mass = startMass; + pBall = addBall(Vector2(100, 150), 20, cMagenta, s_startBalls); + pBall->mass = fStartMass; - ball = addBall(Vector2(100, 150), 20, cMagenta, startBalls); - ball->mass = startMass; + pBall = addBall(Vector2(150, 150), 20, cCyan, s_startBalls); + pBall->mass = fStartMass; - ball = addBall(Vector2(150, 150), 20, cCyan, startBalls); - ball->mass = startMass; - for( int i = 0; i<50; i++) + for( int i = 0; i < 50; i++) { - addBall(Vector2(200+i*2, 200+i*2), 10, cCyan); + addBall(Vector2(200 + i * 2, 200 + i * 2), 10, cCyan); } - // HACK - // add a polygon into the mix (currently not cleaned up) + vector points; points.push_back(Vector2(500,500)); points.push_back(Vector2(300,500)); points.push_back(Vector2(500,300)); - manager::add(new Polygon(points, cRed)); + addPoly(points, cRed, s_startPolys); } void creator::clean() { - removeAllBalls(startBalls); - removeAllBalls(mouseBalls); + removeAllBalls(s_startBalls); + removeAllBalls(s_mouseBalls); + + removeAllPolys(s_startPolys); } /// ***** Public Methods ***** void creator::addBall(const Vector2& pos, float radius, const float* color) { - addBall(pos, radius, color, mouseBalls); + addBall(pos, radius, color, s_mouseBalls); } void creator::removeBall() { - removeBall(mouseBalls); + removeBall(s_mouseBalls); } void creator::removeAllBalls() { - removeAllBalls(mouseBalls); + removeAllBalls(s_mouseBalls); } void creator::handleInput() @@ -115,42 +127,66 @@ void creator::handleInput() if(input::mouseLeft()) addBall(input::mousePosition(), 10, cCyan); - if(input::mouseRight() && ! mouseBalls.empty()) + if(input::mouseRight() && ! s_mouseBalls.empty()) removeBall(); } /// ***** Private Methods ***** -Ball* addBall(const Vector2& pos, - float radius, - const float* color, - queBall& que) +Ball* addBall(const Vector2& vecPos, + float fRadius, + const float* color, + queBall& que) +{ + Ball* pBall = new Ball(vecPos, fRadius, color); + + que.push(pBall); + manager::add(pBall); + + return pBall; +} +Polygon* addPoly(const vector& vecPoints, + const float* color, + quePoly& que) { - Ball* ball = new Ball(pos, radius, color); + Polygon* pPoly = new Polygon(vecPoints, color); - que.push(ball); - manager::add(ball); + que.push(pPoly); + manager::add(pPoly); - return ball; + return pPoly; } void removeBall(queBall& que) { - Ball* ball = que.front(); + Ball* pBall = que.front(); - manager::remove(ball); + manager::remove(pBall); que.pop(); - delete ball; + delete pBall; } void removeAllBalls(queBall& que) { while(! que.empty() ) { - Ball* ball = que.front(); - que.pop(); - manager::remove(ball); + removeBall(que); + } +} - delete ball; +static void removePoly(quePoly& que) +{ + Polygon* pPoly = que.front(); + + manager::remove(pPoly); + que.pop(); + + delete pPoly; +} +static void removeAllPolys(quePoly& que) +{ + while(! que.empty() ) + { + removePoly(que); } }