From: Patrik Gornicz Date: Wed, 9 Sep 2009 03:16:11 +0000 (-0400) Subject: removed usage of std::set, added removeItem and removeAt to Array class X-Git-Url: http://gitweb.pgornicz.com/?a=commitdiff_plain;h=d9a027e933ef9435ec6ed1adc13a8044740465f2;p=libbear.git removed usage of std::set, added removeItem and removeAt to Array class --- diff --git a/lib/include/bear/Array.h b/lib/include/bear/Array.h index e4d6479..18a3b53 100644 --- a/lib/include/bear/Array.h +++ b/lib/include/bear/Array.h @@ -43,6 +43,8 @@ namespace bear void popBack(); void clear(); + void removeAt(unsigned int uiIndex); + void removeItem(const Item& i); private: unsigned int getSpaceNeeded(unsigned int uiSize) const; @@ -325,6 +327,46 @@ void bear::Array::clear() } template < class Item > +void bear::Array::removeAt(unsigned int uiIndex) +{ + // This is very bad. Fix your code + BEAR_DASSERT(uiIndex < getLength()); + + Item * pStart = withinBounds( m_pHead + uiIndex, m_pArrayItems, m_pArrayItems + m_uiAllocated ); + + // TODO: this could be done using at most 2 memcpys + while( pStart != m_pTail ) + { + // Get the next element + Item * pNext = getNextTail(pStart, m_uiAllocated, m_pArrayItems); + + // Copy the item in array + *pStart = *pNext; + + // Advance the pointer to the next spot + pStart = pNext; + } + + m_pTail = getPrevTail(m_pTail, m_uiAllocated, m_pArrayItems); +} + +template < class Item > +void bear::Array::removeItem(const Item& i) +{ + // Find the first matching item + for(unsigned int ii = 0; ii < getLength(); ii++) + { + if( i == getAt(ii) ) + { + removeAt(ii); + return; // done + } + } + + // not found, do nothing. +} + +template < class Item > void bear::Array::allocFirstTime(unsigned int uiSize) { // santiy checks diff --git a/physics/src/GameStates/Running.cpp b/physics/src/GameStates/Running.cpp index 1356891..6b265a1 100644 --- a/physics/src/GameStates/Running.cpp +++ b/physics/src/GameStates/Running.cpp @@ -20,17 +20,6 @@ #include "entityManager.h" -/// ***** Constructors/Destructors ***** - -Running::Running() -{ - manager::init(); -} -Running::~Running() -{ - manager::clean(); -} - /// ***** Public Class Methods ***** void Running::handleInput(bool on_top) const diff --git a/physics/src/GameStates/Running.h b/physics/src/GameStates/Running.h index 3abe8ce..5e02edc 100644 --- a/physics/src/GameStates/Running.h +++ b/physics/src/GameStates/Running.h @@ -26,8 +26,8 @@ class Running : public GameState { public: - Running(); - virtual ~Running(); + Running() {}; + virtual ~Running() {}; virtual void handleInput(bool=false) const; virtual void update(float, bool=false) const; diff --git a/physics/src/collisionManager.cpp b/physics/src/collisionManager.cpp index 338de43..d6ba5d7 100644 --- a/physics/src/collisionManager.cpp +++ b/physics/src/collisionManager.cpp @@ -48,30 +48,40 @@ static const int sc_iyDivisions = 16; static const int sc_ixScreenSize = 800; static const int sc_iyScreenSize = 600; -setPhys divisions[sc_ixDivisions][sc_iyDivisions]; +ArrayPhys divisions[sc_ixDivisions][sc_iyDivisions]; /// ***** Initializers/Cleaners ***** void collision::init() { - + for( int x = 0; x < sc_ixDivisions; x++ ) + { + for( int y = 0; y < sc_iyDivisions; y++ ) + { + divisions[x][y].init(); + } + } } void collision::clean() { - + for( int x = 0; x < sc_ixDivisions; x++ ) + { + for( int y = 0; y < sc_iyDivisions; y++ ) + { + divisions[x][y].fini(); + } + } } /// ***** Public Methods ***** -void collision::update(setPhys& sp) +void collision::update(ArrayPhys& a) { clearEntities(); - for( setPhys::iterator it = sp.begin(); - it != sp.end(); - it++ ) + for( unsigned int ii = 0; ii < a.getLength(); ii++ ) { - placeEntity(*it); + placeEntity(a.getAt(ii)); } updateEntities(); @@ -81,13 +91,9 @@ void collision::update(setPhys& sp) void clearEntities() { - for( int x = 0; - x < sc_ixDivisions; - x++ ) + for( int x = 0; x < sc_ixDivisions; x++ ) { - for( int y = 0; - y < sc_iyDivisions; - y++ ) + for( int y = 0; y < sc_iyDivisions; y++ ) { divisions[x][y].clear(); } @@ -149,33 +155,35 @@ start: if(y < 0 || sc_iyDivisions <= y) break; - divisions[x][y].insert(ppe); + divisions[x][y].pushFront(ppe); } } } void updateEntities() { - for( int x = 0; - x < sc_ixDivisions; - x++ ) + for( int x = 0; x < sc_ixDivisions; x++ ) { - for( int y = 0; - y < sc_iyDivisions; - y++ ) + for( int y = 0; y < sc_iyDivisions; y++ ) { - for( setPhys::iterator it1 = divisions[x][y].begin(); - it1 != divisions[x][y].end(); - it1++ ) + for( unsigned int i1 = 0; i1 < divisions[x][y].getLength(); i1++ ) { - for( setPhys::iterator it2 = divisions[x][y].begin(); - it2 != divisions[x][y].end(); - it2++ ) + for( unsigned int i2 = 0; i2 < divisions[x][y].getLength(); i2++ ) { - if( *it1 == *it2 ) + if( i1 == i2 ) + { break; + } + + PhysicsEntity* ppe1 = divisions[x][y].getAt(i1); + PhysicsEntity* ppe2 = divisions[x][y].getAt(i2); + + BEAR_DASSERT(NULL != ppe1); + BEAR_DASSERT(NULL != ppe2); + + BEAR_DASSERT(ppe1 != ppe2); - applyCollision(*it1, *it2); + applyCollision(ppe1, ppe2); } } } diff --git a/physics/src/collisionManager.h b/physics/src/collisionManager.h index 72151d7..c9c420b 100644 --- a/physics/src/collisionManager.h +++ b/physics/src/collisionManager.h @@ -18,10 +18,11 @@ #ifndef COLLISIONMANAGER_H #define COLLISIONMANAGER_H +#include + #include "Entities/PhysicsEntity.h" -#include -typedef std::set setPhys; +typedef bear::Array ArrayPhys; /// ***** Header Methods ***** namespace collision @@ -29,7 +30,7 @@ namespace collision void init(); void clean(); - void update(setPhys&); + void update(ArrayPhys& a); } #endif // COLLISIONMANAGER_H diff --git a/physics/src/effectManager.cpp b/physics/src/effectManager.cpp index 0060652..e5fcd15 100644 --- a/physics/src/effectManager.cpp +++ b/physics/src/effectManager.cpp @@ -18,10 +18,9 @@ #include "effectManager.h" #include +#include using namespace bear; -#include - #include "Effects/Effect.h" #include "Effects/Gravity.h" #include "Effects/GravityWell.h" @@ -32,8 +31,8 @@ using namespace bear; /// ***** Private Variables ***** -typedef std::set setEffect; -setEffect active_Effects; +typedef bear::Array ArrayEffect; +ArrayEffect active_Effects; GravityWell* pMouseWell; @@ -41,19 +40,21 @@ GravityWell* pMouseWell; void effect::init() { + active_Effects.init(); + pMouseWell = new GravityWell(Vector2(0,0)); - active_Effects.insert(new Screen()); - active_Effects.insert(new Gravity()); + active_Effects.pushFront(new Screen()); + active_Effects.pushFront(new Gravity()); } void effect::clean() { - for( setEffect::iterator it = active_Effects.begin(); - it != active_Effects.end(); - it++ ) + for( unsigned int ii = 0; ii < active_Effects.getLength(); ii++ ) { // TODO !!!!!!!!! } + + active_Effects.fini(); } /// ***** Public Methods ***** @@ -68,20 +69,21 @@ void effect::handleInput() pMouseWell->setPosition(input::mousePosition()); if(cfg::mouseWellOn()) - active_Effects.insert(pMouseWell); + active_Effects.pushFront(pMouseWell); if(cfg::mouseWellOff()) - active_Effects.erase(pMouseWell); + active_Effects.removeItem(pMouseWell); } Vector2 effect::positionDelta(const PhysicsEntity* ppe, float fTimeStep) { Vector2 acc(0,0); - for( setEffect::iterator it = active_Effects.begin(); - it != active_Effects.end(); - it++ ) + for( unsigned int ii = 0; ii < active_Effects.getLength(); ii++ ) { - acc += (*it)->positionDelta(ppe, fTimeStep); + Effect* pe = active_Effects.getAt(ii); + + BEAR_DASSERT(NULL != pe); + acc += pe->positionDelta(ppe, fTimeStep); } return acc; @@ -90,11 +92,12 @@ Vector2 effect::velocityDelta(const PhysicsEntity* ppe, float fTimeStep) { Vector2 acc(0,0); - for( setEffect::iterator it = active_Effects.begin(); - it != active_Effects.end(); - it++ ) + for( unsigned int ii = 0; ii < active_Effects.getLength(); ii++ ) { - acc += (*it)->velocityDelta(ppe, fTimeStep); + Effect* pe = active_Effects.getAt(ii); + + BEAR_DASSERT(NULL != pe); + acc += pe->velocityDelta(ppe, fTimeStep); } return acc; @@ -103,11 +106,12 @@ Vector2 effect::forceDelta(const PhysicsEntity* ppe, float fTimeStep) { Vector2 acc(0,0); - for( setEffect::iterator it = active_Effects.begin(); - it != active_Effects.end(); - it++ ) + for( unsigned int ii = 0; ii < active_Effects.getLength(); ii++ ) { - acc += (*it)->forceDelta(ppe, fTimeStep); + Effect* pe = active_Effects.getAt(ii); + + BEAR_DASSERT(NULL != pe); + acc += pe->forceDelta(ppe, fTimeStep); } return acc; diff --git a/physics/src/entityCreator.cpp b/physics/src/entityCreator.cpp index 3e2429f..b19798b 100644 --- a/physics/src/entityCreator.cpp +++ b/physics/src/entityCreator.cpp @@ -58,6 +58,8 @@ static void removeAllPolys(quePoly& que); void creator::init() { + manager::init(); + Ball* pBall; float fStartMass = 5; @@ -123,6 +125,8 @@ void creator::clean() removeAllBalls(s_startBalls); s_startBalls.fini(); + + manager::clean(); } /// ***** Public Methods ***** diff --git a/physics/src/entityManager.cpp b/physics/src/entityManager.cpp index 4bb5fee..3d9e939 100644 --- a/physics/src/entityManager.cpp +++ b/physics/src/entityManager.cpp @@ -20,9 +20,9 @@ #include #include #include +#include using namespace bear; -#include #include #include "Entities/Entity.h" @@ -41,15 +41,15 @@ static void addOrRemovePhysics(); /// ***** Private Variables ***** -typedef std::set setPart; -static setPart particles_To_Add; -static setPart active_Particles; -static setPart particles_To_Remove; +typedef bear::Array ArrayPart; +static ArrayPart particles_To_Add; +static ArrayPart active_Particles; +static ArrayPart particles_To_Remove; -typedef std::set setPhys; -static setPhys physics_To_Add; -static setPhys active_Physics; -static setPhys physics_To_Remove; +typedef bear::Array ArrayPhys; +static ArrayPhys physics_To_Add; +static ArrayPhys active_Physics; +static ArrayPhys physics_To_Remove; static Lock muSetPart; static Lock muSetPhys; @@ -58,6 +58,14 @@ static Lock muSetPhys; void manager::init() { + particles_To_Add.init(); + active_Particles.init(); + particles_To_Remove.init(); + + physics_To_Add.init(); + active_Physics.init(); + physics_To_Remove.init(); + muSetPart.init(); muSetPhys.init(); @@ -69,6 +77,14 @@ void manager::clean() muSetPhys.fini(); muSetPart.fini(); + + particles_To_Add.fini(); + active_Particles.fini(); + particles_To_Remove.fini(); + + physics_To_Add.fini(); + active_Physics.fini(); + physics_To_Remove.fini(); } /// ***** Public Methods ***** @@ -81,7 +97,7 @@ void manager::add(Entity* pe) Particle* pp = dynamic_cast(pe); if( pp != NULL ) { - particles_To_Add.insert(pp); + particles_To_Add.pushFront(pp); return; } } @@ -90,7 +106,7 @@ void manager::add(Entity* pe) PhysicsEntity* ppe = dynamic_cast(pe); if( ppe != NULL ) { - physics_To_Add.insert(ppe); + physics_To_Add.pushFront(ppe); return; } } @@ -106,7 +122,7 @@ void manager::remove(Entity* pe) Particle* p = dynamic_cast(pe); if( p != NULL ) { - particles_To_Remove.insert(p); + particles_To_Remove.pushFront(p); return; } } @@ -116,7 +132,7 @@ void manager::remove(Entity* pe) PhysicsEntity* ppe = dynamic_cast(pe); if( ppe != NULL ) { - physics_To_Remove.insert(ppe); + physics_To_Remove.pushFront(ppe); return; } } @@ -143,11 +159,12 @@ void manager::draw() addOrRemoveParticles(); // draw active Particle*s - for( setPart::iterator it = active_Particles.begin(); - it != active_Particles.end(); - it++ ) + for( unsigned int ii = 0; ii < active_Particles.getLength(); ii++ ) { - (*it)->draw(); + Particle* p = active_Particles.getAt(ii); + + BEAR_DASSERT(NULL != p); + p->draw(); } } @@ -157,11 +174,12 @@ void manager::draw() addOrRemovePhysics(); // draw active PhysicsEntity*s - for( setPhys::iterator it = active_Physics.begin(); - it != active_Physics.end(); - it++ ) + for( unsigned int ii = 0; ii < active_Physics.getLength(); ii++ ) { - (*it)->draw(); + PhysicsEntity* p = active_Physics.getAt(ii); + + BEAR_DASSERT(NULL != p); + p->draw(); } } } @@ -173,11 +191,12 @@ void updateParticles(float time_step) addOrRemoveParticles(); // update active Particle*s - for( setPart::iterator it = active_Particles.begin(); - it != active_Particles.end(); - it++ ) + for( unsigned int ii = 0; ii < active_Particles.getLength(); ii++ ) { - (*it)->update(time_step); + Particle* pp = active_Particles.getAt(ii); + + BEAR_DASSERT(NULL != pp); + pp->update(time_step); } } void updatePhysics(float time_step) @@ -188,11 +207,12 @@ void updatePhysics(float time_step) collision::update(active_Physics); // update active PhysicsEntity*s - for( setPhys::iterator it = active_Physics.begin(); - it != active_Physics.end(); - it++ ) + for( unsigned int ii = 0; ii < active_Physics.getLength(); ii++ ) { - (*it)->update(time_step); + PhysicsEntity* ppe = active_Physics.getAt(ii); + + BEAR_DASSERT(NULL != ppe); + ppe->update(time_step); } } void addOrRemoveParticles() @@ -200,20 +220,22 @@ void addOrRemoveParticles() Autolock lock( &muSetPart ); // add new Particle*s to Active - for( setPart::iterator it = particles_To_Add.begin(); - it != particles_To_Add.end(); - it++ ) + for( unsigned int ii = 0; ii < particles_To_Add.getLength(); ii++ ) { - active_Particles.insert(*it); + Particle* pp = particles_To_Add.getAt(ii); + + BEAR_DASSERT(NULL != pp); + active_Particles.pushFront(pp); } particles_To_Add.clear(); // remove dead Particle*s from Active - for( setPart::iterator it = particles_To_Remove.begin(); - it != particles_To_Remove.end(); - it++ ) + for( unsigned int ii = 0; ii < particles_To_Remove.getLength(); ii++ ) { - active_Particles.erase(*it); + Particle* pp = particles_To_Remove.getAt(ii); + + BEAR_DASSERT(NULL != pp); + active_Particles.removeItem(pp); } particles_To_Remove.clear(); } @@ -222,20 +244,22 @@ void addOrRemovePhysics() Autolock lock( &muSetPhys ); // add new PhysicsEntity*s to Active - for( setPhys::iterator it = physics_To_Add.begin(); - it != physics_To_Add.end(); - it++ ) + for( unsigned int ii = 0; ii < physics_To_Add.getLength(); ii++ ) { - active_Physics.insert(*it); + PhysicsEntity* ppe = physics_To_Add.getAt(ii); + + BEAR_DASSERT(NULL != ppe); + active_Physics.pushFront(ppe); } physics_To_Add.clear(); // remove dead PhysicsEntity*s from Active - for( setPhys::iterator it = physics_To_Remove.begin(); - it != physics_To_Remove.end(); - it++ ) + for( unsigned int ii = 0; ii < physics_To_Remove.getLength(); ii++ ) { - active_Physics.erase(*it); + PhysicsEntity* ppe = physics_To_Remove.getAt(ii); + + BEAR_DASSERT(NULL != ppe); + active_Physics.removeItem(ppe); } physics_To_Remove.clear(); }