2010-12-27 10:26:56 +08:00
|
|
|
/****************************************************************************
|
2012-06-14 15:13:16 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-03-19 10:59:01 +08:00
|
|
|
Copyright (c) 2009-2010 Ricardo Quesada
|
|
|
|
Copyright (C) 2009 Matt Oswald
|
2011-07-01 16:11:31 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2010-12-27 10:26:56 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __CC_SPRITE_BATCH_NODE_H__
|
|
|
|
#define __CC_SPRITE_BATCH_NODE_H__
|
|
|
|
|
2012-06-19 13:50:11 +08:00
|
|
|
#include "base_nodes/CCNode.h"
|
2010-12-27 10:26:56 +08:00
|
|
|
#include "CCProtocols.h"
|
2012-06-19 13:50:11 +08:00
|
|
|
#include "textures/CCTextureAtlas.h"
|
2010-12-27 10:26:56 +08:00
|
|
|
#include "ccMacros.h"
|
2012-06-19 13:50:11 +08:00
|
|
|
#include "cocoa/CCArray.h"
|
2010-12-27 10:26:56 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup sprite_nodes
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
#define kDefaultSpriteBatchCapacity 29
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
class Sprite;
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
/** SpriteBatchNode is like a batch node: if it contains children, it will draw them in 1 single OpenGL call
|
2012-04-18 18:43:45 +08:00
|
|
|
* (often known as "batch draw").
|
|
|
|
*
|
2013-06-20 14:13:12 +08:00
|
|
|
* A SpriteBatchNode can reference one and only one texture (one image file, one texture atlas).
|
|
|
|
* Only the Sprites that are contained in that texture can be added to the SpriteBatchNode.
|
|
|
|
* All Sprites added to a SpriteBatchNode are drawn in one OpenGL ES draw call.
|
|
|
|
* If the Sprites are not added to a SpriteBatchNode then an OpenGL ES draw call will be needed for each one, which is less efficient.
|
2012-04-18 18:43:45 +08:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* Limitations:
|
2013-06-20 14:13:12 +08:00
|
|
|
* - The only object that is accepted as child (or grandchild, grand-grandchild, etc...) is Sprite or any subclass of Sprite. eg: particles, labels and layer can't be added to a SpriteBatchNode.
|
2012-04-18 18:43:45 +08:00
|
|
|
* - 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.
|
|
|
|
*
|
|
|
|
* @since v0.7.1
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
class CC_DLL SpriteBatchNode : public Node, public TextureProtocol
|
2010-12-27 10:26:56 +08:00
|
|
|
{
|
2012-04-18 18:43:45 +08:00
|
|
|
public:
|
2013-04-12 06:02:09 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
SpriteBatchNode();
|
|
|
|
~SpriteBatchNode();
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// property
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// retain
|
2013-06-20 14:13:12 +08:00
|
|
|
inline TextureAtlas* getTextureAtlas(void) { return _textureAtlas; }
|
|
|
|
inline void setTextureAtlas(TextureAtlas* textureAtlas)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (textureAtlas != _textureAtlas)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CC_SAFE_RETAIN(textureAtlas);
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_textureAtlas);
|
|
|
|
_textureAtlas = textureAtlas;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
inline Array* getDescendants(void) { return _descendants; }
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
/** creates a SpriteBatchNode with a texture2d and capacity of children.
|
2012-04-19 14:35:52 +08:00
|
|
|
The capacity will be increased in 33% in runtime if it run out of space.
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
static SpriteBatchNode* createWithTexture(Texture2D* tex, unsigned int capacity);
|
|
|
|
static SpriteBatchNode* createWithTexture(Texture2D* tex) {
|
|
|
|
return SpriteBatchNode::createWithTexture(tex, kDefaultSpriteBatchCapacity);
|
2012-07-28 02:49:33 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
/** creates a SpriteBatchNode with a file image (.png, .jpeg, .pvr, etc) and capacity of children.
|
2012-04-19 14:35:52 +08:00
|
|
|
The capacity will be increased in 33% in runtime if it run out of space.
|
|
|
|
The file will be loaded using the TextureMgr.
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
static SpriteBatchNode* create(const char* fileImage, unsigned int capacity);
|
|
|
|
static SpriteBatchNode* create(const char* fileImage) {
|
|
|
|
return SpriteBatchNode::create(fileImage, kDefaultSpriteBatchCapacity);
|
2012-07-28 02:49:33 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
/** initializes a SpriteBatchNode with a texture2d and capacity of children.
|
2012-04-19 14:35:52 +08:00
|
|
|
The capacity will be increased in 33% in runtime if it run out of space.
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
bool initWithTexture(Texture2D *tex, unsigned int capacity);
|
|
|
|
/** initializes a SpriteBatchNode with a file image (.png, .jpeg, .pvr, etc) and a capacity of children.
|
2012-04-19 14:35:52 +08:00
|
|
|
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);
|
2012-05-31 06:31:08 +08:00
|
|
|
bool init();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
void increaseAtlasCapacity();
|
|
|
|
|
|
|
|
/** removes a child given a certain index. It will also cleanup the running actions depending on the cleanup parameter.
|
2013-06-20 14:13:12 +08:00
|
|
|
@warning Removing a child from a SpriteBatchNode is very slow
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
|
|
|
void removeChildAtIndex(unsigned int index, bool doCleanup);
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void insertChild(Sprite *child, unsigned int index);
|
|
|
|
void appendChild(Sprite* sprite);
|
|
|
|
void removeSpriteFromAtlas(Sprite *sprite);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
unsigned int rebuildIndexInOrder(Sprite *parent, unsigned int index);
|
|
|
|
unsigned int highestAtlasIndexInChild(Sprite *sprite);
|
|
|
|
unsigned int lowestAtlasIndexInChild(Sprite *sprite);
|
|
|
|
unsigned int atlasIndexForChild(Sprite *sprite, int z);
|
2012-04-19 14:35:52 +08:00
|
|
|
/* Sprites use this to start sortChildren, don't call this manually */
|
|
|
|
void reorderBatch(bool reorder);
|
2013-07-16 03:43:22 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// Overrides
|
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
// TextureProtocol
|
2013-07-16 03:43:22 +08:00
|
|
|
virtual Texture2D* getTexture(void) override;
|
|
|
|
virtual void setTexture(Texture2D *texture) override;
|
|
|
|
virtual void setBlendFunc(const BlendFunc &blendFunc) override;
|
|
|
|
virtual const BlendFunc& getBlendFunc(void) const override;
|
|
|
|
|
|
|
|
virtual void visit(void) override;
|
|
|
|
virtual void addChild(Node * child) override;
|
|
|
|
virtual void addChild(Node * child, int zOrder) override;
|
|
|
|
virtual void addChild(Node * child, int zOrder, int tag) override;
|
|
|
|
virtual void reorderChild(Node * child, int zOrder) override;
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-16 03:43:22 +08:00
|
|
|
virtual void removeChild(Node* child, bool cleanup) override;
|
|
|
|
virtual void removeAllChildrenWithCleanup(bool cleanup) override;
|
|
|
|
virtual void sortAllChildren() override;
|
|
|
|
virtual void draw(void) override;
|
2012-04-18 18:43:45 +08:00
|
|
|
|
|
|
|
protected:
|
2013-06-20 14:13:12 +08:00
|
|
|
/** Inserts a quad at a certain index into the texture atlas. The Sprite 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 Sprite won't be updated.
|
|
|
|
For example: a tile map (TMXMap) or a label with lots of characters (LabelBMFont)
|
2012-11-14 18:05:15 +08:00
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
void insertQuadFromSprite(Sprite *sprite, unsigned int index);
|
|
|
|
/** Updates a quad at a certain index into the texture atlas. The Sprite 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 Sprite won't be updated.
|
|
|
|
For example: a tile map (TMXMap) or a label with lots of characters (LabelBMFont)
|
2012-11-14 18:05:15 +08:00
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
void updateQuadFromSprite(Sprite *sprite, unsigned int index);
|
2012-04-18 18:43:45 +08:00
|
|
|
/* 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
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
SpriteBatchNode * addSpriteWithoutQuad(Sprite*child, unsigned int z, int aTag);
|
2012-04-18 18:43:45 +08:00
|
|
|
|
|
|
|
private:
|
2013-06-20 14:13:12 +08:00
|
|
|
void updateAtlasIndex(Sprite* sprite, int* curIndex);
|
2012-04-19 14:35:52 +08:00
|
|
|
void swap(int oldIndex, int newIndex);
|
|
|
|
void updateBlendFunc();
|
2012-04-18 18:43:45 +08:00
|
|
|
|
|
|
|
protected:
|
2013-06-20 14:13:12 +08:00
|
|
|
TextureAtlas *_textureAtlas;
|
2013-07-05 16:49:22 +08:00
|
|
|
BlendFunc _blendFunc;
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
// all descendants: children, gran children, etc...
|
2013-06-20 14:13:12 +08:00
|
|
|
Array* _descendants;
|
2012-04-18 18:43:45 +08:00
|
|
|
};
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
// end of sprite_nodes group
|
|
|
|
/// @}
|
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_END
|
2010-12-27 10:26:56 +08:00
|
|
|
|
|
|
|
#endif // __CC_SPRITE_BATCH_NODE_H__
|