CCSpriteBatchNode(精灵池 open draw draw。。。。。close)

         Cocos2d中的CCSprite         


#ifndef __CC_SPRITE_BATCH_NODE_H__

#define __CC_SPRITE_BATCH_NODE_H__


#include "base_nodes/CCNode.h"

#include "CCProtocols.h"

#include "textures/CCTextureAtlas.h"

#include "ccMacros.h"

#include "cocoa/CCArray.h"


NS_CC_BEGIN


#define kDefaultSpriteBatchCapacity   29  //默认子节点数量是29


class CCSprite;


/** CCSpriteBatchNode is like a batch node: if it contains children, it will draw them in 1 single OpenGL call

* (often known as "batch draw").

*

* A CCSpriteBatchNode can reference(引用) one and only one texture (one image file, one texture atlas).

* Only the CCSprites that are contained in that texture can be added to the CCSpriteBatchNode.

* All CCSprites added to a CCSpriteBatchNode are drawn in one OpenGL ES draw call.

* If the CCSprites are not added to a CCSpriteBatchNode then an OpenGL ES draw call will be needed for each one, which is less efficient(有效率的).


1、一个精灵池节点仅引用一张纹理

2、仅当精灵使用的纹理在 精灵池纹理中可获得时    可以加入精灵池节点

3、精灵池中所有节点的绘制 再一次渲染里完成

4、普通精灵没加入到精灵池中时  每绘制一个精灵 就会调用一次OpenGL ES draw  效率低


* Limitations:

*  - The only object that is accepted as child (or grandchild(孙), grand-grandchild, etc...) is CCSprite or any subclass of CCSprite. eg: particles, labels and layer can't be added to a CCSpriteBatchNode.

*  - Either all(或者全部) its children are Aliased(别名) or Antialiased(属性). It can't be a mix(混合). This is because "alias" is a property of the texture, and all the sprites share the same texture.


1、仅精灵和其子节点 可以被加入精灵池

2、

* @since v0.7.1

*/

class CC_DLL CCSpriteBatchNode : public CCNode, public CCTextureProtocol

{

public:


    CCSpriteBatchNode();

    ~CCSpriteBatchNode();


    // property

    

    // retain

    inline CCTextureAtlas* getTextureAtlas(void) { return m_pobTextureAtlas; }

    inline void setTextureAtlas(CCTextureAtlas* textureAtlas) 

    { 

        if (textureAtlas != m_pobTextureAtlas)

        {

            CC_SAFE_RETAIN(textureAtlas);

            CC_SAFE_RELEASE(m_pobTextureAtlas);

            m_pobTextureAtlas = textureAtlas;

        }

    }


    inline CCArray* getDescendants(void) { return m_pobDescendants; } //descendants 子节点


    /** creates a CCSpriteBatchNode with a texture2d and capacity(容量) of children.

    The capacity will be increased in 33% in runtime(运行时间) if it run out of(用完) space. //用完自动增加

    */

    static CCSpriteBatchNode* createWithTexture(CCTexture2D* tex, unsigned int capacity);

{

    CCSpriteBatchNode *batchNode = new CCSpriteBatchNode();

    batchNode->initWithTexture(tex, capacity);

    batchNode->autorelease();


    return batchNode;

}




    static CCSpriteBatchNode* createWithTexture(CCTexture2D* tex) {

        return CCSpriteBatchNode::createWithTexture(tex, kDefaultSpriteBatchCapacity); // kDefaultSpriteBatchCapacity =29

    }


    /** creates a CCSpriteBatchNode with a file image (.png, .jpeg, .pvr, etc) and capacity of children.

    The capacity will be increased in 33% in runtime if it run out of space.

    The file will be loaded using the TextureMgr.

    */

    static CCSpriteBatchNode* create(const char* fileImage, unsigned int capacity);

    static CCSpriteBatchNode* create(const char* fileImage) {

        return CCSpriteBatchNode::create(fileImage, kDefaultSpriteBatchCapacity);

    }


    /** initializes a CCSpriteBatchNode with a texture2d and capacity of children.

    The capacity will be increased in 33% in runtime if it run out of space.

    */

    bool initWithTexture(CCTexture2D *tex, unsigned int capacity);

{

    m_blendFunc.src = CC_BLEND_SRC;

    m_blendFunc.dst = CC_BLEND_DST;

    m_pobTextureAtlas = new CCTextureAtlas();


    if (0 == capacity)

    {

        capacity = kDefaultSpriteBatchCapacity;

    }

    

    m_pobTextureAtlas->initWithTexture(tex, capacity);


    updateBlendFunc();


    // no lazy alloc in this node

    m_pChildren = new CCArray();

    m_pChildren->initWithCapacity(capacity);


    m_pobDescendants = new CCArray();

    m_pobDescendants->initWithCapacity(capacity);


    setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor));

    return true;

}


    /** initializes a CCSpriteBatchNode with a file image (.png, .jpeg, .pvr, etc) and a capacity of children.

    The capacity will be increased in 33% in runtime if it run out of space.

    The file will be loaded using the TextureMgr.

    */

    bool initWithFile(const char* fileImage, unsigned int capacity);

{

    CCTexture2D *pTexture2D = CCTextureCache::sharedTextureCache()->addImage(fileImage);

    return initWithTexture(pTexture2D, capacity);

}







    bool init();


    void increaseAtlasCapacity();


    /** removes a child given a certain index. It will also cleanup the running actions depending on the cleanup parameter.

    @warning Removing a child from a CCSpriteBatchNode is very slow

    */

    void removeChildAtIndex(unsigned int index, bool doCleanup);


    void insertChild(CCSprite *child, unsigned int index);

    void appendChild(CCSprite* sprite);

    void removeSpriteFromAtlas(CCSprite *sprite);


    unsigned int rebuildIndexInOrder(CCSprite *parent, unsigned int index);

    unsigned int highestAtlasIndexInChild(CCSprite *sprite);

    unsigned int lowestAtlasIndexInChild(CCSprite *sprite);

    unsigned int atlasIndexForChild(CCSprite *sprite, int z);

    /* Sprites use this to start sortChildren, don't call this manually */

    void reorderBatch(bool reorder);

    // CCTextureProtocol

    virtual CCTexture2D* getTexture(void);

    virtual void setTexture(CCTexture2D *texture);

    virtual void setBlendFunc(ccBlendFunc blendFunc);

    virtual ccBlendFunc getBlendFunc(void);


    virtual void visit(void);

    virtual void addChild(CCNode * child);

    virtual void addChild(CCNode * child, int zOrder);

    virtual void addChild(CCNode * child, int zOrder, int tag);

    virtual void reorderChild(CCNode * child, int zOrder);

        

    virtual void removeChild(CCNode* child, bool cleanup);

    virtual void removeAllChildrenWithCleanup(bool cleanup);

    virtual void sortAllChildren();

    virtual void draw(void);


protected:

    /** Inserts a quad at a certain index into the texture atlas. The CCSprite won't be added into the children array.

     This method should be called only when you are dealing with very big AtlasSrite and when most of the CCSprite won't be updated.

     For example: a tile map (CCTMXMap) or a label with lots of characters (CCLabelBMFont)

     */

    void insertQuadFromSprite(CCSprite *sprite, unsigned int index);

    /** Updates a quad at a certain index into the texture atlas. The CCSprite won't be added into the children array.

     This method should be called only when you are dealing with very big AtlasSrite and when most of the CCSprite won't be updated.

     For example: a tile map (CCTMXMap) or a label with lots of characters (CCLabelBMFont)

     */

    void updateQuadFromSprite(CCSprite *sprite, unsigned int index);

    /* This is the opposite of "addQuadFromSprite.

    It add the sprite to the children and descendants array, but it doesn't update add it to the texture atlas

    */

    CCSpriteBatchNode * addSpriteWithoutQuad(CCSprite*child, unsigned int z, int aTag);


private:

    void updateAtlasIndex(CCSprite* sprite, int* curIndex);

    void swap(int oldIndex, int newIndex);

    void updateBlendFunc();


protected:

    CCTextureAtlas *m_pobTextureAtlas;

    ccBlendFunc m_blendFunc;


    // all descendants: children, gran children, etc...

    CCArray* m_pobDescendants;

};


// end of sprite_nodes group

/// @}


NS_CC_END


#endif // __CC_SPRITE_BATCH_NODE_H__


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值