{
public:
Array();
+ Array(const Array<Item>& a);
virtual ~Array() {}
void init(unsigned int uiStartSize = 8, unsigned int uiSizeMultiplier = 2);
void trim();
bool isEmpty() const;
+ unsigned int getLength() const;
+
+ const Item& getAt(unsigned int uiIndex) const;
const Item& getFront() const;
const Item& getBack() const;
void popFront();
void popBack();
+ void clear();
+
private:
- unsigned int getLength() const;
unsigned int getSpaceNeeded(unsigned int uiSize) const;
// alloc helpers
}
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,
}
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
}
}
+template < class Item >
+void bear::Array<Item>::clear()
+{
+ // HACK
+ while(!isEmpty())
+ {
+ popFront();
+ }
+}
+
template < class Item >
unsigned int bear::Array<Item>::getLength() const
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;
void bear::DASSERT(bool fAssert)
{
+ if(!fAssert)
+ {
+ bear::debug::printTrace();
+ }
+
//#ifdef DEBUG
assert(fAssert);
//#else
/// ***** 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 *****
/// ***** 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;
}
}
#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;
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;
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))
{
const float* color,
queBall& que);
-static Polygon* addPoly(const vector<Vector2>& vecPoints,
+static Polygon* addPoly(const Array<Vector2>& vecPoints,
const float* color,
quePoly& que);
}
- 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();
return pBall;
}
-Polygon* addPoly(const vector<Vector2>& vecPoints,
+Polygon* addPoly(const Array<Vector2>& vecPoints,
const float* color,
quePoly& que)
{
#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"
/// ***** 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;
void game::init()
{
+ s_active_States.init();
+ s_possible_States.init();
+
s_bPopState = false;
s_pPushState = NULL;
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");
}
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;
}
{
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);
}
}
{
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);
}
}
/// ***** Private Method Headers *****
void glDrawCircle(int);
-void glDrawPolygon( const std::vector<Vector2>& points );
+void glDrawPolygon( const Array<Vector2>& points );
void sdlInit();
void glInit();
void graphics::drawPolygon
(
- const std::vector<Vector2>& points,
+ const Array<Vector2>& points,
const float* color
)
{
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);
}
#define GRAPHICS_H
#include <bear/Vector2.h>
+#include <bear/Array.h>
using namespace bear;
-#include <vector>
-
/// ***** Header Methods *****
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