expanded Array and replaced all usages of std::vector with bear::Array
authorPatrik Gornicz <Gornicz.P@gmail.com>
Wed, 2 Sep 2009 04:32:37 +0000 (00:32 -0400)
committerPatrik Gornicz <Gornicz.P@gmail.com>
Wed, 2 Sep 2009 04:32:37 +0000 (00:32 -0400)
lib/include/bear/Array.h
lib/src/debug.cpp
physics/src/Entities/Polygon.cpp
physics/src/Entities/Polygon.h
physics/src/collisionManager.cpp
physics/src/entityCreator.cpp
physics/src/game.cpp
physics/src/graphics/graphics.cpp
physics/src/graphics/graphics.h

index 5b8d56d..632ff72 100644 (file)
@@ -19,6 +19,7 @@ namespace bear
   {
   public:
     Array();
+    Array(const Array<Item>& a);
     virtual ~Array() {}
 
     void init(unsigned int uiStartSize = 8, unsigned int uiSizeMultiplier = 2);
@@ -28,6 +29,9 @@ namespace bear
     void trim();
 
     bool isEmpty() const;
+    unsigned int getLength() const;
+
+    const Item& getAt(unsigned int uiIndex) const;
 
     const Item& getFront() const;
     const Item& getBack() const;
@@ -38,8 +42,9 @@ namespace bear
     void popFront();
     void popBack();
 
+    void clear();
+
   private:
-    unsigned int getLength() const;
     unsigned int getSpaceNeeded(unsigned int uiSize) const;
 
     // alloc helpers
@@ -81,6 +86,28 @@ bear::Array<Item>::Array()
 }
 
 template < class Item >
+bear::Array<Item>::Array(const Array<Item>& a)
+    :   m_uiStartSize(0),
+        m_uiSizeMultiplier(0),
+        m_uiAllocated(0),
+        m_pArrayItems(NULL),
+        m_pHead(NULL),
+        m_pTail(NULL)
+{
+  // TODO: talk with colton about init/fini when using a copy constructor
+  //       (potentialling called from a constructor's initailizer list.)
+
+  init();
+  alloc(a.getLength());
+
+  // HACK: this should be a memcpy or something better
+  for(unsigned int ui=0; ui < a.getLength(); ui++)
+  {
+    pushBack(a.getAt(ui));
+  }
+}
+
+template < class Item >
 void bear::Array<Item>::init
 (
   unsigned int uiStartSize,
@@ -172,6 +199,15 @@ bool bear::Array<Item>::isEmpty() const
 }
 
 template < class Item >
+const Item& bear::Array<Item>::getAt(unsigned int uiIndex) const
+{
+  // This is very bad. Fix your code
+  DASSERT(uiIndex < getLength());
+
+  return *withinBounds( m_pHead + uiIndex, m_pArrayItems, m_pArrayItems + m_uiAllocated );
+}
+
+template < class Item >
 const Item& bear::Array<Item>::getFront() const
 {
   // This is very bad. Fix your code
@@ -265,6 +301,16 @@ void bear::Array<Item>::popBack()
   }
 }
 
+template < class Item >
+void bear::Array<Item>::clear()
+{
+  // HACK
+  while(!isEmpty())
+  {
+    popFront();
+  }
+}
+
 
 template < class Item >
 unsigned int bear::Array<Item>::getLength() const
@@ -341,6 +387,7 @@ void bear::Array<Item>::allocForce(unsigned int uiSize)
     Item * pNewItem = pNewHead;
     Item * pOldItem = pOldHead;
 
+    // TODO: this could be done using at most 2 memcpys
     for( unsigned int ii = 0; ii < uiOldLength; ii++ )
     {
       *pNewItem = *pOldItem;
index 443b147..e56ade4 100644 (file)
@@ -83,6 +83,11 @@ void bear::DPF(int iLevel, const char* pstr)
 
 void bear::DASSERT(bool fAssert)
 {
+  if(!fAssert)
+  {
+    bear::debug::printTrace();
+  }
+
 //#ifdef DEBUG
     assert(fAssert);
 //#else
index 9f1aa43..d93cd32 100644 (file)
@@ -26,17 +26,17 @@ using namespace bear;
 
 /// ***** Constructors/Destructors *****
 
-Polygon::Polygon(const vector<Vector2>& points, const float* color)
+Polygon::Polygon(const Array<Vector2>& points, const float* color)
     : PhysicsEntity(Vector2(0,0)), m_points(points), m_color(color)
 {
-    DASSERT(0 < points.size());
+    DASSERT(0 < points.getLength());
 
     createBindingBox();
     centerPosition();
 }
 Polygon::~Polygon()
 {
-
+    m_points.fini();
 }
 
 /// ***** Public Class Methods *****
@@ -49,18 +49,18 @@ void Polygon::draw() const
 /// ***** Private Class Methods *****
 void Polygon::createBindingBox()
 {
-    DASSERT(0 < m_points.size());
+    DASSERT(0 < m_points.getLength());
 
-    m_maxP = m_points.at(0);
-    m_minP = m_points.at(0);
+    m_maxP = m_points.getAt(0);
+    m_minP = m_points.getAt(0);
 
-    for(unsigned int i=1; i<m_points.size(); i++)
+    for(unsigned int i=1; i<m_points.getLength(); i++)
     {
-             if(m_points[i].m_fX < m_minP.m_fX) m_minP.m_fX = m_points[i].m_fX;
-        else if(m_points[i].m_fX > m_maxP.m_fX) m_maxP.m_fX = m_points[i].m_fX;
+             if(m_points.getAt(i).m_fX < m_minP.m_fX) m_minP.m_fX = m_points.getAt(i).m_fX;
+        else if(m_points.getAt(i).m_fX > m_maxP.m_fX) m_maxP.m_fX = m_points.getAt(i).m_fX;
 
-             if(m_points[i].m_fY < m_minP.m_fY) m_minP.m_fY = m_points[i].m_fY;
-        else if(m_points[i].m_fY > m_maxP.m_fY) m_maxP.m_fY = m_points[i].m_fY;
+             if(m_points.getAt(i).m_fY < m_minP.m_fY) m_minP.m_fY = m_points.getAt(i).m_fY;
+        else if(m_points.getAt(i).m_fY > m_maxP.m_fY) m_maxP.m_fY = m_points.getAt(i).m_fY;
     }
 }
 
index 7941f88..bc3a9bb 100644 (file)
 #define POLYGON_H
 
 #include <bear/Vector2.h>
+#include <bear/Array.h>
 using namespace bear;
 
 #include "PhysicsEntity.h"
 
-#include <vector>
-using std::vector;
-
 
 /// ***** Header Class *****
 
 class Polygon: public PhysicsEntity
 {
     public:
-        Polygon(const vector<Vector2>&, const float* color);
+        Polygon(const Array<Vector2>&, const float* color);
         virtual ~Polygon();
 
         virtual void draw() const;
@@ -41,7 +39,7 @@ class Polygon: public PhysicsEntity
         Vector2 m_maxP; // stores the max bounding box point
         Vector2 m_minP; // stores the min bounding box point
 
-        vector<Vector2> m_points;
+        Array<Vector2> m_points;
 
         const float* m_color;
         
index c9af58d..2242a88 100644 (file)
@@ -340,16 +340,16 @@ bool getInfo(const Polygon* pPoly, const Ball* pBall, CollisionInfo* pcInfo)
     int iTot = 0;
 
     {
-        const vector<Vector2>& pts = pPoly->m_points;
-        unsigned int num = pts.size();
+        const Array<Vector2>& pts = pPoly->m_points;
+        unsigned int num = pts.getLength();
 
         for (unsigned int i = 0; i < num; i++)
         {
             Vector2 vec = vectorToLine(vecPos,
-                                       pts[i].m_fX,
-                                       pts[i].m_fY,
-                                       pts[(i + 1) % num].m_fX,
-                                       pts[(i + 1) % num].m_fY);
+                                       pts.getAt(i).m_fX,
+                                       pts.getAt(i).m_fY,
+                                       pts.getAt((i + 1) % num).m_fX,
+                                       pts.getAt((i + 1) % num).m_fY);
 
             if (vec.sqrLength() <= fRad*fRad && 0 < vec.dot(vecVelo))
             {
index 79ac89b..3e2429f 100644 (file)
@@ -44,7 +44,7 @@ static Ball* addBall(const Vector2& vecPos,
                      const float* color,
                      queBall& que);
 
-static Polygon* addPoly(const vector<Vector2>& vecPoints,
+static Polygon* addPoly(const Array<Vector2>& vecPoints,
                         const float* color,
                         quePoly& que);
 
@@ -97,12 +97,17 @@ void creator::init()
     }
 
 
-    vector<Vector2> points;
-    points.push_back(Vector2(500,500));
-    points.push_back(Vector2(300,500));
-    points.push_back(Vector2(500,300));
+    {
+        Array<Vector2> points;
+        points.init();
+
+        points.pushBack(Vector2(500,500));
+        points.pushBack(Vector2(300,500));
+        points.pushBack(Vector2(500,300));
 
-    addPoly(points, cRed, s_startPolys);
+        addPoly(points, cRed, s_startPolys);
+        points.fini();
+    }
 
 
     s_startPolys.trim();
@@ -158,7 +163,7 @@ Ball* addBall(const Vector2& vecPos,
 
     return pBall;
 }
-Polygon* addPoly(const vector<Vector2>& vecPoints,
+Polygon* addPoly(const Array<Vector2>& vecPoints,
                  const float* color,
                  quePoly& que)
 {
index 000bdf4..0c10644 100644 (file)
 #include "game.h"
 
 #include <bear/debug.h>
+#include <bear/Array.h>
 using namespace bear;
 
-#include <vector>
-using std::vector;
-
 #include "entityCreator.h"
 #include "effectManager.h"
 
@@ -35,11 +33,11 @@ using std::vector;
 /// ***** Private Variables *****
 
 // The stack of active game states
-static vector<GameState*> s_active_States;
+static Array<GameState*> s_active_States;
 
 // Pointers to each possible game state
 // inserted and removed from the s_active_States
-static vector<GameState*> s_possible_States;
+static Array<GameState*> s_possible_States;
 
 // true if the top state requested itself to be poped
 static bool s_bPopState;
@@ -51,6 +49,9 @@ static GameState* s_pPushState;
 
 void game::init()
 {
+    s_active_States.init();
+    s_possible_States.init();
+
     s_bPopState = false;
     s_pPushState = NULL;
 
@@ -59,11 +60,11 @@ void game::init()
     effect::init();
 
     Running* pRunning = new Running();
-    s_possible_States.push_back(pRunning);
-    s_possible_States.push_back(new Paused());
-    s_possible_States.push_back(new CreatingPolygon());
+    s_possible_States.pushBack(pRunning);
+    s_possible_States.pushBack(new Paused());
+    s_possible_States.pushBack(new CreatingPolygon());
 
-    s_active_States.push_back(pRunning);
+    s_active_States.pushBack(pRunning);
 
     DPF(0, "World Created");
 }
@@ -73,25 +74,26 @@ void game::clean()
     effect::clean();
     creator::clean();
 
-    for(unsigned int i=0; i < s_possible_States.size(); i++)
+    for(unsigned int i=0; i < s_possible_States.getLength(); i++)
     {
-        delete s_possible_States[i];
+        delete s_possible_States.getAt(i);
     }
-    s_possible_States.clear();
-    s_active_States.clear();
+
+    s_active_States.fini();
+    s_possible_States.fini();
 }
 
 void game::handleInput()
 {
     creator::handleInput();
 
-    int iLast = s_active_States.size() -1;
+    int iLast = s_active_States.getLength() - 1;
 
-    for(unsigned int i=0; i < s_possible_States.size(); i++)
+    for(unsigned int i=0; i < s_possible_States.getLength(); i++)
     {
-        GameState* pState = s_possible_States[i];
+        GameState* pState = s_possible_States.getAt(i);
 
-        if(s_active_States[iLast] != pState && pState->pushMe())
+        if(s_active_States.getAt(iLast) != pState && pState->pushMe())
         {
             s_pPushState = pState;
         }
@@ -104,13 +106,13 @@ void game::handleInput()
     {
         if( i == iLast )
         {
-            if(s_active_States[i]->popMe())
+            if(s_active_States.getAt(i)->popMe())
                 s_bPopState = true;
             else
-                s_active_States[i]->handleInput(true);
+                s_active_States.getAt(i)->handleInput(true);
         }
         else
-            s_active_States[i]->handleInput(false);
+            s_active_States.getAt(i)->handleInput(false);
     }
 }
 
@@ -118,38 +120,38 @@ void game::update(float fTimeStep)
 {
     if(s_bPopState)
     {
-        s_active_States.pop_back();
+        s_active_States.popBack();
         s_bPopState = false;
     }
 
     if(s_pPushState != NULL)
     {
-        s_active_States.push_back(s_pPushState);
+        s_active_States.pushBack(s_pPushState);
         s_pPushState = NULL;
     }
 
-    int iLast = s_active_States.size() -1;
+    int iLast = s_active_States.getLength() - 1;
     for( int i = 0;
          i <= iLast;
          i++ )
     {
         if( i == iLast )
-            s_active_States[i]->update(fTimeStep, true);
+            s_active_States.getAt(i)->update(fTimeStep, true);
         else
-            s_active_States[i]->update(fTimeStep, false);
+            s_active_States.getAt(i)->update(fTimeStep, false);
     }
 }
 
 void game::draw()
 {
-    int iLast = s_active_States.size() -1;
+    int iLast = s_active_States.getLength() - 1;
     for( int i = 0;
          i <= iLast;
          i++ )
     {
         if( i == iLast )
-            s_active_States[i]->draw(true);
+            s_active_States.getAt(i)->draw(true);
         else
-            s_active_States[i]->draw(false);
+            s_active_States.getAt(i)->draw(false);
     }
 }
index afc4441..baf36d8 100644 (file)
@@ -35,7 +35,7 @@ using std::endl;
 /// ***** Private Method Headers *****
 
 void glDrawCircle(int);
-void glDrawPolygon( const std::vector<Vector2>& points );
+void glDrawPolygon( const Array<Vector2>& points );
 void sdlInit();
 void glInit();
 
@@ -69,7 +69,7 @@ void graphics::drawCircle(float radius, const Vector2& pos, const float* color)
 
 void graphics::drawPolygon
 (
-  const std::vector<Vector2>& points,
+  const Array<Vector2>& points,
   const float* color
 )
 {
@@ -98,12 +98,12 @@ void glDrawCircle(int pieces)
     glEnd();
 }
 
-void glDrawPolygon( const std::vector<Vector2>& points )
+void glDrawPolygon( const Array<Vector2>& points )
 {
     glBegin(GL_POLYGON);
-        for(unsigned int n = 0; n < points.size(); n++)
+        for(unsigned int n = 0; n < points.getLength(); n++)
         {
-            const Vector2& vec = points.at(n);
+            const Vector2& vec = points.getAt(n);
 
             glVertex3f(vec.m_fX, vec.m_fY, 0);
         }
index 6c58ef7..349677f 100644 (file)
 #define GRAPHICS_H
 
 #include <bear/Vector2.h>
+#include <bear/Array.h>
 using namespace bear;
 
-#include <vector>
-
 
 /// ***** Header Methods *****
 
@@ -32,7 +31,7 @@ namespace graphics
     void clean();
 
     void drawCircle(float radius, const Vector2&, const float* color = 0);
-    void drawPolygon(const std::vector<Vector2>& points, const float* color = 0);
+    void drawPolygon(const Array<Vector2>& points, const float* color = 0);
 }
 
 #endif // GRAPHICS_H