加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 20:41:54 所属栏目:百科 来源:网络整理
导读: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 CCSi

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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读