void popBack();
void clear();
+ void removeAt(unsigned int uiIndex);
+ void removeItem(const Item& i);
private:
unsigned int getSpaceNeeded(unsigned int uiSize) const;
}
template < class Item >
+void bear::Array<Item>::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<Item>::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<Item>::allocFirstTime(unsigned int uiSize)
{
// santiy checks
#include "entityManager.h"
-/// ***** Constructors/Destructors *****
-
-Running::Running()
-{
- manager::init();
-}
-Running::~Running()
-{
- manager::clean();
-}
-
/// ***** Public Class Methods *****
void Running::handleInput(bool on_top) const
class Running : public GameState
{
public:
- Running();
- virtual ~Running();
+ Running() {};
+ virtual ~Running() {};
virtual void handleInput(bool=false) const;
virtual void update(float, bool=false) const;
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();
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();
}
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);
}
}
}
#ifndef COLLISIONMANAGER_H
#define COLLISIONMANAGER_H
+#include <bear/Array.h>
+
#include "Entities/PhysicsEntity.h"
-#include <set>
-typedef std::set<PhysicsEntity*> setPhys;
+typedef bear::Array<PhysicsEntity*> ArrayPhys;
/// ***** Header Methods *****
namespace collision
void init();
void clean();
- void update(setPhys&);
+ void update(ArrayPhys& a);
}
#endif // COLLISIONMANAGER_H
#include "effectManager.h"
#include <bear/Vector2.h>
+#include <bear/Array.h>
using namespace bear;
-#include <set>
-
#include "Effects/Effect.h"
#include "Effects/Gravity.h"
#include "Effects/GravityWell.h"
/// ***** Private Variables *****
-typedef std::set<Effect*> setEffect;
-setEffect active_Effects;
+typedef bear::Array<Effect*> ArrayEffect;
+ArrayEffect active_Effects;
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 *****
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;
{
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;
{
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;
void creator::init()
{
+ manager::init();
+
Ball* pBall;
float fStartMass = 5;
removeAllBalls(s_startBalls);
s_startBalls.fini();
+
+ manager::clean();
}
/// ***** Public Methods *****
#include <bear/debug.h>
#include <bear/Lock.h>
#include <bear/Autolock.h>
+#include <bear/Array.h>
using namespace bear;
-#include <set>
#include <SDL/SDL.h>
#include "Entities/Entity.h"
/// ***** Private Variables *****
-typedef std::set<Particle*> setPart;
-static setPart particles_To_Add;
-static setPart active_Particles;
-static setPart particles_To_Remove;
+typedef bear::Array<Particle*> ArrayPart;
+static ArrayPart particles_To_Add;
+static ArrayPart active_Particles;
+static ArrayPart particles_To_Remove;
-typedef std::set<PhysicsEntity*> setPhys;
-static setPhys physics_To_Add;
-static setPhys active_Physics;
-static setPhys physics_To_Remove;
+typedef bear::Array<PhysicsEntity*> ArrayPhys;
+static ArrayPhys physics_To_Add;
+static ArrayPhys active_Physics;
+static ArrayPhys physics_To_Remove;
static Lock muSetPart;
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();
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 *****
Particle* pp = dynamic_cast<Particle*>(pe);
if( pp != NULL )
{
- particles_To_Add.insert(pp);
+ particles_To_Add.pushFront(pp);
return;
}
}
PhysicsEntity* ppe = dynamic_cast<PhysicsEntity*>(pe);
if( ppe != NULL )
{
- physics_To_Add.insert(ppe);
+ physics_To_Add.pushFront(ppe);
return;
}
}
Particle* p = dynamic_cast<Particle*>(pe);
if( p != NULL )
{
- particles_To_Remove.insert(p);
+ particles_To_Remove.pushFront(p);
return;
}
}
PhysicsEntity* ppe = dynamic_cast<PhysicsEntity*>(pe);
if( ppe != NULL )
{
- physics_To_Remove.insert(ppe);
+ physics_To_Remove.pushFront(ppe);
return;
}
}
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();
}
}
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();
}
}
}
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)
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()
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();
}
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();
}