diff --git a/cocos2dx/base_nodes/CCNode.cpp b/cocos2dx/base_nodes/CCNode.cpp index 7309b27594..b7970dca2e 100644 --- a/cocos2dx/base_nodes/CCNode.cpp +++ b/cocos2dx/base_nodes/CCNode.cpp @@ -39,7 +39,7 @@ THE SOFTWARE. // externals #include "kazmath/GL/matrix.h" -#if CC_COCOSNODE_RENDER_SUBPIXEL +#if CC_NODE_RENDER_SUBPIXEL #define RENDER_IN_SUBPIXEL #else #define RENDER_IN_SUBPIXEL (int) diff --git a/cocos2dx/include/CCArray.h b/cocos2dx/include/CCArray.h index 12af469e47..53e86f84df 100755 --- a/cocos2dx/include/CCArray.h +++ b/cocos2dx/include/CCArray.h @@ -43,24 +43,25 @@ __arr__++) I found that it's not work in C++. So it keep what it's look like in version 1.0.0-rc3. ---By Bin */ -#define CCARRAY_FOREACH(__array__, __object__) \ - if (__array__ && __array__->data->num > 0) \ - for(CCObject** arr = __array__->data->arr, **end = __array__->data->arr + __array__->data->num-1; \ - arr <= end && ((__object__ = *arr) != NULL/* || true*/); \ +#define CCARRAY_FOREACH(__array__, __object__) \ + if ((__array__) && (__array__)->data->num > 0) \ + for(CCObject** arr = (__array__)->data->arr, **end = (__array__)->data->arr + (__array__)->data->num-1; \ + arr <= end && (((__object__) = *arr) != NULL/* || true*/); \ arr++) -#define CCARRAY_FOREACH_REVERSE(__array__, __object__) \ - if (__array__ && __array__->data->num > 0) \ - for(CCObject** arr = __array__->data->arr + __array__->data->num-1, **end = __array__->data->arr; \ - arr >= end && ((__object__ = *arr) != NULL/* || true*/); \ +#define CCARRAY_FOREACH_REVERSE(__array__, __object__) \ + if ((__array__) && (__array__)->data->num > 0) \ + for(CCObject** arr = (__array__)->data->arr + (__array__)->data->num-1, **end = (__array__)->data->arr; \ + arr >= end && (((__object__) = *arr) != NULL/* || true*/); \ arr--) #if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0) -#define CCARRAY_VERIFY_TYPE(__array__, __type__) \ - do { \ - if (__array__ && __array__->data->num > 0) \ - for(CCObject** arr = __array__->data->arr, **end = __array__->data->arr + __array__->data->num-1; arr <= end; arr++) \ - CCAssert(dynamic_cast<__type__>(*arr), "element type is wrong!"); \ +#define CCARRAY_VERIFY_TYPE(__array__, __type__) \ + do { \ + if ((__array__) && (__array__)->data->num > 0) \ + for(CCObject** arr = (__array__)->data->arr, \ + **end = (__array__)->data->arr + (__array__)->data->num-1; arr <= end; arr++) \ + CCAssert(dynamic_cast<__type__>(*arr), "element type is wrong!"); \ } while(false) #else #define CCARRAY_VERIFY_TYPE(__array__, __type__) void(0) diff --git a/cocos2dx/include/CCNode.h b/cocos2dx/include/CCNode.h index 919f62e650..a670c92a60 100755 --- a/cocos2dx/include/CCNode.h +++ b/cocos2dx/include/CCNode.h @@ -36,590 +36,607 @@ #include "CCGLProgram.h" #include "kazmath/kazmath.h" -namespace cocos2d { - class CCCamera; - class CCGridBase; - class CCPoint; - class CCTouch; - class CCAction; - class CCRGBAProtocol; - class CCLabelProtocol; - class CCScheduler; - class CCActionManager; +NS_CC_BEGIN - enum { - kCCNodeTagInvalid = -1, - }; - - enum { - kCCNodeOnEnter, - kCCNodeOnExit - }; +class CCCamera; +class CCGridBase; +class CCPoint; +class CCTouch; +class CCAction; +class CCRGBAProtocol; +class CCLabelProtocol; +class CCScheduler; +class CCActionManager; -#define arrayMakeObjectsPerformSelectorWithType(pArray, func, type) \ -do { \ - if(pArray && pArray->count() > 0) \ - { \ - CCObject* child; \ - CCARRAY_FOREACH(pArray, child) \ - { \ - type* pNode = (type*) child; \ - if(pNode && (0 != func)) \ - { \ - (pNode->*func)(); \ - } \ - } \ - } \ -} \ +enum { + kCCNodeTagInvalid = -1, +}; + +enum { + kCCNodeOnEnter, + kCCNodeOnExit +}; + +#define arrayMakeObjectsPerformSelectorWithType(pArray, func, elementType) \ +do { \ + if(pArray && pArray->count() > 0) \ + { \ + CCObject* child; \ + CCARRAY_FOREACH(pArray, child) \ + { \ + elementType pNode = (elementType) child; \ + if(pNode && (0 != func)) \ + { \ + (pNode->*func)(); \ + } \ + } \ + } \ +} \ while(false) +#define arrayMakeObjectsPerformSelectorWithObject(pArray, func, pObject, elementType) \ +do { \ + if(pArray && pArray->count() > 0) \ + { \ + CCObject* child = NULL; \ + CCARRAY_FOREACH(pArray, child) \ + { \ + elementType pNode = (elementType) child; \ + if(pNode && (0 != func)) \ + { \ + (pNode->*func)(pObject); \ + } \ + } \ + } \ +} \ +while(false) - /** @brief CCNode is the main element. Anything thats gets drawn or contains things that get drawn is a CCNode. - The most popular CCNodes are: CCScene, CCLayer, CCSprite, CCMenu. +/** @brief CCNode is the main element. Anything thats gets drawn or contains things that get drawn is a CCNode. + The most popular CCNodes are: CCScene, CCLayer, CCSprite, CCMenu. - The main features of a CCNode are: - - They can contain other CCNode nodes (addChild, getChildByTag, removeChild, etc) - - They can schedule periodic callback (schedule, unschedule, etc) - - They can execute actions (runAction, stopAction, etc) + The main features of a CCNode are: + - They can contain other CCNode nodes (addChild, getChildByTag, removeChild, etc) + - They can schedule periodic callback (schedule, unschedule, etc) + - They can execute actions (runAction, stopAction, etc) - Some CCNode nodes provide extra functionality for them or their children. + Some CCNode nodes provide extra functionality for them or their children. - Subclassing a CCNode usually means (one/all) of: - - overriding init to initialize resources and schedule callbacks - - create callbacks to handle the advancement of time - - overriding draw to render the node + Subclassing a CCNode usually means (one/all) of: + - overriding init to initialize resources and schedule callbacks + - create callbacks to handle the advancement of time + - overriding draw to render the node - Features of CCNode: - - position - - scale (x, y) - - rotation (in degrees, clockwise) - - CCCamera (an interface to gluLookAt ) - - CCGridBase (to do mesh transformations) - - anchor point - - size - - visible - - z-order - - openGL z position + Features of CCNode: + - position + - scale (x, y) + - rotation (in degrees, clockwise) + - CCCamera (an interface to gluLookAt ) + - CCGridBase (to do mesh transformations) + - anchor point + - size + - visible + - z-order + - openGL z position - Default values: - - rotation: 0 - - position: (x=0,y=0) - - scale: (x=1,y=1) - - contentSize: (x=0,y=0) - - anchorPoint: (x=0,y=0) + Default values: + - rotation: 0 + - position: (x=0,y=0) + - scale: (x=1,y=1) + - contentSize: (x=0,y=0) + - anchorPoint: (x=0,y=0) - Limitations: - - A CCNode is a "void" object. It doesn't have a texture + Limitations: + - A CCNode is a "void" object. It doesn't have a texture - Order in transformations with grid disabled - -# The node will be translated (position) - -# The node will be rotated (rotation) - -# The node will be scaled (scale) - -# The node will be moved according to the camera values (camera) + Order in transformations with grid disabled + -# The node will be translated (position) + -# The node will be rotated (rotation) + -# The node will be scaled (scale) + -# The node will be moved according to the camera values (camera) - Order in transformations with grid enabled - -# The node will be translated (position) - -# The node will be rotated (rotation) - -# The node will be scaled (scale) - -# The grid will capture the screen - -# The node will be moved according to the camera values (camera) - -# The grid will render the captured screen + Order in transformations with grid enabled + -# The node will be translated (position) + -# The node will be rotated (rotation) + -# The node will be scaled (scale) + -# The grid will capture the screen + -# The node will be moved according to the camera values (camera) + -# The grid will render the captured screen - Camera: - - Each node has a camera. By default it points to the center of the CCNode. + Camera: + - Each node has a camera. By default it points to the center of the CCNode. + */ + +class CC_DLL CCNode : public CCObject +{ + + // variable property + + /** The z order of the node relative to it's "brothers": children of the same parent */ + CC_PROPERTY_READONLY(int, m_nZOrder, ZOrder) + + /** The real openGL Z vertex. + Differences between openGL Z vertex and cocos2d Z order: + - OpenGL Z modifies the Z vertex, and not the Z order in the relation between parent-children + - OpenGL Z might require to set 2D projection + - cocos2d Z order works OK if all the nodes uses the same openGL Z vertex. eg: vertexZ = 0 + @warning: Use it at your own risk since it might break the cocos2d parent-children z order + @since v0.8 */ + CC_PROPERTY(float, m_fVertexZ, VertexZ) - class CC_DLL CCNode : public CCObject - { + /** The rotation (angle) of the node in degrees. 0 is the default rotation angle. Positive values rotate node CW. */ + CC_PROPERTY(float, m_fRotation, Rotation) - // variable property + /** Get the scale factor of the node. + @warning: Assert when m_fScaleX != m_fScaleY. + */ + float getScale(); + /** The scale factor of the node. 1.0 is the default scale factor. It modifies the X and Y scale at the same time. */ + void setScale(float scale); - /** The z order of the node relative to it's "brothers": children of the same parent */ - CC_PROPERTY_READONLY(int, m_nZOrder, ZOrder) + /** The scale factor of the node. 1.0 is the default scale factor. It only modifies the X scale factor. */ + CC_PROPERTY(float, m_fScaleX, ScaleX) - /** The real openGL Z vertex. - Differences between openGL Z vertex and cocos2d Z order: - - OpenGL Z modifies the Z vertex, and not the Z order in the relation between parent-children - - OpenGL Z might require to set 2D projection - - cocos2d Z order works OK if all the nodes uses the same openGL Z vertex. eg: vertexZ = 0 - @warning: Use it at your own risk since it might break the cocos2d parent-children z order - @since v0.8 - */ - CC_PROPERTY(float, m_fVertexZ, VertexZ) + /** The scale factor of the node. 1.0 is the default scale factor. It only modifies the Y scale factor. */ + CC_PROPERTY(float, m_fScaleY, ScaleY) - /** The rotation (angle) of the node in degrees. 0 is the default rotation angle. Positive values rotate node CW. */ - CC_PROPERTY(float, m_fRotation, Rotation) + /** Position (x,y) of the node in OpenGL coordinates. (0,0) is the left-bottom corner. */ + CC_PROPERTY_PASS_BY_REF(CCPoint, m_tPosition, Position) - /** Get the scale factor of the node. - @warning: Assert when m_fScaleX != m_fScaleY. - */ - float getScale(); - /** The scale factor of the node. 1.0 is the default scale factor. It modifies the X and Y scale at the same time. */ - void setScale(float scale); + /** get/set Position for Lua (pass number faster than CCPoint object) - /** The scale factor of the node. 1.0 is the default scale factor. It only modifies the X scale factor. */ - CC_PROPERTY(float, m_fScaleX, ScaleX) + lua code: + local pos = node:getPositionLua() -- return CCPoint object from C++ + local x, y = node:getPosition() -- return x, y values from C++ + local x = node:getPositionX() + local y = node:getPositionY() + node:setPosition(x, y) -- pass x, y values to C++ + node:setPositionX(x) + node:setPositionY(y) + node:setPositionInPixels(x, y) -- pass x, y values to C++ + */ + const CCPoint& getPositionLua(void); + void getPosition(float* x, float* y); + float getPositionX(void); + float getPositionY(void); + void setPositionX(float x); + void setPositionY(float y); + void setPosition(float x, float y); + void _setZOrder(int z); + /** The X skew angle of the node in degrees. + This angle describes the shear distortion in the X direction. + Thus, it is the angle between the Y axis and the left edge of the shape + The default skewX angle is 0. Positive values distort the node in a CW direction. + */ + CC_PROPERTY(float, m_fSkewX, SkewX) - /** The scale factor of the node. 1.0 is the default scale factor. It only modifies the Y scale factor. */ - CC_PROPERTY(float, m_fScaleY, ScaleY) + /** The Y skew angle of the node in degrees. + This angle describes the shear distortion in the Y direction. + Thus, it is the angle between the X axis and the bottom edge of the shape + The default skewY angle is 0. Positive values distort the node in a CCW direction. + */ + CC_PROPERTY(float, m_fSkewY, SkewY) - /** Position (x,y) of the node in OpenGL coordinates. (0,0) is the left-bottom corner. */ - CC_PROPERTY_PASS_BY_REF(CCPoint, m_tPosition, Position) + CC_PROPERTY_READONLY(CCArray*, m_pChildren, Children) - /** get/set Position for Lua (pass number faster than CCPoint object) + /** Get children count */ + unsigned int getChildrenCount(void); - lua code: - local pos = node:getPositionLua() -- return CCPoint object from C++ - local x, y = node:getPosition() -- return x, y values from C++ - local x = node:getPositionX() - local y = node:getPositionY() - node:setPosition(x, y) -- pass x, y values to C++ - node:setPositionX(x) - node:setPositionY(y) - node:setPositionInPixels(x, y) -- pass x, y values to C++ - */ - const CCPoint& getPositionLua(void); - void getPosition(float* x, float* y); - float getPositionX(void); - float getPositionY(void); - void setPositionX(float x); - void setPositionY(float y); - void setPosition(float x, float y); - void _setZOrder(int z); - /** The X skew angle of the node in degrees. - This angle describes the shear distortion in the X direction. - Thus, it is the angle between the Y axis and the left edge of the shape - The default skewX angle is 0. Positive values distort the node in a CW direction. - */ - CC_PROPERTY(float, m_fSkewX, SkewX) + /** A CCCamera object that lets you move the node using a gluLookAt + */ + CC_PROPERTY_READONLY(CCCamera *, m_pCamera, Camera) - /** The Y skew angle of the node in degrees. - This angle describes the shear distortion in the Y direction. - Thus, it is the angle between the X axis and the bottom edge of the shape - The default skewY angle is 0. Positive values distort the node in a CCW direction. - */ - CC_PROPERTY(float, m_fSkewY, SkewY) + /** A CCGrid object that is used when applying effects */ + CC_PROPERTY(CCGridBase *, m_pGrid, Grid) - CC_PROPERTY_READONLY(CCArray*, m_pChildren, Children) + /** Whether of not the node is visible. Default is true */ + CC_PROPERTY(bool, m_bIsVisible, IsVisible) - /** Get children count */ - unsigned int getChildrenCount(void); + /** anchorPoint is the point around which all transformations and positioning manipulations take place. + It's like a pin in the node where it is "attached" to its parent. + The anchorPoint is normalized, like a percentage. (0,0) means the bottom-left corner and (1,1) means the top-right corner. + But you can use values higher than (1,1) and lower than (0,0) too. + The default anchorPoint is (0.5,0.5), so it starts in the center of the node. + @since v0.8 + */ + CC_PROPERTY_PASS_BY_REF(CCPoint, m_tAnchorPoint, AnchorPoint) - /** A CCCamera object that lets you move the node using a gluLookAt - */ - CC_PROPERTY_READONLY(CCCamera *, m_pCamera, Camera) + /** The anchorPoint in absolute pixels. + Since v0.8 you can only read it. If you wish to modify it, use anchorPoint instead + */ + CC_PROPERTY_READONLY_PASS_BY_REF(CCPoint, m_tAnchorPointInPoints, AnchorPointInPoints) - /** A CCGrid object that is used when applying effects */ - CC_PROPERTY(CCGridBase *, m_pGrid, Grid) + /** The untransformed size of the node. + The contentSize remains the same no matter the node is scaled or rotated. + All nodes has a size. Layer and Scene has the same size of the screen. + @since v0.8 + */ + CC_PROPERTY_PASS_BY_REF(CCSize, m_tContentSize, ContentSize) - /** Whether of not the node is visible. Default is true */ - CC_PROPERTY(bool, m_bIsVisible, IsVisible) + /** whether or not the node is running */ + CC_PROPERTY_READONLY(bool, m_bIsRunning, IsRunning) - /** anchorPoint is the point around which all transformations and positioning manipulations take place. - It's like a pin in the node where it is "attached" to its parent. - The anchorPoint is normalized, like a percentage. (0,0) means the bottom-left corner and (1,1) means the top-right corner. - But you can use values higher than (1,1) and lower than (0,0) too. - The default anchorPoint is (0.5,0.5), so it starts in the center of the node. - @since v0.8 - */ - CC_PROPERTY_PASS_BY_REF(CCPoint, m_tAnchorPoint, AnchorPoint) + /** A weak reference to the parent */ + CC_PROPERTY(CCNode *, m_pParent, Parent) - /** The anchorPoint in absolute pixels. - Since v0.8 you can only read it. If you wish to modify it, use anchorPoint instead - */ - CC_PROPERTY_READONLY_PASS_BY_REF(CCPoint, m_tAnchorPointInPoints, AnchorPointInPoints) + /** If true the transformtions will be relative to it's anchor point. + * Sprites, Labels and any other sizeble object use it have it enabled by default. + * Scenes, Layers and other "whole screen" object don't use it, have it disabled by default. + */ + CC_PROPERTY(bool, m_bIsRelativeAnchorPoint, IsRelativeAnchorPoint) - /** The untransformed size of the node. - The contentSize remains the same no matter the node is scaled or rotated. - All nodes has a size. Layer and Scene has the same size of the screen. - @since v0.8 - */ - CC_PROPERTY_PASS_BY_REF(CCSize, m_tContentSize, ContentSize) + /** A tag used to identify the node easily */ + CC_PROPERTY(int, m_nTag, Tag) - /** whether or not the node is running */ - CC_PROPERTY_READONLY(bool, m_bIsRunning, IsRunning) + /** A custom user data pointer */ + CC_PROPERTY(void *, m_pUserData, UserData) + /** Similar to userData, but instead of holding a void* it holds an id */ + CC_SYNTHESIZE_RETAIN(CCObject*, m_pUserObject, UserObject); - /** A weak reference to the parent */ - CC_PROPERTY(CCNode *, m_pParent, Parent) - - /** If true the transformtions will be relative to it's anchor point. - * Sprites, Labels and any other sizeble object use it have it enabled by default. - * Scenes, Layers and other "whole screen" object don't use it, have it disabled by default. - */ - CC_PROPERTY(bool, m_bIsRelativeAnchorPoint, IsRelativeAnchorPoint) - - /** A tag used to identify the node easily */ - CC_PROPERTY(int, m_nTag, Tag) - - /** A custom user data pointer */ - CC_PROPERTY(void *, m_pUserData, UserData) - /** Similar to userData, but instead of holding a void* it holds an id */ - CC_SYNTHESIZE_RETAIN(CCObject*, m_pUserObject, UserObject); - - /** Shader Program - @since v2.0 - */ - CC_SYNTHESIZE_RETAIN(CCGLProgram*, m_pShaderProgram, ShaderProgram); + /** Shader Program + @since v2.0 + */ + CC_SYNTHESIZE_RETAIN(CCGLProgram*, m_pShaderProgram, ShaderProgram); - /** used internally for zOrder sorting, don't change this manually */ - CC_SYNTHESIZE(int, m_nOrderOfArrival, OrderOfArrival); + /** used internally for zOrder sorting, don't change this manually */ + CC_SYNTHESIZE(int, m_nOrderOfArrival, OrderOfArrival); - /** GL server side state - @since v2.0 - */ - CC_SYNTHESIZE(ccGLServerState, m_glServerState, GLServerState); + /** GL server side state + @since v2.0 + */ + CC_SYNTHESIZE(ccGLServerState, m_glServerState, GLServerState); - /** CCActionManager used by all the actions. - IMPORTANT: If you set a new CCActionManager, then previously created actions are going to be removed. - @since v2.0 - */ - CC_PROPERTY(CCActionManager*, m_pActionManager, ActionManager); + /** CCActionManager used by all the actions. + IMPORTANT: If you set a new CCActionManager, then previously created actions are going to be removed. + @since v2.0 + */ + CC_PROPERTY(CCActionManager*, m_pActionManager, ActionManager); - /** CCScheduler used to schedule all "updates" and timers. - IMPORTANT: If you set a new CCScheduler, then previously created timers/update are going to be removed. - @since v2.0 - */ - CC_PROPERTY(CCScheduler*, m_pScheduler, Scheduler); + /** CCScheduler used to schedule all "updates" and timers. + IMPORTANT: If you set a new CCScheduler, then previously created timers/update are going to be removed. + @since v2.0 + */ + CC_PROPERTY(CCScheduler*, m_pScheduler, Scheduler); - protected: +protected: - // transform - CCAffineTransform m_tTransform, m_tInverse; + // transform + CCAffineTransform m_tTransform, m_tInverse; #ifdef CC_NODE_TRANSFORM_USING_AFFINE_MATRIX - GLfloat m_pTransformGL[16]; + GLfloat m_pTransformGL[16]; #endif - // To reduce memory, place bools that are not properties here: - bool m_bIsTransformDirty; - bool m_bIsInverseDirty; - bool m_bReorderChildDirty; - int m_nScriptHandler; + // To reduce memory, place bools that are not properties here: + bool m_bIsTransformDirty; + bool m_bIsInverseDirty; + bool m_bReorderChildDirty; + int m_nScriptHandler; - private: +private: + //! lazy allocs + void childrenAlloc(void); - //! lazy allocs - void childrenAlloc(void); + //! helper that reorder a child + void insertChild(CCNode* child, int z); - //! helper that reorder a child - void insertChild(CCNode* child, int z); + //! used internally to alter the zOrder variable. DON'T call this method manually + void setZOrder(int z); + + + void detachChild(CCNode *child, bool doCleanup); - //! used internally to alter the zOrder variable. DON'T call this method manually - void setZOrder(int z); - - - void detachChild(CCNode *child, bool doCleanup); + typedef void (CCNode::*callbackFunc)(void); - typedef void (CCNode::*callbackFunc)(void); + void arrayMakeObjectsPerformSelector(CCArray* pArray, callbackFunc func); - void arrayMakeObjectsPerformSelector(CCArray* pArray, callbackFunc func); + CCPoint convertToWindowSpace(const CCPoint& nodePoint); - CCPoint convertToWindowSpace(const CCPoint& nodePoint); +public: - public: + CCNode(void); - CCNode(void); + virtual ~CCNode(void); - virtual ~CCNode(void); + char * description(void); - char * description(void); + /** allocates and initializes a node. + The node will be created as "autorelease". + */ + static CCNode * node(void); - /** allocates and initializes a node. - The node will be created as "autorelease". - */ - static CCNode * node(void); + //scene managment - //scene managment + /** callback that is called every time the CCNode enters the 'stage'. + If the CCNode enters the 'stage' with a transition, this callback is called when the transition starts. + During onEnter you can't a "sister/brother" node. + */ + virtual void onEnter(); - /** callback that is called every time the CCNode enters the 'stage'. - If the CCNode enters the 'stage' with a transition, this callback is called when the transition starts. - During onEnter you can't a "sister/brother" node. - */ - virtual void onEnter(); + /** callback that is called when the CCNode enters in the 'stage'. + If the CCNode enters the 'stage' with a transition, this callback is called when the transition finishes. + @since v0.8 + */ + virtual void onEnterTransitionDidFinish(); - /** callback that is called when the CCNode enters in the 'stage'. - If the CCNode enters the 'stage' with a transition, this callback is called when the transition finishes. - @since v0.8 - */ - virtual void onEnterTransitionDidFinish(); + /** callback that is called every time the CCNode leaves the 'stage'. + If the CCNode leaves the 'stage' with a transition, this callback is called when the transition finishes. + During onExit you can't access a sibling node. + */ + virtual void onExit(); - /** callback that is called every time the CCNode leaves the 'stage'. - If the CCNode leaves the 'stage' with a transition, this callback is called when the transition finishes. - During onExit you can't access a sibling node. - */ - virtual void onExit(); + /** callback that is called every time the CCNode leaves the 'stage'. + If the CCNode leaves the 'stage' with a transition, this callback is called when the transition starts. + */ + virtual void onExitTransitionDidStart(); - /** callback that is called every time the CCNode leaves the 'stage'. - If the CCNode leaves the 'stage' with a transition, this callback is called when the transition starts. - */ - virtual void onExitTransitionDidStart(); + /** Register onEnter/onExit handler script function + + Script handler auto unregister after onEnter(). + */ + virtual void registerScriptHandler(int nHandler); + virtual void unregisterScriptHandler(void); - /** Register onEnter/onExit handler script function - - Script handler auto unregister after onEnter(). - */ - virtual void registerScriptHandler(int nHandler); - virtual void unregisterScriptHandler(void); + // composition: ADD - // composition: ADD + /** Adds a child to the container with z-order as 0. + If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately. + @since v0.7.1 + */ + virtual void addChild(CCNode * child); - /** Adds a child to the container with z-order as 0. - If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately. - @since v0.7.1 - */ - virtual void addChild(CCNode * child); + /** Adds a child to the container with a z-order + If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately. + @since v0.7.1 + */ + virtual void addChild(CCNode * child, int zOrder); - /** Adds a child to the container with a z-order - If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately. - @since v0.7.1 - */ - virtual void addChild(CCNode * child, int zOrder); + /** Adds a child to the container with z order and tag + If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately. + @since v0.7.1 + */ + virtual void addChild(CCNode * child, int zOrder, int tag); - /** Adds a child to the container with z order and tag - If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately. - @since v0.7.1 - */ - virtual void addChild(CCNode * child, int zOrder, int tag); + // composition: REMOVE - // composition: REMOVE + /** Remove itself from its parent node. If cleanup is true, then also remove all actions and callbacks. + If the node orphan, then nothing happens. + @since v0.99.3 + */ + void removeFromParentAndCleanup(bool cleanup); - /** Remove itself from its parent node. If cleanup is true, then also remove all actions and callbacks. - If the node orphan, then nothing happens. - @since v0.99.3 - */ - void removeFromParentAndCleanup(bool cleanup); + /** Removes a child from the container. It will also cleanup all running actions depending on the cleanup parameter. + @since v0.7.1 + */ + virtual void removeChild(CCNode* child, bool cleanup); - /** Removes a child from the container. It will also cleanup all running actions depending on the cleanup parameter. - @since v0.7.1 - */ - virtual void removeChild(CCNode* child, bool cleanup); + /** Removes a child from the container by tag value. It will also cleanup all running actions depending on the cleanup parameter + @since v0.7.1 + */ + void removeChildByTag(int tag, bool cleanup); - /** Removes a child from the container by tag value. It will also cleanup all running actions depending on the cleanup parameter - @since v0.7.1 - */ - void removeChildByTag(int tag, bool cleanup); + /** Removes all children from the container and do a cleanup all running actions depending on the cleanup parameter. + @since v0.7.1 + */ + virtual void removeAllChildrenWithCleanup(bool cleanup); - /** Removes all children from the container and do a cleanup all running actions depending on the cleanup parameter. - @since v0.7.1 - */ - virtual void removeAllChildrenWithCleanup(bool cleanup); + // composition: GET + /** Gets a child from the container given its tag + @return returns a CCNode object + @since v0.7.1 + */ + CCNode * getChildByTag(int tag); - // composition: GET - /** Gets a child from the container given its tag - @return returns a CCNode object - @since v0.7.1 - */ - CCNode * getChildByTag(int tag); + /** Reorders a child according to a new z value. + * The child MUST be already added. + */ + virtual void reorderChild(CCNode * child, int zOrder); - /** Reorders a child according to a new z value. - * The child MUST be already added. - */ - virtual void reorderChild(CCNode * child, int zOrder); + /** performance improvement, Sort the children array once before drawing, instead of every time when a child is added or reordered + don't call this manually unless a child added needs to be removed in the same frame */ + virtual void sortAllChildren(); - /** performance improvement, Sort the children array once before drawing, instead of every time when a child is added or reordered - don't call this manually unless a child added needs to be removed in the same frame */ - virtual void sortAllChildren(); + /** Stops all running actions and schedulers + @since v0.8 + */ + virtual void cleanup(void); - /** Stops all running actions and schedulers - @since v0.8 - */ - virtual void cleanup(void); + // draw - // draw + /** Override this method to draw your own node. + The following GL states will be enabled by default: + - glEnableClientState(GL_VERTEX_ARRAY); + - glEnableClientState(GL_COLOR_ARRAY); + - glEnableClientState(GL_TEXTURE_COORD_ARRAY); + - glEnable(GL_TEXTURE_2D); - /** Override this method to draw your own node. - The following GL states will be enabled by default: - - glEnableClientState(GL_VERTEX_ARRAY); - - glEnableClientState(GL_COLOR_ARRAY); - - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - - glEnable(GL_TEXTURE_2D); + AND YOU SHOULD NOT DISABLE THEM AFTER DRAWING YOUR NODE - AND YOU SHOULD NOT DISABLE THEM AFTER DRAWING YOUR NODE + But if you enable any other GL state, you should disable it after drawing your node. + */ + virtual void draw(void); - But if you enable any other GL state, you should disable it after drawing your node. - */ - virtual void draw(void); + /** recursive method that visit its children and draw them */ + virtual void visit(void); - /** recursive method that visit its children and draw them */ - virtual void visit(void); + // transformations - // transformations + /** performs OpenGL view-matrix transformation based on position, scale, rotation and other attributes. */ + void transform(void); - /** performs OpenGL view-matrix transformation based on position, scale, rotation and other attributes. */ - void transform(void); + /** performs OpenGL view-matrix transformation of it's ancestors. + Generally the ancestors are already transformed, but in certain cases (eg: attaching a FBO) + it's necessary to transform the ancestors again. + @since v0.7.2 + */ + void transformAncestors(void); - /** performs OpenGL view-matrix transformation of it's ancestors. - Generally the ancestors are already transformed, but in certain cases (eg: attaching a FBO) - it's necessary to transform the ancestors again. - @since v0.7.2 - */ - void transformAncestors(void); + /** returns a "local" axis aligned bounding box of the node. + The returned box is relative only to its parent. - /** returns a "local" axis aligned bounding box of the node. - The returned box is relative only to its parent. + @since v0.8.2 + */ + CCRect boundingBox(void); - @since v0.8.2 - */ - CCRect boundingBox(void); + // actions - // actions + /** Executes an action, and returns the action that is executed. + The node becomes the action's target. + @warning Starting from v0.8 actions don't retain their target anymore. + @since v0.7.1 + @return An Action pointer + */ - /** Executes an action, and returns the action that is executed. - The node becomes the action's target. - @warning Starting from v0.8 actions don't retain their target anymore. - @since v0.7.1 - @return An Action pointer - */ + CCAction* runAction(CCAction* action); - CCAction* runAction(CCAction* action); + /** Removes all actions from the running action list */ + void stopAllActions(void); - /** Removes all actions from the running action list */ - void stopAllActions(void); + /** Removes an action from the running action list */ + void stopAction(CCAction* action); - /** Removes an action from the running action list */ - void stopAction(CCAction* action); + /** Removes an action from the running action list given its tag + @since v0.7.1 + */ + void stopActionByTag(int tag); - /** Removes an action from the running action list given its tag - @since v0.7.1 - */ - void stopActionByTag(int tag); + /** Gets an action from the running action list given its tag + @since v0.7.1 + @return the Action the with the given tag + */ + CCAction* getActionByTag(int tag); - /** Gets an action from the running action list given its tag - @since v0.7.1 - @return the Action the with the given tag - */ - CCAction* getActionByTag(int tag); - - /** Returns the numbers of actions that are running plus the ones that are schedule to run (actions in actionsToAdd and actions arrays). - * Composable actions are counted as 1 action. Example: - * If you are running 1 Sequence of 7 actions, it will return 1. - * If you are running 7 Sequences of 2 actions, it will return 7. - */ - unsigned int numberOfRunningActions(void); + /** Returns the numbers of actions that are running plus the ones that are schedule to run (actions in actionsToAdd and actions arrays). + * Composable actions are counted as 1 action. Example: + * If you are running 1 Sequence of 7 actions, it will return 1. + * If you are running 7 Sequences of 2 actions, it will return 7. + */ + unsigned int numberOfRunningActions(void); - // timers + // timers - /** check whether a selector is scheduled. */ - bool isScheduled(SEL_SCHEDULE selector); + /** check whether a selector is scheduled. */ + bool isScheduled(SEL_SCHEDULE selector); - /** schedules the "update" method. It will use the order number 0. This method will be called every frame. - Scheduled methods with a lower order value will be called before the ones that have a higher order value. - Only one "update" method could be scheduled per node. + /** schedules the "update" method. It will use the order number 0. This method will be called every frame. + Scheduled methods with a lower order value will be called before the ones that have a higher order value. + Only one "update" method could be scheduled per node. - @since v0.99.3 - */ - void scheduleUpdate(void); + @since v0.99.3 + */ + void scheduleUpdate(void); - /** schedules the "update" selector with a custom priority. This selector will be called every frame. - Scheduled selectors with a lower priority will be called before the ones that have a higher value. - Only one "update" selector could be scheduled per node (You can't have 2 'update' selectors). + /** schedules the "update" selector with a custom priority. This selector will be called every frame. + Scheduled selectors with a lower priority will be called before the ones that have a higher value. + Only one "update" selector could be scheduled per node (You can't have 2 'update' selectors). - @since v0.99.3 - */ - void scheduleUpdateWithPriority(int priority); + @since v0.99.3 + */ + void scheduleUpdateWithPriority(int priority); - /* unschedules the "update" method. + /* unschedules the "update" method. - @since v0.99.3 - */ - void unscheduleUpdate(void); + @since v0.99.3 + */ + void unscheduleUpdate(void); - /** schedules a selector. - The scheduled selector will be ticked every frame - */ - void schedule(SEL_SCHEDULE selector); + /** schedules a selector. + The scheduled selector will be ticked every frame + */ + void schedule(SEL_SCHEDULE selector); - /** schedules a custom selector with an interval time in seconds. - If time is 0 it will be ticked every frame. - If time is 0, it is recommended to use 'scheduleUpdate' instead. - If the selector is already scheduled, then the interval parameter - will be updated without scheduling it again. - */ - void schedule(SEL_SCHEDULE selector, ccTime interval); + /** schedules a custom selector with an interval time in seconds. + If time is 0 it will be ticked every frame. + If time is 0, it is recommended to use 'scheduleUpdate' instead. + If the selector is already scheduled, then the interval parameter + will be updated without scheduling it again. + */ + void schedule(SEL_SCHEDULE selector, ccTime interval); - /** - repeat will execute the action repeat + 1 times, for a continues action use kCCRepeatForever - delay is the amount of time the action will wait before execution - */ - void schedule(SEL_SCHEDULE selector, ccTime interval, unsigned int repeat, ccTime delay); + /** + repeat will execute the action repeat + 1 times, for a continues action use kCCRepeatForever + delay is the amount of time the action will wait before execution + */ + void schedule(SEL_SCHEDULE selector, ccTime interval, unsigned int repeat, ccTime delay); - /** - Schedules a selector that runs only once, with a delay of 0 or larger - */ - void scheduleOnce(SEL_SCHEDULE selector, ccTime delay); + /** + Schedules a selector that runs only once, with a delay of 0 or larger + */ + void scheduleOnce(SEL_SCHEDULE selector, ccTime delay); - /** unschedules a custom selector.*/ - void unschedule(SEL_SCHEDULE selector); + /** unschedules a custom selector.*/ + void unschedule(SEL_SCHEDULE selector); - /** unschedule all scheduled selectors: custom selectors, and the 'update' selector. - Actions are not affected by this method. - @since v0.99.3 - */ - void unscheduleAllSelectors(void); + /** unschedule all scheduled selectors: custom selectors, and the 'update' selector. + Actions are not affected by this method. + @since v0.99.3 + */ + void unscheduleAllSelectors(void); - /** resumes all scheduled selectors and actions. - Called internally by onEnter - */ - void resumeSchedulerAndActions(void); - /** pauses all scheduled selectors and actions. - Called internally by onExit - */ - void pauseSchedulerAndActions(void); + /** resumes all scheduled selectors and actions. + Called internally by onEnter + */ + void resumeSchedulerAndActions(void); + /** pauses all scheduled selectors and actions. + Called internally by onExit + */ + void pauseSchedulerAndActions(void); - // transformation methods + // transformation methods - /** Returns the matrix that transform the node's (local) space coordinates into the parent's space coordinates. - The matrix is in Pixels. - @since v0.7.1 - */ - virtual CCAffineTransform nodeToParentTransform(void); + /** Returns the matrix that transform the node's (local) space coordinates into the parent's space coordinates. + The matrix is in Pixels. + @since v0.7.1 + */ + virtual CCAffineTransform nodeToParentTransform(void); - /** Returns the matrix that transform parent's space coordinates to the node's (local) space coordinates. - The matrix is in Pixels. - @since v0.7.1 - */ - virtual CCAffineTransform parentToNodeTransform(void); + /** Returns the matrix that transform parent's space coordinates to the node's (local) space coordinates. + The matrix is in Pixels. + @since v0.7.1 + */ + virtual CCAffineTransform parentToNodeTransform(void); - /** Retrusn the world affine transform matrix. The matrix is in Pixels. - @since v0.7.1 - */ - virtual CCAffineTransform nodeToWorldTransform(void); + /** Retrusn the world affine transform matrix. The matrix is in Pixels. + @since v0.7.1 + */ + virtual CCAffineTransform nodeToWorldTransform(void); - /** Returns the inverse world affine transform matrix. The matrix is in Pixels. - @since v0.7.1 - */ - virtual CCAffineTransform worldToNodeTransform(void); + /** Returns the inverse world affine transform matrix. The matrix is in Pixels. + @since v0.7.1 + */ + virtual CCAffineTransform worldToNodeTransform(void); - /** Converts a Point to node (local) space coordinates. The result is in Points. - @since v0.7.1 - */ - CCPoint convertToNodeSpace(const CCPoint& worldPoint); - /** Converts a Point to world space coordinates. The result is in Points. - @since v0.7.1 - */ - CCPoint convertToWorldSpace(const CCPoint& nodePoint); - /** Converts a Point to node (local) space coordinates. The result is in Points. - treating the returned/received node point as anchor relative. - @since v0.7.1 - */ - CCPoint convertToNodeSpaceAR(const CCPoint& worldPoint); - /** Converts a local Point to world space coordinates.The result is in Points. - treating the returned/received node point as anchor relative. - @since v0.7.1 - */ - CCPoint convertToWorldSpaceAR(const CCPoint& nodePoint); + /** Converts a Point to node (local) space coordinates. The result is in Points. + @since v0.7.1 + */ + CCPoint convertToNodeSpace(const CCPoint& worldPoint); + /** Converts a Point to world space coordinates. The result is in Points. + @since v0.7.1 + */ + CCPoint convertToWorldSpace(const CCPoint& nodePoint); + /** Converts a Point to node (local) space coordinates. The result is in Points. + treating the returned/received node point as anchor relative. + @since v0.7.1 + */ + CCPoint convertToNodeSpaceAR(const CCPoint& worldPoint); + /** Converts a local Point to world space coordinates.The result is in Points. + treating the returned/received node point as anchor relative. + @since v0.7.1 + */ + CCPoint convertToWorldSpaceAR(const CCPoint& nodePoint); - /** convenience methods which take a CCTouch instead of CCPoint - @since v0.7.1 - */ - CCPoint convertTouchToNodeSpace(CCTouch * touch); + /** convenience methods which take a CCTouch instead of CCPoint + @since v0.7.1 + */ + CCPoint convertTouchToNodeSpace(CCTouch * touch); - /** converts a CCTouch (world coordinates) into a local coordiante. This method is AR (Anchor Relative). - @since v0.7.1 - */ - CCPoint convertTouchToNodeSpaceAR(CCTouch * touch); - }; -}//namespace cocos2d + /** converts a CCTouch (world coordinates) into a local coordiante. This method is AR (Anchor Relative). + @since v0.7.1 + */ + CCPoint convertTouchToNodeSpaceAR(CCTouch * touch); +}; + +NS_CC_END #endif // __PLATFOMR_CCNODE_H__ diff --git a/cocos2dx/include/CCSprite.h b/cocos2dx/include/CCSprite.h index 87143c9bdd..a418cdc75c 100755 --- a/cocos2dx/include/CCSprite.h +++ b/cocos2dx/include/CCSprite.h @@ -155,6 +155,9 @@ public: */ static CCSprite* spriteWithFile(const char *pszFileName, const CCRect& rect); + /** Creates an sprite. + */ + static CCSprite* node(); public: CCSprite(void); virtual ~CCSprite(void); @@ -213,74 +216,74 @@ public: The rect used will be the size of the texture. The offset will be (0,0). */ - bool initWithTexture(CCTexture2D *pTexture); + virtual bool initWithTexture(CCTexture2D *pTexture); /** Initializes an sprite with a texture and a rect. The offset will be (0,0). */ - bool initWithTexture(CCTexture2D *pTexture, const CCRect& rect); + virtual bool initWithTexture(CCTexture2D *pTexture, const CCRect& rect); /** Initializes an sprite with a texture and a rect in points, optionally rotated. The offset will be (0,0). IMPORTANT: This is the designated initializer. */ - bool initWithTexture(CCTexture2D *pTexture, const CCRect& rect, bool rotated); + virtual bool initWithTexture(CCTexture2D *pTexture, const CCRect& rect, bool rotated); // Initializes an sprite with an sprite frame. - bool initWithSpriteFrame(CCSpriteFrame *pSpriteFrame); + virtual bool initWithSpriteFrame(CCSpriteFrame *pSpriteFrame); /** Initializes an sprite with an sprite frame name. An CCSpriteFrame will be fetched from the CCSpriteFrameCache by name. If the CCSpriteFrame doesn't exist it will raise an exception. @since v0.9 */ - bool initWithSpriteFrameName(const char *pszSpriteFrameName); + virtual bool initWithSpriteFrameName(const char *pszSpriteFrameName); /** Initializes an sprite with an image filename. The rect used will be the size of the image. The offset will be (0,0). */ - bool initWithFile(const char *pszFilename); + virtual bool initWithFile(const char *pszFilename); /** Initializes an sprite with an image filename, and a rect. The offset will be (0,0). */ - bool initWithFile(const char *pszFilename, const CCRect& rect); + virtual bool initWithFile(const char *pszFilename, const CCRect& rect); // BatchNode methods /** updates the quad according the the rotation, position, scale values. */ - void updateTransform(void); + virtual void updateTransform(void); /** updates the texture rect of the CCSprite in points. It will call setTextureRect:rotated:untrimmedSize with rotated = NO, and utrimmedSize = rect.size. */ - void setTextureRect(const CCRect& rect); + virtual void setTextureRect(const CCRect& rect); /** set the texture rect, rectRotated and untrimmed size of the CCSprite in points. It will update the texture coordinates and the vertex rectangle. */ - void setTextureRect(const CCRect& rect, bool rotated, const CCSize& untrimmedSize); + virtual void setTextureRect(const CCRect& rect, bool rotated, const CCSize& untrimmedSize); /** set the vertex rect. It will be called internally by setTextureRect. Useful if you want to create 2x images from SD images in Retina Display. Do not call it manually. Use setTextureRect instead. */ - void setVertexRect(const CCRect& rect); + virtual void setVertexRect(const CCRect& rect); // Frames /** sets a new display frame to the CCSprite. */ - void setDisplayFrame(CCSpriteFrame *pNewFrame); + virtual void setDisplayFrame(CCSpriteFrame *pNewFrame); /** returns whether or not a CCSpriteFrame is being displayed */ - bool isFrameDisplayed(CCSpriteFrame *pFrame); + virtual bool isFrameDisplayed(CCSpriteFrame *pFrame); /** returns the current displayed frame. */ - CCSpriteFrame* displayFrame(void); + virtual CCSpriteFrame* displayFrame(void); - CCSpriteBatchNode* getBatchNode(void); - void setBatchNode(CCSpriteBatchNode *pobSpriteBatchNode); + virtual CCSpriteBatchNode* getBatchNode(void); + virtual void setBatchNode(CCSpriteBatchNode *pobSpriteBatchNode); // Animation @@ -288,7 +291,7 @@ public: The animation name will be get from the CCAnimationCache @since v0.99.5 */ - void setDisplayFrameWithAnimationName(const char *animationName, int frameIndex); + virtual void setDisplayFrameWithAnimationName(const char *animationName, int frameIndex); protected: virtual void setTextureCoords(CCRect rect); diff --git a/cocos2dx/sprite_nodes/CCAnimationCache.cpp b/cocos2dx/sprite_nodes/CCAnimationCache.cpp index 345d98b8ba..842f029dfc 100644 --- a/cocos2dx/sprite_nodes/CCAnimationCache.cpp +++ b/cocos2dx/sprite_nodes/CCAnimationCache.cpp @@ -216,15 +216,14 @@ void CCAnimationCache::addAnimationsWithDictionary(CCDictionary* dictionary) if( properties ) { version = atoi(valueForKey("format", properties)); - } + CCArray* spritesheets = (CCArray*)properties->objectForKey("spritesheets"); - CCArray* spritesheets = (CCArray*)properties->objectForKey("spritesheets"); - - CCObject* pObj = NULL; - CCARRAY_FOREACH(spritesheets, pObj) - { - CCString* name = (CCString*)(pObj); - CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(name->c_str()); + CCObject* pObj = NULL; + CCARRAY_FOREACH(spritesheets, pObj) + { + CCString* name = (CCString*)(pObj); + CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(name->c_str()); + } } switch (version) { diff --git a/cocos2dx/sprite_nodes/CCSprite.cpp b/cocos2dx/sprite_nodes/CCSprite.cpp index 3b876d7c22..aa67c839fc 100644 --- a/cocos2dx/sprite_nodes/CCSprite.cpp +++ b/cocos2dx/sprite_nodes/CCSprite.cpp @@ -137,6 +137,20 @@ CCSprite* CCSprite::spriteWithSpriteFrameName(const char *pszSpriteFrameName) return spriteWithSpriteFrame(pFrame); } +CCSprite* CCSprite::node() +{ + CCSprite *pSprite = new CCSprite(); + if (pSprite && pSprite->init()) + { + pSprite->autorelease(); + } + else + { + CC_SAFE_DELETE(pSprite); + } + return pSprite; +} + bool CCSprite::init(void) { return initWithTexture(NULL, CCRectZero); @@ -505,7 +519,7 @@ void CCSprite::updateTransform(void) // recursively iterate over children if( m_bHasChildren ) { - arrayMakeObjectsPerformSelectorWithType(m_pChildren, &CCSprite::updateTransform, CCSprite); + arrayMakeObjectsPerformSelectorWithType(m_pChildren, &CCSprite::updateTransform, CCSprite*); } #if CC_SPRITE_DEBUG_DRAW // draw bounding box @@ -701,7 +715,7 @@ void CCSprite::sortAllChildren() if ( m_pobBatchNode) { - arrayMakeObjectsPerformSelectorWithType(m_pChildren, &CCSprite::sortAllChildren, CCSprite); + arrayMakeObjectsPerformSelectorWithType(m_pChildren, &CCSprite::sortAllChildren, CCSprite*); } m_bReorderChildDirty = false; diff --git a/cocos2dx/sprite_nodes/CCSpriteBatchNode.cpp b/cocos2dx/sprite_nodes/CCSpriteBatchNode.cpp index e0fce84d0c..307ab68a44 100644 --- a/cocos2dx/sprite_nodes/CCSpriteBatchNode.cpp +++ b/cocos2dx/sprite_nodes/CCSpriteBatchNode.cpp @@ -98,10 +98,12 @@ bool CCSpriteBatchNode::initWithTexture(CCTexture2D *tex, unsigned int capacity) updateBlendFunc(); // no lazy alloc in this node - m_pChildren = CCArray::array(); - m_pobDescendants = CCArray::array(); - m_pChildren->retain(); - m_pobDescendants->retain(); + m_pChildren = new CCArray(); + m_pChildren->initWithCapacity(capacity); + + m_pobDescendants = new CCArray(); + m_pobDescendants->initWithCapacity(capacity); + setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor)); return true; } @@ -169,10 +171,10 @@ void CCSpriteBatchNode::visit(void) void CCSpriteBatchNode::addChild(CCNode *child, int zOrder, int tag) { CCAssert(child != NULL, "child should not be null"); - + CCAssert(dynamic_cast(child) != NULL, "CCSpriteBatchNode only supports CCSprites as children"); CCSprite *pSprite = (CCSprite*)(child); // check CCSprite is using the same texture id - CCAssert(pSprite->getTexture()->getName() == m_pobTextureAtlas->getTexture()->getName(), ""); + CCAssert(pSprite->getTexture()->getName() == m_pobTextureAtlas->getTexture()->getName(), "CCSprite is not using the same texture id"); CCNode::addChild(child, zOrder, tag); @@ -231,18 +233,7 @@ void CCSpriteBatchNode::removeChildAtIndex(unsigned int uIndex, bool bDoCleanup) void CCSpriteBatchNode::removeAllChildrenWithCleanup(bool bCleanup) { // Invalidate atlas index. issue #569 - if (m_pChildren && m_pChildren->count() > 0) - { - CCObject* pObject = NULL; - CCARRAY_FOREACH(m_pChildren, pObject) - { - CCSprite* pChild = (CCSprite*) pObject; - if (pChild) - { - removeSpriteFromAtlas(pChild); - } - } - } + arrayMakeObjectsPerformSelectorWithObject(m_pobDescendants, &CCSprite::setBatchNode, NULL, CCSprite*); CCNode::removeAllChildrenWithCleanup(bCleanup); @@ -280,7 +271,7 @@ void CCSpriteBatchNode::sortAllChildren() if (m_pChildren->count() > 0) { //first sort all children recursively based on zOrder - arrayMakeObjectsPerformSelectorWithType(m_pChildren, &CCSprite::sortAllChildren, CCSprite); + arrayMakeObjectsPerformSelectorWithType(m_pChildren, &CCSprite::sortAllChildren, CCSprite*); int index=0; @@ -403,7 +394,7 @@ void CCSpriteBatchNode::draw(void) CC_NODE_DRAW_SETUP(); - arrayMakeObjectsPerformSelectorWithType(m_pChildren, &CCSprite::updateTransform, CCSprite); + arrayMakeObjectsPerformSelectorWithType(m_pChildren, &CCSprite::updateTransform, CCSprite*); ccGLBlendFunc( m_blendFunc.src, m_blendFunc.dst ); @@ -561,57 +552,41 @@ unsigned int CCSpriteBatchNode::atlasIndexForChild(CCSprite *pobSprite, int nZ) // add child helper -void CCSpriteBatchNode::insertChild(CCSprite *pobSprite, unsigned int uIndex) +void CCSpriteBatchNode::insertChild(CCSprite *pSprite, unsigned int uIndex) { - pobSprite->setBatchNode(this); - pobSprite->setAtlasIndex(uIndex); - pobSprite->setDirty(true); + pSprite->setBatchNode(this); + pSprite->setAtlasIndex(uIndex); + pSprite->setDirty(true); - if (m_pobTextureAtlas->getTotalQuads() == m_pobTextureAtlas->getCapacity()) - { - increaseAtlasCapacity(); - } + if(m_pobTextureAtlas->getTotalQuads() == m_pobTextureAtlas->getCapacity()) + { + increaseAtlasCapacity(); + } - ccV3F_C4B_T2F_Quad quad = pobSprite->getQuad(); - m_pobTextureAtlas->insertQuad(&quad, uIndex); + ccV3F_C4B_T2F_Quad quad = pSprite->getQuad(); + m_pobTextureAtlas->insertQuad(&quad, uIndex); - m_pobDescendants->insertObject(pobSprite, uIndex); + ccArray *descendantsData = m_pobDescendants->data; - // update indices - unsigned int i = 0; - if (m_pobDescendants && m_pobDescendants->count() > 0) - { - CCObject* pObject = NULL; - CCARRAY_FOREACH(m_pobDescendants, pObject) - { - CCSprite* pChild = (CCSprite*) pObject; - if (pChild) - { - if (i > uIndex) - { - pChild->setAtlasIndex(pChild->getAtlasIndex() + 1); - } + ccArrayInsertObjectAtIndex(descendantsData, pSprite, uIndex); - ++i; - } - } - } + // update indices + unsigned int i = uIndex+1; + + CCSprite* pChild = NULL; + for(; inum; i++){ + pChild = (CCSprite*)descendantsData->arr[i]; + pChild->setAtlasIndex(pChild->getAtlasIndex() + 1); + } - // add children recursively - CCArray *pChildren = pobSprite->getChildren(); - if (pChildren && pChildren->count() > 0) - { - CCObject* pObject = NULL; - CCARRAY_FOREACH(pChildren, pObject) - { - CCSprite* pChild = (CCSprite*) pObject; - if (pChild) - { - unsigned int uIndex = atlasIndexForChild(pChild, pChild->getZOrder()); - insertChild(pChild, uIndex); - } - } - } + // add children recursively + CCObject* pObj = NULL; + CCARRAY_FOREACH(pSprite->getChildren(), pObj) + { + pChild = (CCSprite*)pObj; + unsigned int idx = atlasIndexForChild(pChild, pChild->getZOrder()); + insertChild(pChild, idx); + } } // addChild helper, faster than insertChild @@ -754,18 +729,17 @@ CCSpriteBatchNode * CCSpriteBatchNode::addSpriteWithoutQuad(CCSprite*child, unsi // XXX: optimize with a binary search int i=0; - if (m_pobDescendants && m_pobDescendants->count() > 0) - { - CCObject* pObject = NULL; - CCARRAY_FOREACH(m_pobDescendants, pObject) - { - CCSprite* pChild = (CCSprite*) pObject; - if (pChild && (pChild->getAtlasIndex() >= z)) - { - ++i; - } + + CCObject* pObject = NULL; + CCARRAY_FOREACH(m_pobDescendants, pObject) + { + CCSprite* pChild = (CCSprite*) pObject; + if (pChild && (pChild->getAtlasIndex() >= z)) + { + ++i; } } + m_pobDescendants->insertObject(child, i); // IMPORTANT: Call super, and not self. Avoid adding it to the texture atlas array diff --git a/tests/tests/SpriteTest/SpriteTest.cpp.REMOVED.git-id b/tests/tests/SpriteTest/SpriteTest.cpp.REMOVED.git-id index e7b9b7de47..304172efe9 100644 --- a/tests/tests/SpriteTest/SpriteTest.cpp.REMOVED.git-id +++ b/tests/tests/SpriteTest/SpriteTest.cpp.REMOVED.git-id @@ -1 +1 @@ -20e32b9a1ad61e6bf3e179cc5e1cd7d7bdec57fa \ No newline at end of file +45fa60a7892453b6d2be3554738d829d7743303e \ No newline at end of file diff --git a/tests/tests/SpriteTest/SpriteTest.h b/tests/tests/SpriteTest/SpriteTest.h index b850a4f66e..29ebe13782 100644 --- a/tests/tests/SpriteTest/SpriteTest.h +++ b/tests/tests/SpriteTest/SpriteTest.h @@ -483,7 +483,6 @@ public: SpriteBatchNodeReorderOneChild(); void reorderSprite(ccTime dt); virtual std::string title(); - virtual std::string subtitle(); private: CCSpriteBatchNode *m_pBatchNode; CCSprite *m_pReorderSprite; diff --git a/tests/tests/TileMapTest/TileMapTest.cpp b/tests/tests/TileMapTest/TileMapTest.cpp index bc180e1e87..1f16689a5c 100644 --- a/tests/tests/TileMapTest/TileMapTest.cpp +++ b/tests/tests/TileMapTest/TileMapTest.cpp @@ -805,7 +805,7 @@ TMXIsoZorder::TMXIsoZorder() addChild(map, 0, kTagTileMap); CCSize s = map->getContentSize(); - ////----UXLOG("ContentSize: %f, %f", s.width,s.height); + CCLOG("ContentSize: %f, %f", s.width,s.height); map->setPosition(ccp(-s.width/2,0)); m_tamara = CCSprite::spriteWithFile(s_pPathSister1); @@ -1011,7 +1011,7 @@ TMXOrthoVertexZ::TMXOrthoVertexZ() // can use any CCSprite and it will work OK. CCTMXLayer* layer = map->layerNamed("trees"); m_tamara = layer->tileAt(ccp(0,11)); - CCLOG("%@ vertexZ: %f", m_tamara, m_tamara->getVertexZ()); + CCLOG("%p vertexZ: %f", m_tamara, m_tamara->getVertexZ()); m_tamara->retain(); CCActionInterval* move = CCMoveBy::actionWithDuration(10, ccpMult( ccp(400,450), 1/CC_CONTENT_SCALE_FACTOR()));