微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Cocos2d-x结构学习八CCSize、CCPoint、CCTextureAtlas、FIX_POS、CC_PROPERTY_READONLY

1、CCSize:一个保存宽度和高度的类,简单不解释了

class CC_DLL CCSize
{
public:
    float width;
    float height;

public:
    CCSize();
    CCSize(float width,float height);
    CCSize(const CCSize& other);
    CCSize(const CCPoint& point);
    CCSize& operator= (const CCSize& other);
    CCSize& operator= (const CCPoint& point);
    CCSize operator+(const CCSize& right) const;
    CCSize operator-(const CCSize& right) const;
    CCSize operator*(float a) const;
    CCSize operator/(float a) const;
    void setSize(float width,float height);
    bool equals(const CCSize& target) const;
};
2、CCPoint:一个保存坐标和相关计算的数学类
class CC_DLL CCPoint
{
public:
    float x;
    float y;

public:
    CCPoint();
    CCPoint(float x,float y);
    CCPoint(const CCPoint& other);
    CCPoint(const CCSize& size);
    CCPoint& operator= (const CCPoint& other);
    CCPoint& operator= (const CCSize& size);
    CCPoint operator+(const CCPoint& right) const;
    CCPoint operator-(const CCPoint& right) const;
    CCPoint operator-() const;
    CCPoint operator*(float a) const;
    CCPoint operator/(float a) const;
    void setPoint(float x,float y);
    bool equals(const CCPoint& target) const;
 
    bool fuzzyEquals(const CCPoint& target,float variance) const;  //带偏移(精度)的相等判断

    inline float getLength() const {               //获取向量长度
        return sqrtf(x*x + y*y);
    };
    inline float getLengthSq() const {
        return dot(*this);
    };

    inline float getdistanceSq(const CCPoint& other) const {
        return (*this - other).getLengthSq();
    };

    inline float getdistance(const CCPoint& other) const {
        return (*this - other).getLength();
    };

    inline float getAngle() const {               //返回向量和X轴间的角度
        return atan2f(y,x);
    };

    float getAngle(const CCPoint& other) const;
    inline float dot(const CCPoint& other) const {
        return x*other.x + y*other.y;
    };

    inline float cross(const CCPoint& other) const {
        return x*other.y - y*other.x;
    };

    inline CCPoint getPerp() const {         //计算正交向量
        return CCPoint(-y,x);
    };

    inline CCPoint getRPerp() const {
        return CCPoint(y,-x);
    };
    inline CCPoint project(const CCPoint& other) const {
        return other * (dot(other)/other.dot(other));
    };

    inline CCPoint rotate(const CCPoint& other) const {
        return CCPoint(x*other.x - y*other.y,x*other.y + y*other.x);
    };

    inline CCPoint unrotate(const CCPoint& other) const {
        return CCPoint(x*other.x + y*other.y,y*other.x - x*other.y);
    };

    inline CCPoint normalize() const {
        float length = getLength();
        if(length == 0.) return CCPoint(1.f,0);
        return *this / getLength();
    };

    inline CCPoint lerp(const CCPoint& other,float alpha) const {           //线性插值
        return *this * (1.f - alpha) + other * alpha;
    };
3、FIX_POS:宏,截取一个数在指定范围内
#define FIX_POS(_pos,_min,_max) \
    if (_pos < _min)        \
    _pos = _min;        \
else if (_pos > _max)   \
    _pos = _max;        \
4、CCTextureAtlas:纹理地图集类,继承自CCObject
class CC_DLL CCTextureAtlas : public CCObject 
{
protected:
    glushort*           m_pIndices;
#if CC_TEXTURE_ATLAS_USE_VAO
    gluint              m_uVAOname;
#endif
    gluint              m_pBuffersVBO[2];    //0顶点,1索引
    bool                m_bDirty;

    CC_PROPERTY_READONLY(unsigned int,m_uTotalQuads,TotalQuads)
    CC_PROPERTY_READONLY(unsigned int,m_uCapacity,Capacity)
    CC_PROPERTY(CCTexture2D *,m_pTexture,Texture)
    CC_PROPERTY(ccV3F_C4B_T2F_Quad *,m_pQuads,Quads)

public:
    CCTextureAtlas();
    virtual ~CCTextureAtlas();

    const char* description();          //获得描述信息

    static CCTextureAtlas* create(const char* file,unsigned int capacity);    //几个创建和初始化函数
    bool initWithFile(const char* file,unsigned int capacity);
    static CCTextureAtlas* createWithTexture(CCTexture2D *texture,unsigned int capacity);
    bool initWithTexture(CCTexture2D *texture,unsigned int capacity);

    void updateQuad(ccV3F_C4B_T2F_Quad* quad,unsigned int index);                        //QUAD相关处理函数
    void insertQuad(ccV3F_C4B_T2F_Quad* quad,unsigned int index);
    void insertQuads(ccV3F_C4B_T2F_Quad* quads,unsigned int index,unsigned int amount);
    void insertQuadFromIndex(unsigned int fromIndex,unsigned int newIndex);
    void removeQuadAtIndex(unsigned int index);
    void removeQuadsAtIndex(unsigned int index,unsigned int amount);
    void removeAllQuads();

    bool resizeCapacity(unsigned int n);
    void increasetotalQuadsWith(unsigned int amount);
    void moveQuadsFromIndex(unsigned int oldindex,unsigned int amount,unsigned int newIndex);
    void moveQuadsFromIndex(unsigned int index,unsigned int newIndex);
    void fillWithEmptyQuadsFromIndex(unsigned int index,unsigned int amount);
    void drawNumberOfQuads(unsigned int n);
    void drawNumberOfQuads(unsigned int n,unsigned int start);
    void drawQuads();
   
    void listenBackToForeground(CCObject *obj);
    inline void setDirty(bool bDirty) { m_bDirty = bDirty; }

private:
    void setupIndices();
    void mapBuffers();
#if CC_TEXTURE_ATLAS_USE_VAO
    void setupVBOandVAO();
#else
    void setupVBO();
#endif
};
5、CC_PROPERTY_READONLY:定义和获取属性函数,简化操作
#define CC_PROPERTY_READONLY(varType,varName,funName)\
protected: varType varName;\
public: virtual varType get##funName(void);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐