Skip to content

Commit 156d1ff

Browse files
author
Walzer
committed
issue #161,add CCSpriteBatchNode
1 parent 0d0b68a commit 156d1ff

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

cocos2dx/include/CCSpriteBatchNode.h

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/****************************************************************************
2+
Copyright (c) 2010 cocos2d-x.org
3+
4+
http://www.cocos2d-x.org
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
23+
****************************************************************************/
24+
25+
#include "CCNode.h"
26+
#include "CCProtocols.h"
27+
#include "CCTextureAtlas.h"
28+
#include "ccMacros.h"
29+
30+
namespace cocos2d {
31+
32+
class CCSprite;
33+
class CCTexture2D;
34+
35+
/** CCSpriteBatchNode is like a batch node: if it contains children, it will draw them in 1 single OpenGL call
36+
* (often known as "batch draw").
37+
*
38+
* A CCSpriteBatchNode can reference one and only one texture (one image file, one texture atlas).
39+
* Only the CCSprites that are contained in that texture can be added to the CCSpriteBatchNode.
40+
* All CCSprites added to a CCSpriteBatchNode are drawn in one OpenGL ES draw call.
41+
* 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.
42+
*
43+
*
44+
* Limitations:
45+
* - 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.
46+
* - 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.
47+
*
48+
* @since v0.7.1
49+
*/
50+
class CCX_DLL CCSpriteBatchNode : public CCNode, public CCTextureProtocol
51+
{
52+
protected:
53+
/** returns the TextureAtlas that is used */
54+
CCTextureAtlas *m_pTextureAtlas;
55+
/** conforms to CCTextureProtocol protocol */
56+
ccBlendFunc m_tBlendFunc;
57+
/** descendants (children, gran children, etc) */
58+
/// chneyan @todo CCArray *descendants_; what is CCArray
59+
60+
public:
61+
CCSpriteBatchNode();
62+
virtual ~CCSpriteBatchNode();
63+
/** creates a CCSpriteBatchNode with a texture2d and a default capacity of 29 children.
64+
The capacity will be increased in 33% in runtime if it run out of space.
65+
*/
66+
CCSpriteBatchNode * batchNodeWithTexture(CCTexture2D *texture);
67+
CCSpriteBatchNode * spriteSheetWithTexture(CCTexture2D *texture);// DEPRECATED_ATTRIBUTE;
68+
69+
/** creates a CCSpriteBatchNode with a texture2d and capacity of children.
70+
The capacity will be increased in 33% in runtime if it run out of space.
71+
*/
72+
CCSpriteBatchNode * batchNodeWithTexture(CCTexture2D *texture, unsigned int capacity);
73+
CCSpriteBatchNode * spriteSheetWithTexture(CCTexture2D *texture, unsigned int capacity); //DEPRECATED_ATTRIBUTE;
74+
75+
/** creates a CCSpriteBatchNode with a file image (.png, .jpeg, .pvr, etc) with a default capacity of 29 children.
76+
The capacity will be increased in 33% in runtime if it run out of space.
77+
The file will be loaded using the TextureMgr.
78+
*/
79+
CCSpriteBatchNode * batchNodeWithFile(const char * fileImage);
80+
CCSpriteBatchNode * spriteSheetWithFile(const char * fileImage);// DEPRECATED_ATTRIBUTE;
81+
82+
/** creates a CCSpriteBatchNode with a file image (.png, .jpeg, .pvr, etc) and capacity of children.
83+
The capacity will be increased in 33% in runtime if it run out of space.
84+
The file will be loaded using the TextureMgr.
85+
*/
86+
CCSpriteBatchNode * batchNodeWithFile(const char *fileImage, unsigned int capacity);
87+
CCSpriteBatchNode * spriteSheetWithFile(const char *fileImage, unsigned int capacity);//DEPRECATED_ATTRIBUTE;
88+
89+
/** initializes a CCSpriteBatchNode with a texture2d and capacity of children.
90+
The capacity will be increased in 33% in runtime if it run out of space.
91+
*/
92+
bool initWithTexture(CCTexture2D *texture, unsigned int capacity);
93+
/** initializes a CCSpriteBatchNode with a file image (.png, .jpeg, .pvr, etc) and a capacity of children.
94+
The capacity will be increased in 33% in runtime if it run out of space.
95+
The file will be loaded using the TextureMgr.
96+
*/
97+
bool initWithFile(const char *fileImage, unsigned int capacity);
98+
99+
void increaseAtlasCapacity();
100+
101+
/** creates an sprite with a rect in the CCSpriteBatchNode.
102+
It's the same as:
103+
- create an standard CCSsprite
104+
- set the usingSpriteSheet = YES
105+
- set the textureAtlas to the same texture Atlas as the CCSpriteBatchNode
106+
@deprecated Use [CCSprite spriteWithBatchNode:rect:] instead;
107+
*/
108+
CCSprite* createSpriteWithRect(CGRect rect);// DEPRECATED_ATTRIBUTE;
109+
110+
/** initializes a previously created sprite with a rect. This sprite will have the same texture as the CCSpriteBatchNode.
111+
It's the same as:
112+
- initialize an standard CCSsprite
113+
- set the usingBatchNode = YES
114+
- set the textureAtlas to the same texture Atlas as the CCSpriteBatchNode
115+
@since v0.99.0
116+
@deprecated Use [CCSprite initWithBatchNode:rect:] instead;
117+
*/
118+
void initSprite(CCSprite* sprite, CGRect rect);// DEPRECATED_ATTRIBUTE;
119+
120+
/** removes a child given a certain index. It will also cleanup the running actions depending on the cleanup parameter.
121+
@warning Removing a child from a CCSpriteBatchNode is very slow
122+
*/
123+
void removeChildAtIndex(int index, bool cleanup);
124+
125+
/** removes a child given a reference. It will also cleanup the running actions depending on the cleanup parameter.
126+
@warning Removing a child from a CCSpriteBatchNode is very slow
127+
*/
128+
void removeChild(CCSprite *sprite, bool cleanup);
129+
130+
void insertChild(CCSprite *child, int inAtlasAtIndex);
131+
void removeSpriteFromAtlas(CCSprite *sprite);
132+
133+
unsigned int rebuildIndexInOrder(CCSprite *parent, unsigned int atlasIndex);
134+
unsigned int atlasIndexForChild(CCSprite *sprite, int z);
135+
//super methods;
136+
virtual CCTexture2D *getTexture();
137+
virtual void setTexture(CCTexture2D *texture);
138+
virtual ccBlendFunc getBlendFunc();
139+
virtual void setBlendFunc(ccBlendFunc blendFunc);
140+
};
141+
} // namespace cocos2d

0 commit comments

Comments
 (0)