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

前端之家收集整理的这篇文章主要介绍了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);
原文链接:https://www.f2er.com/cocos2dx/343807.html

猜你在找的Cocos2d-x相关文章