X-Git-Url: http://gitweb.pgornicz.com/gitweb.cgi?p=physics.git;a=blobdiff_plain;f=src%2FentityCreator.cpp;h=34b9a56be616a4c1db3039046f1535205d9a596f;hp=aa5db0ef5dbe3e7cb44b7b5ea6900fc106c1cdb1;hb=a41bba6fc95ac24bcfc802574839b0a965692079;hpb=f72f4a59026b8daa160908170f366c843aca5bb9 diff --git a/src/entityCreator.cpp b/src/entityCreator.cpp index aa5db0e..34b9a56 100644 --- a/src/entityCreator.cpp +++ b/src/entityCreator.cpp @@ -1,89 +1,203 @@ +/* + * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + #include "entityCreator.h" -#include +#include #include "entityManager.h" +#include "input/inputManager.h" + #include "Entities/Ball.h" +#include "Entities/Polygon.h" #include "graphics/colors.h" -/// ***** Private Method Headers ***** -typedef std::queue queBall; -queBall Balls; /// ***** Private Variables ***** -/// ***** Public Methods ***** +//static bear::Queue s_test; + +typedef bear::Queue queBall; +typedef bear::Queue quePoly; + +static queBall s_startBalls; +static queBall s_mouseBalls; + +static quePoly s_startPolys; + +/// ***** Private Method Headers ***** + +static Ball* addBall(const Vector2& vecPos, + float fRadius, + const float* color, + queBall& que); + +static Polygon* addPoly(const vector& vecPoints, + const float* color, + quePoly& 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; + Ball* pBall; + float fStartMass = 5; - // needs to be first for the overlap - ball = new Ball(Vector2(50, 50), 20, cWhite); + s_startBalls.init(); + s_mouseBalls.init(); - Balls.push(ball); - manager::add(ball); + s_startPolys.init(); - ball = new Ball(Vector2(100, 50), 20, cBlack); + pBall = addBall(Vector2(50, 50), 20, cWhite, s_startBalls); + pBall->mass = fStartMass; - Balls.push(ball); - manager::add(ball); + pBall = addBall(Vector2(150, 50), 20, cGrey, s_startBalls); + pBall->mass = fStartMass; - ball = new Ball(Vector2(150, 50), 20, cGrey); + pBall = addBall(Vector2(50, 100), 20, cRed, s_startBalls); + pBall->mass = fStartMass; - Balls.push(ball); - manager::add(ball); + pBall = addBall(Vector2(100, 100), 20, cGreen, s_startBalls); + pBall->mass = fStartMass; - ball = new Ball(Vector2(50, 100), 20, cRed); + pBall = addBall(Vector2(150, 100), 20, cBlue, s_startBalls); + pBall->mass = fStartMass; - Balls.push(ball); - manager::add(ball); + pBall = addBall(Vector2(50, 150), 20, cYellow, s_startBalls); + pBall->mass = fStartMass; - ball = new Ball(Vector2(100, 100), 20, cGreen); + pBall = addBall(Vector2(100, 150), 20, cMagenta, s_startBalls); + pBall->mass = fStartMass; - Balls.push(ball); - manager::add(ball); + pBall = addBall(Vector2(150, 150), 20, cCyan, s_startBalls); + pBall->mass = fStartMass; - ball = new Ball(Vector2(150, 100), 20, cBlue); - Balls.push(ball); - manager::add(ball); + for( int i = 0; i < 50; i++) + { + addBall(Vector2(200 + i * 2, 200 + i * 2), 10, cCyan); + } - ball = new Ball(Vector2(50, 150), 20, cYellow); - Balls.push(ball); - manager::add(ball); + vector points; + points.push_back(Vector2(500,500)); + points.push_back(Vector2(300,500)); + points.push_back(Vector2(500,300)); - ball = new Ball(Vector2(100, 150), 20, cMagenta); + addPoly(points, cRed, s_startPolys); +} +void creator::clean() +{ + removeAllPolys(s_startPolys); + s_startPolys.fini(); - Balls.push(ball); - manager::add(ball); + removeAllBalls(s_mouseBalls); + s_mouseBalls.fini(); - ball = new Ball(Vector2(150, 150), 20, cCyan); + removeAllBalls(s_startBalls); + s_startBalls.fini(); +} + +/// ***** Public Methods ***** - Balls.push(ball); - manager::add(ball); +void creator::addBall(const Vector2& pos, float radius, const float* color) +{ + addBall(pos, radius, color, s_mouseBalls); } -void creator::clean() +void creator::removeBall() { - removeAllBalls(); + removeBall(s_mouseBalls); +} +void creator::removeAllBalls() +{ + removeAllBalls(s_mouseBalls); } -void creator::addBall() +void creator::handleInput() { + if(input::mouseLeft()) + addBall(input::mousePosition(), 10, cCyan); + if(input::mouseRight() && ! s_mouseBalls.isEmpty()) + removeBall(); } -void creator::removeBall() + +/// ***** Private Methods ***** + +Ball* addBall(const Vector2& vecPos, + float fRadius, + const float* color, + queBall& que) { + Ball* pBall = new Ball(vecPos, fRadius, color); + + que.pushBack(pBall); + manager::add(pBall); + return pBall; } -void creator::removeAllBalls() +Polygon* addPoly(const vector& vecPoints, + const float* color, + quePoly& que) +{ + Polygon* pPoly = new Polygon(vecPoints, color); + + que.pushBack(pPoly); + manager::add(pPoly); + + return pPoly; +} + +void removeBall(queBall& que) { - while(! Balls.empty() ) + Ball* pBall = que.getFront(); + + manager::remove(pBall); + que.popFront(); + + delete pBall; +} +void removeAllBalls(queBall& que) +{ + while(! que.isEmpty() ) { - Ball* ball = Balls.front(); - Balls.pop(); - manager::remove(ball); + removeBall(que); + } +} - delete ball; +static void removePoly(quePoly& que) +{ + Polygon* pPoly = que.getFront(); + + manager::remove(pPoly); + que.popFront(); + + delete pPoly; +} +static void removeAllPolys(quePoly& que) +{ + while(! que.isEmpty() ) + { + removePoly(que); } }