issue #1468:change CCPoint CCSize CCRect from class to struct

This commit is contained in:
minggo 2013-01-14 14:45:16 +08:00
parent e1ca1fdb2b
commit 984b304ec5
4 changed files with 83 additions and 88 deletions

View File

@ -67,107 +67,128 @@ CCPointArray* CCPointArray::create(unsigned int capacity)
bool CCPointArray::initWithCapacity(unsigned int capacity)
{
m_pControlPoints = new CCArray(capacity);
m_pControlPoints = new vector<CCPoint*>();
return true;
}
CCObject* CCPointArray::copyWithZone(cocos2d::CCZone *zone)
{
CCArray *newArray = (CCArray*)m_pControlPoints->copy();
vector<CCPoint*> *newArray = new vector<CCPoint*>();
vector<CCPoint*>::iterator iter;
for (iter = m_pControlPoints->begin(); iter != m_pControlPoints->end(); ++iter)
{
newArray->push_back(new CCPoint((*iter)->x, (*iter)->y));
}
CCPointArray *points = CCPointArray::create(10);
points->retain();
points->setControlPoints(newArray);
newArray->release();
return points;
}
CCPointArray::~CCPointArray()
{
CC_SAFE_RELEASE_NULL(m_pControlPoints);
vector<CCPoint*>::iterator iter;
for (iter = m_pControlPoints->begin(); iter != m_pControlPoints->end(); ++iter)
{
delete *iter;
}
delete m_pControlPoints;
}
CCPointArray::CCPointArray() :m_pControlPoints(NULL){}
void CCPointArray::addControlPoint(CCPoint controlPoint)
const std::vector<CCPoint*>* CCPointArray::getControlPoints()
{
// should create a new object: CCPoint
// because developers are accustomed to using
// addControlPoint(ccp(x, y))
// which assumes controlPoint is a temporary struct
// but CCArray::addObject() will retain the passed object, so temp
// should be an object created in the heap.
CCPoint *temp = new CCPoint(controlPoint.x, controlPoint.y);
m_pControlPoints->addObject(temp);
temp->release();
return m_pControlPoints;
}
void CCPointArray::setControlPoints(vector<CCPoint*> *controlPoints)
{
CCAssert(controlPoints != NULL, "control ponts should not be null");
// delete old ponts
vector<CCPoint*>::iterator iter;
for (iter = m_pControlPoints->begin(); iter != m_pControlPoints->end(); ++iter)
{
delete *iter;
}
delete m_pControlPoints;
m_pControlPoints = controlPoints;
}
void CCPointArray::addControlPoint(CCPoint controlPoint)
{
m_pControlPoints->push_back(new CCPoint(controlPoint.x, controlPoint.y));
}
void CCPointArray::insertControlPoint(CCPoint &controlPoint, unsigned int index)
{
// should create a new object: CCPoint
// because developers are accustomed to using
// insertControlPoint(ccp(x, y))
// which assumes controlPoint is a temporary struct
// but CCArray::insertObject() will retain the passed object, so temp
// should be an object created in the heap.
CCPoint *temp = new CCPoint(controlPoint.x, controlPoint.y);
m_pControlPoints->insertObject(temp, index);
temp->release();
m_pControlPoints->insert(m_pControlPoints->begin() + index, temp);
}
CCPoint CCPointArray::getControlPointAtIndex(unsigned int index)
{
index = MIN(m_pControlPoints->count()-1, MAX(index, 0));
CCPoint point = *((CCPoint*)m_pControlPoints->objectAtIndex(index));
return point;
index = MIN(m_pControlPoints->size()-1, MAX(index, 0));
return *(m_pControlPoints->at(index));
}
void CCPointArray::replaceControlPoint(cocos2d::CCPoint &controlPoint, unsigned int index)
{
// should create a new object: CCPoint
// because developers are accustomed to using
// replaceControlPoint(ccp(x, y))
// which assumes controlPoint is a temporary struct
// but CCArray::insertObject() will retain the passed object, so temp
// should be an object created in the heap.
CCPoint *temp = new CCPoint(controlPoint.x, controlPoint.y);
m_pControlPoints->replaceObjectAtIndex(index, temp);
temp->release();
CCPoint *temp = m_pControlPoints->at(index);
temp->x = controlPoint.x;
temp->y = controlPoint.y;
}
void CCPointArray::removeControlPointAtIndex(unsigned int index)
{
m_pControlPoints->removeObjectAtIndex(index);
m_pControlPoints->erase(m_pControlPoints->begin() + index);
}
unsigned int CCPointArray::count()
{
return m_pControlPoints->count();
return m_pControlPoints->size();
}
CCPointArray* CCPointArray::reverse()
{
CCArray *newArray = new CCArray(m_pControlPoints->count());
for (int i = m_pControlPoints->count()-1; i >= 0; --i)
vector<CCPoint*> *newArray = new vector<CCPoint*>();
vector<CCPoint*>::reverse_iterator iter;
CCPoint *point = NULL;
for (iter = m_pControlPoints->rbegin(); iter != m_pControlPoints->rend(); ++iter)
{
newArray->addObject(m_pControlPoints->objectAtIndex(i));
point = *iter;
newArray->push_back(new CCPoint(point->x, point->y));
}
CCPointArray *config = CCPointArray::create(0);
config->setControlPoints(newArray);
newArray->release();
return config;
}
void CCPointArray::reverseInline()
{
unsigned int l = m_pControlPoints->count();
unsigned int l = m_pControlPoints->size();
CCPoint *p1 = NULL;
CCPoint *p2 = NULL;
int x, y;
for (unsigned int i = 0; i < l/2; ++i)
{
m_pControlPoints->exchangeObjectAtIndex(i, l-i-1);
p1 = m_pControlPoints->at(i);
p2 = m_pControlPoints->at(l-i-1);
x = p1->x;
y = p1->y;
p1->x = p2->x;
p1->y = p2->y;
p2->x = x;
p2->y = y;
}
}
@ -250,7 +271,7 @@ void CCCardinalSplineTo::startWithTarget(cocos2d::CCNode *pTarget)
// Issue #1441
m_fDeltaT = (float) 1 / (m_pPoints->count() - 1);
m_previousPosition = pTarget->getPosition();
m_previousPosition = pTarget->getPosition();
m_accumulatedDiff = CCPointZero;
}
@ -304,12 +325,12 @@ void CCCardinalSplineTo::update(float time)
CCPoint newPos = ccCardinalSplineAt(pp0, pp1, pp2, pp3, m_fTension, lt);
// Support for stacked actions
CCNode *node = m_pTarget;
CCPoint diff = ccpSub( node->getPosition(), m_previousPosition);
if( diff.x !=0 || diff.y != 0 ) {
m_accumulatedDiff = ccpAdd( m_accumulatedDiff, diff);
newPos = ccpAdd( newPos, m_accumulatedDiff);
// Support for stacked actions
CCNode *node = m_pTarget;
CCPoint diff = ccpSub( node->getPosition(), m_previousPosition);
if( diff.x !=0 || diff.y != 0 ) {
m_accumulatedDiff = ccpAdd( m_accumulatedDiff, diff);
newPos = ccpAdd( newPos, m_accumulatedDiff);
}
this->updatePosition(newPos);

View File

@ -37,6 +37,8 @@
#ifndef __CCACTION_CATMULLROM_H__
#define __CCACTION_CATMULLROM_H__
#include <vector>
#include "CCActionInterval.h"
#include "base_nodes/CCNode.h"
#include "cocoa/CCGeometry.h"
@ -91,16 +93,12 @@ public:
virtual CCObject* copyWithZone(CCZone *zone);
inline CCArray* getControlPoints(){ return m_pControlPoints; }
inline void setControlPoints(CCArray *controlPoints)
{
CC_SAFE_RETAIN(controlPoints);
CC_SAFE_RELEASE(m_pControlPoints);
m_pControlPoints = controlPoints;
}
const std::vector<CCPoint*>* getControlPoints();
void setControlPoints(std::vector<CCPoint*> *controlPoints);
private:
/** Array that contains the control points */
CCArray *m_pControlPoints;
std::vector<CCPoint*> *m_pControlPoints;
};
/** Cardinal Spline path.
@ -141,7 +139,7 @@ protected:
CCPointArray *m_pPoints;
float m_fDeltaT;
float m_fTension;
CCPoint m_previousPosition;
CCPoint m_previousPosition;
CCPoint m_accumulatedDiff;
};

View File

@ -55,13 +55,6 @@ void CCPoint::setPoint(float x, float y)
this->y = y;
}
CCObject* CCPoint::copyWithZone(CCZone* pZone)
{
CCPoint* pRet = new CCPoint();
pRet->setPoint(this->x, this->y);
return pRet;
}
bool CCPoint::equals(const CCPoint& target) const
{
return ((x == target.x) && (y == target.y));
@ -96,13 +89,6 @@ void CCSize::setSize(float width, float height)
this->height = height;
}
CCObject* CCSize::copyWithZone(CCZone* pZone)
{
CCSize* pRet = new CCSize();
pRet->setSize(this->width, this->width);
return pRet;
}
bool CCSize::equals(const CCSize& target) const
{
return ((width == target.width) && (height == target.height));
@ -143,13 +129,6 @@ void CCRect::setRect(float x, float y, float width, float height)
size.height = height;
}
CCObject* CCRect::copyWithZone(CCZone* pZone)
{
CCRect* pRet = new CCRect();
pRet->setRect(this->origin.x, this->origin.y, this->size.width, this->size.height);
return pRet;
}
bool CCRect::equals(const CCRect& rect) const
{
return (origin.equals(rect.origin) &&

View File

@ -35,7 +35,7 @@ NS_CC_BEGIN
* @{
*/
class CC_DLL CCPoint : public CCObject
struct CCPoint
{
public:
float x;
@ -47,11 +47,10 @@ public:
CCPoint(const CCPoint& other);
CCPoint& operator= (const CCPoint& other);
void setPoint(float x, float y);
virtual CCObject* copyWithZone(CCZone* pZone);
bool equals(const CCPoint& target) const;
};
class CC_DLL CCSize : public CCObject
struct CCSize
{
public:
float width;
@ -63,11 +62,10 @@ public:
CCSize(const CCSize& other);
CCSize& operator= (const CCSize& other);
void setSize(float width, float height);
virtual CCObject* copyWithZone(CCZone* pZone);
bool equals(const CCSize& target) const;
};
class CC_DLL CCRect : public CCObject
struct CCRect
{
public:
CCPoint origin;
@ -79,7 +77,6 @@ public:
CCRect(const CCRect& other);
CCRect& operator= (const CCRect& other);
void setRect(float x, float y, float width, float height);
virtual CCObject* copyWithZone(CCZone* pZone);
float getMinX() const; /// return the leftmost x-value of current rect
float getMidX() const; /// return the midpoint x-value of current rect
float getMaxX() const; /// return the rightmost x-value of current rect