From d6bfe0d54436cf10805f41aa0427f43ea8c02f20 Mon Sep 17 00:00:00 2001 From: Patrik Gornicz Date: Wed, 2 Sep 2009 00:32:37 -0400 Subject: [PATCH] expanded Array and replaced all usages of std::vector with bear::Array --- lib/include/bear/Array.h | 49 ++++++++++++++++++++++++++++++++- lib/src/debug.cpp | 5 ++++ physics/src/Entities/Polygon.cpp | 22 +++++++-------- physics/src/Entities/Polygon.h | 8 ++---- physics/src/collisionManager.cpp | 12 ++++---- physics/src/entityCreator.cpp | 19 ++++++++----- physics/src/game.cpp | 58 ++++++++++++++++++++------------------- physics/src/graphics/graphics.cpp | 10 +++---- physics/src/graphics/graphics.h | 5 ++-- 9 files changed, 122 insertions(+), 66 deletions(-) diff --git a/lib/include/bear/Array.h b/lib/include/bear/Array.h index 5b8d56d..632ff72 100644 --- a/lib/include/bear/Array.h +++ b/lib/include/bear/Array.h @@ -19,6 +19,7 @@ namespace bear { public: Array(); + Array(const Array& 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::Array() } template < class Item > +bear::Array::Array(const Array& 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::init ( unsigned int uiStartSize, @@ -172,6 +199,15 @@ bool bear::Array::isEmpty() const } template < class Item > +const Item& bear::Array::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::getFront() const { // This is very bad. Fix your code @@ -265,6 +301,16 @@ void bear::Array::popBack() } } +template < class Item > +void bear::Array::clear() +{ + // HACK + while(!isEmpty()) + { + popFront(); + } +} + template < class Item > unsigned int bear::Array::getLength() const @@ -341,6 +387,7 @@ void bear::Array::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; diff --git a/lib/src/debug.cpp b/lib/src/debug.cpp index 443b147..e56ade4 100644 --- a/lib/src/debug.cpp +++ b/lib/src/debug.cpp @@ -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 diff --git a/physics/src/Entities/Polygon.cpp b/physics/src/Entities/Polygon.cpp index 9f1aa43..d93cd32 100644 --- a/physics/src/Entities/Polygon.cpp +++ b/physics/src/Entities/Polygon.cpp @@ -26,17 +26,17 @@ using namespace bear; /// ***** Constructors/Destructors ***** -Polygon::Polygon(const vector& points, const float* color) +Polygon::Polygon(const Array& 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_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; } } diff --git a/physics/src/Entities/Polygon.h b/physics/src/Entities/Polygon.h index 7941f88..bc3a9bb 100644 --- a/physics/src/Entities/Polygon.h +++ b/physics/src/Entities/Polygon.h @@ -19,20 +19,18 @@ #define POLYGON_H #include +#include using namespace bear; #include "PhysicsEntity.h" -#include -using std::vector; - /// ***** Header Class ***** class Polygon: public PhysicsEntity { public: - Polygon(const vector&, const float* color); + Polygon(const Array&, 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 m_points; + Array m_points; const float* m_color; diff --git a/physics/src/collisionManager.cpp b/physics/src/collisionManager.cpp index c9af58d..2242a88 100644 --- a/physics/src/collisionManager.cpp +++ b/physics/src/collisionManager.cpp @@ -340,16 +340,16 @@ bool getInfo(const Polygon* pPoly, const Ball* pBall, CollisionInfo* pcInfo) int iTot = 0; { - const vector& pts = pPoly->m_points; - unsigned int num = pts.size(); + const Array& 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)) { diff --git a/physics/src/entityCreator.cpp b/physics/src/entityCreator.cpp index 79ac89b..3e2429f 100644 --- a/physics/src/entityCreator.cpp +++ b/physics/src/entityCreator.cpp @@ -44,7 +44,7 @@ static Ball* addBall(const Vector2& vecPos, const float* color, queBall& que); -static Polygon* addPoly(const vector& vecPoints, +static Polygon* addPoly(const Array& vecPoints, const float* color, quePoly& que); @@ -97,12 +97,17 @@ void creator::init() } - vector points; - points.push_back(Vector2(500,500)); - points.push_back(Vector2(300,500)); - points.push_back(Vector2(500,300)); + { + Array 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& vecPoints, +Polygon* addPoly(const Array& vecPoints, const float* color, quePoly& que) { diff --git a/physics/src/game.cpp b/physics/src/game.cpp index 000bdf4..0c10644 100644 --- a/physics/src/game.cpp +++ b/physics/src/game.cpp @@ -18,11 +18,9 @@ #include "game.h" #include +#include using namespace bear; -#include -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 s_active_States; +static Array s_active_States; // Pointers to each possible game state // inserted and removed from the s_active_States -static vector s_possible_States; +static Array 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); } } diff --git a/physics/src/graphics/graphics.cpp b/physics/src/graphics/graphics.cpp index afc4441..baf36d8 100644 --- a/physics/src/graphics/graphics.cpp +++ b/physics/src/graphics/graphics.cpp @@ -35,7 +35,7 @@ using std::endl; /// ***** Private Method Headers ***** void glDrawCircle(int); -void glDrawPolygon( const std::vector& points ); +void glDrawPolygon( const Array& 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& points, + const Array& points, const float* color ) { @@ -98,12 +98,12 @@ void glDrawCircle(int pieces) glEnd(); } -void glDrawPolygon( const std::vector& points ) +void glDrawPolygon( const Array& 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); } diff --git a/physics/src/graphics/graphics.h b/physics/src/graphics/graphics.h index 6c58ef7..349677f 100644 --- a/physics/src/graphics/graphics.h +++ b/physics/src/graphics/graphics.h @@ -19,10 +19,9 @@ #define GRAPHICS_H #include +#include using namespace bear; -#include - /// ***** 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& points, const float* color = 0); + void drawPolygon(const Array& points, const float* color = 0); } #endif // GRAPHICS_H -- 2.10.2