Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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_u@R_498_10586@lQuads,@R_498_10586@lQuads)
    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 increase@R_498_10586@lQuadsWith(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);

大佬总结

以上是大佬教程为你收集整理的Cocos2d-x结构学习(八)CCSize、CCPoint、CCTextureAtlas、FIX_POS、CC_PROPERTY_READONLY全部内容,希望文章能够帮你解决Cocos2d-x结构学习(八)CCSize、CCPoint、CCTextureAtlas、FIX_POS、CC_PROPERTY_READONLY所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。