entityCreator setup to use bear::Queue
[physics.git] / src / entityCreator.cpp
index a183d87..34b9a56 100644 (file)
+/*
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+
 #include "entityCreator.h"
 
-#include <queue>
+#include <bear/Queue.h>
 
 #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<Ball*> queBall;
-queBall Balls;
 
 /// ***** Private Variables *****
 
-/// ***** Public Methods *****
+//static bear::Queue<int> s_test;
+
+typedef bear::Queue<Ball*>      queBall;
+typedef bear::Queue<Polygon*>   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<Vector2>& 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;
 
-    ball = new Ball(Vector2(50, 50), 20, cWhite);
+    s_startBalls.init();
+    s_mouseBalls.init();
 
-    ball->applyImpulse(Vector2(0.25,0.05)),
-    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;
 
-    ball->applyImpulse(Vector2(-0.15,-0.05)),
-    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;
 
-    ball->applyImpulse(Vector2(0.25,0.15)),
-    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;
 
-    ball->applyImpulse(Vector2(0.35,-0.15)),
-    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;
 
-    ball->applyImpulse(Vector2(-0.15,0.55)),
-    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);
 
-    ball->applyImpulse(Vector2(0.25,0.15)),
-    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);
 
-    ball->applyImpulse(Vector2(0.25,-0.05)),
-    Balls.push(ball);
-    manager::add(ball);
+    vector<Vector2> 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();
 
-    ball->applyImpulse(Vector2(-0.15,-0.05)),
-    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 *****
 
-    ball->applyImpulse(Vector2(-0.15,0.05)),
-    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<Vector2>& 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);
     }
 }