2010-12-27 10:26:56 +08:00
|
|
|
/****************************************************************************
|
2011-03-19 10:59:01 +08:00
|
|
|
Copyright (c) 2009-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2009 Matt Oswald
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-07-01 16:11:31 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2016-08-05 09:42:15 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies 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__
|
|
|
|
|
2013-09-03 07:44:25 +08:00
|
|
|
#include <vector>
|
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCNode.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "base/CCProtocols.h"
|
|
|
|
#include "renderer/CCTextureAtlas.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "renderer/CCBatchCommand.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
|
|
|
/**
|
2015-03-21 17:06:26 +08:00
|
|
|
* @addtogroup _2d
|
2012-06-20 18:09:11 +08:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
|
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
|
2015-03-21 17:06:26 +08:00
|
|
|
* (often known as "batch draw").
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Limitations:
|
|
|
|
* - 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.
|
|
|
|
* - 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
|
|
|
{
|
2013-09-01 01:38:10 +08:00
|
|
|
static const int DEFAULT_CAPACITY = 29;
|
2013-07-20 13:01:27 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
public:
|
2015-03-21 17:06:26 +08:00
|
|
|
/** Creates a SpriteBatchNode with a texture2d and capacity of children.
|
|
|
|
* The capacity will be increased in 33% in runtime if it runs out of space.
|
|
|
|
*
|
|
|
|
* @param tex A texture2d.
|
|
|
|
* @param capacity The capacity of children.
|
|
|
|
* @return Return an autorelease object.
|
2013-07-18 07:56:19 +08:00
|
|
|
*/
|
2013-12-12 12:07:20 +08:00
|
|
|
static SpriteBatchNode* createWithTexture(Texture2D* tex, ssize_t capacity = DEFAULT_CAPACITY);
|
2013-07-18 07:56:19 +08:00
|
|
|
|
2015-03-21 17:06:26 +08:00
|
|
|
/** Creates a SpriteBatchNode with a file image (.png, .jpeg, .pvr, etc) and capacity of children.
|
|
|
|
* The capacity will be increased in 33% in runtime if it runs out of space.
|
|
|
|
* The file will be loaded using the TextureMgr.
|
|
|
|
*
|
|
|
|
* @param fileImage A file image (.png, .jpeg, .pvr, etc).
|
|
|
|
* @param capacity The capacity of children.
|
|
|
|
* @return Return an autorelease object.
|
2013-07-18 07:56:19 +08:00
|
|
|
*/
|
2013-12-24 15:49:58 +08:00
|
|
|
static SpriteBatchNode* create(const std::string& fileImage, ssize_t capacity = DEFAULT_CAPACITY);
|
2013-07-18 07:56:19 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2015-03-21 17:06:26 +08:00
|
|
|
/** Returns the TextureAtlas object.
|
|
|
|
*
|
|
|
|
* @return The TextureAtlas object.
|
|
|
|
*/
|
2016-09-12 09:44:21 +08:00
|
|
|
TextureAtlas* getTextureAtlas() { return _textureAtlas; }
|
2013-09-03 07:44:25 +08:00
|
|
|
|
2015-03-21 17:06:26 +08:00
|
|
|
/** Sets the TextureAtlas object.
|
|
|
|
*
|
|
|
|
* @param textureAtlas The TextureAtlas object.
|
|
|
|
*/
|
2016-09-12 09:44:21 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-21 17:06:26 +08:00
|
|
|
/** Returns an array with the descendants (children, gran children, etc.).
|
|
|
|
* This is specific to BatchNode. In order to use the children, use getChildren() instead.
|
|
|
|
*
|
|
|
|
* @return An array with the descendants (children, gran children, etc.).
|
|
|
|
*/
|
2016-09-12 09:44:21 +08:00
|
|
|
const std::vector<Sprite*>& getDescendants() const { return _descendants; }
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-03-21 17:06:26 +08:00
|
|
|
/** Increase the Atlas Capacity. */
|
2012-04-19 14:35:52 +08:00
|
|
|
void increaseAtlasCapacity();
|
|
|
|
|
2015-03-21 17:06:26 +08:00
|
|
|
/** Removes a child given a certain index. It will also cleanup the running actions depending on the cleanup parameter.
|
|
|
|
*
|
|
|
|
* @param index A certain index.
|
|
|
|
* @param doCleanup Whether or not to cleanup the running actions.
|
|
|
|
* @warning Removing a child from a SpriteBatchNode is very slow.
|
|
|
|
*/
|
2013-12-12 12:07:20 +08:00
|
|
|
void removeChildAtIndex(ssize_t index, bool doCleanup);
|
2015-03-21 17:06:26 +08:00
|
|
|
|
|
|
|
/** Append the child.
|
|
|
|
*
|
|
|
|
* @param sprite A Sprite.
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
void appendChild(Sprite* sprite);
|
2015-03-21 17:06:26 +08:00
|
|
|
|
|
|
|
/** Remove a sprite from Atlas.
|
|
|
|
*
|
|
|
|
* @param sprite A Sprite.
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
void removeSpriteFromAtlas(Sprite *sprite);
|
2015-03-21 17:06:26 +08:00
|
|
|
|
|
|
|
/** Rebuild index with a sprite all child.
|
|
|
|
*
|
|
|
|
* @param parent The parent sprite.
|
|
|
|
* @param index The child index.
|
|
|
|
* @return Index.
|
|
|
|
*/
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t rebuildIndexInOrder(Sprite *parent, ssize_t index);
|
2015-03-21 17:06:26 +08:00
|
|
|
|
|
|
|
/** Get the Max image block index,in all child.
|
|
|
|
*
|
|
|
|
* @param sprite The parent sprite.
|
|
|
|
* @return Index.
|
|
|
|
*/
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t highestAtlasIndexInChild(Sprite *sprite);
|
2015-03-21 17:06:26 +08:00
|
|
|
|
|
|
|
/** Get the Min image block index,in all child.
|
|
|
|
*
|
|
|
|
* @param sprite The parent sprite.
|
|
|
|
* @return Index.
|
|
|
|
*/
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t lowestAtlasIndexInChild(Sprite *sprite);
|
2015-03-21 17:06:26 +08:00
|
|
|
|
|
|
|
/** Get the nearest index from the sprite in z.
|
|
|
|
*
|
|
|
|
* @param sprite The parent sprite.
|
|
|
|
* @param z Z order for drawing priority.
|
|
|
|
* @return Index.
|
|
|
|
*/
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t atlasIndexForChild(Sprite *sprite, int z);
|
2015-03-21 17:06:26 +08:00
|
|
|
/* Sprites use this to start sortChildren, don't call this manually. */
|
2012-04-19 14:35:52 +08:00
|
|
|
void reorderBatch(bool reorder);
|
2013-07-16 03:43:22 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// Overrides
|
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
// TextureProtocol
|
2013-12-12 12:07:20 +08:00
|
|
|
virtual Texture2D* getTexture() const override;
|
2013-07-16 03:43:22 +08:00
|
|
|
virtual void setTexture(Texture2D *texture) override;
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
*@code
|
2015-03-21 17:06:26 +08:00
|
|
|
* When this function bound into js or lua,the parameter will be changed.
|
|
|
|
* In js: var setBlendFunc(var src, var dst).
|
|
|
|
* @endcode
|
2013-09-13 11:41:20 +08:00
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-07-16 03:43:22 +08:00
|
|
|
virtual void setBlendFunc(const BlendFunc &blendFunc) override;
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-12-12 12:07:20 +08:00
|
|
|
virtual const BlendFunc& getBlendFunc() const override;
|
2013-07-16 03:43:22 +08:00
|
|
|
|
2015-03-28 14:28:16 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
*/
|
2014-05-31 07:42:05 +08:00
|
|
|
virtual void visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) override;
|
2013-12-23 16:58:26 +08:00
|
|
|
|
|
|
|
using Node::addChild;
|
2013-07-16 03:43:22 +08:00
|
|
|
virtual void addChild(Node * child, int zOrder, int tag) override;
|
2014-06-25 11:27:48 +08:00
|
|
|
virtual void addChild(Node * child, int zOrder, const std::string &name) override;
|
2013-09-03 07:44:25 +08:00
|
|
|
virtual void reorderChild(Node *child, int zOrder) override;
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2015-03-28 14:28:16 +08:00
|
|
|
virtual void removeChild(Node *child, bool cleanup) override;
|
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-07-16 03:43:22 +08:00
|
|
|
virtual void removeAllChildrenWithCleanup(bool cleanup) override;
|
2015-03-28 14:28:16 +08:00
|
|
|
virtual void sortAllChildren() override;
|
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
|
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-12-13 16:47:42 +08:00
|
|
|
virtual std::string getDescription() const override;
|
2012-04-18 18:43:45 +08:00
|
|
|
|
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.
|
2015-12-03 20:00:51 +08:00
|
|
|
* This method should be called only when you are dealing with very big AtlasSprite and when most of the Sprite won't be updated.
|
2015-03-21 17:06:26 +08:00
|
|
|
* For example: a tile map (TMXMap) or a label with lots of characters (LabelBMFont).
|
2012-11-14 18:05:15 +08:00
|
|
|
*/
|
2013-12-12 12:07:20 +08:00
|
|
|
void insertQuadFromSprite(Sprite *sprite, ssize_t index);
|
2014-03-17 19:31:58 +08:00
|
|
|
/* This is the opposite of "addQuadFromSprite.
|
2015-03-21 17:06:26 +08:00
|
|
|
* It add the sprite to the children and descendants array, but it doesn't update add it to the texture atlas
|
|
|
|
*/
|
2014-03-17 19:31:58 +08:00
|
|
|
SpriteBatchNode * addSpriteWithoutQuad(Sprite *child, int z, int aTag);
|
2016-08-14 00:38:42 +08:00
|
|
|
|
|
|
|
/** reserves capacity for the batch node.
|
|
|
|
If the current capacity is bigger, nothing happens.
|
|
|
|
otherwise, a new capacity is allocated */
|
|
|
|
void reserveCapacity(ssize_t newCapacity);
|
2014-04-10 14:11:40 +08:00
|
|
|
CC_CONSTRUCTOR_ACCESS:
|
|
|
|
/**
|
|
|
|
* @js ctor
|
|
|
|
*/
|
|
|
|
SpriteBatchNode();
|
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
|
|
|
virtual ~SpriteBatchNode();
|
|
|
|
|
|
|
|
/** initializes a SpriteBatchNode with a texture2d and capacity of children.
|
2015-01-26 09:50:58 +08:00
|
|
|
The capacity will be increased in 33% in runtime if it runs out of space.
|
2014-04-10 14:11:40 +08:00
|
|
|
*/
|
2015-01-26 09:49:57 +08:00
|
|
|
bool initWithTexture(Texture2D *tex, ssize_t capacity = DEFAULT_CAPACITY);
|
2014-04-10 14:11:40 +08:00
|
|
|
/** initializes a SpriteBatchNode with a file image (.png, .jpeg, .pvr, etc) and a capacity of children.
|
2015-01-26 09:50:58 +08:00
|
|
|
The capacity will be increased in 33% in runtime if it runs out of space.
|
2014-04-10 14:11:40 +08:00
|
|
|
The file will be loaded using the TextureMgr.
|
|
|
|
* @js init
|
|
|
|
* @lua init
|
|
|
|
*/
|
2015-01-26 09:49:57 +08:00
|
|
|
bool initWithFile(const std::string& fileImage, ssize_t capacity = DEFAULT_CAPACITY);
|
2015-03-15 02:33:15 +08:00
|
|
|
bool init() override;
|
2014-04-10 14:11:40 +08:00
|
|
|
|
2014-02-18 14:30:51 +08:00
|
|
|
protected:
|
2013-06-20 14:13:12 +08:00
|
|
|
/** Updates a quad at a certain index into the texture atlas. The Sprite won't be added into the children array.
|
2015-12-03 20:00:51 +08:00
|
|
|
This method should be called only when you are dealing with very big AtlasSprite and when most of the Sprite won't be updated.
|
2013-06-20 14:13:12 +08:00
|
|
|
For example: a tile map (TMXMap) or a label with lots of characters (LabelBMFont)
|
2012-11-14 18:05:15 +08:00
|
|
|
*/
|
2014-03-17 19:31:58 +08:00
|
|
|
void updateQuadFromSprite(Sprite *sprite, ssize_t index);
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-12-12 12:07:20 +08:00
|
|
|
void updateAtlasIndex(Sprite* sprite, ssize_t* curIndex);
|
|
|
|
void swap(ssize_t oldIndex, ssize_t newIndex);
|
2012-04-19 14:35:52 +08:00
|
|
|
void updateBlendFunc();
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TextureAtlas *_textureAtlas;
|
2013-07-05 16:49:22 +08:00
|
|
|
BlendFunc _blendFunc;
|
2014-01-16 08:07:38 +08:00
|
|
|
BatchCommand _batchCommand; // render command
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-23 21:51:19 +08:00
|
|
|
// all descendants: children, grand children, etc...
|
2013-09-03 07:44:25 +08:00
|
|
|
// There is not need to retain/release these objects, since they are already retained by _children
|
|
|
|
// So, using std::vector<Sprite*> is slightly faster than using cocos2d::Array for this particular case
|
|
|
|
std::vector<Sprite*> _descendants;
|
2012-04-18 18:43:45 +08:00
|
|
|
};
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
// end of sprite_nodes group
|
2015-03-21 17:06:26 +08:00
|
|
|
/** @} */
|
2012-06-20 18:09:11 +08:00
|
|
|
|
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__
|